Home | History | Annotate | Line # | Download | only in common
linux_file.c revision 1.86.2.2
      1 /*	$NetBSD: linux_file.c,v 1.86.2.2 2007/12/26 21:38:59 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.86.2.2 2007/12/26 21:38:59 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/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 #include <compat/linux/common/linux_ipc.h>
     74 #include <compat/linux/common/linux_sem.h>
     75 
     76 #include <compat/linux/linux_syscallargs.h>
     77 
     78 static int linux_to_bsd_ioflags(int);
     79 static int bsd_to_linux_ioflags(int);
     80 static void bsd_to_linux_flock(struct flock *, struct linux_flock *);
     81 static void linux_to_bsd_flock(struct linux_flock *, struct flock *);
     82 #ifndef __amd64__
     83 static void bsd_to_linux_stat(struct stat *, struct linux_stat *);
     84 #endif
     85 
     86 /*
     87  * Some file-related calls are handled here. The usual flag conversion
     88  * an structure conversion is done, and alternate emul path searching.
     89  */
     90 
     91 /*
     92  * The next two functions convert between the Linux and NetBSD values
     93  * of the flags used in open(2) and fcntl(2).
     94  */
     95 static int
     96 linux_to_bsd_ioflags(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(int bflags)
    117 {
    118 	int res = 0;
    119 
    120 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
    121 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
    122 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
    123 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
    124 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
    125 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
    126 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
    127 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
    128 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
    129 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
    130 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
    131 
    132 	return res;
    133 }
    134 
    135 /*
    136  * creat(2) is an obsolete function, but it's present as a Linux
    137  * system call, so let's deal with it.
    138  *
    139  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
    140  * in syscalls.master anyway so this doesn't have to be special cased.
    141  *
    142  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    143  */
    144 int
    145 linux_sys_creat(struct lwp *l, const struct linux_sys_creat_args *uap, register_t *retval)
    146 {
    147 	/* {
    148 		syscallarg(const char *) path;
    149 		syscallarg(int) mode;
    150 	} */
    151 	struct sys_open_args oa;
    152 
    153 	SCARG(&oa, path) = SCARG(uap, path);
    154 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    155 	SCARG(&oa, mode) = SCARG(uap, mode);
    156 
    157 	return sys_open(l, &oa, retval);
    158 }
    159 
    160 /*
    161  * open(2). Take care of the different flag values, and let the
    162  * NetBSD syscall do the real work. See if this operation
    163  * gives the current process a controlling terminal.
    164  * (XXX is this necessary?)
    165  */
    166 int
    167 linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap, register_t *retval)
    168 {
    169 	/* {
    170 		syscallarg(const char *) path;
    171 		syscallarg(int) flags;
    172 		syscallarg(int) mode;
    173 	} */
    174 	struct proc *p = l->l_proc;
    175 	int error, fl;
    176 	struct sys_open_args boa;
    177 
    178 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    179 
    180 	SCARG(&boa, path) = SCARG(uap, path);
    181 	SCARG(&boa, flags) = fl;
    182 	SCARG(&boa, mode) = SCARG(uap, mode);
    183 
    184 	if ((error = sys_open(l, &boa, retval)))
    185 		return error;
    186 
    187 	/*
    188 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    189 	 * If we are a session leader, and we don't have a controlling
    190 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    191 	 * this the controlling terminal.
    192 	 */
    193         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    194                 struct filedesc *fdp = p->p_fd;
    195                 struct file     *fp;
    196 
    197 		fp = fd_getfile(fdp, *retval);
    198 
    199                 /* ignore any error, just give it a try */
    200                 if (fp != NULL) {
    201 			FILE_USE(fp);
    202 			if (fp->f_type == DTYPE_VNODE) {
    203 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY,
    204 				    (void *) 0, l);
    205 			}
    206 			FILE_UNUSE(fp, l);
    207 		}
    208         }
    209 	return 0;
    210 }
    211 
    212 /*
    213  * The next two functions take care of converting the flock
    214  * structure back and forth between Linux and NetBSD format.
    215  * The only difference in the structures is the order of
    216  * the fields, and the 'whence' value.
    217  */
    218 static void
    219 bsd_to_linux_flock(struct flock *bfp, struct linux_flock *lfp)
    220 {
    221 
    222 	lfp->l_start = bfp->l_start;
    223 	lfp->l_len = bfp->l_len;
    224 	lfp->l_pid = bfp->l_pid;
    225 	lfp->l_whence = bfp->l_whence;
    226 	switch (bfp->l_type) {
    227 	case F_RDLCK:
    228 		lfp->l_type = LINUX_F_RDLCK;
    229 		break;
    230 	case F_UNLCK:
    231 		lfp->l_type = LINUX_F_UNLCK;
    232 		break;
    233 	case F_WRLCK:
    234 		lfp->l_type = LINUX_F_WRLCK;
    235 		break;
    236 	}
    237 }
    238 
    239 static void
    240 linux_to_bsd_flock(struct linux_flock *lfp, struct flock *bfp)
    241 {
    242 
    243 	bfp->l_start = lfp->l_start;
    244 	bfp->l_len = lfp->l_len;
    245 	bfp->l_pid = lfp->l_pid;
    246 	bfp->l_whence = lfp->l_whence;
    247 	switch (lfp->l_type) {
    248 	case LINUX_F_RDLCK:
    249 		bfp->l_type = F_RDLCK;
    250 		break;
    251 	case LINUX_F_UNLCK:
    252 		bfp->l_type = F_UNLCK;
    253 		break;
    254 	case LINUX_F_WRLCK:
    255 		bfp->l_type = F_WRLCK;
    256 		break;
    257 	}
    258 }
    259 
    260 /*
    261  * Most actions in the fcntl() call are straightforward; simply
    262  * pass control to the NetBSD system call. A few commands need
    263  * conversions after the actual system call has done its work,
    264  * because the flag values and lock structure are different.
    265  */
    266 int
    267 linux_sys_fcntl(struct lwp *l, const struct linux_sys_fcntl_args *uap, register_t *retval)
    268 {
    269 	/* {
    270 		syscallarg(int) fd;
    271 		syscallarg(int) cmd;
    272 		syscallarg(void *) arg;
    273 	} */
    274 	struct proc *p = l->l_proc;
    275 	int fd, cmd, error;
    276 	u_long val;
    277 	void *arg;
    278 	struct linux_flock lfl;
    279 	struct flock bfl;
    280 	struct sys_fcntl_args fca;
    281 	struct filedesc *fdp;
    282 	struct file *fp;
    283 	struct vnode *vp;
    284 	struct vattr va;
    285 	const struct cdevsw *cdev;
    286 	long pgid;
    287 	struct pgrp *pgrp;
    288 	struct tty *tp, *(*d_tty)(dev_t);
    289 
    290 	fd = SCARG(uap, fd);
    291 	cmd = SCARG(uap, cmd);
    292 	arg = (void *) SCARG(uap, arg);
    293 
    294 	switch (cmd) {
    295 	case LINUX_F_DUPFD:
    296 		cmd = F_DUPFD;
    297 		break;
    298 	case LINUX_F_GETFD:
    299 		cmd = F_GETFD;
    300 		break;
    301 	case LINUX_F_SETFD:
    302 		cmd = F_SETFD;
    303 		break;
    304 	case LINUX_F_GETFL:
    305 		SCARG(&fca, fd) = fd;
    306 		SCARG(&fca, cmd) = F_GETFL;
    307 		SCARG(&fca, arg) = arg;
    308 		if ((error = sys_fcntl(l, &fca, retval)))
    309 			return error;
    310 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    311 		return 0;
    312 	case LINUX_F_SETFL: {
    313 		struct file	*fp1 = NULL;
    314 
    315 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
    316 		/*
    317 		 * Linux seems to have same semantics for sending SIGIO to the
    318 		 * read side of socket, but slightly different semantics
    319 		 * for SIGIO to the write side.  Rather than sending the SIGIO
    320 		 * every time it's possible to write (directly) more data, it
    321 		 * only sends SIGIO if last write(2) failed due to insufficient
    322 		 * memory to hold the data. This is compatible enough
    323 		 * with NetBSD semantics to not do anything about the
    324 		 * difference.
    325 		 *
    326 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
    327 		 * ones and DTYPE_PIPE ones. For these, we don't set
    328 		 * the underlying flags (we don't pass O_ASYNC flag down
    329 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
    330 		 * so that F_GETFL would report the ASYNC i/o is on.
    331 		 */
    332 		if (val & O_ASYNC) {
    333 			if (((fp1 = fd_getfile(p->p_fd, fd)) == NULL))
    334 			    return (EBADF);
    335 
    336 			FILE_USE(fp1);
    337 
    338 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
    339 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
    340 			    || (fp1->f_type == DTYPE_PIPE))
    341 				val &= ~O_ASYNC;
    342 			else {
    343 				/* not a pipe, do not modify anything */
    344 				FILE_UNUSE(fp1, l);
    345 				fp1 = NULL;
    346 			}
    347 		}
    348 
    349 		SCARG(&fca, fd) = fd;
    350 		SCARG(&fca, cmd) = F_SETFL;
    351 		SCARG(&fca, arg) = (void *) val;
    352 
    353 		error = sys_fcntl(l, &fca, retval);
    354 
    355 		/* Now set the FASYNC flag for pipes */
    356 		if (fp1) {
    357 			if (!error)
    358 				fp1->f_flag |= FASYNC;
    359 			FILE_UNUSE(fp1, l);
    360 		}
    361 
    362 		return (error);
    363 	    }
    364 	case LINUX_F_GETLK:
    365 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    366 			return error;
    367 		linux_to_bsd_flock(&lfl, &bfl);
    368 		error = do_fcntl_lock(l, fd, F_GETLK, &bfl);
    369 		if (error)
    370 			return error;
    371 		bsd_to_linux_flock(&bfl, &lfl);
    372 		return copyout(&lfl, arg, sizeof lfl);
    373 
    374 	case LINUX_F_SETLK:
    375 	case LINUX_F_SETLKW:
    376 		cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
    377 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    378 			return error;
    379 		linux_to_bsd_flock(&lfl, &bfl);
    380 		return do_fcntl_lock(l, fd, cmd, &bfl);
    381 
    382 	case LINUX_F_SETOWN:
    383 	case LINUX_F_GETOWN:
    384 		/*
    385 		 * We need to route fcntl() for tty descriptors around normal
    386 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
    387 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
    388 		 * this is not a problem.
    389 		 */
    390 		fdp = p->p_fd;
    391 		if ((fp = fd_getfile(fdp, fd)) == NULL)
    392 			return EBADF;
    393 		FILE_USE(fp);
    394 
    395 		/* Check it's a character device vnode */
    396 		if (fp->f_type != DTYPE_VNODE
    397 		    || (vp = (struct vnode *)fp->f_data) == NULL
    398 		    || vp->v_type != VCHR) {
    399 			FILE_UNUSE(fp, l);
    400 
    401 	    not_tty:
    402 			/* Not a tty, proceed with common fcntl() */
    403 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    404 			break;
    405 		}
    406 
    407 		error = VOP_GETATTR(vp, &va, l->l_cred);
    408 
    409 		FILE_UNUSE(fp, l);
    410 
    411 		if (error)
    412 			return error;
    413 
    414 		cdev = cdevsw_lookup(va.va_rdev);
    415 		if (cdev == NULL)
    416 			return (ENXIO);
    417 		d_tty = cdev->d_tty;
    418 		if (!d_tty || (!(tp = (*d_tty)(va.va_rdev))))
    419 			goto not_tty;
    420 
    421 		/* set tty pg_id appropriately */
    422 		if (cmd == LINUX_F_GETOWN) {
    423 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
    424 			return 0;
    425 		}
    426 		mutex_enter(&proclist_lock);
    427 		if ((long)arg <= 0) {
    428 			pgid = -(long)arg;
    429 		} else {
    430 			struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
    431 			if (p1 == NULL)
    432 				return (ESRCH);
    433 			pgid = (long)p1->p_pgrp->pg_id;
    434 		}
    435 		pgrp = pg_find(pgid, PFIND_LOCKED);
    436 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
    437 			mutex_exit(&proclist_lock);
    438 			return EPERM;
    439 		}
    440 		tp->t_pgrp = pgrp;
    441 		mutex_exit(&proclist_lock);
    442 		return 0;
    443 
    444 	default:
    445 		return EOPNOTSUPP;
    446 	}
    447 
    448 	SCARG(&fca, fd) = fd;
    449 	SCARG(&fca, cmd) = cmd;
    450 	SCARG(&fca, arg) = arg;
    451 
    452 	return sys_fcntl(l, &fca, retval);
    453 }
    454 
    455 #if !defined(__amd64__)
    456 /*
    457  * Convert a NetBSD stat structure to a Linux stat structure.
    458  * Only the order of the fields and the padding in the structure
    459  * is different. linux_fakedev is a machine-dependent function
    460  * which optionally converts device driver major/minor numbers
    461  * (XXX horrible, but what can you do against code that compares
    462  * things against constant major device numbers? sigh)
    463  */
    464 static void
    465 bsd_to_linux_stat(struct stat *bsp, struct linux_stat *lsp)
    466 {
    467 
    468 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
    469 	lsp->lst_ino     = bsp->st_ino;
    470 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
    471 	if (bsp->st_nlink >= (1 << 15))
    472 		lsp->lst_nlink = (1 << 15) - 1;
    473 	else
    474 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
    475 	lsp->lst_uid     = bsp->st_uid;
    476 	lsp->lst_gid     = bsp->st_gid;
    477 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    478 	lsp->lst_size    = bsp->st_size;
    479 	lsp->lst_blksize = bsp->st_blksize;
    480 	lsp->lst_blocks  = bsp->st_blocks;
    481 	lsp->lst_atime   = bsp->st_atime;
    482 	lsp->lst_mtime   = bsp->st_mtime;
    483 	lsp->lst_ctime   = bsp->st_ctime;
    484 #ifdef LINUX_STAT_HAS_NSEC
    485 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    486 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    487 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    488 #endif
    489 }
    490 
    491 /*
    492  * The stat functions below are plain sailing. stat and lstat are handled
    493  * by one function to avoid code duplication.
    494  */
    495 int
    496 linux_sys_fstat(struct lwp *l, const struct linux_sys_fstat_args *uap, register_t *retval)
    497 {
    498 	/* {
    499 		syscallarg(int) fd;
    500 		syscallarg(linux_stat *) sp;
    501 	} */
    502 	struct linux_stat tmplst;
    503 	struct stat tmpst;
    504 	int error;
    505 
    506 	error = do_sys_fstat(l, SCARG(uap, fd), &tmpst);
    507 	if (error != 0)
    508 		return error;
    509 	bsd_to_linux_stat(&tmpst, &tmplst);
    510 
    511 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    512 }
    513 
    514 static int
    515 linux_stat1(struct lwp *l, const struct linux_sys_stat_args *uap, register_t *retval, int flags)
    516 {
    517 	struct linux_stat tmplst;
    518 	struct stat tmpst;
    519 	int error;
    520 
    521 	error = do_sys_stat(l, SCARG(uap, path), flags, &tmpst);
    522 	if (error != 0)
    523 		return error;
    524 
    525 	bsd_to_linux_stat(&tmpst, &tmplst);
    526 
    527 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    528 }
    529 
    530 int
    531 linux_sys_stat(struct lwp *l, const struct linux_sys_stat_args *uap, register_t *retval)
    532 {
    533 	/* {
    534 		syscallarg(const char *) path;
    535 		syscallarg(struct linux_stat *) sp;
    536 	} */
    537 
    538 	return linux_stat1(l, uap, retval, FOLLOW);
    539 }
    540 
    541 /* Note: this is "newlstat" in the Linux sources */
    542 /*	(we don't bother with the old lstat currently) */
    543 int
    544 linux_sys_lstat(struct lwp *l, const struct linux_sys_lstat_args *uap, register_t *retval)
    545 {
    546 	/* {
    547 		syscallarg(const char *) path;
    548 		syscallarg(struct linux_stat *) sp;
    549 	} */
    550 
    551 	return linux_stat1(l, (const void *)uap, retval, NOFOLLOW);
    552 }
    553 #endif /* !__amd64__ */
    554 
    555 /*
    556  * The following syscalls are mostly here because of the alternate path check.
    557  */
    558 int
    559 linux_sys_unlink(struct lwp *l, const struct linux_sys_unlink_args *uap, register_t *retval)
    560 {
    561 	/* {
    562 		syscallarg(const char *) path;
    563 	} */
    564 	int error;
    565 	struct nameidata nd;
    566 
    567 	error = sys_unlink(l, (const void *)uap, retval);
    568 	if (error != EPERM)
    569 		return (error);
    570 
    571 	/*
    572 	 * Linux returns EISDIR if unlink(2) is called on a directory.
    573 	 * We return EPERM in such cases. To emulate correct behaviour,
    574 	 * check if the path points to directory and return EISDIR if this
    575 	 * is the case.
    576 	 */
    577 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
    578 	    SCARG(uap, path));
    579 	if (namei(&nd) == 0) {
    580 		struct stat sb;
    581 
    582 		if (vn_stat(nd.ni_vp, &sb, l) == 0
    583 		    && S_ISDIR(sb.st_mode))
    584 			error = EISDIR;
    585 
    586 		vput(nd.ni_vp);
    587 	}
    588 
    589 	return (error);
    590 }
    591 
    592 int
    593 linux_sys_mknod(struct lwp *l, const struct linux_sys_mknod_args *uap, register_t *retval)
    594 {
    595 	/* {
    596 		syscallarg(const char *) path;
    597 		syscallarg(int) mode;
    598 		syscallarg(int) dev;
    599 	} */
    600 
    601 	/*
    602 	 * BSD handles FIFOs separately
    603 	 */
    604 	if (S_ISFIFO(SCARG(uap, mode))) {
    605 		struct sys_mkfifo_args bma;
    606 
    607 		SCARG(&bma, path) = SCARG(uap, path);
    608 		SCARG(&bma, mode) = SCARG(uap, mode);
    609 		return sys_mkfifo(l, &bma, retval);
    610 	} else {
    611 		struct sys_mknod_args bma;
    612 
    613 		SCARG(&bma, path) = SCARG(uap, path);
    614 		SCARG(&bma, mode) = SCARG(uap, mode);
    615 		/*
    616 		 * Linux device numbers uses 8 bits for minor and 8 bits
    617 		 * for major. Due to how we map our major and minor,
    618 		 * this just fits into our dev_t. Just mask off the
    619 		 * upper 16bit to remove any random junk.
    620 		 */
    621 		SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
    622 		return sys_mknod(l, &bma, retval);
    623 	}
    624 }
    625 
    626 #if defined(__i386__) || defined(__m68k__) || \
    627     defined(__arm__)
    628 int
    629 linux_sys_chown16(struct lwp *l, const struct linux_sys_chown16_args *uap, register_t *retval)
    630 {
    631 	/* {
    632 		syscallarg(const char *) path;
    633 		syscallarg(int) uid;
    634 		syscallarg(int) gid;
    635 	} */
    636 	struct sys___posix_chown_args bca;
    637 
    638 	SCARG(&bca, path) = SCARG(uap, path);
    639 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    640 		(uid_t)-1 : SCARG(uap, uid);
    641 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    642 		(gid_t)-1 : SCARG(uap, gid);
    643 
    644 	return sys___posix_chown(l, &bca, retval);
    645 }
    646 
    647 int
    648 linux_sys_fchown16(struct lwp *l, const struct linux_sys_fchown16_args *uap, register_t *retval)
    649 {
    650 	/* {
    651 		syscallarg(int) fd;
    652 		syscallarg(int) uid;
    653 		syscallarg(int) gid;
    654 	} */
    655 	struct sys___posix_fchown_args bfa;
    656 
    657 	SCARG(&bfa, fd) = SCARG(uap, fd);
    658 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    659 		(uid_t)-1 : SCARG(uap, uid);
    660 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    661 		(gid_t)-1 : SCARG(uap, gid);
    662 
    663 	return sys___posix_fchown(l, &bfa, retval);
    664 }
    665 
    666 int
    667 linux_sys_lchown16(struct lwp *l, const struct linux_sys_lchown16_args *uap, register_t *retval)
    668 {
    669 	/* {
    670 		syscallarg(char *) path;
    671 		syscallarg(int) uid;
    672 		syscallarg(int) gid;
    673 	} */
    674 	struct sys___posix_lchown_args bla;
    675 
    676 	SCARG(&bla, path) = SCARG(uap, path);
    677 	SCARG(&bla, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    678 		(uid_t)-1 : SCARG(uap, uid);
    679 	SCARG(&bla, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    680 		(gid_t)-1 : SCARG(uap, gid);
    681 
    682 	return sys___posix_lchown(l, &bla, retval);
    683 }
    684 #endif /* __i386__ || __m68k__ || __arm__ || __amd64__ */
    685 
    686 /*
    687  * This is just fsync() for now (just as it is in the Linux kernel)
    688  * Note: this is not implemented under Linux on Alpha and Arm
    689  *	but should still be defined in our syscalls.master.
    690  *	(syscall #148 on the arm)
    691  */
    692 int
    693 linux_sys_fdatasync(struct lwp *l, const struct linux_sys_fdatasync_args *uap, register_t *retval)
    694 {
    695 	/* {
    696 		syscallarg(int) fd;
    697 	} */
    698 
    699 	return sys_fsync(l, (const void *)uap, retval);
    700 }
    701 
    702 /*
    703  * pread(2).
    704  */
    705 int
    706 linux_sys_pread(struct lwp *l, const struct linux_sys_pread_args *uap, register_t *retval)
    707 {
    708 	/* {
    709 		syscallarg(int) fd;
    710 		syscallarg(void *) buf;
    711 		syscallarg(size_t) nbyte;
    712 		syscallarg(linux_off_t) offset;
    713 	} */
    714 	struct sys_pread_args pra;
    715 
    716 	SCARG(&pra, fd) = SCARG(uap, fd);
    717 	SCARG(&pra, buf) = SCARG(uap, buf);
    718 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    719 	SCARG(&pra, offset) = SCARG(uap, offset);
    720 
    721 	return sys_pread(l, &pra, retval);
    722 }
    723 
    724 /*
    725  * pwrite(2).
    726  */
    727 int
    728 linux_sys_pwrite(struct lwp *l, const struct linux_sys_pwrite_args *uap, register_t *retval)
    729 {
    730 	/* {
    731 		syscallarg(int) fd;
    732 		syscallarg(void *) buf;
    733 		syscallarg(size_t) nbyte;
    734 		syscallarg(linux_off_t) offset;
    735 	} */
    736 	struct sys_pwrite_args pra;
    737 
    738 	SCARG(&pra, fd) = SCARG(uap, fd);
    739 	SCARG(&pra, buf) = SCARG(uap, buf);
    740 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    741 	SCARG(&pra, offset) = SCARG(uap, offset);
    742 
    743 	return sys_pwrite(l, &pra, retval);
    744 }
    745 
    746 #define LINUX_NOT_SUPPORTED(fun) \
    747 int \
    748 fun(struct lwp *l, const struct fun##_args *uap, register_t *retval) \
    749 { \
    750 	return EOPNOTSUPP; \
    751 }
    752 
    753 LINUX_NOT_SUPPORTED(linux_sys_setxattr)
    754 LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
    755 LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
    756 
    757 LINUX_NOT_SUPPORTED(linux_sys_getxattr)
    758 LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
    759 LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
    760 
    761 LINUX_NOT_SUPPORTED(linux_sys_listxattr)
    762 LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
    763 LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
    764 
    765 LINUX_NOT_SUPPORTED(linux_sys_removexattr)
    766 LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
    767 LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
    768 
    769