Home | History | Annotate | Line # | Download | only in common
linux_file.c revision 1.110
      1 /*	$NetBSD: linux_file.c,v 1.110 2014/05/06 13:21:50 njoly Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 2008 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Functions in multiarch:
     34  *	linux_sys_llseek	: linux_llseek.c
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.110 2014/05/06 13:21:50 njoly Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/namei.h>
     43 #include <sys/proc.h>
     44 #include <sys/file.h>
     45 #include <sys/fcntl.h>
     46 #include <sys/stat.h>
     47 #include <sys/filedesc.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/kernel.h>
     50 #include <sys/mount.h>
     51 #include <sys/malloc.h>
     52 #include <sys/namei.h>
     53 #include <sys/vnode.h>
     54 #include <sys/tty.h>
     55 #include <sys/socketvar.h>
     56 #include <sys/conf.h>
     57 #include <sys/pipe.h>
     58 
     59 #include <sys/syscallargs.h>
     60 #include <sys/vfs_syscalls.h>
     61 
     62 #include <compat/linux/common/linux_types.h>
     63 #include <compat/linux/common/linux_signal.h>
     64 #include <compat/linux/common/linux_fcntl.h>
     65 #include <compat/linux/common/linux_util.h>
     66 #include <compat/linux/common/linux_machdep.h>
     67 #include <compat/linux/common/linux_ipc.h>
     68 #include <compat/linux/common/linux_sem.h>
     69 
     70 #include <compat/linux/linux_syscallargs.h>
     71 
     72 static int linux_to_bsd_ioflags(int);
     73 static int bsd_to_linux_ioflags(int);
     74 #ifndef __amd64__
     75 static void bsd_to_linux_stat(struct stat *, struct linux_stat *);
     76 #endif
     77 
     78 conv_linux_flock(linux, flock)
     79 
     80 /*
     81  * Some file-related calls are handled here. The usual flag conversion
     82  * an structure conversion is done, and alternate emul path searching.
     83  */
     84 
     85 /*
     86  * The next two functions convert between the Linux and NetBSD values
     87  * of the flags used in open(2) and fcntl(2).
     88  */
     89 static int
     90 linux_to_bsd_ioflags(int lflags)
     91 {
     92 	int res = 0;
     93 
     94 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
     95 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
     96 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
     97 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
     98 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
     99 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
    100 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
    101 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
    102 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
    103 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
    104 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
    105 	res |= cvtto_bsd_mask(lflags, LINUX_O_DIRECTORY, O_DIRECTORY);
    106 	res |= cvtto_bsd_mask(lflags, LINUX_O_CLOEXEC, O_CLOEXEC);
    107 
    108 	return res;
    109 }
    110 
    111 static int
    112 bsd_to_linux_ioflags(int bflags)
    113 {
    114 	int res = 0;
    115 
    116 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
    117 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
    118 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
    119 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
    120 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
    121 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
    122 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
    123 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
    124 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
    125 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
    126 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
    127 	res |= cvtto_linux_mask(bflags, O_DIRECTORY, LINUX_O_DIRECTORY);
    128 	res |= cvtto_linux_mask(bflags, O_CLOEXEC, LINUX_O_CLOEXEC);
    129 
    130 	return res;
    131 }
    132 
    133 /*
    134  * creat(2) is an obsolete function, but it's present as a Linux
    135  * system call, so let's deal with it.
    136  *
    137  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
    138  * in syscalls.master anyway so this doesn't have to be special cased.
    139  *
    140  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    141  */
    142 int
    143 linux_sys_creat(struct lwp *l, const struct linux_sys_creat_args *uap, register_t *retval)
    144 {
    145 	/* {
    146 		syscallarg(const char *) path;
    147 		syscallarg(linux_umode_t) mode;
    148 	} */
    149 	struct sys_open_args oa;
    150 
    151 	SCARG(&oa, path) = SCARG(uap, path);
    152 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    153 	SCARG(&oa, mode) = SCARG(uap, mode);
    154 
    155 	return sys_open(l, &oa, retval);
    156 }
    157 
    158 static void
    159 linux_open_ctty(struct lwp *l, int flags, int fd)
    160 {
    161 	struct proc *p = l->l_proc;
    162 
    163 	/*
    164 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    165 	 * If we are a session leader, and we don't have a controlling
    166 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    167 	 * this the controlling terminal.
    168 	 */
    169         if (!(flags & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    170                 file_t *fp;
    171 
    172 		fp = fd_getfile(fd);
    173 
    174                 /* ignore any error, just give it a try */
    175                 if (fp != NULL) {
    176 			if (fp->f_type == DTYPE_VNODE) {
    177 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, NULL);
    178 			}
    179 			fd_putfile(fd);
    180 		}
    181         }
    182 }
    183 
    184 /*
    185  * open(2). Take care of the different flag values, and let the
    186  * NetBSD syscall do the real work. See if this operation
    187  * gives the current process a controlling terminal.
    188  * (XXX is this necessary?)
    189  */
    190 int
    191 linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap, register_t *retval)
    192 {
    193 	/* {
    194 		syscallarg(const char *) path;
    195 		syscallarg(int) flags;
    196 		syscallarg(linux_umode_t) mode;
    197 	} */
    198 	int error, fl;
    199 	struct sys_open_args boa;
    200 
    201 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    202 
    203 	SCARG(&boa, path) = SCARG(uap, path);
    204 	SCARG(&boa, flags) = fl;
    205 	SCARG(&boa, mode) = SCARG(uap, mode);
    206 
    207 	if ((error = sys_open(l, &boa, retval)))
    208 		return error;
    209 
    210 	linux_open_ctty(l, fl, *retval);
    211 	return 0;
    212 }
    213 
    214 int
    215 linux_sys_openat(struct lwp *l, const struct linux_sys_openat_args *uap, register_t *retval)
    216 {
    217 	/* {
    218 		syscallarg(int) fd;
    219 		syscallarg(const char *) path;
    220 		syscallarg(int) flags;
    221 		syscallarg(linux_umode_t) mode;
    222 	} */
    223 	int error, fl;
    224 	struct sys_openat_args boa;
    225 
    226 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    227 
    228 	SCARG(&boa, fd) = SCARG(uap, fd);
    229 	SCARG(&boa, path) = SCARG(uap, path);
    230 	SCARG(&boa, oflags) = fl;
    231 	SCARG(&boa, mode) = SCARG(uap, mode);
    232 
    233 	if ((error = sys_openat(l, &boa, retval)))
    234 		return error;
    235 
    236 	linux_open_ctty(l, fl, *retval);
    237 	return 0;
    238 }
    239 
    240 /*
    241  * Most actions in the fcntl() call are straightforward; simply
    242  * pass control to the NetBSD system call. A few commands need
    243  * conversions after the actual system call has done its work,
    244  * because the flag values and lock structure are different.
    245  */
    246 int
    247 linux_sys_fcntl(struct lwp *l, const struct linux_sys_fcntl_args *uap, register_t *retval)
    248 {
    249 	/* {
    250 		syscallarg(int) fd;
    251 		syscallarg(int) cmd;
    252 		syscallarg(void *) arg;
    253 	} */
    254 	struct proc *p = l->l_proc;
    255 	int fd, cmd, error;
    256 	u_long val;
    257 	void *arg;
    258 	struct sys_fcntl_args fca;
    259 	file_t *fp;
    260 	struct vnode *vp;
    261 	struct vattr va;
    262 	long pgid;
    263 	struct pgrp *pgrp;
    264 	struct tty *tp;
    265 
    266 	fd = SCARG(uap, fd);
    267 	cmd = SCARG(uap, cmd);
    268 	arg = SCARG(uap, arg);
    269 
    270 	switch (cmd) {
    271 
    272 	case LINUX_F_DUPFD:
    273 		cmd = F_DUPFD;
    274 		break;
    275 
    276 	case LINUX_F_GETFD:
    277 		cmd = F_GETFD;
    278 		break;
    279 
    280 	case LINUX_F_SETFD:
    281 		cmd = F_SETFD;
    282 		break;
    283 
    284 	case LINUX_F_GETFL:
    285 		SCARG(&fca, fd) = fd;
    286 		SCARG(&fca, cmd) = F_GETFL;
    287 		SCARG(&fca, arg) = arg;
    288 		if ((error = sys_fcntl(l, &fca, retval)))
    289 			return error;
    290 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    291 		return 0;
    292 
    293 	case LINUX_F_SETFL: {
    294 		file_t	*fp1 = NULL;
    295 
    296 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
    297 		/*
    298 		 * Linux seems to have same semantics for sending SIGIO to the
    299 		 * read side of socket, but slightly different semantics
    300 		 * for SIGIO to the write side.  Rather than sending the SIGIO
    301 		 * every time it's possible to write (directly) more data, it
    302 		 * only sends SIGIO if last write(2) failed due to insufficient
    303 		 * memory to hold the data. This is compatible enough
    304 		 * with NetBSD semantics to not do anything about the
    305 		 * difference.
    306 		 *
    307 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
    308 		 * ones and DTYPE_PIPE ones. For these, we don't set
    309 		 * the underlying flags (we don't pass O_ASYNC flag down
    310 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
    311 		 * so that F_GETFL would report the ASYNC i/o is on.
    312 		 */
    313 		if (val & O_ASYNC) {
    314 			if (((fp1 = fd_getfile(fd)) == NULL))
    315 			    return (EBADF);
    316 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
    317 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
    318 			    || (fp1->f_type == DTYPE_PIPE))
    319 				val &= ~O_ASYNC;
    320 			else {
    321 				/* not a pipe, do not modify anything */
    322 				fd_putfile(fd);
    323 				fp1 = NULL;
    324 			}
    325 		}
    326 
    327 		SCARG(&fca, fd) = fd;
    328 		SCARG(&fca, cmd) = F_SETFL;
    329 		SCARG(&fca, arg) = (void *) val;
    330 
    331 		error = sys_fcntl(l, &fca, retval);
    332 
    333 		/* Now set the FASYNC flag for pipes */
    334 		if (fp1) {
    335 			if (!error) {
    336 				mutex_enter(&fp1->f_lock);
    337 				fp1->f_flag |= FASYNC;
    338 				mutex_exit(&fp1->f_lock);
    339 			}
    340 			fd_putfile(fd);
    341 		}
    342 
    343 		return (error);
    344 	    }
    345 
    346 	case LINUX_F_GETLK:
    347 		do_linux_getlk(fd, cmd, arg, linux, flock);
    348 
    349 	case LINUX_F_SETLK:
    350 	case LINUX_F_SETLKW:
    351 		do_linux_setlk(fd, cmd, arg, linux, flock, LINUX_F_SETLK);
    352 
    353 	case LINUX_F_SETOWN:
    354 	case LINUX_F_GETOWN:
    355 		/*
    356 		 * We need to route fcntl() for tty descriptors around normal
    357 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
    358 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
    359 		 * this is not a problem.
    360 		 */
    361 		if ((fp = fd_getfile(fd)) == NULL)
    362 			return EBADF;
    363 
    364 		/* Check it's a character device vnode */
    365 		if (fp->f_type != DTYPE_VNODE
    366 		    || (vp = (struct vnode *)fp->f_data) == NULL
    367 		    || vp->v_type != VCHR) {
    368 			fd_putfile(fd);
    369 
    370 	    not_tty:
    371 			/* Not a tty, proceed with common fcntl() */
    372 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    373 			break;
    374 		}
    375 
    376 		vn_lock(vp, LK_SHARED | LK_RETRY);
    377 		error = VOP_GETATTR(vp, &va, l->l_cred);
    378 		VOP_UNLOCK(vp);
    379 
    380 		fd_putfile(fd);
    381 
    382 		if (error)
    383 			return error;
    384 
    385 		if ((tp = cdev_tty(va.va_rdev)) == NULL)
    386 			goto not_tty;
    387 
    388 		/* set tty pg_id appropriately */
    389 		mutex_enter(proc_lock);
    390 		if (cmd == LINUX_F_GETOWN) {
    391 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
    392 			mutex_exit(proc_lock);
    393 			return 0;
    394 		}
    395 		if ((long)arg <= 0) {
    396 			pgid = -(long)arg;
    397 		} else {
    398 			struct proc *p1 = proc_find((long)arg);
    399 			if (p1 == NULL) {
    400 				mutex_exit(proc_lock);
    401 				return (ESRCH);
    402 			}
    403 			pgid = (long)p1->p_pgrp->pg_id;
    404 		}
    405 		pgrp = pgrp_find(pgid);
    406 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
    407 			mutex_exit(proc_lock);
    408 			return EPERM;
    409 		}
    410 		tp->t_pgrp = pgrp;
    411 		mutex_exit(proc_lock);
    412 		return 0;
    413 
    414 	default:
    415 		return EOPNOTSUPP;
    416 	}
    417 
    418 	SCARG(&fca, fd) = fd;
    419 	SCARG(&fca, cmd) = cmd;
    420 	SCARG(&fca, arg) = arg;
    421 
    422 	return sys_fcntl(l, &fca, retval);
    423 }
    424 
    425 #if !defined(__amd64__)
    426 /*
    427  * Convert a NetBSD stat structure to a Linux stat structure.
    428  * Only the order of the fields and the padding in the structure
    429  * is different. linux_fakedev is a machine-dependent function
    430  * which optionally converts device driver major/minor numbers
    431  * (XXX horrible, but what can you do against code that compares
    432  * things against constant major device numbers? sigh)
    433  */
    434 static void
    435 bsd_to_linux_stat(struct stat *bsp, struct linux_stat *lsp)
    436 {
    437 
    438 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
    439 	lsp->lst_ino     = bsp->st_ino;
    440 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
    441 	if (bsp->st_nlink >= (1 << 15))
    442 		lsp->lst_nlink = (1 << 15) - 1;
    443 	else
    444 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
    445 	lsp->lst_uid     = bsp->st_uid;
    446 	lsp->lst_gid     = bsp->st_gid;
    447 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    448 	lsp->lst_size    = bsp->st_size;
    449 	lsp->lst_blksize = bsp->st_blksize;
    450 	lsp->lst_blocks  = bsp->st_blocks;
    451 	lsp->lst_atime   = bsp->st_atime;
    452 	lsp->lst_mtime   = bsp->st_mtime;
    453 	lsp->lst_ctime   = bsp->st_ctime;
    454 #ifdef LINUX_STAT_HAS_NSEC
    455 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    456 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    457 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    458 #endif
    459 }
    460 
    461 /*
    462  * The stat functions below are plain sailing. stat and lstat are handled
    463  * by one function to avoid code duplication.
    464  */
    465 int
    466 linux_sys_fstat(struct lwp *l, const struct linux_sys_fstat_args *uap, register_t *retval)
    467 {
    468 	/* {
    469 		syscallarg(int) fd;
    470 		syscallarg(linux_stat *) sp;
    471 	} */
    472 	struct linux_stat tmplst;
    473 	struct stat tmpst;
    474 	int error;
    475 
    476 	error = do_sys_fstat(SCARG(uap, fd), &tmpst);
    477 	if (error != 0)
    478 		return error;
    479 	bsd_to_linux_stat(&tmpst, &tmplst);
    480 
    481 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    482 }
    483 
    484 static int
    485 linux_stat1(const struct linux_sys_stat_args *uap, register_t *retval, int flags)
    486 {
    487 	struct linux_stat tmplst;
    488 	struct stat tmpst;
    489 	int error;
    490 
    491 	error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
    492 	if (error != 0)
    493 		return error;
    494 
    495 	bsd_to_linux_stat(&tmpst, &tmplst);
    496 
    497 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    498 }
    499 
    500 int
    501 linux_sys_stat(struct lwp *l, const struct linux_sys_stat_args *uap, register_t *retval)
    502 {
    503 	/* {
    504 		syscallarg(const char *) path;
    505 		syscallarg(struct linux_stat *) sp;
    506 	} */
    507 
    508 	return linux_stat1(uap, retval, FOLLOW);
    509 }
    510 
    511 /* Note: this is "newlstat" in the Linux sources */
    512 /*	(we don't bother with the old lstat currently) */
    513 int
    514 linux_sys_lstat(struct lwp *l, const struct linux_sys_lstat_args *uap, register_t *retval)
    515 {
    516 	/* {
    517 		syscallarg(const char *) path;
    518 		syscallarg(struct linux_stat *) sp;
    519 	} */
    520 
    521 	return linux_stat1((const void *)uap, retval, NOFOLLOW);
    522 }
    523 #endif /* !__amd64__ */
    524 
    525 /*
    526  * The following syscalls are mostly here because of the alternate path check.
    527  */
    528 
    529 int
    530 linux_sys_linkat(struct lwp *l, const struct linux_sys_linkat_args *uap, register_t *retval)
    531 {
    532 	/* {
    533 		syscallarg(int) fd1;
    534 		syscallarg(const char *) name1;
    535 		syscallarg(int) fd2;
    536 		syscallarg(const char *) name2;
    537 		syscallarg(int) flags;
    538 	} */
    539 	int fd1 = SCARG(uap, fd1);
    540 	const char *name1 = SCARG(uap, name1);
    541 	int fd2 = SCARG(uap, fd2);
    542 	const char *name2 = SCARG(uap, name2);
    543 	int follow;
    544 
    545 	follow = SCARG(uap, flags) & LINUX_AT_SYMLINK_FOLLOW;
    546 
    547 	return do_sys_linkat(l, fd1, name1, fd2, name2, follow, retval);
    548 }
    549 
    550 static int
    551 linux_unlink_dircheck(const char *path)
    552 {
    553 	struct nameidata nd;
    554 	struct pathbuf *pb;
    555 	int error;
    556 
    557 	/*
    558 	 * Linux returns EISDIR if unlink(2) is called on a directory.
    559 	 * We return EPERM in such cases. To emulate correct behaviour,
    560 	 * check if the path points to directory and return EISDIR if this
    561 	 * is the case.
    562 	 *
    563 	 * XXX this should really not copy in the path buffer twice...
    564 	 */
    565 	error = pathbuf_copyin(path, &pb);
    566 	if (error) {
    567 		return error;
    568 	}
    569 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
    570 	if (namei(&nd) == 0) {
    571 		struct stat sb;
    572 
    573 		if (vn_stat(nd.ni_vp, &sb) == 0
    574 		    && S_ISDIR(sb.st_mode))
    575 			error = EISDIR;
    576 
    577 		vput(nd.ni_vp);
    578 	}
    579 	pathbuf_destroy(pb);
    580 	return error ? error : EPERM;
    581 }
    582 
    583 int
    584 linux_sys_unlink(struct lwp *l, const struct linux_sys_unlink_args *uap, register_t *retval)
    585 {
    586 	/* {
    587 		syscallarg(const char *) path;
    588 	} */
    589 	int error;
    590 
    591 	error = sys_unlink(l, (const void *)uap, retval);
    592 	if (error == EPERM)
    593 		error = linux_unlink_dircheck(SCARG(uap, path));
    594 
    595 	return error;
    596 }
    597 
    598 int
    599 linux_sys_unlinkat(struct lwp *l, const struct linux_sys_unlinkat_args *uap, register_t *retval)
    600 {
    601 	/* {
    602 		syscallarg(int) fd;
    603 		syscallarg(const char *) path;
    604 		syscallarg(int) flag;
    605 	} */
    606 	struct sys_unlinkat_args ua;
    607 	int error;
    608 
    609 	SCARG(&ua, fd) = SCARG(uap, fd);
    610 	SCARG(&ua, path) = SCARG(uap, path);
    611 	SCARG(&ua, flag) = linux_to_bsd_atflags(SCARG(uap, flag));
    612 
    613 	error = sys_unlinkat(l, &ua, retval);
    614 	if (error == EPERM)
    615 		error = linux_unlink_dircheck(SCARG(uap, path));
    616 
    617 	return error;
    618 }
    619 
    620 int
    621 linux_sys_mknod(struct lwp *l, const struct linux_sys_mknod_args *uap, register_t *retval)
    622 {
    623 	/* {
    624 		syscallarg(const char *) path;
    625 		syscallarg(linux_umode_t) mode;
    626 		syscallarg(unsigned) dev;
    627 	} */
    628 	struct linux_sys_mknodat_args ua;
    629 
    630 	SCARG(&ua, fd) = LINUX_AT_FDCWD;
    631 	SCARG(&ua, path) = SCARG(uap, path);
    632 	SCARG(&ua, mode) = SCARG(uap, mode);
    633 	SCARG(&ua, dev) = SCARG(uap, dev);
    634 
    635 	return linux_sys_mknodat(l, &ua, retval);
    636 }
    637 
    638 int
    639 linux_sys_mknodat(struct lwp *l, const struct linux_sys_mknodat_args *uap, register_t *retval)
    640 {
    641 	/* {
    642 		syscallarg(int) fd;
    643 		syscallarg(const char *) path;
    644 		syscallarg(linux_umode_t) mode;
    645 		syscallarg(unsigned) dev;
    646 	} */
    647 
    648 	/*
    649 	 * BSD handles FIFOs separately
    650 	 */
    651 	if (S_ISFIFO(SCARG(uap, mode))) {
    652 		struct sys_mkfifoat_args bma;
    653 
    654 		SCARG(&bma, fd) = SCARG(uap, fd);
    655 		SCARG(&bma, path) = SCARG(uap, path);
    656 		SCARG(&bma, mode) = SCARG(uap, mode);
    657 		return sys_mkfifoat(l, &bma, retval);
    658 	} else {
    659 
    660 		/*
    661 		 * Linux device numbers uses 8 bits for minor and 8 bits
    662 		 * for major. Due to how we map our major and minor,
    663 		 * this just fits into our dev_t. Just mask off the
    664 		 * upper 16bit to remove any random junk.
    665 		 */
    666 
    667 		return do_sys_mknodat(l, SCARG(uap, fd), SCARG(uap, path),
    668 		    SCARG(uap, mode), SCARG(uap, dev) & 0xffff, retval,
    669 		    UIO_USERSPACE);
    670 	}
    671 }
    672 
    673 int
    674 linux_sys_fchmodat(struct lwp *l, const struct linux_sys_fchmodat_args *uap, register_t *retval)
    675 {
    676 	/* {
    677 		syscallarg(int) fd;
    678 		syscallarg(const char *) path;
    679 		syscallarg(linux_umode_t) mode;
    680 	} */
    681 
    682 	return do_sys_chmodat(l, SCARG(uap, fd), SCARG(uap, path),
    683 			      SCARG(uap, mode), AT_SYMLINK_FOLLOW);
    684 }
    685 
    686 int
    687 linux_sys_fchownat(struct lwp *l, const struct linux_sys_fchownat_args *uap, register_t *retval)
    688 {
    689 	/* {
    690 		syscallarg(int) fd;
    691 		syscallarg(const char *) path;
    692 		syscallarg(uid_t) owner;
    693 		syscallarg(gid_t) group;
    694 		syscallarg(int) flag;
    695 	} */
    696 	int flag;
    697 
    698 	flag = linux_to_bsd_atflags(SCARG(uap, flag));
    699 	return do_sys_chownat(l, SCARG(uap, fd), SCARG(uap, path),
    700 			      SCARG(uap, owner), SCARG(uap, group), flag);
    701 }
    702 
    703 int
    704 linux_sys_faccessat(struct lwp *l, const struct linux_sys_faccessat_args *uap, register_t *retval)
    705 {
    706 	/* {
    707 		syscallarg(int) fd;
    708 		syscallarg(const char *) path;
    709 		syscallarg(int) amode;
    710 	} */
    711 
    712 	return do_sys_accessat(l, SCARG(uap, fd), SCARG(uap, path),
    713 	     SCARG(uap, amode), AT_SYMLINK_FOLLOW);
    714 }
    715 
    716 /*
    717  * This is just fsync() for now (just as it is in the Linux kernel)
    718  * Note: this is not implemented under Linux on Alpha and Arm
    719  *	but should still be defined in our syscalls.master.
    720  *	(syscall #148 on the arm)
    721  */
    722 int
    723 linux_sys_fdatasync(struct lwp *l, const struct linux_sys_fdatasync_args *uap, register_t *retval)
    724 {
    725 	/* {
    726 		syscallarg(int) fd;
    727 	} */
    728 
    729 	return sys_fsync(l, (const void *)uap, retval);
    730 }
    731 
    732 /*
    733  * pread(2).
    734  */
    735 int
    736 linux_sys_pread(struct lwp *l, const struct linux_sys_pread_args *uap, register_t *retval)
    737 {
    738 	/* {
    739 		syscallarg(int) fd;
    740 		syscallarg(void *) buf;
    741 		syscallarg(size_t) nbyte;
    742 		syscallarg(off_t) offset;
    743 	} */
    744 	struct sys_pread_args pra;
    745 
    746 	SCARG(&pra, fd) = SCARG(uap, fd);
    747 	SCARG(&pra, buf) = SCARG(uap, buf);
    748 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    749 	SCARG(&pra, PAD) = 0;
    750 	SCARG(&pra, offset) = SCARG(uap, offset);
    751 
    752 	return sys_pread(l, &pra, retval);
    753 }
    754 
    755 /*
    756  * pwrite(2).
    757  */
    758 int
    759 linux_sys_pwrite(struct lwp *l, const struct linux_sys_pwrite_args *uap, register_t *retval)
    760 {
    761 	/* {
    762 		syscallarg(int) fd;
    763 		syscallarg(void *) buf;
    764 		syscallarg(size_t) nbyte;
    765 		syscallarg(off_t) offset;
    766 	} */
    767 	struct sys_pwrite_args pra;
    768 
    769 	SCARG(&pra, fd) = SCARG(uap, fd);
    770 	SCARG(&pra, buf) = SCARG(uap, buf);
    771 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    772 	SCARG(&pra, PAD) = 0;
    773 	SCARG(&pra, offset) = SCARG(uap, offset);
    774 
    775 	return sys_pwrite(l, &pra, retval);
    776 }
    777 
    778 int
    779 linux_sys_dup3(struct lwp *l, const struct linux_sys_dup3_args *uap,
    780     register_t *retval)
    781 {
    782 	/* {
    783 		syscallarg(int) from;
    784 		syscallarg(int) to;
    785 		syscallarg(int) flags;
    786 	} */
    787 	int error;
    788 	if ((error = sys_dup2(l, (const struct sys_dup2_args *)uap, retval)))
    789 		return error;
    790 
    791 	if (SCARG(uap, flags) & LINUX_O_CLOEXEC)
    792 		fd_set_exclose(l, SCARG(uap, to), true);
    793 
    794 	return 0;
    795 }
    796 
    797 
    798 int
    799 linux_to_bsd_atflags(int lflags)
    800 {
    801 	int bflags = 0;
    802 
    803 	if (lflags & LINUX_AT_SYMLINK_NOFOLLOW)
    804 		bflags |= AT_SYMLINK_NOFOLLOW;
    805 	if (lflags & LINUX_AT_REMOVEDIR)
    806 		bflags |= AT_REMOVEDIR;
    807 	if (lflags & LINUX_AT_SYMLINK_FOLLOW)
    808 		bflags |= AT_SYMLINK_FOLLOW;
    809 
    810 	return bflags;
    811 }
    812 
    813 
    814 #define LINUX_NOT_SUPPORTED(fun) \
    815 int \
    816 fun(struct lwp *l, const struct fun##_args *uap, register_t *retval) \
    817 { \
    818 	return EOPNOTSUPP; \
    819 }
    820 
    821 LINUX_NOT_SUPPORTED(linux_sys_setxattr)
    822 LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
    823 LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
    824 
    825 LINUX_NOT_SUPPORTED(linux_sys_getxattr)
    826 LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
    827 LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
    828 
    829 LINUX_NOT_SUPPORTED(linux_sys_listxattr)
    830 LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
    831 LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
    832 
    833 LINUX_NOT_SUPPORTED(linux_sys_removexattr)
    834 LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
    835 LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
    836