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