Home | History | Annotate | Line # | Download | only in common
linux_file.c revision 1.127
      1  1.127  riastrad /*	$NetBSD: linux_file.c,v 1.127 2024/10/01 17:00:27 riastradh Exp $	*/
      2   1.23       erh 
      3   1.23       erh /*-
      4   1.93        ad  * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
      5   1.23       erh  * All rights reserved.
      6   1.23       erh  *
      7   1.23       erh  * This code is derived from software contributed to The NetBSD Foundation
      8   1.25      fvdl  * by Frank van der Linden and Eric Haszlakiewicz.
      9   1.23       erh  *
     10   1.23       erh  * Redistribution and use in source and binary forms, with or without
     11   1.23       erh  * modification, are permitted provided that the following conditions
     12   1.23       erh  * are met:
     13   1.23       erh  * 1. Redistributions of source code must retain the above copyright
     14   1.23       erh  *    notice, this list of conditions and the following disclaimer.
     15   1.23       erh  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.23       erh  *    notice, this list of conditions and the following disclaimer in the
     17   1.23       erh  *    documentation and/or other materials provided with the distribution.
     18   1.23       erh  *
     19   1.23       erh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.23       erh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.23       erh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.23       erh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.23       erh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.23       erh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.23       erh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.23       erh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.23       erh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.23       erh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.23       erh  * POSSIBILITY OF SUCH DAMAGE.
     30    1.1      fvdl  */
     31    1.1      fvdl 
     32   1.23       erh /*
     33   1.23       erh  * Functions in multiarch:
     34   1.23       erh  *	linux_sys_llseek	: linux_llseek.c
     35   1.23       erh  */
     36   1.43     lukem 
     37   1.43     lukem #include <sys/cdefs.h>
     38  1.127  riastrad __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.127 2024/10/01 17:00:27 riastradh Exp $");
     39   1.23       erh 
     40  1.125  christos #include <sys/types.h>
     41    1.1      fvdl #include <sys/param.h>
     42    1.1      fvdl #include <sys/systm.h>
     43    1.1      fvdl #include <sys/namei.h>
     44    1.1      fvdl #include <sys/proc.h>
     45    1.1      fvdl #include <sys/file.h>
     46  1.103  christos #include <sys/fcntl.h>
     47    1.1      fvdl #include <sys/stat.h>
     48  1.125  christos #include <sys/vfs_syscalls.h>
     49    1.1      fvdl #include <sys/filedesc.h>
     50    1.1      fvdl #include <sys/ioctl.h>
     51    1.1      fvdl #include <sys/kernel.h>
     52    1.1      fvdl #include <sys/mount.h>
     53   1.81       dsl #include <sys/namei.h>
     54   1.13      fvdl #include <sys/vnode.h>
     55   1.13      fvdl #include <sys/tty.h>
     56   1.39      manu #include <sys/socketvar.h>
     57   1.13      fvdl #include <sys/conf.h>
     58   1.41  jdolecek #include <sys/pipe.h>
     59  1.125  christos #include <sys/fstrans.h>
     60    1.1      fvdl #include <sys/syscallargs.h>
     61   1.81       dsl #include <sys/vfs_syscalls.h>
     62    1.1      fvdl 
     63   1.24  christos #include <compat/linux/common/linux_types.h>
     64   1.24  christos #include <compat/linux/common/linux_signal.h>
     65   1.24  christos #include <compat/linux/common/linux_fcntl.h>
     66   1.24  christos #include <compat/linux/common/linux_util.h>
     67   1.24  christos #include <compat/linux/common/linux_machdep.h>
     68   1.84     njoly #include <compat/linux/common/linux_ipc.h>
     69   1.84     njoly #include <compat/linux/common/linux_sem.h>
     70   1.24  christos 
     71    1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     72   1.14  christos 
     73  1.125  christos #ifdef DEBUG_LINUX
     74  1.125  christos #define DPRINTF(a, ...)	uprintf(a, __VA_ARGS__)
     75  1.125  christos #else
     76  1.125  christos #define DPRINTF(a, ...)
     77  1.125  christos #endif
     78  1.125  christos 
     79  1.125  christos #define LINUX_COPY_FILE_RANGE_MAX_CHUNK 8192
     80  1.125  christos 
     81   1.87       dsl static int bsd_to_linux_ioflags(int);
     82  1.121       ryo #if !defined(__aarch64__) && !defined(__amd64__)
     83   1.87       dsl static void bsd_to_linux_stat(struct stat *, struct linux_stat *);
     84   1.67      manu #endif
     85   1.14  christos 
     86   1.92       dsl conv_linux_flock(linux, flock)
     87   1.92       dsl 
     88    1.1      fvdl /*
     89    1.1      fvdl  * Some file-related calls are handled here. The usual flag conversion
     90    1.1      fvdl  * an structure conversion is done, and alternate emul path searching.
     91    1.1      fvdl  */
     92    1.1      fvdl 
     93    1.1      fvdl /*
     94    1.1      fvdl  * The next two functions convert between the Linux and NetBSD values
     95    1.1      fvdl  * of the flags used in open(2) and fcntl(2).
     96    1.1      fvdl  */
     97  1.112     njoly int
     98   1.88       dsl linux_to_bsd_ioflags(int lflags)
     99    1.1      fvdl {
    100    1.1      fvdl 	int res = 0;
    101    1.1      fvdl 
    102    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
    103    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
    104    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
    105  1.113     njoly 
    106    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
    107    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
    108    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
    109    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
    110  1.113     njoly 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
    111  1.113     njoly 	res |= cvtto_bsd_mask(lflags, LINUX_O_NONBLOCK, O_NONBLOCK);
    112    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
    113    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
    114    1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
    115  1.113     njoly 	res |= cvtto_bsd_mask(lflags, LINUX_O_DIRECT, O_DIRECT);
    116  1.100       chs 	res |= cvtto_bsd_mask(lflags, LINUX_O_DIRECTORY, O_DIRECTORY);
    117  1.113     njoly 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOFOLLOW, O_NOFOLLOW);
    118  1.102  christos 	res |= cvtto_bsd_mask(lflags, LINUX_O_CLOEXEC, O_CLOEXEC);
    119    1.1      fvdl 
    120    1.1      fvdl 	return res;
    121    1.1      fvdl }
    122    1.1      fvdl 
    123    1.1      fvdl static int
    124   1.88       dsl bsd_to_linux_ioflags(int bflags)
    125    1.1      fvdl {
    126    1.1      fvdl 	int res = 0;
    127    1.1      fvdl 
    128    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
    129    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
    130    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
    131  1.113     njoly 
    132    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
    133    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
    134    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
    135    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
    136  1.113     njoly 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
    137  1.113     njoly 	res |= cvtto_linux_mask(bflags, O_NONBLOCK, LINUX_O_NONBLOCK);
    138    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
    139    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
    140    1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
    141  1.113     njoly 	res |= cvtto_linux_mask(bflags, O_DIRECT, LINUX_O_DIRECT);
    142  1.100       chs 	res |= cvtto_linux_mask(bflags, O_DIRECTORY, LINUX_O_DIRECTORY);
    143  1.113     njoly 	res |= cvtto_linux_mask(bflags, O_NOFOLLOW, LINUX_O_NOFOLLOW);
    144  1.102  christos 	res |= cvtto_linux_mask(bflags, O_CLOEXEC, LINUX_O_CLOEXEC);
    145    1.1      fvdl 
    146    1.1      fvdl 	return res;
    147    1.1      fvdl }
    148    1.1      fvdl 
    149  1.120   thorpej static inline off_t
    150  1.120   thorpej linux_hilo_to_off_t(unsigned long hi, unsigned long lo)
    151  1.120   thorpej {
    152  1.120   thorpej #ifdef _LP64
    153  1.120   thorpej 	/*
    154  1.120   thorpej 	 * Linux discards the "hi" portion on LP64 platforms; even though
    155  1.120   thorpej 	 * glibc puts of the upper 32-bits of the offset into the "hi"
    156  1.120   thorpej 	 * argument regardless, the "lo" argument has all the bits in
    157  1.120   thorpej 	 * this case.
    158  1.120   thorpej 	 */
    159  1.126  riastrad 	(void) hi;
    160  1.120   thorpej 	return (off_t)lo;
    161  1.120   thorpej #else
    162  1.120   thorpej 	return (((off_t)hi) << 32) | lo;
    163  1.120   thorpej #endif /* _LP64 */
    164  1.120   thorpej }
    165  1.120   thorpej 
    166  1.121       ryo #if !defined(__aarch64__)
    167    1.1      fvdl /*
    168    1.1      fvdl  * creat(2) is an obsolete function, but it's present as a Linux
    169    1.1      fvdl  * system call, so let's deal with it.
    170    1.1      fvdl  *
    171   1.23       erh  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
    172   1.23       erh  * in syscalls.master anyway so this doesn't have to be special cased.
    173   1.23       erh  *
    174    1.1      fvdl  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    175    1.1      fvdl  */
    176    1.1      fvdl int
    177  1.127  riastrad linux_sys_creat(struct lwp *l, const struct linux_sys_creat_args *uap,
    178  1.127  riastrad     register_t *retval)
    179   1.11   thorpej {
    180   1.90       dsl 	/* {
    181   1.27  christos 		syscallarg(const char *) path;
    182  1.108     njoly 		syscallarg(linux_umode_t) mode;
    183   1.90       dsl 	} */
    184   1.12   mycroft 	struct sys_open_args oa;
    185    1.1      fvdl 
    186    1.1      fvdl 	SCARG(&oa, path) = SCARG(uap, path);
    187    1.1      fvdl 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    188    1.1      fvdl 	SCARG(&oa, mode) = SCARG(uap, mode);
    189   1.12   mycroft 
    190   1.56   thorpej 	return sys_open(l, &oa, retval);
    191    1.1      fvdl }
    192  1.121       ryo #endif
    193    1.1      fvdl 
    194  1.106       chs static void
    195  1.106       chs linux_open_ctty(struct lwp *l, int flags, int fd)
    196  1.106       chs {
    197  1.106       chs 	struct proc *p = l->l_proc;
    198  1.106       chs 
    199  1.106       chs 	/*
    200  1.106       chs 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    201  1.106       chs 	 * If we are a session leader, and we don't have a controlling
    202  1.106       chs 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    203  1.106       chs 	 * this the controlling terminal.
    204  1.106       chs 	 */
    205  1.106       chs         if (!(flags & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    206  1.106       chs                 file_t *fp;
    207  1.106       chs 
    208  1.106       chs 		fp = fd_getfile(fd);
    209  1.106       chs 
    210  1.106       chs                 /* ignore any error, just give it a try */
    211  1.106       chs                 if (fp != NULL) {
    212  1.106       chs 			if (fp->f_type == DTYPE_VNODE) {
    213  1.106       chs 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, NULL);
    214  1.106       chs 			}
    215  1.106       chs 			fd_putfile(fd);
    216  1.106       chs 		}
    217  1.106       chs         }
    218  1.106       chs }
    219  1.106       chs 
    220    1.1      fvdl /*
    221    1.1      fvdl  * open(2). Take care of the different flag values, and let the
    222    1.1      fvdl  * NetBSD syscall do the real work. See if this operation
    223    1.1      fvdl  * gives the current process a controlling terminal.
    224    1.1      fvdl  * (XXX is this necessary?)
    225    1.1      fvdl  */
    226    1.1      fvdl int
    227  1.127  riastrad linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap,
    228  1.127  riastrad     register_t *retval)
    229   1.11   thorpej {
    230   1.90       dsl 	/* {
    231   1.27  christos 		syscallarg(const char *) path;
    232    1.1      fvdl 		syscallarg(int) flags;
    233  1.108     njoly 		syscallarg(linux_umode_t) mode;
    234   1.90       dsl 	} */
    235    1.1      fvdl 	int error, fl;
    236   1.12   mycroft 	struct sys_open_args boa;
    237    1.1      fvdl 
    238    1.2      fvdl 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    239    1.1      fvdl 
    240    1.1      fvdl 	SCARG(&boa, path) = SCARG(uap, path);
    241    1.1      fvdl 	SCARG(&boa, flags) = fl;
    242    1.1      fvdl 	SCARG(&boa, mode) = SCARG(uap, mode);
    243    1.2      fvdl 
    244   1.56   thorpej 	if ((error = sys_open(l, &boa, retval)))
    245  1.113     njoly 		return (error == EFTYPE) ? ELOOP : error;
    246    1.1      fvdl 
    247  1.106       chs 	linux_open_ctty(l, fl, *retval);
    248  1.106       chs 	return 0;
    249  1.106       chs }
    250  1.106       chs 
    251  1.106       chs int
    252  1.127  riastrad linux_sys_openat(struct lwp *l, const struct linux_sys_openat_args *uap,
    253  1.127  riastrad     register_t *retval)
    254  1.106       chs {
    255  1.106       chs 	/* {
    256  1.107     njoly 		syscallarg(int) fd;
    257  1.106       chs 		syscallarg(const char *) path;
    258  1.106       chs 		syscallarg(int) flags;
    259  1.108     njoly 		syscallarg(linux_umode_t) mode;
    260  1.106       chs 	} */
    261  1.106       chs 	int error, fl;
    262  1.106       chs 	struct sys_openat_args boa;
    263  1.106       chs 
    264  1.106       chs 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    265  1.106       chs 
    266  1.106       chs 	SCARG(&boa, fd) = SCARG(uap, fd);
    267  1.106       chs 	SCARG(&boa, path) = SCARG(uap, path);
    268  1.106       chs 	SCARG(&boa, oflags) = fl;
    269  1.106       chs 	SCARG(&boa, mode) = SCARG(uap, mode);
    270   1.38   thorpej 
    271  1.106       chs 	if ((error = sys_openat(l, &boa, retval)))
    272  1.113     njoly 		return (error == EFTYPE) ? ELOOP : error;
    273    1.1      fvdl 
    274  1.106       chs 	linux_open_ctty(l, fl, *retval);
    275    1.1      fvdl 	return 0;
    276    1.1      fvdl }
    277    1.1      fvdl 
    278    1.1      fvdl /*
    279    1.1      fvdl  * Most actions in the fcntl() call are straightforward; simply
    280    1.1      fvdl  * pass control to the NetBSD system call. A few commands need
    281    1.1      fvdl  * conversions after the actual system call has done its work,
    282    1.1      fvdl  * because the flag values and lock structure are different.
    283    1.1      fvdl  */
    284    1.1      fvdl int
    285  1.127  riastrad linux_sys_fcntl(struct lwp *l, const struct linux_sys_fcntl_args *uap,
    286  1.127  riastrad     register_t *retval)
    287   1.11   thorpej {
    288   1.90       dsl 	/* {
    289    1.1      fvdl 		syscallarg(int) fd;
    290    1.1      fvdl 		syscallarg(int) cmd;
    291    1.1      fvdl 		syscallarg(void *) arg;
    292   1.90       dsl 	} */
    293   1.56   thorpej 	struct proc *p = l->l_proc;
    294   1.23       erh 	int fd, cmd, error;
    295   1.23       erh 	u_long val;
    296   1.83       dsl 	void *arg;
    297   1.12   mycroft 	struct sys_fcntl_args fca;
    298   1.93        ad 	file_t *fp;
    299   1.13      fvdl 	struct vnode *vp;
    300   1.13      fvdl 	struct vattr va;
    301   1.13      fvdl 	long pgid;
    302   1.13      fvdl 	struct pgrp *pgrp;
    303   1.94        ad 	struct tty *tp;
    304    1.1      fvdl 
    305    1.1      fvdl 	fd = SCARG(uap, fd);
    306    1.1      fvdl 	cmd = SCARG(uap, cmd);
    307   1.92       dsl 	arg = SCARG(uap, arg);
    308    1.1      fvdl 
    309    1.1      fvdl 	switch (cmd) {
    310   1.92       dsl 
    311    1.1      fvdl 	case LINUX_F_DUPFD:
    312    1.1      fvdl 		cmd = F_DUPFD;
    313    1.1      fvdl 		break;
    314   1.92       dsl 
    315    1.1      fvdl 	case LINUX_F_GETFD:
    316    1.1      fvdl 		cmd = F_GETFD;
    317    1.1      fvdl 		break;
    318   1.92       dsl 
    319    1.1      fvdl 	case LINUX_F_SETFD:
    320    1.1      fvdl 		cmd = F_SETFD;
    321    1.1      fvdl 		break;
    322   1.92       dsl 
    323    1.1      fvdl 	case LINUX_F_GETFL:
    324    1.1      fvdl 		SCARG(&fca, fd) = fd;
    325    1.1      fvdl 		SCARG(&fca, cmd) = F_GETFL;
    326    1.1      fvdl 		SCARG(&fca, arg) = arg;
    327   1.56   thorpej 		if ((error = sys_fcntl(l, &fca, retval)))
    328    1.1      fvdl 			return error;
    329    1.1      fvdl 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    330    1.1      fvdl 		return 0;
    331   1.92       dsl 
    332   1.41  jdolecek 	case LINUX_F_SETFL: {
    333   1.93        ad 		file_t	*fp1 = NULL;
    334   1.41  jdolecek 
    335   1.23       erh 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
    336   1.39      manu 		/*
    337   1.41  jdolecek 		 * Linux seems to have same semantics for sending SIGIO to the
    338   1.64       abs 		 * read side of socket, but slightly different semantics
    339   1.41  jdolecek 		 * for SIGIO to the write side.  Rather than sending the SIGIO
    340   1.41  jdolecek 		 * every time it's possible to write (directly) more data, it
    341   1.41  jdolecek 		 * only sends SIGIO if last write(2) failed due to insufficient
    342   1.41  jdolecek 		 * memory to hold the data. This is compatible enough
    343   1.41  jdolecek 		 * with NetBSD semantics to not do anything about the
    344   1.41  jdolecek 		 * difference.
    345   1.65     perry 		 *
    346   1.41  jdolecek 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
    347   1.41  jdolecek 		 * ones and DTYPE_PIPE ones. For these, we don't set
    348   1.41  jdolecek 		 * the underlying flags (we don't pass O_ASYNC flag down
    349   1.41  jdolecek 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
    350   1.41  jdolecek 		 * so that F_GETFL would report the ASYNC i/o is on.
    351   1.39      manu 		 */
    352   1.41  jdolecek 		if (val & O_ASYNC) {
    353   1.93        ad 			if (((fp1 = fd_getfile(fd)) == NULL))
    354   1.39      manu 			    return (EBADF);
    355   1.69  christos 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
    356   1.69  christos 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
    357   1.69  christos 			    || (fp1->f_type == DTYPE_PIPE))
    358   1.41  jdolecek 				val &= ~O_ASYNC;
    359   1.41  jdolecek 			else {
    360   1.41  jdolecek 				/* not a pipe, do not modify anything */
    361   1.93        ad 				fd_putfile(fd);
    362   1.69  christos 				fp1 = NULL;
    363   1.39      manu 			}
    364   1.41  jdolecek 		}
    365   1.41  jdolecek 
    366   1.41  jdolecek 		SCARG(&fca, fd) = fd;
    367   1.41  jdolecek 		SCARG(&fca, cmd) = F_SETFL;
    368   1.79  christos 		SCARG(&fca, arg) = (void *) val;
    369   1.39      manu 
    370   1.56   thorpej 		error = sys_fcntl(l, &fca, retval);
    371   1.41  jdolecek 
    372   1.41  jdolecek 		/* Now set the FASYNC flag for pipes */
    373   1.69  christos 		if (fp1) {
    374   1.93        ad 			if (!error) {
    375   1.93        ad 				mutex_enter(&fp1->f_lock);
    376   1.69  christos 				fp1->f_flag |= FASYNC;
    377   1.93        ad 				mutex_exit(&fp1->f_lock);
    378   1.93        ad 			}
    379   1.93        ad 			fd_putfile(fd);
    380   1.39      manu 		}
    381   1.41  jdolecek 
    382   1.41  jdolecek 		return (error);
    383   1.41  jdolecek 	    }
    384   1.92       dsl 
    385    1.1      fvdl 	case LINUX_F_GETLK:
    386   1.92       dsl 		do_linux_getlk(fd, cmd, arg, linux, flock);
    387   1.47  christos 
    388    1.1      fvdl 	case LINUX_F_SETLK:
    389    1.1      fvdl 	case LINUX_F_SETLKW:
    390   1.92       dsl 		do_linux_setlk(fd, cmd, arg, linux, flock, LINUX_F_SETLK);
    391   1.47  christos 
    392    1.1      fvdl 	case LINUX_F_SETOWN:
    393   1.65     perry 	case LINUX_F_GETOWN:
    394   1.13      fvdl 		/*
    395   1.49  jdolecek 		 * We need to route fcntl() for tty descriptors around normal
    396   1.49  jdolecek 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
    397   1.49  jdolecek 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
    398   1.49  jdolecek 		 * this is not a problem.
    399   1.13      fvdl 		 */
    400   1.93        ad 		if ((fp = fd_getfile(fd)) == NULL)
    401   1.13      fvdl 			return EBADF;
    402   1.60  jdolecek 
    403   1.60  jdolecek 		/* Check it's a character device vnode */
    404   1.60  jdolecek 		if (fp->f_type != DTYPE_VNODE
    405   1.60  jdolecek 		    || (vp = (struct vnode *)fp->f_data) == NULL
    406   1.60  jdolecek 		    || vp->v_type != VCHR) {
    407   1.93        ad 			fd_putfile(fd);
    408   1.60  jdolecek 
    409   1.53   gehenna 	    not_tty:
    410   1.49  jdolecek 			/* Not a tty, proceed with common fcntl() */
    411   1.13      fvdl 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    412   1.49  jdolecek 			break;
    413   1.49  jdolecek 		}
    414   1.47  christos 
    415  1.104   hannken 		vn_lock(vp, LK_SHARED | LK_RETRY);
    416   1.86     pooka 		error = VOP_GETATTR(vp, &va, l->l_cred);
    417  1.104   hannken 		VOP_UNLOCK(vp);
    418   1.60  jdolecek 
    419   1.93        ad 		fd_putfile(fd);
    420   1.60  jdolecek 
    421   1.60  jdolecek 		if (error)
    422   1.49  jdolecek 			return error;
    423   1.60  jdolecek 
    424   1.94        ad 		if ((tp = cdev_tty(va.va_rdev)) == NULL)
    425   1.53   gehenna 			goto not_tty;
    426   1.49  jdolecek 
    427   1.49  jdolecek 		/* set tty pg_id appropriately */
    428  1.118        ad 		mutex_enter(&proc_lock);
    429   1.49  jdolecek 		if (cmd == LINUX_F_GETOWN) {
    430   1.58       dsl 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
    431  1.118        ad 			mutex_exit(&proc_lock);
    432   1.49  jdolecek 			return 0;
    433   1.49  jdolecek 		}
    434   1.49  jdolecek 		if ((long)arg <= 0) {
    435   1.49  jdolecek 			pgid = -(long)arg;
    436   1.49  jdolecek 		} else {
    437   1.99     rmind 			struct proc *p1 = proc_find((long)arg);
    438   1.99     rmind 			if (p1 == NULL) {
    439  1.118        ad 				mutex_exit(&proc_lock);
    440   1.49  jdolecek 				return (ESRCH);
    441   1.99     rmind 			}
    442   1.49  jdolecek 			pgid = (long)p1->p_pgrp->pg_id;
    443   1.13      fvdl 		}
    444   1.99     rmind 		pgrp = pgrp_find(pgid);
    445   1.78       dsl 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
    446  1.118        ad 			mutex_exit(&proc_lock);
    447   1.49  jdolecek 			return EPERM;
    448   1.78       dsl 		}
    449   1.49  jdolecek 		tp->t_pgrp = pgrp;
    450  1.118        ad 		mutex_exit(&proc_lock);
    451   1.49  jdolecek 		return 0;
    452   1.47  christos 
    453  1.115     njoly 	case LINUX_F_DUPFD_CLOEXEC:
    454  1.115     njoly 		cmd = F_DUPFD_CLOEXEC;
    455  1.115     njoly 		break;
    456  1.115     njoly 
    457  1.123  christos 	case LINUX_F_ADD_SEALS:
    458  1.123  christos 		cmd = F_ADD_SEALS;
    459  1.123  christos 		break;
    460  1.123  christos 
    461  1.123  christos 	case LINUX_F_GET_SEALS:
    462  1.123  christos 		cmd = F_GET_SEALS;
    463  1.123  christos 		break;
    464  1.123  christos 
    465    1.1      fvdl 	default:
    466    1.1      fvdl 		return EOPNOTSUPP;
    467    1.1      fvdl 	}
    468    1.1      fvdl 
    469    1.1      fvdl 	SCARG(&fca, fd) = fd;
    470    1.1      fvdl 	SCARG(&fca, cmd) = cmd;
    471    1.1      fvdl 	SCARG(&fca, arg) = arg;
    472   1.12   mycroft 
    473   1.56   thorpej 	return sys_fcntl(l, &fca, retval);
    474    1.1      fvdl }
    475    1.1      fvdl 
    476  1.121       ryo #if !defined(__aarch64__) && !defined(__amd64__)
    477    1.1      fvdl /*
    478    1.1      fvdl  * Convert a NetBSD stat structure to a Linux stat structure.
    479    1.1      fvdl  * Only the order of the fields and the padding in the structure
    480    1.9      fvdl  * is different. linux_fakedev is a machine-dependent function
    481    1.9      fvdl  * which optionally converts device driver major/minor numbers
    482    1.9      fvdl  * (XXX horrible, but what can you do against code that compares
    483    1.9      fvdl  * things against constant major device numbers? sigh)
    484    1.1      fvdl  */
    485    1.1      fvdl static void
    486   1.88       dsl bsd_to_linux_stat(struct stat *bsp, struct linux_stat *lsp)
    487    1.1      fvdl {
    488   1.12   mycroft 
    489  1.119  riastrad 	memset(lsp, 0, sizeof(*lsp));
    490   1.45  christos 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
    491    1.1      fvdl 	lsp->lst_ino     = bsp->st_ino;
    492   1.19  christos 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
    493   1.19  christos 	if (bsp->st_nlink >= (1 << 15))
    494   1.19  christos 		lsp->lst_nlink = (1 << 15) - 1;
    495   1.19  christos 	else
    496   1.19  christos 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
    497    1.1      fvdl 	lsp->lst_uid     = bsp->st_uid;
    498    1.1      fvdl 	lsp->lst_gid     = bsp->st_gid;
    499   1.45  christos 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    500    1.1      fvdl 	lsp->lst_size    = bsp->st_size;
    501    1.1      fvdl 	lsp->lst_blksize = bsp->st_blksize;
    502    1.1      fvdl 	lsp->lst_blocks  = bsp->st_blocks;
    503    1.1      fvdl 	lsp->lst_atime   = bsp->st_atime;
    504    1.1      fvdl 	lsp->lst_mtime   = bsp->st_mtime;
    505    1.1      fvdl 	lsp->lst_ctime   = bsp->st_ctime;
    506   1.66  christos #ifdef LINUX_STAT_HAS_NSEC
    507   1.66  christos 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    508   1.66  christos 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    509   1.66  christos 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    510   1.66  christos #endif
    511    1.1      fvdl }
    512    1.1      fvdl 
    513    1.1      fvdl /*
    514    1.1      fvdl  * The stat functions below are plain sailing. stat and lstat are handled
    515    1.1      fvdl  * by one function to avoid code duplication.
    516    1.1      fvdl  */
    517    1.1      fvdl int
    518  1.127  riastrad linux_sys_fstat(struct lwp *l, const struct linux_sys_fstat_args *uap,
    519  1.127  riastrad     register_t *retval)
    520   1.11   thorpej {
    521   1.90       dsl 	/* {
    522    1.1      fvdl 		syscallarg(int) fd;
    523    1.1      fvdl 		syscallarg(linux_stat *) sp;
    524   1.90       dsl 	} */
    525    1.1      fvdl 	struct linux_stat tmplst;
    526   1.81       dsl 	struct stat tmpst;
    527    1.1      fvdl 	int error;
    528    1.1      fvdl 
    529   1.93        ad 	error = do_sys_fstat(SCARG(uap, fd), &tmpst);
    530   1.81       dsl 	if (error != 0)
    531    1.1      fvdl 		return error;
    532    1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    533    1.1      fvdl 
    534   1.81       dsl 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    535    1.1      fvdl }
    536    1.1      fvdl 
    537    1.1      fvdl static int
    538  1.127  riastrad linux_stat1(const struct linux_sys_stat_args *uap, register_t *retval,
    539  1.127  riastrad     int flags)
    540    1.1      fvdl {
    541    1.1      fvdl 	struct linux_stat tmplst;
    542   1.81       dsl 	struct stat tmpst;
    543    1.1      fvdl 	int error;
    544    1.1      fvdl 
    545   1.93        ad 	error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
    546   1.81       dsl 	if (error != 0)
    547    1.1      fvdl 		return error;
    548    1.1      fvdl 
    549    1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    550    1.1      fvdl 
    551   1.81       dsl 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    552    1.1      fvdl }
    553    1.1      fvdl 
    554    1.1      fvdl int
    555  1.127  riastrad linux_sys_stat(struct lwp *l, const struct linux_sys_stat_args *uap,
    556  1.127  riastrad     register_t *retval)
    557   1.11   thorpej {
    558   1.90       dsl 	/* {
    559   1.27  christos 		syscallarg(const char *) path;
    560    1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    561   1.90       dsl 	} */
    562   1.11   thorpej 
    563   1.93        ad 	return linux_stat1(uap, retval, FOLLOW);
    564    1.1      fvdl }
    565    1.1      fvdl 
    566   1.23       erh /* Note: this is "newlstat" in the Linux sources */
    567   1.23       erh /*	(we don't bother with the old lstat currently) */
    568    1.1      fvdl int
    569  1.127  riastrad linux_sys_lstat(struct lwp *l, const struct linux_sys_lstat_args *uap,
    570  1.127  riastrad     register_t *retval)
    571   1.11   thorpej {
    572   1.90       dsl 	/* {
    573   1.27  christos 		syscallarg(const char *) path;
    574    1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    575   1.90       dsl 	} */
    576   1.11   thorpej 
    577   1.93        ad 	return linux_stat1((const void *)uap, retval, NOFOLLOW);
    578    1.1      fvdl }
    579  1.121       ryo #endif /* !__aarch64__ && !__amd64__ */
    580    1.1      fvdl 
    581    1.1      fvdl /*
    582   1.10      fvdl  * The following syscalls are mostly here because of the alternate path check.
    583    1.1      fvdl  */
    584  1.106       chs 
    585    1.1      fvdl int
    586  1.127  riastrad linux_sys_linkat(struct lwp *l, const struct linux_sys_linkat_args *uap,
    587  1.127  riastrad     register_t *retval)
    588    1.2      fvdl {
    589   1.90       dsl 	/* {
    590  1.106       chs 		syscallarg(int) fd1;
    591  1.106       chs 		syscallarg(const char *) name1;
    592  1.106       chs 		syscallarg(int) fd2;
    593  1.106       chs 		syscallarg(const char *) name2;
    594  1.106       chs 		syscallarg(int) flags;
    595   1.90       dsl 	} */
    596  1.106       chs 	int fd1 = SCARG(uap, fd1);
    597  1.106       chs 	const char *name1 = SCARG(uap, name1);
    598  1.106       chs 	int fd2 = SCARG(uap, fd2);
    599  1.106       chs 	const char *name2 = SCARG(uap, name2);
    600  1.106       chs 	int follow;
    601  1.106       chs 
    602  1.106       chs 	follow = SCARG(uap, flags) & LINUX_AT_SYMLINK_FOLLOW;
    603  1.106       chs 
    604  1.106       chs 	return do_sys_linkat(l, fd1, name1, fd2, name2, follow, retval);
    605  1.106       chs }
    606  1.106       chs 
    607  1.106       chs static int
    608  1.106       chs linux_unlink_dircheck(const char *path)
    609  1.106       chs {
    610  1.106       chs 	struct nameidata nd;
    611  1.101  dholland 	struct pathbuf *pb;
    612  1.106       chs 	int error;
    613   1.63  jdolecek 
    614   1.63  jdolecek 	/*
    615   1.63  jdolecek 	 * Linux returns EISDIR if unlink(2) is called on a directory.
    616   1.63  jdolecek 	 * We return EPERM in such cases. To emulate correct behaviour,
    617   1.63  jdolecek 	 * check if the path points to directory and return EISDIR if this
    618   1.63  jdolecek 	 * is the case.
    619  1.101  dholland 	 *
    620  1.101  dholland 	 * XXX this should really not copy in the path buffer twice...
    621   1.63  jdolecek 	 */
    622  1.106       chs 	error = pathbuf_copyin(path, &pb);
    623  1.106       chs 	if (error) {
    624  1.106       chs 		return error;
    625  1.101  dholland 	}
    626  1.101  dholland 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
    627   1.63  jdolecek 	if (namei(&nd) == 0) {
    628   1.63  jdolecek 		struct stat sb;
    629   1.63  jdolecek 
    630   1.93        ad 		if (vn_stat(nd.ni_vp, &sb) == 0
    631   1.63  jdolecek 		    && S_ISDIR(sb.st_mode))
    632   1.63  jdolecek 			error = EISDIR;
    633   1.63  jdolecek 
    634   1.63  jdolecek 		vput(nd.ni_vp);
    635   1.63  jdolecek 	}
    636  1.101  dholland 	pathbuf_destroy(pb);
    637  1.106       chs 	return error ? error : EPERM;
    638  1.106       chs }
    639   1.63  jdolecek 
    640  1.106       chs int
    641  1.127  riastrad linux_sys_unlink(struct lwp *l, const struct linux_sys_unlink_args *uap,
    642  1.127  riastrad     register_t *retval)
    643  1.106       chs {
    644  1.106       chs 	/* {
    645  1.106       chs 		syscallarg(const char *) path;
    646  1.106       chs 	} */
    647  1.106       chs 	int error;
    648  1.106       chs 
    649  1.106       chs 	error = sys_unlink(l, (const void *)uap, retval);
    650  1.106       chs 	if (error == EPERM)
    651  1.106       chs 		error = linux_unlink_dircheck(SCARG(uap, path));
    652  1.106       chs 
    653  1.106       chs 	return error;
    654  1.106       chs }
    655  1.106       chs 
    656  1.106       chs int
    657  1.127  riastrad linux_sys_unlinkat(struct lwp *l, const struct linux_sys_unlinkat_args *uap,
    658  1.127  riastrad     register_t *retval)
    659  1.106       chs {
    660  1.106       chs 	/* {
    661  1.106       chs 		syscallarg(int) fd;
    662  1.106       chs 		syscallarg(const char *) path;
    663  1.106       chs 		syscallarg(int) flag;
    664  1.106       chs 	} */
    665  1.106       chs 	struct sys_unlinkat_args ua;
    666  1.106       chs 	int error;
    667  1.106       chs 
    668  1.106       chs 	SCARG(&ua, fd) = SCARG(uap, fd);
    669  1.106       chs 	SCARG(&ua, path) = SCARG(uap, path);
    670  1.106       chs 	SCARG(&ua, flag) = linux_to_bsd_atflags(SCARG(uap, flag));
    671  1.106       chs 
    672  1.106       chs 	error = sys_unlinkat(l, &ua, retval);
    673  1.106       chs 	if (error == EPERM)
    674  1.106       chs 		error = linux_unlink_dircheck(SCARG(uap, path));
    675  1.106       chs 
    676  1.106       chs 	return error;
    677    1.2      fvdl }
    678    1.2      fvdl 
    679   1.10      fvdl int
    680  1.127  riastrad linux_sys_mknod(struct lwp *l, const struct linux_sys_mknod_args *uap,
    681  1.127  riastrad     register_t *retval)
    682   1.11   thorpej {
    683   1.90       dsl 	/* {
    684   1.27  christos 		syscallarg(const char *) path;
    685  1.106       chs 		syscallarg(linux_umode_t) mode;
    686  1.106       chs 		syscallarg(unsigned) dev;
    687  1.106       chs 	} */
    688  1.106       chs 	struct linux_sys_mknodat_args ua;
    689  1.106       chs 
    690  1.106       chs 	SCARG(&ua, fd) = LINUX_AT_FDCWD;
    691  1.106       chs 	SCARG(&ua, path) = SCARG(uap, path);
    692  1.106       chs 	SCARG(&ua, mode) = SCARG(uap, mode);
    693  1.106       chs 	SCARG(&ua, dev) = SCARG(uap, dev);
    694  1.106       chs 
    695  1.106       chs 	return linux_sys_mknodat(l, &ua, retval);
    696  1.106       chs }
    697  1.106       chs 
    698  1.106       chs int
    699  1.127  riastrad linux_sys_mknodat(struct lwp *l, const struct linux_sys_mknodat_args *uap,
    700  1.127  riastrad     register_t *retval)
    701  1.106       chs {
    702  1.106       chs 	/* {
    703  1.106       chs 		syscallarg(int) fd;
    704  1.106       chs 		syscallarg(const char *) path;
    705  1.106       chs 		syscallarg(linux_umode_t) mode;
    706  1.106       chs 		syscallarg(unsigned) dev;
    707   1.90       dsl 	} */
    708    1.2      fvdl 
    709   1.10      fvdl 	/*
    710   1.40       wiz 	 * BSD handles FIFOs separately
    711   1.10      fvdl 	 */
    712   1.74     pavel 	if (S_ISFIFO(SCARG(uap, mode))) {
    713  1.106       chs 		struct sys_mkfifoat_args bma;
    714   1.54  jdolecek 
    715  1.106       chs 		SCARG(&bma, fd) = SCARG(uap, fd);
    716   1.21   thorpej 		SCARG(&bma, path) = SCARG(uap, path);
    717   1.21   thorpej 		SCARG(&bma, mode) = SCARG(uap, mode);
    718  1.106       chs 		return sys_mkfifoat(l, &bma, retval);
    719   1.54  jdolecek 	} else {
    720   1.54  jdolecek 
    721   1.54  jdolecek 		/*
    722   1.54  jdolecek 		 * Linux device numbers uses 8 bits for minor and 8 bits
    723   1.54  jdolecek 		 * for major. Due to how we map our major and minor,
    724   1.82       dsl 		 * this just fits into our dev_t. Just mask off the
    725   1.54  jdolecek 		 * upper 16bit to remove any random junk.
    726   1.54  jdolecek 		 */
    727  1.106       chs 
    728  1.106       chs 		return do_sys_mknodat(l, SCARG(uap, fd), SCARG(uap, path),
    729  1.116     kamil 		    SCARG(uap, mode), SCARG(uap, dev) & 0xffff, UIO_USERSPACE);
    730   1.54  jdolecek 	}
    731    1.2      fvdl }
    732    1.2      fvdl 
    733  1.106       chs int
    734  1.127  riastrad linux_sys_fchmodat(struct lwp *l, const struct linux_sys_fchmodat_args *uap,
    735  1.127  riastrad     register_t *retval)
    736  1.106       chs {
    737  1.106       chs 	/* {
    738  1.106       chs 		syscallarg(int) fd;
    739  1.106       chs 		syscallarg(const char *) path;
    740  1.106       chs 		syscallarg(linux_umode_t) mode;
    741  1.106       chs 	} */
    742  1.106       chs 
    743  1.106       chs 	return do_sys_chmodat(l, SCARG(uap, fd), SCARG(uap, path),
    744  1.106       chs 			      SCARG(uap, mode), AT_SYMLINK_FOLLOW);
    745  1.106       chs }
    746  1.106       chs 
    747  1.106       chs int
    748  1.127  riastrad linux_sys_fchownat(struct lwp *l, const struct linux_sys_fchownat_args *uap,
    749  1.127  riastrad     register_t *retval)
    750  1.106       chs {
    751  1.106       chs 	/* {
    752  1.106       chs 		syscallarg(int) fd;
    753  1.106       chs 		syscallarg(const char *) path;
    754  1.106       chs 		syscallarg(uid_t) owner;
    755  1.106       chs 		syscallarg(gid_t) group;
    756  1.106       chs 		syscallarg(int) flag;
    757  1.106       chs 	} */
    758  1.106       chs 	int flag;
    759  1.106       chs 
    760  1.106       chs 	flag = linux_to_bsd_atflags(SCARG(uap, flag));
    761  1.106       chs 	return do_sys_chownat(l, SCARG(uap, fd), SCARG(uap, path),
    762  1.106       chs 			      SCARG(uap, owner), SCARG(uap, group), flag);
    763  1.106       chs }
    764  1.106       chs 
    765  1.106       chs int
    766  1.127  riastrad linux_sys_faccessat(struct lwp *l, const struct linux_sys_faccessat_args *uap,
    767  1.127  riastrad     register_t *retval)
    768  1.106       chs {
    769  1.106       chs 	/* {
    770  1.106       chs 		syscallarg(int) fd;
    771  1.106       chs 		syscallarg(const char *) path;
    772  1.106       chs 		syscallarg(int) amode;
    773  1.106       chs 	} */
    774  1.106       chs 
    775  1.106       chs 	return do_sys_accessat(l, SCARG(uap, fd), SCARG(uap, path),
    776  1.106       chs 	     SCARG(uap, amode), AT_SYMLINK_FOLLOW);
    777  1.106       chs }
    778  1.106       chs 
    779   1.15      fvdl /*
    780   1.15      fvdl  * This is just fsync() for now (just as it is in the Linux kernel)
    781   1.23       erh  * Note: this is not implemented under Linux on Alpha and Arm
    782   1.23       erh  *	but should still be defined in our syscalls.master.
    783   1.23       erh  *	(syscall #148 on the arm)
    784   1.15      fvdl  */
    785   1.15      fvdl int
    786  1.127  riastrad linux_sys_fdatasync(struct lwp *l, const struct linux_sys_fdatasync_args *uap,
    787  1.127  riastrad     register_t *retval)
    788   1.15      fvdl {
    789   1.90       dsl 	/* {
    790   1.15      fvdl 		syscallarg(int) fd;
    791   1.90       dsl 	} */
    792   1.90       dsl 
    793   1.90       dsl 	return sys_fsync(l, (const void *)uap, retval);
    794   1.28      tron }
    795   1.28      tron 
    796   1.28      tron /*
    797   1.28      tron  * pread(2).
    798   1.28      tron  */
    799   1.28      tron int
    800  1.127  riastrad linux_sys_pread(struct lwp *l, const struct linux_sys_pread_args *uap,
    801  1.127  riastrad     register_t *retval)
    802   1.28      tron {
    803   1.90       dsl 	/* {
    804   1.28      tron 		syscallarg(int) fd;
    805   1.28      tron 		syscallarg(void *) buf;
    806   1.28      tron 		syscallarg(size_t) nbyte;
    807  1.109     njoly 		syscallarg(off_t) offset;
    808   1.90       dsl 	} */
    809   1.28      tron 	struct sys_pread_args pra;
    810   1.28      tron 
    811   1.28      tron 	SCARG(&pra, fd) = SCARG(uap, fd);
    812   1.28      tron 	SCARG(&pra, buf) = SCARG(uap, buf);
    813   1.28      tron 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    814  1.109     njoly 	SCARG(&pra, PAD) = 0;
    815   1.28      tron 	SCARG(&pra, offset) = SCARG(uap, offset);
    816   1.28      tron 
    817   1.62  jdolecek 	return sys_pread(l, &pra, retval);
    818   1.28      tron }
    819   1.28      tron 
    820   1.28      tron /*
    821   1.28      tron  * pwrite(2).
    822   1.28      tron  */
    823   1.28      tron int
    824  1.127  riastrad linux_sys_pwrite(struct lwp *l, const struct linux_sys_pwrite_args *uap,
    825  1.127  riastrad     register_t *retval)
    826   1.28      tron {
    827   1.90       dsl 	/* {
    828   1.28      tron 		syscallarg(int) fd;
    829   1.28      tron 		syscallarg(void *) buf;
    830   1.28      tron 		syscallarg(size_t) nbyte;
    831  1.110     njoly 		syscallarg(off_t) offset;
    832   1.90       dsl 	} */
    833   1.28      tron 	struct sys_pwrite_args pra;
    834   1.28      tron 
    835   1.28      tron 	SCARG(&pra, fd) = SCARG(uap, fd);
    836   1.28      tron 	SCARG(&pra, buf) = SCARG(uap, buf);
    837   1.28      tron 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    838  1.109     njoly 	SCARG(&pra, PAD) = 0;
    839   1.28      tron 	SCARG(&pra, offset) = SCARG(uap, offset);
    840   1.28      tron 
    841   1.62  jdolecek 	return sys_pwrite(l, &pra, retval);
    842    1.1      fvdl }
    843   1.68      fvdl 
    844  1.120   thorpej /*
    845  1.120   thorpej  * preadv(2)
    846  1.120   thorpej  */
    847  1.120   thorpej int
    848  1.120   thorpej linux_sys_preadv(struct lwp *l, const struct linux_sys_preadv_args *uap,
    849  1.120   thorpej     register_t *retval)
    850  1.120   thorpej {
    851  1.120   thorpej 	/* {
    852  1.120   thorpej 		syscallarg(int) fd;
    853  1.120   thorpej 		syscallarg(const struct iovec *) iovp;
    854  1.120   thorpej 		syscallarg(int) iovcnt;
    855  1.120   thorpej 		syscallarg(unsigned long) off_lo;
    856  1.120   thorpej 		syscallarg(unsigned long) off_hi;
    857  1.120   thorpej 	} */
    858  1.120   thorpej 	struct sys_preadv_args ua;
    859  1.120   thorpej 
    860  1.120   thorpej 	SCARG(&ua, fd) = SCARG(uap, fd);
    861  1.120   thorpej 	SCARG(&ua, iovp) = SCARG(uap, iovp);
    862  1.120   thorpej 	SCARG(&ua, iovcnt) = SCARG(uap, iovcnt);
    863  1.120   thorpej 	SCARG(&ua, PAD) = 0;
    864  1.120   thorpej 	SCARG(&ua, offset) = linux_hilo_to_off_t(SCARG(uap, off_hi),
    865  1.120   thorpej 						 SCARG(uap, off_lo));
    866  1.120   thorpej 	return sys_preadv(l, &ua, retval);
    867  1.120   thorpej }
    868  1.120   thorpej 
    869  1.120   thorpej /*
    870  1.120   thorpej  * pwritev(2)
    871  1.120   thorpej  */
    872  1.120   thorpej int
    873  1.120   thorpej linux_sys_pwritev(struct lwp *l, const struct linux_sys_pwritev_args *uap,
    874  1.120   thorpej     register_t *retval)
    875  1.120   thorpej {
    876  1.120   thorpej 	/* {
    877  1.120   thorpej 		syscallarg(int) fd;
    878  1.120   thorpej 		syscallarg(const struct iovec *) iovp;
    879  1.120   thorpej 		syscallarg(int) iovcnt;
    880  1.120   thorpej 		syscallarg(unsigned long) off_lo;
    881  1.120   thorpej 		syscallarg(unsigned long) off_hi;
    882  1.120   thorpej 	} */
    883  1.120   thorpej 	struct sys_pwritev_args ua;
    884  1.120   thorpej 
    885  1.120   thorpej 	SCARG(&ua, fd) = SCARG(uap, fd);
    886  1.120   thorpej 	SCARG(&ua, iovp) = (const void *)SCARG(uap, iovp);
    887  1.120   thorpej 	SCARG(&ua, iovcnt) = SCARG(uap, iovcnt);
    888  1.120   thorpej 	SCARG(&ua, PAD) = 0;
    889  1.120   thorpej 	SCARG(&ua, offset) = linux_hilo_to_off_t(SCARG(uap, off_hi),
    890  1.120   thorpej 						 SCARG(uap, off_lo));
    891  1.120   thorpej 	return sys_pwritev(l, &ua, retval);
    892  1.120   thorpej }
    893  1.120   thorpej 
    894  1.103  christos int
    895  1.103  christos linux_sys_dup3(struct lwp *l, const struct linux_sys_dup3_args *uap,
    896  1.103  christos     register_t *retval)
    897  1.103  christos {
    898  1.103  christos 	/* {
    899  1.103  christos 		syscallarg(int) from;
    900  1.103  christos 		syscallarg(int) to;
    901  1.103  christos 		syscallarg(int) flags;
    902  1.103  christos 	} */
    903  1.111     njoly 	int flags;
    904  1.111     njoly 
    905  1.111     njoly 	flags = linux_to_bsd_ioflags(SCARG(uap, flags));
    906  1.111     njoly 	if ((flags & ~O_CLOEXEC) != 0)
    907  1.111     njoly 		return EINVAL;
    908  1.103  christos 
    909  1.111     njoly 	if (SCARG(uap, from) == SCARG(uap, to))
    910  1.111     njoly 		return EINVAL;
    911  1.103  christos 
    912  1.111     njoly 	return dodup(l, SCARG(uap, from), SCARG(uap, to), flags, retval);
    913  1.103  christos }
    914  1.103  christos 
    915  1.105     njoly 
    916  1.105     njoly int
    917  1.105     njoly linux_to_bsd_atflags(int lflags)
    918  1.105     njoly {
    919  1.105     njoly 	int bflags = 0;
    920  1.105     njoly 
    921  1.105     njoly 	if (lflags & LINUX_AT_SYMLINK_NOFOLLOW)
    922  1.105     njoly 		bflags |= AT_SYMLINK_NOFOLLOW;
    923  1.105     njoly 	if (lflags & LINUX_AT_REMOVEDIR)
    924  1.105     njoly 		bflags |= AT_REMOVEDIR;
    925  1.105     njoly 	if (lflags & LINUX_AT_SYMLINK_FOLLOW)
    926  1.105     njoly 		bflags |= AT_SYMLINK_FOLLOW;
    927  1.105     njoly 
    928  1.105     njoly 	return bflags;
    929  1.105     njoly }
    930  1.105     njoly 
    931  1.124  christos int
    932  1.124  christos linux_sys_faccessat2(lwp_t *l, const struct linux_sys_faccessat2_args *uap,
    933  1.124  christos     register_t *retval)
    934  1.124  christos {
    935  1.124  christos 	/* {
    936  1.124  christos 		syscallarg(int) fd;
    937  1.124  christos 		syscallarg(const char *) path;
    938  1.124  christos 		syscallarg(int) amode;
    939  1.124  christos 		syscallarg(int) flags;
    940  1.124  christos 	}*/
    941  1.124  christos 	int flag = linux_to_bsd_atflags(SCARG(uap, flags));
    942  1.124  christos 	int mode = SCARG(uap, amode);
    943  1.124  christos 	int fd = SCARG(uap, fd);
    944  1.124  christos 	const char *path = SCARG(uap, path);
    945  1.124  christos 
    946  1.124  christos 	return do_sys_accessat(l, fd, path, mode, flag);
    947  1.124  christos }
    948  1.124  christos 
    949  1.105     njoly 
    950  1.126  riastrad int
    951  1.125  christos linux_sys_sync_file_range(lwp_t *l,
    952  1.125  christos     const struct linux_sys_sync_file_range_args *uap, register_t *retval)
    953  1.125  christos {
    954  1.125  christos 	/* {
    955  1.125  christos 		syscallarg(int) fd;
    956  1.125  christos 		syscallarg(off_t) offset;
    957  1.125  christos 		syscallarg(off_t) nbytes;
    958  1.125  christos 		syscallarg(unsigned int) flags;
    959  1.125  christos 	} */
    960  1.126  riastrad 
    961  1.125  christos 	struct sys_fsync_range_args ua;
    962  1.125  christos 
    963  1.125  christos 	if (SCARG(uap, offset) < 0 || SCARG(uap, nbytes) < 0 ||
    964  1.125  christos 	    ((SCARG(uap, flags) & ~LINUX_SYNC_FILE_RANGE_ALL) != 0))
    965  1.125  christos 		return EINVAL;
    966  1.125  christos 
    967  1.125  christos 	/* Fill ua with uap */
    968  1.125  christos 	SCARG(&ua, fd) = SCARG(uap, fd);
    969  1.125  christos 	SCARG(&ua, flags) = SCARG(uap, flags);
    970  1.125  christos 
    971  1.125  christos 	/* Round down offset to page boundary */
    972  1.125  christos 	SCARG(&ua, start) = rounddown(SCARG(uap, offset), PAGE_SIZE);
    973  1.125  christos 	SCARG(&ua, length) = SCARG(uap, nbytes);
    974  1.125  christos 	if (SCARG(&ua, length) != 0) {
    975  1.125  christos 		/* Round up length to nbytes+offset to page boundary */
    976  1.125  christos 		SCARG(&ua, length) = roundup(SCARG(uap, nbytes)
    977  1.126  riastrad 		    + SCARG(uap, offset) - SCARG(&ua, start), PAGE_SIZE);
    978  1.125  christos 	}
    979  1.126  riastrad 
    980  1.125  christos 	return sys_fsync_range(l, &ua, retval);
    981  1.125  christos }
    982  1.125  christos 
    983  1.126  riastrad int
    984  1.125  christos linux_sys_syncfs(lwp_t *l, const struct linux_sys_syncfs_args *uap,
    985  1.125  christos     register_t *retval)
    986  1.125  christos {
    987  1.125  christos 	/* {
    988  1.125  christos 		syscallarg(int) fd;
    989  1.125  christos 	} */
    990  1.125  christos 
    991  1.125  christos 	struct mount *mp;
    992  1.125  christos 	struct vnode *vp;
    993  1.125  christos 	file_t *fp;
    994  1.125  christos 	int error, fd;
    995  1.125  christos 	fd = SCARG(uap, fd);
    996  1.126  riastrad 
    997  1.125  christos 	/* Get file pointer */
    998  1.125  christos 	if ((error = fd_getvnode(fd, &fp)) != 0)
    999  1.125  christos 		return error;
   1000  1.126  riastrad 
   1001  1.125  christos 	/* Get vnode and mount point */
   1002  1.125  christos 	vp = fp->f_vnode;
   1003  1.125  christos 	mp = vp->v_mount;
   1004  1.126  riastrad 
   1005  1.125  christos 	mutex_enter(mp->mnt_updating);
   1006  1.125  christos 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
   1007  1.125  christos 		int asyncflag = mp->mnt_flag & MNT_ASYNC;
   1008  1.125  christos 		mp->mnt_flag &= ~MNT_ASYNC;
   1009  1.125  christos 		VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
   1010  1.125  christos 		if (asyncflag)
   1011  1.125  christos 			mp->mnt_flag |= MNT_ASYNC;
   1012  1.125  christos 	}
   1013  1.125  christos 	mutex_exit(mp->mnt_updating);
   1014  1.126  riastrad 
   1015  1.125  christos 	/* Cleanup vnode and file pointer */
   1016  1.125  christos 	vrele(vp);
   1017  1.125  christos 	fd_putfile(fd);
   1018  1.125  christos 	return 0;
   1019  1.125  christos 
   1020  1.125  christos }
   1021  1.125  christos 
   1022  1.126  riastrad int
   1023  1.125  christos linux_sys_renameat2(struct lwp *l, const struct linux_sys_renameat2_args *uap,
   1024  1.125  christos     register_t *retval)
   1025  1.125  christos {
   1026  1.125  christos 	/* {
   1027  1.125  christos 		syscallarg(int) fromfd;
   1028  1.125  christos 		syscallarg(const char *) from;
   1029  1.125  christos 		syscallarg(int) tofd;
   1030  1.125  christos 		syscallarg(const char *) to;
   1031  1.125  christos 		syscallarg(unsigned int) flags;
   1032  1.125  christos 	} */
   1033  1.125  christos 
   1034  1.125  christos 	struct sys_renameat_args ua;
   1035  1.125  christos 	SCARG(&ua, fromfd) = SCARG(uap, fromfd);
   1036  1.125  christos 	SCARG(&ua, from) = SCARG(uap, from);
   1037  1.125  christos 	SCARG(&ua, tofd) = SCARG(uap, tofd);
   1038  1.125  christos 	SCARG(&ua, to) = SCARG(uap, to);
   1039  1.125  christos 
   1040  1.125  christos 	unsigned int flags = SCARG(uap, flags);
   1041  1.125  christos 	int error;
   1042  1.125  christos 
   1043  1.125  christos 	if (flags != 0) {
   1044  1.125  christos 		if (flags & ~LINUX_RENAME_ALL)
   1045  1.125  christos 			return EINVAL;
   1046  1.126  riastrad 		if ((flags & LINUX_RENAME_EXCHANGE) != 0 &&
   1047  1.125  christos 		    (flags & (LINUX_RENAME_NOREPLACE | LINUX_RENAME_WHITEOUT))
   1048  1.125  christos 		    != 0)
   1049  1.125  christos 			return EINVAL;
   1050  1.125  christos 		/*
   1051  1.125  christos 		 * Suppoting renameat2 flags without support from file systems
   1052  1.126  riastrad 		 * becomes a messy affair cause of locks and how VOP_RENAME
   1053  1.125  christos 		 * protocol is implemented. So, return EOPNOTSUPP for now.
   1054  1.125  christos 		 */
   1055  1.125  christos 		return EOPNOTSUPP;
   1056  1.125  christos 	}
   1057  1.125  christos 
   1058  1.125  christos 	error = sys_renameat(l, &ua, retval);
   1059  1.125  christos 	return error;
   1060  1.125  christos }
   1061  1.125  christos 
   1062  1.127  riastrad int
   1063  1.127  riastrad linux_sys_copy_file_range(lwp_t *l,
   1064  1.125  christos     const struct linux_sys_copy_file_range_args *uap, register_t *retval)
   1065  1.125  christos {
   1066  1.125  christos 	/* {
   1067  1.125  christos 		syscallarg(int) fd_in;
   1068  1.125  christos 		syscallarg(unsigned long) off_in;
   1069  1.125  christos 		syscallarg(int) fd_out;
   1070  1.125  christos 		syscallarg(unsigned long) off_out;
   1071  1.125  christos 		syscallarg(size_t) len;
   1072  1.125  christos 		syscallarg(unsigned int) flags;
   1073  1.125  christos 	} */
   1074  1.125  christos 
   1075  1.125  christos 	int fd_in, fd_out;
   1076  1.125  christos 	file_t *fp_in, *fp_out;
   1077  1.125  christos 	struct vnode *invp, *outvp;
   1078  1.125  christos 	off_t off_in = 0, off_out = 0;
   1079  1.125  christos 	struct vattr vattr_in, vattr_out;
   1080  1.125  christos 	ssize_t total_copied = 0;
   1081  1.125  christos 	size_t bytes_left, to_copy;
   1082  1.125  christos 	bool have_off_in = false, have_off_out = false;
   1083  1.125  christos 	int error = 0;
   1084  1.125  christos 	size_t len = SCARG(uap, len);
   1085  1.125  christos 	unsigned int flags = SCARG(uap, flags);
   1086  1.125  christos 	// Structures for actual copy
   1087  1.125  christos 	char *buffer = NULL;
   1088  1.125  christos 	struct uio auio;
   1089  1.125  christos 	struct iovec aiov;
   1090  1.125  christos 
   1091  1.125  christos 
   1092  1.125  christos 	if (len > SSIZE_MAX) {
   1093  1.125  christos 		DPRINTF("%s: len is greater than SSIZE_MAX\n",
   1094  1.125  christos 		    __func__);
   1095  1.125  christos 		return EOVERFLOW;
   1096  1.125  christos 	}
   1097  1.125  christos 
   1098  1.125  christos 	if(flags != 0) {
   1099  1.125  christos 		DPRINTF("%s: unsupported flags %#x\n", __func__, flags);
   1100  1.125  christos 		return EINVAL;
   1101  1.125  christos 	}
   1102  1.126  riastrad 
   1103  1.125  christos 	fd_in = SCARG(uap, fd_in);
   1104  1.125  christos 	fd_out = SCARG(uap, fd_out);
   1105  1.125  christos 	error = fd_getvnode(fd_in, &fp_in);
   1106  1.125  christos 	if (error) {
   1107  1.125  christos 		return error;
   1108  1.125  christos 	}
   1109  1.125  christos 
   1110  1.125  christos 	error = fd_getvnode(fd_out, &fp_out);
   1111  1.125  christos 	if (error) {
   1112  1.125  christos 		    fd_putfile(fd_in);
   1113  1.125  christos 		    return error;
   1114  1.125  christos 	}
   1115  1.126  riastrad 
   1116  1.125  christos 	invp = fp_in->f_vnode;
   1117  1.125  christos 	outvp = fp_out->f_vnode;
   1118  1.125  christos 
   1119  1.125  christos 	/* Get attributes of input and output files */
   1120  1.125  christos 	VOP_GETATTR(invp, &vattr_in, l->l_cred);
   1121  1.125  christos 	VOP_GETATTR(outvp, &vattr_out, l->l_cred);
   1122  1.126  riastrad 
   1123  1.125  christos 	/* Check if input and output files are regular files */
   1124  1.125  christos 	if (vattr_in.va_type == VDIR || vattr_out.va_type == VDIR) {
   1125  1.125  christos 		error = EISDIR;
   1126  1.125  christos 		DPRINTF("%s: Input or output is a directory\n", __func__);
   1127  1.125  christos 		goto out;
   1128  1.126  riastrad 	}
   1129  1.126  riastrad 	if ((SCARG(uap, off_in) != NULL && *SCARG(uap, off_in) < 0) ||
   1130  1.125  christos 	   (SCARG(uap, off_out) != NULL && *SCARG(uap, off_out) < 0) ||
   1131  1.125  christos 	   vattr_in.va_type != VREG || vattr_out.va_type != VREG)
   1132  1.125  christos         {
   1133  1.125  christos 		error = EINVAL;
   1134  1.125  christos 		DPRINTF("%s: Invalid offset or file type\n", __func__);
   1135  1.125  christos 		goto out;
   1136  1.125  christos 	}
   1137  1.125  christos 
   1138  1.125  christos 	if ((fp_in->f_flag & FREAD) == 0 ||
   1139  1.125  christos 	    (fp_out->f_flag & FWRITE) == 0 || (fp_out->f_flag & FAPPEND) != 0) {
   1140  1.125  christos 		DPRINTF("%s: input file can't be read or output file "
   1141  1.125  christos 		    "can't be written\n", __func__);
   1142  1.125  christos 		error = EBADF;
   1143  1.125  christos 		goto out;
   1144  1.125  christos 	}
   1145  1.125  christos 	/* Retrieve and validate offsets if provided */
   1146  1.125  christos 	if (SCARG(uap, off_in) != NULL) {
   1147  1.125  christos 	    error = copyin(SCARG(uap, off_in), &off_in, sizeof(off_in));
   1148  1.125  christos 	    if (error) {
   1149  1.125  christos 		    goto out;
   1150  1.125  christos 	    }
   1151  1.125  christos 	    have_off_in = true;
   1152  1.125  christos 	}
   1153  1.125  christos 
   1154  1.125  christos 	if (SCARG(uap, off_out) != NULL) {
   1155  1.125  christos 	    error = copyin(SCARG(uap, off_out), &off_out, sizeof(off_out));
   1156  1.125  christos 	    if (error) {
   1157  1.125  christos 		    goto out;
   1158  1.125  christos 	    }
   1159  1.125  christos 	    have_off_out = true;
   1160  1.125  christos 	}
   1161  1.126  riastrad 
   1162  1.125  christos 	off_t new_size = off_out + len;
   1163  1.125  christos 	if (new_size < 0) {
   1164  1.125  christos 		DPRINTF("%s: New size is greater than OFF_MAX\n", __func__);
   1165  1.125  christos 		error = EFBIG;
   1166  1.125  christos 		goto out;
   1167  1.125  christos 	}
   1168  1.125  christos 
   1169  1.125  christos 	/* Identify overlapping ranges */
   1170  1.125  christos 	if ((invp == outvp) &&
   1171  1.125  christos 	    ((off_in <= off_out && off_in + (off_t)len > off_out) ||
   1172  1.125  christos 	    (off_in > off_out && off_out + (off_t)len > off_in))) {
   1173  1.125  christos 		DPRINTF("%s: Ranges overlap\n", __func__);
   1174  1.125  christos 		error = EINVAL;
   1175  1.125  christos 		goto out;
   1176  1.125  christos 	}
   1177  1.125  christos 
   1178  1.125  christos 	buffer = kmem_alloc(LINUX_COPY_FILE_RANGE_MAX_CHUNK, KM_SLEEP);
   1179  1.125  christos 	/* Allocation cannot fail, so no need for error handling? */
   1180  1.125  christos 	if (buffer == NULL) {
   1181  1.125  christos 		error = ENOMEM;
   1182  1.125  christos 		goto out;
   1183  1.125  christos 	}
   1184  1.125  christos 
   1185  1.125  christos 	bytes_left = len;
   1186  1.125  christos 
   1187  1.125  christos 	while (bytes_left > 0) {
   1188  1.125  christos 		to_copy = MIN(bytes_left, LINUX_COPY_FILE_RANGE_MAX_CHUNK);
   1189  1.126  riastrad 
   1190  1.125  christos 		/* Lock the input vnode for reading */
   1191  1.125  christos 		vn_lock(fp_in->f_vnode, LK_SHARED | LK_RETRY);
   1192  1.125  christos 		/* Set up iovec and uio for reading */
   1193  1.125  christos 		aiov.iov_base = buffer;
   1194  1.125  christos 		aiov.iov_len = to_copy;
   1195  1.125  christos 		auio.uio_iov = &aiov;
   1196  1.125  christos 		auio.uio_iovcnt = 1;
   1197  1.125  christos 		auio.uio_offset = have_off_in ? off_in : fp_in->f_offset;
   1198  1.125  christos 		auio.uio_resid = to_copy;
   1199  1.125  christos 		auio.uio_rw = UIO_READ;
   1200  1.125  christos 		auio.uio_vmspace = l->l_proc->p_vmspace;
   1201  1.125  christos 		UIO_SETUP_SYSSPACE(&auio);
   1202  1.125  christos 
   1203  1.125  christos 		/* Perform read using vn_read */
   1204  1.125  christos 		error = VOP_READ(fp_in->f_vnode, &auio, 0, l->l_cred);
   1205  1.125  christos 		VOP_UNLOCK(fp_in->f_vnode);
   1206  1.125  christos 		if (error) {
   1207  1.125  christos 			DPRINTF("%s: Read error %d\n", __func__, error);
   1208  1.125  christos 			break;
   1209  1.125  christos 		}
   1210  1.125  christos 
   1211  1.125  christos 		size_t read_bytes = to_copy - auio.uio_resid;
   1212  1.125  christos 		if (read_bytes == 0) {
   1213  1.125  christos 			/* EOF reached */
   1214  1.125  christos 			break;
   1215  1.125  christos 		}
   1216  1.125  christos 
   1217  1.125  christos 		/* Lock the output vnode for writing */
   1218  1.125  christos 		vn_lock(fp_out->f_vnode, LK_EXCLUSIVE | LK_RETRY);
   1219  1.125  christos 		/* Set up iovec and uio for writing */
   1220  1.125  christos 		aiov.iov_base = buffer;
   1221  1.125  christos 		aiov.iov_len = read_bytes;
   1222  1.125  christos 		auio.uio_iov = &aiov;
   1223  1.125  christos 		auio.uio_iovcnt = 1;
   1224  1.125  christos 		auio.uio_offset = have_off_out ? off_out : fp_out->f_offset;
   1225  1.125  christos 		auio.uio_resid = read_bytes;
   1226  1.125  christos 		auio.uio_rw = UIO_WRITE;
   1227  1.125  christos 		auio.uio_vmspace = l->l_proc->p_vmspace;
   1228  1.125  christos 		UIO_SETUP_SYSSPACE(&auio);
   1229  1.125  christos 
   1230  1.125  christos 		/* Perform the write */
   1231  1.125  christos 		error = VOP_WRITE(fp_out->f_vnode, &auio, 0, l->l_cred);
   1232  1.125  christos 		VOP_UNLOCK(fp_out->f_vnode);
   1233  1.125  christos 		if (error) {
   1234  1.125  christos 			DPRINTF("%s: Write error %d\n", __func__, error);
   1235  1.125  christos 			break;
   1236  1.125  christos 		}
   1237  1.125  christos 		size_t written_bytes = read_bytes - auio.uio_resid;
   1238  1.125  christos 		total_copied += written_bytes;
   1239  1.125  christos 		bytes_left -= written_bytes;
   1240  1.125  christos 
   1241  1.125  christos 		/* Update offsets if provided */
   1242  1.125  christos 		if (have_off_in) {
   1243  1.125  christos 			off_in += written_bytes;
   1244  1.125  christos 		} else {
   1245  1.125  christos 			fp_in->f_offset += written_bytes;
   1246  1.125  christos 		}
   1247  1.125  christos 		if (have_off_out) {
   1248  1.125  christos 			off_out += written_bytes;
   1249  1.125  christos 		} else {
   1250  1.125  christos 			fp_out->f_offset += written_bytes;
   1251  1.125  christos 		}
   1252  1.125  christos 	}
   1253  1.125  christos 
   1254  1.125  christos 	if (have_off_in) {
   1255  1.125  christos 		/* Adjust user space offset */
   1256  1.126  riastrad 		error = copyout(&off_in, SCARG(uap, off_in), sizeof(off_t));
   1257  1.125  christos 		if (error) {
   1258  1.125  christos 			DPRINTF("%s: Error adjusting user space offset\n",
   1259  1.125  christos 			    __func__);
   1260  1.125  christos 		}
   1261  1.125  christos 		goto out;
   1262  1.125  christos 	}
   1263  1.125  christos 
   1264  1.125  christos 	if (have_off_out) {
   1265  1.125  christos 		/* Adjust user space offset */
   1266  1.126  riastrad 		error = copyout(&off_out, SCARG(uap, off_out), sizeof(off_t));
   1267  1.125  christos 		if (error) {
   1268  1.125  christos 			DPRINTF("%s: Error adjusting user space offset\n",
   1269  1.125  christos 			    __func__);
   1270  1.125  christos 		}
   1271  1.125  christos 	}
   1272  1.125  christos 
   1273  1.125  christos 	*retval = total_copied;
   1274  1.125  christos out:
   1275  1.125  christos 	if (buffer) {
   1276  1.125  christos 		kmem_free(buffer, LINUX_COPY_FILE_RANGE_MAX_CHUNK);
   1277  1.125  christos 	}
   1278  1.125  christos 	if (fp_out) {
   1279  1.125  christos 		fd_putfile(fd_out);
   1280  1.125  christos 	}
   1281  1.125  christos 	if (fp_in) {
   1282  1.125  christos 		fd_putfile(fd_in);
   1283  1.125  christos 	}
   1284  1.125  christos 	return error;
   1285  1.125  christos }
   1286  1.125  christos 
   1287  1.125  christos 
   1288   1.75  christos #define LINUX_NOT_SUPPORTED(fun) \
   1289   1.75  christos int \
   1290   1.90       dsl fun(struct lwp *l, const struct fun##_args *uap, register_t *retval) \
   1291   1.75  christos { \
   1292   1.75  christos 	return EOPNOTSUPP; \
   1293   1.75  christos }
   1294   1.75  christos 
   1295   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_setxattr)
   1296   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
   1297   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
   1298   1.75  christos 
   1299   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_getxattr)
   1300   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
   1301   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
   1302   1.75  christos 
   1303   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_listxattr)
   1304   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
   1305   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
   1306   1.75  christos 
   1307   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_removexattr)
   1308   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
   1309   1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
   1310  1.117  jdolecek 
   1311  1.117  jdolecek /*
   1312  1.117  jdolecek  * For now just return EOPNOTSUPP, this makes glibc posix_fallocate()
   1313  1.117  jdolecek  * to fallback to emulation.
   1314  1.117  jdolecek  * XXX Right now no filesystem actually implements fallocate support,
   1315  1.117  jdolecek  * so no need for mapping.
   1316  1.117  jdolecek  */
   1317  1.117  jdolecek LINUX_NOT_SUPPORTED(linux_sys_fallocate)
   1318