Home | History | Annotate | Line # | Download | only in common
linux_file.c revision 1.96
      1  1.96    martin /*	$NetBSD: linux_file.c,v 1.96 2008/04/28 20:23:43 martin 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.96    martin __KERNEL_RCSID(0, "$NetBSD: linux_file.c,v 1.96 2008/04/28 20:23:43 martin Exp $");
     39  1.23       erh 
     40   1.1      fvdl #include <sys/param.h>
     41   1.1      fvdl #include <sys/systm.h>
     42   1.1      fvdl #include <sys/namei.h>
     43   1.1      fvdl #include <sys/proc.h>
     44   1.1      fvdl #include <sys/file.h>
     45   1.1      fvdl #include <sys/stat.h>
     46   1.1      fvdl #include <sys/filedesc.h>
     47   1.1      fvdl #include <sys/ioctl.h>
     48   1.1      fvdl #include <sys/kernel.h>
     49   1.1      fvdl #include <sys/mount.h>
     50   1.1      fvdl #include <sys/malloc.h>
     51  1.81       dsl #include <sys/namei.h>
     52  1.13      fvdl #include <sys/vnode.h>
     53  1.13      fvdl #include <sys/tty.h>
     54  1.39      manu #include <sys/socketvar.h>
     55  1.13      fvdl #include <sys/conf.h>
     56  1.41  jdolecek #include <sys/pipe.h>
     57   1.1      fvdl 
     58   1.1      fvdl #include <sys/syscallargs.h>
     59  1.81       dsl #include <sys/vfs_syscalls.h>
     60   1.1      fvdl 
     61  1.24  christos #include <compat/linux/common/linux_types.h>
     62  1.24  christos #include <compat/linux/common/linux_signal.h>
     63  1.24  christos #include <compat/linux/common/linux_fcntl.h>
     64  1.24  christos #include <compat/linux/common/linux_util.h>
     65  1.24  christos #include <compat/linux/common/linux_machdep.h>
     66  1.84     njoly #include <compat/linux/common/linux_ipc.h>
     67  1.84     njoly #include <compat/linux/common/linux_sem.h>
     68  1.24  christos 
     69   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     70  1.14  christos 
     71  1.87       dsl static int linux_to_bsd_ioflags(int);
     72  1.87       dsl static int bsd_to_linux_ioflags(int);
     73  1.67      manu #ifndef __amd64__
     74  1.87       dsl static void bsd_to_linux_stat(struct stat *, struct linux_stat *);
     75  1.67      manu #endif
     76  1.14  christos 
     77  1.92       dsl conv_linux_flock(linux, flock)
     78  1.92       dsl 
     79   1.1      fvdl /*
     80   1.1      fvdl  * Some file-related calls are handled here. The usual flag conversion
     81   1.1      fvdl  * an structure conversion is done, and alternate emul path searching.
     82   1.1      fvdl  */
     83   1.1      fvdl 
     84   1.1      fvdl /*
     85   1.1      fvdl  * The next two functions convert between the Linux and NetBSD values
     86   1.1      fvdl  * of the flags used in open(2) and fcntl(2).
     87   1.1      fvdl  */
     88   1.1      fvdl static int
     89  1.88       dsl linux_to_bsd_ioflags(int lflags)
     90   1.1      fvdl {
     91   1.1      fvdl 	int res = 0;
     92   1.1      fvdl 
     93   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
     94   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
     95   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
     96   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
     97   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
     98   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
     99   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
    100   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
    101   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
    102   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
    103   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
    104   1.1      fvdl 
    105   1.1      fvdl 	return res;
    106   1.1      fvdl }
    107   1.1      fvdl 
    108   1.1      fvdl static int
    109  1.88       dsl bsd_to_linux_ioflags(int bflags)
    110   1.1      fvdl {
    111   1.1      fvdl 	int res = 0;
    112   1.1      fvdl 
    113   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
    114   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
    115   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
    116   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
    117   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
    118   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
    119   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
    120   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
    121   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
    122   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
    123   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
    124   1.1      fvdl 
    125   1.1      fvdl 	return res;
    126   1.1      fvdl }
    127   1.1      fvdl 
    128   1.1      fvdl /*
    129   1.1      fvdl  * creat(2) is an obsolete function, but it's present as a Linux
    130   1.1      fvdl  * system call, so let's deal with it.
    131   1.1      fvdl  *
    132  1.23       erh  * Note: On the Alpha this doesn't really exist in Linux, but it's defined
    133  1.23       erh  * in syscalls.master anyway so this doesn't have to be special cased.
    134  1.23       erh  *
    135   1.1      fvdl  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    136   1.1      fvdl  */
    137   1.1      fvdl int
    138  1.90       dsl linux_sys_creat(struct lwp *l, const struct linux_sys_creat_args *uap, register_t *retval)
    139  1.11   thorpej {
    140  1.90       dsl 	/* {
    141  1.27  christos 		syscallarg(const char *) path;
    142   1.1      fvdl 		syscallarg(int) mode;
    143  1.90       dsl 	} */
    144  1.12   mycroft 	struct sys_open_args oa;
    145   1.1      fvdl 
    146   1.1      fvdl 	SCARG(&oa, path) = SCARG(uap, path);
    147   1.1      fvdl 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    148   1.1      fvdl 	SCARG(&oa, mode) = SCARG(uap, mode);
    149  1.12   mycroft 
    150  1.56   thorpej 	return sys_open(l, &oa, retval);
    151   1.1      fvdl }
    152   1.1      fvdl 
    153   1.1      fvdl /*
    154   1.1      fvdl  * open(2). Take care of the different flag values, and let the
    155   1.1      fvdl  * NetBSD syscall do the real work. See if this operation
    156   1.1      fvdl  * gives the current process a controlling terminal.
    157   1.1      fvdl  * (XXX is this necessary?)
    158   1.1      fvdl  */
    159   1.1      fvdl int
    160  1.90       dsl linux_sys_open(struct lwp *l, const struct linux_sys_open_args *uap, register_t *retval)
    161  1.11   thorpej {
    162  1.90       dsl 	/* {
    163  1.27  christos 		syscallarg(const char *) path;
    164   1.1      fvdl 		syscallarg(int) flags;
    165   1.1      fvdl 		syscallarg(int) mode;
    166  1.90       dsl 	} */
    167  1.56   thorpej 	struct proc *p = l->l_proc;
    168   1.1      fvdl 	int error, fl;
    169  1.12   mycroft 	struct sys_open_args boa;
    170   1.1      fvdl 
    171   1.2      fvdl 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    172   1.1      fvdl 
    173   1.1      fvdl 	SCARG(&boa, path) = SCARG(uap, path);
    174   1.1      fvdl 	SCARG(&boa, flags) = fl;
    175   1.1      fvdl 	SCARG(&boa, mode) = SCARG(uap, mode);
    176   1.2      fvdl 
    177  1.56   thorpej 	if ((error = sys_open(l, &boa, retval)))
    178   1.1      fvdl 		return error;
    179   1.1      fvdl 
    180   1.1      fvdl 	/*
    181   1.1      fvdl 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    182   1.1      fvdl 	 * If we are a session leader, and we don't have a controlling
    183   1.1      fvdl 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    184   1.1      fvdl 	 * this the controlling terminal.
    185  1.65     perry 	 */
    186  1.77        ad         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
    187  1.93        ad                 file_t *fp;
    188  1.38   thorpej 
    189  1.93        ad 		fp = fd_getfile(*retval);
    190   1.1      fvdl 
    191   1.1      fvdl                 /* ignore any error, just give it a try */
    192  1.57      yamt                 if (fp != NULL) {
    193  1.57      yamt 			if (fp->f_type == DTYPE_VNODE) {
    194  1.93        ad 				(fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, NULL);
    195  1.57      yamt 			}
    196  1.93        ad 			fd_putfile(*retval);
    197  1.57      yamt 		}
    198   1.1      fvdl         }
    199   1.1      fvdl 	return 0;
    200   1.1      fvdl }
    201   1.1      fvdl 
    202   1.1      fvdl /*
    203   1.1      fvdl  * Most actions in the fcntl() call are straightforward; simply
    204   1.1      fvdl  * pass control to the NetBSD system call. A few commands need
    205   1.1      fvdl  * conversions after the actual system call has done its work,
    206   1.1      fvdl  * because the flag values and lock structure are different.
    207   1.1      fvdl  */
    208   1.1      fvdl int
    209  1.90       dsl linux_sys_fcntl(struct lwp *l, const struct linux_sys_fcntl_args *uap, register_t *retval)
    210  1.11   thorpej {
    211  1.90       dsl 	/* {
    212   1.1      fvdl 		syscallarg(int) fd;
    213   1.1      fvdl 		syscallarg(int) cmd;
    214   1.1      fvdl 		syscallarg(void *) arg;
    215  1.90       dsl 	} */
    216  1.56   thorpej 	struct proc *p = l->l_proc;
    217  1.23       erh 	int fd, cmd, error;
    218  1.23       erh 	u_long val;
    219  1.83       dsl 	void *arg;
    220  1.12   mycroft 	struct sys_fcntl_args fca;
    221  1.93        ad 	file_t *fp;
    222  1.13      fvdl 	struct vnode *vp;
    223  1.13      fvdl 	struct vattr va;
    224  1.13      fvdl 	long pgid;
    225  1.13      fvdl 	struct pgrp *pgrp;
    226  1.94        ad 	struct tty *tp;
    227   1.1      fvdl 
    228   1.1      fvdl 	fd = SCARG(uap, fd);
    229   1.1      fvdl 	cmd = SCARG(uap, cmd);
    230  1.92       dsl 	arg = SCARG(uap, arg);
    231   1.1      fvdl 
    232   1.1      fvdl 	switch (cmd) {
    233  1.92       dsl 
    234   1.1      fvdl 	case LINUX_F_DUPFD:
    235   1.1      fvdl 		cmd = F_DUPFD;
    236   1.1      fvdl 		break;
    237  1.92       dsl 
    238   1.1      fvdl 	case LINUX_F_GETFD:
    239   1.1      fvdl 		cmd = F_GETFD;
    240   1.1      fvdl 		break;
    241  1.92       dsl 
    242   1.1      fvdl 	case LINUX_F_SETFD:
    243   1.1      fvdl 		cmd = F_SETFD;
    244   1.1      fvdl 		break;
    245  1.92       dsl 
    246   1.1      fvdl 	case LINUX_F_GETFL:
    247   1.1      fvdl 		SCARG(&fca, fd) = fd;
    248   1.1      fvdl 		SCARG(&fca, cmd) = F_GETFL;
    249   1.1      fvdl 		SCARG(&fca, arg) = arg;
    250  1.56   thorpej 		if ((error = sys_fcntl(l, &fca, retval)))
    251   1.1      fvdl 			return error;
    252   1.1      fvdl 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    253   1.1      fvdl 		return 0;
    254  1.92       dsl 
    255  1.41  jdolecek 	case LINUX_F_SETFL: {
    256  1.93        ad 		file_t	*fp1 = NULL;
    257  1.41  jdolecek 
    258  1.23       erh 		val = linux_to_bsd_ioflags((unsigned long)SCARG(uap, arg));
    259  1.39      manu 		/*
    260  1.41  jdolecek 		 * Linux seems to have same semantics for sending SIGIO to the
    261  1.64       abs 		 * read side of socket, but slightly different semantics
    262  1.41  jdolecek 		 * for SIGIO to the write side.  Rather than sending the SIGIO
    263  1.41  jdolecek 		 * every time it's possible to write (directly) more data, it
    264  1.41  jdolecek 		 * only sends SIGIO if last write(2) failed due to insufficient
    265  1.41  jdolecek 		 * memory to hold the data. This is compatible enough
    266  1.41  jdolecek 		 * with NetBSD semantics to not do anything about the
    267  1.41  jdolecek 		 * difference.
    268  1.65     perry 		 *
    269  1.41  jdolecek 		 * Linux does NOT send SIGIO for pipes. Deal with socketpair
    270  1.41  jdolecek 		 * ones and DTYPE_PIPE ones. For these, we don't set
    271  1.41  jdolecek 		 * the underlying flags (we don't pass O_ASYNC flag down
    272  1.41  jdolecek 		 * to sys_fcntl()), but set the FASYNC flag for file descriptor,
    273  1.41  jdolecek 		 * so that F_GETFL would report the ASYNC i/o is on.
    274  1.39      manu 		 */
    275  1.41  jdolecek 		if (val & O_ASYNC) {
    276  1.93        ad 			if (((fp1 = fd_getfile(fd)) == NULL))
    277  1.39      manu 			    return (EBADF);
    278  1.69  christos 			if (((fp1->f_type == DTYPE_SOCKET) && fp1->f_data
    279  1.69  christos 			      && ((struct socket *)fp1->f_data)->so_state & SS_ISAPIPE)
    280  1.69  christos 			    || (fp1->f_type == DTYPE_PIPE))
    281  1.41  jdolecek 				val &= ~O_ASYNC;
    282  1.41  jdolecek 			else {
    283  1.41  jdolecek 				/* not a pipe, do not modify anything */
    284  1.93        ad 				fd_putfile(fd);
    285  1.69  christos 				fp1 = NULL;
    286  1.39      manu 			}
    287  1.41  jdolecek 		}
    288  1.41  jdolecek 
    289  1.41  jdolecek 		SCARG(&fca, fd) = fd;
    290  1.41  jdolecek 		SCARG(&fca, cmd) = F_SETFL;
    291  1.79  christos 		SCARG(&fca, arg) = (void *) val;
    292  1.39      manu 
    293  1.56   thorpej 		error = sys_fcntl(l, &fca, retval);
    294  1.41  jdolecek 
    295  1.41  jdolecek 		/* Now set the FASYNC flag for pipes */
    296  1.69  christos 		if (fp1) {
    297  1.93        ad 			if (!error) {
    298  1.93        ad 				mutex_enter(&fp1->f_lock);
    299  1.69  christos 				fp1->f_flag |= FASYNC;
    300  1.93        ad 				mutex_exit(&fp1->f_lock);
    301  1.93        ad 			}
    302  1.93        ad 			fd_putfile(fd);
    303  1.39      manu 		}
    304  1.41  jdolecek 
    305  1.41  jdolecek 		return (error);
    306  1.41  jdolecek 	    }
    307  1.92       dsl 
    308   1.1      fvdl 	case LINUX_F_GETLK:
    309  1.92       dsl 		do_linux_getlk(fd, cmd, arg, linux, flock);
    310  1.47  christos 
    311   1.1      fvdl 	case LINUX_F_SETLK:
    312   1.1      fvdl 	case LINUX_F_SETLKW:
    313  1.92       dsl 		do_linux_setlk(fd, cmd, arg, linux, flock, LINUX_F_SETLK);
    314  1.47  christos 
    315   1.1      fvdl 	case LINUX_F_SETOWN:
    316  1.65     perry 	case LINUX_F_GETOWN:
    317  1.13      fvdl 		/*
    318  1.49  jdolecek 		 * We need to route fcntl() for tty descriptors around normal
    319  1.49  jdolecek 		 * fcntl(), since NetBSD tty TIOC{G,S}PGRP semantics is too
    320  1.49  jdolecek 		 * restrictive for Linux F_{G,S}ETOWN. For non-tty descriptors,
    321  1.49  jdolecek 		 * this is not a problem.
    322  1.13      fvdl 		 */
    323  1.93        ad 		if ((fp = fd_getfile(fd)) == NULL)
    324  1.13      fvdl 			return EBADF;
    325  1.60  jdolecek 
    326  1.60  jdolecek 		/* Check it's a character device vnode */
    327  1.60  jdolecek 		if (fp->f_type != DTYPE_VNODE
    328  1.60  jdolecek 		    || (vp = (struct vnode *)fp->f_data) == NULL
    329  1.60  jdolecek 		    || vp->v_type != VCHR) {
    330  1.93        ad 			fd_putfile(fd);
    331  1.60  jdolecek 
    332  1.53   gehenna 	    not_tty:
    333  1.49  jdolecek 			/* Not a tty, proceed with common fcntl() */
    334  1.13      fvdl 			cmd = cmd == LINUX_F_SETOWN ? F_SETOWN : F_GETOWN;
    335  1.49  jdolecek 			break;
    336  1.49  jdolecek 		}
    337  1.47  christos 
    338  1.86     pooka 		error = VOP_GETATTR(vp, &va, l->l_cred);
    339  1.60  jdolecek 
    340  1.93        ad 		fd_putfile(fd);
    341  1.60  jdolecek 
    342  1.60  jdolecek 		if (error)
    343  1.49  jdolecek 			return error;
    344  1.60  jdolecek 
    345  1.94        ad 		if ((tp = cdev_tty(va.va_rdev)) == NULL)
    346  1.53   gehenna 			goto not_tty;
    347  1.49  jdolecek 
    348  1.49  jdolecek 		/* set tty pg_id appropriately */
    349  1.95        ad 		mutex_enter(proc_lock);
    350  1.49  jdolecek 		if (cmd == LINUX_F_GETOWN) {
    351  1.58       dsl 			retval[0] = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PGID;
    352  1.95        ad 			mutex_exit(proc_lock);
    353  1.49  jdolecek 			return 0;
    354  1.49  jdolecek 		}
    355  1.49  jdolecek 		if ((long)arg <= 0) {
    356  1.49  jdolecek 			pgid = -(long)arg;
    357  1.49  jdolecek 		} else {
    358  1.78       dsl 			struct proc *p1 = p_find((long)arg, PFIND_LOCKED | PFIND_UNLOCK_FAIL);
    359  1.49  jdolecek 			if (p1 == NULL)
    360  1.49  jdolecek 				return (ESRCH);
    361  1.49  jdolecek 			pgid = (long)p1->p_pgrp->pg_id;
    362  1.13      fvdl 		}
    363  1.78       dsl 		pgrp = pg_find(pgid, PFIND_LOCKED);
    364  1.78       dsl 		if (pgrp == NULL || pgrp->pg_session != p->p_session) {
    365  1.95        ad 			mutex_exit(proc_lock);
    366  1.49  jdolecek 			return EPERM;
    367  1.78       dsl 		}
    368  1.49  jdolecek 		tp->t_pgrp = pgrp;
    369  1.95        ad 		mutex_exit(proc_lock);
    370  1.49  jdolecek 		return 0;
    371  1.47  christos 
    372   1.1      fvdl 	default:
    373   1.1      fvdl 		return EOPNOTSUPP;
    374   1.1      fvdl 	}
    375   1.1      fvdl 
    376   1.1      fvdl 	SCARG(&fca, fd) = fd;
    377   1.1      fvdl 	SCARG(&fca, cmd) = cmd;
    378   1.1      fvdl 	SCARG(&fca, arg) = arg;
    379  1.12   mycroft 
    380  1.56   thorpej 	return sys_fcntl(l, &fca, retval);
    381   1.1      fvdl }
    382   1.1      fvdl 
    383  1.67      manu #if !defined(__amd64__)
    384   1.1      fvdl /*
    385   1.1      fvdl  * Convert a NetBSD stat structure to a Linux stat structure.
    386   1.1      fvdl  * Only the order of the fields and the padding in the structure
    387   1.9      fvdl  * is different. linux_fakedev is a machine-dependent function
    388   1.9      fvdl  * which optionally converts device driver major/minor numbers
    389   1.9      fvdl  * (XXX horrible, but what can you do against code that compares
    390   1.9      fvdl  * things against constant major device numbers? sigh)
    391   1.1      fvdl  */
    392   1.1      fvdl static void
    393  1.88       dsl bsd_to_linux_stat(struct stat *bsp, struct linux_stat *lsp)
    394   1.1      fvdl {
    395  1.12   mycroft 
    396  1.45  christos 	lsp->lst_dev     = linux_fakedev(bsp->st_dev, 0);
    397   1.1      fvdl 	lsp->lst_ino     = bsp->st_ino;
    398  1.19  christos 	lsp->lst_mode    = (linux_mode_t)bsp->st_mode;
    399  1.19  christos 	if (bsp->st_nlink >= (1 << 15))
    400  1.19  christos 		lsp->lst_nlink = (1 << 15) - 1;
    401  1.19  christos 	else
    402  1.19  christos 		lsp->lst_nlink = (linux_nlink_t)bsp->st_nlink;
    403   1.1      fvdl 	lsp->lst_uid     = bsp->st_uid;
    404   1.1      fvdl 	lsp->lst_gid     = bsp->st_gid;
    405  1.45  christos 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev, 1);
    406   1.1      fvdl 	lsp->lst_size    = bsp->st_size;
    407   1.1      fvdl 	lsp->lst_blksize = bsp->st_blksize;
    408   1.1      fvdl 	lsp->lst_blocks  = bsp->st_blocks;
    409   1.1      fvdl 	lsp->lst_atime   = bsp->st_atime;
    410   1.1      fvdl 	lsp->lst_mtime   = bsp->st_mtime;
    411   1.1      fvdl 	lsp->lst_ctime   = bsp->st_ctime;
    412  1.66  christos #ifdef LINUX_STAT_HAS_NSEC
    413  1.66  christos 	lsp->lst_atime_nsec   = bsp->st_atimensec;
    414  1.66  christos 	lsp->lst_mtime_nsec   = bsp->st_mtimensec;
    415  1.66  christos 	lsp->lst_ctime_nsec   = bsp->st_ctimensec;
    416  1.66  christos #endif
    417   1.1      fvdl }
    418   1.1      fvdl 
    419   1.1      fvdl /*
    420   1.1      fvdl  * The stat functions below are plain sailing. stat and lstat are handled
    421   1.1      fvdl  * by one function to avoid code duplication.
    422   1.1      fvdl  */
    423   1.1      fvdl int
    424  1.90       dsl linux_sys_fstat(struct lwp *l, const struct linux_sys_fstat_args *uap, register_t *retval)
    425  1.11   thorpej {
    426  1.90       dsl 	/* {
    427   1.1      fvdl 		syscallarg(int) fd;
    428   1.1      fvdl 		syscallarg(linux_stat *) sp;
    429  1.90       dsl 	} */
    430   1.1      fvdl 	struct linux_stat tmplst;
    431  1.81       dsl 	struct stat tmpst;
    432   1.1      fvdl 	int error;
    433   1.1      fvdl 
    434  1.93        ad 	error = do_sys_fstat(SCARG(uap, fd), &tmpst);
    435  1.81       dsl 	if (error != 0)
    436   1.1      fvdl 		return error;
    437   1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    438   1.1      fvdl 
    439  1.81       dsl 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    440   1.1      fvdl }
    441   1.1      fvdl 
    442   1.1      fvdl static int
    443  1.93        ad linux_stat1(const struct linux_sys_stat_args *uap, register_t *retval, int flags)
    444   1.1      fvdl {
    445   1.1      fvdl 	struct linux_stat tmplst;
    446  1.81       dsl 	struct stat tmpst;
    447   1.1      fvdl 	int error;
    448   1.1      fvdl 
    449  1.93        ad 	error = do_sys_stat(SCARG(uap, path), flags, &tmpst);
    450  1.81       dsl 	if (error != 0)
    451   1.1      fvdl 		return error;
    452   1.1      fvdl 
    453   1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    454   1.1      fvdl 
    455  1.81       dsl 	return copyout(&tmplst, SCARG(uap, sp), sizeof tmplst);
    456   1.1      fvdl }
    457   1.1      fvdl 
    458   1.1      fvdl int
    459  1.90       dsl linux_sys_stat(struct lwp *l, const struct linux_sys_stat_args *uap, register_t *retval)
    460  1.11   thorpej {
    461  1.90       dsl 	/* {
    462  1.27  christos 		syscallarg(const char *) path;
    463   1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    464  1.90       dsl 	} */
    465  1.11   thorpej 
    466  1.93        ad 	return linux_stat1(uap, retval, FOLLOW);
    467   1.1      fvdl }
    468   1.1      fvdl 
    469  1.23       erh /* Note: this is "newlstat" in the Linux sources */
    470  1.23       erh /*	(we don't bother with the old lstat currently) */
    471   1.1      fvdl int
    472  1.90       dsl linux_sys_lstat(struct lwp *l, const struct linux_sys_lstat_args *uap, register_t *retval)
    473  1.11   thorpej {
    474  1.90       dsl 	/* {
    475  1.27  christos 		syscallarg(const char *) path;
    476   1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    477  1.90       dsl 	} */
    478  1.11   thorpej 
    479  1.93        ad 	return linux_stat1((const void *)uap, retval, NOFOLLOW);
    480   1.1      fvdl }
    481  1.67      manu #endif /* !__amd64__ */
    482   1.1      fvdl 
    483   1.1      fvdl /*
    484  1.10      fvdl  * The following syscalls are mostly here because of the alternate path check.
    485   1.1      fvdl  */
    486   1.1      fvdl int
    487  1.90       dsl linux_sys_unlink(struct lwp *l, const struct linux_sys_unlink_args *uap, register_t *retval)
    488   1.2      fvdl {
    489  1.90       dsl 	/* {
    490  1.27  christos 		syscallarg(const char *) path;
    491  1.90       dsl 	} */
    492  1.63  jdolecek 	int error;
    493  1.63  jdolecek 	struct nameidata nd;
    494   1.2      fvdl 
    495  1.90       dsl 	error = sys_unlink(l, (const void *)uap, retval);
    496  1.63  jdolecek 	if (error != EPERM)
    497  1.63  jdolecek 		return (error);
    498  1.63  jdolecek 
    499  1.63  jdolecek 	/*
    500  1.63  jdolecek 	 * Linux returns EISDIR if unlink(2) is called on a directory.
    501  1.63  jdolecek 	 * We return EPERM in such cases. To emulate correct behaviour,
    502  1.63  jdolecek 	 * check if the path points to directory and return EISDIR if this
    503  1.63  jdolecek 	 * is the case.
    504  1.63  jdolecek 	 */
    505  1.82       dsl 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
    506  1.89     pooka 	    SCARG(uap, path));
    507  1.63  jdolecek 	if (namei(&nd) == 0) {
    508  1.63  jdolecek 		struct stat sb;
    509  1.63  jdolecek 
    510  1.93        ad 		if (vn_stat(nd.ni_vp, &sb) == 0
    511  1.63  jdolecek 		    && S_ISDIR(sb.st_mode))
    512  1.63  jdolecek 			error = EISDIR;
    513  1.63  jdolecek 
    514  1.63  jdolecek 		vput(nd.ni_vp);
    515  1.63  jdolecek 	}
    516  1.63  jdolecek 
    517  1.63  jdolecek 	return (error);
    518   1.2      fvdl }
    519   1.2      fvdl 
    520  1.10      fvdl int
    521  1.90       dsl linux_sys_mknod(struct lwp *l, const struct linux_sys_mknod_args *uap, register_t *retval)
    522  1.11   thorpej {
    523  1.90       dsl 	/* {
    524  1.27  christos 		syscallarg(const char *) path;
    525   1.2      fvdl 		syscallarg(int) mode;
    526   1.2      fvdl 		syscallarg(int) dev;
    527  1.90       dsl 	} */
    528   1.2      fvdl 
    529  1.10      fvdl 	/*
    530  1.40       wiz 	 * BSD handles FIFOs separately
    531  1.10      fvdl 	 */
    532  1.74     pavel 	if (S_ISFIFO(SCARG(uap, mode))) {
    533  1.54  jdolecek 		struct sys_mkfifo_args bma;
    534  1.54  jdolecek 
    535  1.21   thorpej 		SCARG(&bma, path) = SCARG(uap, path);
    536  1.21   thorpej 		SCARG(&bma, mode) = SCARG(uap, mode);
    537  1.56   thorpej 		return sys_mkfifo(l, &bma, retval);
    538  1.54  jdolecek 	} else {
    539  1.54  jdolecek 		struct sys_mknod_args bma;
    540  1.54  jdolecek 
    541  1.54  jdolecek 		SCARG(&bma, path) = SCARG(uap, path);
    542  1.54  jdolecek 		SCARG(&bma, mode) = SCARG(uap, mode);
    543  1.54  jdolecek 		/*
    544  1.54  jdolecek 		 * Linux device numbers uses 8 bits for minor and 8 bits
    545  1.54  jdolecek 		 * for major. Due to how we map our major and minor,
    546  1.82       dsl 		 * this just fits into our dev_t. Just mask off the
    547  1.54  jdolecek 		 * upper 16bit to remove any random junk.
    548  1.54  jdolecek 		 */
    549  1.54  jdolecek 		SCARG(&bma, dev) = SCARG(uap, dev) & 0xffff;
    550  1.56   thorpej 		return sys_mknod(l, &bma, retval);
    551  1.54  jdolecek 	}
    552   1.2      fvdl }
    553   1.2      fvdl 
    554  1.15      fvdl /*
    555  1.15      fvdl  * This is just fsync() for now (just as it is in the Linux kernel)
    556  1.23       erh  * Note: this is not implemented under Linux on Alpha and Arm
    557  1.23       erh  *	but should still be defined in our syscalls.master.
    558  1.23       erh  *	(syscall #148 on the arm)
    559  1.15      fvdl  */
    560  1.15      fvdl int
    561  1.90       dsl linux_sys_fdatasync(struct lwp *l, const struct linux_sys_fdatasync_args *uap, register_t *retval)
    562  1.15      fvdl {
    563  1.90       dsl 	/* {
    564  1.15      fvdl 		syscallarg(int) fd;
    565  1.90       dsl 	} */
    566  1.90       dsl 
    567  1.90       dsl 	return sys_fsync(l, (const void *)uap, retval);
    568  1.28      tron }
    569  1.28      tron 
    570  1.28      tron /*
    571  1.28      tron  * pread(2).
    572  1.28      tron  */
    573  1.28      tron int
    574  1.90       dsl linux_sys_pread(struct lwp *l, const struct linux_sys_pread_args *uap, register_t *retval)
    575  1.28      tron {
    576  1.90       dsl 	/* {
    577  1.28      tron 		syscallarg(int) fd;
    578  1.28      tron 		syscallarg(void *) buf;
    579  1.28      tron 		syscallarg(size_t) nbyte;
    580  1.28      tron 		syscallarg(linux_off_t) offset;
    581  1.90       dsl 	} */
    582  1.28      tron 	struct sys_pread_args pra;
    583  1.28      tron 
    584  1.28      tron 	SCARG(&pra, fd) = SCARG(uap, fd);
    585  1.28      tron 	SCARG(&pra, buf) = SCARG(uap, buf);
    586  1.28      tron 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    587  1.28      tron 	SCARG(&pra, offset) = SCARG(uap, offset);
    588  1.28      tron 
    589  1.62  jdolecek 	return sys_pread(l, &pra, retval);
    590  1.28      tron }
    591  1.28      tron 
    592  1.28      tron /*
    593  1.28      tron  * pwrite(2).
    594  1.28      tron  */
    595  1.28      tron int
    596  1.90       dsl linux_sys_pwrite(struct lwp *l, const struct linux_sys_pwrite_args *uap, register_t *retval)
    597  1.28      tron {
    598  1.90       dsl 	/* {
    599  1.28      tron 		syscallarg(int) fd;
    600  1.28      tron 		syscallarg(void *) buf;
    601  1.28      tron 		syscallarg(size_t) nbyte;
    602  1.28      tron 		syscallarg(linux_off_t) offset;
    603  1.90       dsl 	} */
    604  1.28      tron 	struct sys_pwrite_args pra;
    605  1.28      tron 
    606  1.28      tron 	SCARG(&pra, fd) = SCARG(uap, fd);
    607  1.28      tron 	SCARG(&pra, buf) = SCARG(uap, buf);
    608  1.28      tron 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    609  1.28      tron 	SCARG(&pra, offset) = SCARG(uap, offset);
    610  1.28      tron 
    611  1.62  jdolecek 	return sys_pwrite(l, &pra, retval);
    612   1.1      fvdl }
    613  1.68      fvdl 
    614  1.75  christos #define LINUX_NOT_SUPPORTED(fun) \
    615  1.75  christos int \
    616  1.90       dsl fun(struct lwp *l, const struct fun##_args *uap, register_t *retval) \
    617  1.75  christos { \
    618  1.75  christos 	return EOPNOTSUPP; \
    619  1.75  christos }
    620  1.75  christos 
    621  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_setxattr)
    622  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lsetxattr)
    623  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fsetxattr)
    624  1.75  christos 
    625  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_getxattr)
    626  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lgetxattr)
    627  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fgetxattr)
    628  1.75  christos 
    629  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_listxattr)
    630  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_llistxattr)
    631  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_flistxattr)
    632  1.75  christos 
    633  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_removexattr)
    634  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_lremovexattr)
    635  1.75  christos LINUX_NOT_SUPPORTED(linux_sys_fremovexattr)
    636  1.90       dsl 
    637