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