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