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