Home | History | Annotate | Line # | Download | only in common
linux_llseek.c revision 1.12
      1  1.12   mycroft /*	$NetBSD: linux_llseek.c,v 1.12 1995/10/07 06:27:05 mycroft Exp $	*/
      2   1.1      fvdl 
      3   1.1      fvdl /*
      4   1.1      fvdl  * Copyright (c) 1995 Frank van der Linden
      5   1.1      fvdl  * All rights reserved.
      6   1.1      fvdl  *
      7   1.1      fvdl  * Redistribution and use in source and binary forms, with or without
      8   1.1      fvdl  * modification, are permitted provided that the following conditions
      9   1.1      fvdl  * are met:
     10   1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     11   1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     12   1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     15   1.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     16   1.1      fvdl  *    must display the following acknowledgement:
     17   1.1      fvdl  *      This product includes software developed for the NetBSD Project
     18   1.1      fvdl  *      by Frank van der Linden
     19   1.1      fvdl  * 4. The name of the author may not be used to endorse or promote products
     20   1.1      fvdl  *    derived from this software without specific prior written permission
     21   1.1      fvdl  *
     22   1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1      fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1      fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1      fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1      fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1      fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1      fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1      fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1      fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1      fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1      fvdl  */
     33   1.1      fvdl 
     34   1.1      fvdl #include <sys/param.h>
     35   1.1      fvdl #include <sys/systm.h>
     36   1.1      fvdl #include <sys/namei.h>
     37   1.1      fvdl #include <sys/proc.h>
     38   1.1      fvdl #include <sys/file.h>
     39   1.1      fvdl #include <sys/stat.h>
     40   1.1      fvdl #include <sys/filedesc.h>
     41   1.1      fvdl #include <sys/ioctl.h>
     42   1.1      fvdl #include <sys/kernel.h>
     43   1.1      fvdl #include <sys/mount.h>
     44   1.1      fvdl #include <sys/malloc.h>
     45   1.1      fvdl 
     46   1.1      fvdl #include <sys/syscallargs.h>
     47   1.1      fvdl 
     48   1.1      fvdl #include <compat/linux/linux_types.h>
     49   1.8   mycroft #include <compat/linux/linux_signal.h>
     50   1.1      fvdl #include <compat/linux/linux_syscallargs.h>
     51   1.1      fvdl #include <compat/linux/linux_fcntl.h>
     52   1.1      fvdl #include <compat/linux/linux_util.h>
     53   1.1      fvdl 
     54   1.1      fvdl /*
     55   1.1      fvdl  * Some file-related calls are handled here. The usual flag conversion
     56   1.1      fvdl  * an structure conversion is done, and alternate emul path searching.
     57   1.1      fvdl  */
     58   1.1      fvdl 
     59   1.1      fvdl /*
     60   1.1      fvdl  * The next two functions convert between the Linux and NetBSD values
     61   1.1      fvdl  * of the flags used in open(2) and fcntl(2).
     62   1.1      fvdl  */
     63   1.1      fvdl static int
     64   1.1      fvdl linux_to_bsd_ioflags(int lflags)
     65   1.1      fvdl {
     66   1.1      fvdl 	int res = 0;
     67   1.1      fvdl 
     68   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_WRONLY, O_WRONLY);
     69   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDONLY, O_RDONLY);
     70   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_RDWR, O_RDWR);
     71   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_CREAT, O_CREAT);
     72   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_EXCL, O_EXCL);
     73   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NOCTTY, O_NOCTTY);
     74   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_TRUNC, O_TRUNC);
     75   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_NDELAY, O_NDELAY);
     76   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_SYNC, O_FSYNC);
     77   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_FASYNC, O_ASYNC);
     78   1.1      fvdl 	res |= cvtto_bsd_mask(lflags, LINUX_O_APPEND, O_APPEND);
     79   1.1      fvdl 
     80   1.1      fvdl 	return res;
     81   1.1      fvdl }
     82   1.1      fvdl 
     83   1.1      fvdl static int
     84   1.1      fvdl bsd_to_linux_ioflags(int bflags)
     85   1.1      fvdl {
     86   1.1      fvdl 	int res = 0;
     87   1.1      fvdl 
     88   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_WRONLY, LINUX_O_WRONLY);
     89   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDONLY, LINUX_O_RDONLY);
     90   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_RDWR, LINUX_O_RDWR);
     91   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_CREAT, LINUX_O_CREAT);
     92   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_EXCL, LINUX_O_EXCL);
     93   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NOCTTY, LINUX_O_NOCTTY);
     94   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_TRUNC, LINUX_O_TRUNC);
     95   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_NDELAY, LINUX_O_NDELAY);
     96   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_FSYNC, LINUX_O_SYNC);
     97   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_ASYNC, LINUX_FASYNC);
     98   1.1      fvdl 	res |= cvtto_linux_mask(bflags, O_APPEND, LINUX_O_APPEND);
     99   1.1      fvdl 
    100   1.1      fvdl 	return res;
    101   1.1      fvdl }
    102   1.1      fvdl 
    103   1.1      fvdl /*
    104   1.1      fvdl  * creat(2) is an obsolete function, but it's present as a Linux
    105   1.1      fvdl  * system call, so let's deal with it.
    106   1.1      fvdl  *
    107   1.1      fvdl  * Just call open(2) with the TRUNC, CREAT and WRONLY flags.
    108   1.1      fvdl  */
    109   1.1      fvdl int
    110  1.12   mycroft linux_sys_creat(p, v, retval)
    111   1.1      fvdl 	struct proc *p;
    112  1.11   thorpej 	void *v;
    113  1.11   thorpej 	register_t *retval;
    114  1.11   thorpej {
    115  1.12   mycroft 	struct linux_sys_creat_args /* {
    116   1.1      fvdl 		syscallarg(char *) path;
    117   1.1      fvdl 		syscallarg(int) mode;
    118  1.11   thorpej 	} */ *uap = v;
    119  1.12   mycroft 	struct sys_open_args oa;
    120   1.1      fvdl 	caddr_t sg;
    121   1.1      fvdl 
    122   1.5  christos 	sg = stackgap_init(p->p_emul);
    123   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    124   1.1      fvdl 
    125   1.1      fvdl 	SCARG(&oa, path) = SCARG(uap, path);
    126   1.1      fvdl 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    127   1.1      fvdl 	SCARG(&oa, mode) = SCARG(uap, mode);
    128  1.12   mycroft 
    129  1.12   mycroft 	return sys_open(p, &oa, retval);
    130   1.1      fvdl }
    131   1.1      fvdl 
    132   1.1      fvdl /*
    133   1.1      fvdl  * open(2). Take care of the different flag values, and let the
    134   1.1      fvdl  * NetBSD syscall do the real work. See if this operation
    135   1.1      fvdl  * gives the current process a controlling terminal.
    136   1.1      fvdl  * (XXX is this necessary?)
    137   1.1      fvdl  */
    138   1.1      fvdl int
    139  1.12   mycroft linux_sys_open(p, v, retval)
    140   1.1      fvdl 	struct proc *p;
    141  1.11   thorpej 	void *v;
    142  1.11   thorpej 	register_t *retval;
    143  1.11   thorpej {
    144  1.12   mycroft 	struct linux_sys_open_args /* {
    145   1.1      fvdl 		syscallarg(char *) path;
    146   1.1      fvdl 		syscallarg(int) flags;
    147   1.1      fvdl 		syscallarg(int) mode;
    148  1.11   thorpej 	} */ *uap = v;
    149   1.1      fvdl 	int error, fl;
    150  1.12   mycroft 	struct sys_open_args boa;
    151   1.1      fvdl 	caddr_t sg;
    152   1.1      fvdl 
    153   1.5  christos 	sg = stackgap_init(p->p_emul);
    154   1.1      fvdl 
    155   1.2      fvdl 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    156   1.1      fvdl 
    157   1.2      fvdl 	if (fl & O_CREAT)
    158   1.5  christos 		LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    159   1.2      fvdl 	else
    160   1.5  christos 		LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    161   1.1      fvdl 
    162   1.1      fvdl 	SCARG(&boa, path) = SCARG(uap, path);
    163   1.1      fvdl 	SCARG(&boa, flags) = fl;
    164   1.1      fvdl 	SCARG(&boa, mode) = SCARG(uap, mode);
    165   1.2      fvdl 
    166  1.12   mycroft 	if ((error = sys_open(p, &boa, retval)))
    167   1.1      fvdl 		return error;
    168   1.1      fvdl 
    169   1.1      fvdl 	/*
    170   1.1      fvdl 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    171   1.1      fvdl 	 * If we are a session leader, and we don't have a controlling
    172   1.1      fvdl 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    173   1.1      fvdl 	 * this the controlling terminal.
    174   1.1      fvdl 	 */
    175   1.1      fvdl         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
    176   1.1      fvdl                 struct filedesc *fdp = p->p_fd;
    177   1.1      fvdl                 struct file     *fp = fdp->fd_ofiles[*retval];
    178   1.1      fvdl 
    179   1.1      fvdl                 /* ignore any error, just give it a try */
    180   1.1      fvdl                 if (fp->f_type == DTYPE_VNODE)
    181   1.1      fvdl                         (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, (caddr_t) 0, p);
    182   1.1      fvdl         }
    183   1.1      fvdl 	return 0;
    184   1.1      fvdl }
    185   1.1      fvdl 
    186   1.1      fvdl /*
    187   1.2      fvdl  * This appears to be part of a Linux attempt to switch to 64 bits file sizes.
    188   1.2      fvdl  */
    189   1.2      fvdl int
    190  1.12   mycroft linux_sys_llseek(p, v, retval)
    191   1.2      fvdl 	struct proc *p;
    192  1.11   thorpej 	void *v;
    193  1.11   thorpej 	register_t *retval;
    194  1.11   thorpej {
    195  1.12   mycroft 	struct linux_sys_llseek_args /* {
    196   1.2      fvdl 		syscallarg(int) fd;
    197   1.2      fvdl 		syscallarg(uint32_t) ohigh;
    198   1.2      fvdl 		syscallarg(uint32_t) olow;
    199   1.2      fvdl 		syscallarg(caddr_t) res;
    200   1.2      fvdl 		syscallarg(int) whence;
    201  1.11   thorpej 	} */ *uap = v;
    202  1.12   mycroft 	struct sys_lseek_args bla;
    203   1.2      fvdl 	int error;
    204   1.2      fvdl 	off_t off;
    205   1.2      fvdl 
    206   1.2      fvdl 	off = SCARG(uap, olow) | (((off_t) SCARG(uap, ohigh)) << 32);
    207   1.2      fvdl 
    208   1.2      fvdl 	SCARG(&bla, fd) = SCARG(uap, fd);
    209   1.2      fvdl 	SCARG(&bla, offset) = off;
    210   1.2      fvdl 	SCARG(&bla, whence) = SCARG(uap, whence);
    211   1.2      fvdl 
    212  1.12   mycroft 	if ((error = sys_lseek(p, &bla, retval)))
    213   1.2      fvdl 		return error;
    214   1.2      fvdl 
    215   1.2      fvdl 	if ((error = copyout(retval, SCARG(uap, res), sizeof (off_t))))
    216   1.2      fvdl 		return error;
    217   1.2      fvdl 
    218   1.2      fvdl 	retval[0] = 0;
    219   1.2      fvdl 	return 0;
    220   1.2      fvdl }
    221   1.2      fvdl 
    222   1.2      fvdl /*
    223   1.1      fvdl  * The next two functions take care of converting the flock
    224   1.1      fvdl  * structure back and forth between Linux and NetBSD format.
    225   1.1      fvdl  * The only difference in the structures is the order of
    226   1.1      fvdl  * the fields, and the 'whence' value.
    227   1.1      fvdl  */
    228   1.1      fvdl static void
    229   1.1      fvdl bsd_to_linux_flock(bfp, lfp)
    230   1.1      fvdl 	struct flock *bfp;
    231   1.1      fvdl 	struct linux_flock *lfp;
    232   1.1      fvdl {
    233  1.12   mycroft 
    234   1.1      fvdl 	lfp->l_start = bfp->l_start;
    235   1.1      fvdl 	lfp->l_len = bfp->l_len;
    236   1.1      fvdl 	lfp->l_pid = bfp->l_pid;
    237   1.3   mycroft 	lfp->l_whence = bfp->l_whence;
    238   1.3   mycroft 	switch (bfp->l_type) {
    239   1.1      fvdl 	case F_RDLCK:
    240   1.3   mycroft 		lfp->l_type = LINUX_F_RDLCK;
    241   1.1      fvdl 		break;
    242   1.1      fvdl 	case F_UNLCK:
    243   1.3   mycroft 		lfp->l_type = LINUX_F_UNLCK;
    244   1.1      fvdl 		break;
    245   1.1      fvdl 	case F_WRLCK:
    246   1.3   mycroft 		lfp->l_type = LINUX_F_WRLCK;
    247   1.1      fvdl 		break;
    248   1.1      fvdl 	}
    249   1.1      fvdl }
    250   1.1      fvdl 
    251   1.1      fvdl static void
    252   1.1      fvdl linux_to_bsd_flock(lfp, bfp)
    253   1.1      fvdl 	struct linux_flock *lfp;
    254   1.1      fvdl 	struct flock *bfp;
    255   1.1      fvdl {
    256  1.12   mycroft 
    257   1.1      fvdl 	bfp->l_start = lfp->l_start;
    258   1.1      fvdl 	bfp->l_len = lfp->l_len;
    259   1.1      fvdl 	bfp->l_pid = lfp->l_pid;
    260   1.3   mycroft 	bfp->l_whence = lfp->l_whence;
    261   1.3   mycroft 	switch (lfp->l_type) {
    262   1.1      fvdl 	case LINUX_F_RDLCK:
    263   1.3   mycroft 		bfp->l_type = F_RDLCK;
    264   1.1      fvdl 		break;
    265   1.1      fvdl 	case LINUX_F_UNLCK:
    266   1.3   mycroft 		bfp->l_type = F_UNLCK;
    267   1.1      fvdl 		break;
    268   1.1      fvdl 	case LINUX_F_WRLCK:
    269   1.3   mycroft 		bfp->l_type = F_WRLCK;
    270   1.1      fvdl 		break;
    271   1.1      fvdl 	}
    272   1.1      fvdl }
    273   1.1      fvdl 
    274   1.1      fvdl /*
    275   1.1      fvdl  * Most actions in the fcntl() call are straightforward; simply
    276   1.1      fvdl  * pass control to the NetBSD system call. A few commands need
    277   1.1      fvdl  * conversions after the actual system call has done its work,
    278   1.1      fvdl  * because the flag values and lock structure are different.
    279   1.1      fvdl  */
    280   1.1      fvdl int
    281  1.12   mycroft linux_sys_fcntl(p, v, retval)
    282   1.1      fvdl 	struct proc *p;
    283  1.11   thorpej 	void *v;
    284  1.11   thorpej 	register_t *retval;
    285  1.11   thorpej {
    286  1.12   mycroft 	struct linux_sys_fcntl_args /* {
    287   1.1      fvdl 		syscallarg(int) fd;
    288   1.1      fvdl 		syscallarg(int) cmd;
    289   1.1      fvdl 		syscallarg(void *) arg;
    290  1.11   thorpej 	} */ *uap = v;
    291   1.6      fvdl 	int fd, cmd, error, val;
    292   1.1      fvdl 	caddr_t arg, sg;
    293   1.1      fvdl 	struct linux_flock lfl;
    294   1.1      fvdl 	struct flock *bfp, bfl;
    295  1.12   mycroft 	struct sys_fcntl_args fca;
    296   1.1      fvdl 
    297   1.1      fvdl 	fd = SCARG(uap, fd);
    298   1.1      fvdl 	cmd = SCARG(uap, cmd);
    299   1.1      fvdl 	arg = (caddr_t) SCARG(uap, arg);
    300   1.1      fvdl 
    301   1.1      fvdl 	switch (cmd) {
    302   1.1      fvdl 	case LINUX_F_DUPFD:
    303   1.1      fvdl 		cmd = F_DUPFD;
    304   1.1      fvdl 		break;
    305   1.1      fvdl 	case LINUX_F_GETFD:
    306   1.1      fvdl 		cmd = F_GETFD;
    307   1.1      fvdl 		break;
    308   1.1      fvdl 	case LINUX_F_SETFD:
    309   1.1      fvdl 		cmd = F_SETFD;
    310   1.1      fvdl 		break;
    311   1.1      fvdl 	case LINUX_F_GETFL:
    312   1.1      fvdl 		SCARG(&fca, fd) = fd;
    313   1.1      fvdl 		SCARG(&fca, cmd) = F_GETFL;
    314   1.1      fvdl 		SCARG(&fca, arg) = arg;
    315  1.12   mycroft 		if ((error = sys_fcntl(p, &fca, retval)))
    316   1.1      fvdl 			return error;
    317   1.1      fvdl 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    318   1.1      fvdl 		return 0;
    319   1.1      fvdl 	case LINUX_F_SETFL:
    320   1.6      fvdl 		val = linux_to_bsd_ioflags((int)SCARG(uap, arg));
    321   1.1      fvdl 		SCARG(&fca, fd) = fd;
    322   1.1      fvdl 		SCARG(&fca, cmd) = F_SETFL;
    323   1.6      fvdl 		SCARG(&fca, arg) = (caddr_t) val;
    324  1.12   mycroft 		return sys_fcntl(p, &fca, retval);
    325   1.1      fvdl 	case LINUX_F_GETLK:
    326   1.5  christos 		sg = stackgap_init(p->p_emul);
    327   1.1      fvdl 		bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
    328   1.1      fvdl 		SCARG(&fca, fd) = fd;
    329   1.1      fvdl 		SCARG(&fca, cmd) = F_GETLK;
    330   1.1      fvdl 		SCARG(&fca, arg) = bfp;
    331  1.12   mycroft 		if ((error = sys_fcntl(p, &fca, retval)))
    332   1.1      fvdl 			return error;
    333   1.1      fvdl 		if ((error = copyin(bfp, &bfl, sizeof bfl)))
    334   1.1      fvdl 			return error;
    335   1.1      fvdl 		bsd_to_linux_flock(&bfl, &lfl);
    336   1.1      fvdl 		return copyout(&lfl, arg, sizeof lfl);
    337   1.1      fvdl 		break;
    338   1.1      fvdl 	case LINUX_F_SETLK:
    339   1.1      fvdl 	case LINUX_F_SETLKW:
    340   1.1      fvdl 		cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
    341   1.1      fvdl 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    342   1.1      fvdl 			return error;
    343   1.1      fvdl 		linux_to_bsd_flock(&lfl, &bfl);
    344   1.5  christos 		sg = stackgap_init(p->p_emul);
    345   1.1      fvdl 		bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
    346   1.1      fvdl 		if ((error = copyout(&bfl, bfp, sizeof bfl)))
    347   1.1      fvdl 			return error;
    348   1.1      fvdl 		SCARG(&fca, fd) = fd;
    349   1.1      fvdl 		SCARG(&fca, cmd) = cmd;
    350   1.1      fvdl 		SCARG(&fca, arg) = bfp;
    351  1.12   mycroft 		return sys_fcntl(p, &fca, retval);
    352   1.1      fvdl 		break;
    353   1.1      fvdl 	case LINUX_F_SETOWN:
    354   1.1      fvdl 		cmd = F_SETOWN;
    355   1.1      fvdl 		break;
    356   1.1      fvdl 	case LINUX_F_GETOWN:
    357   1.1      fvdl 		cmd = F_GETOWN;
    358   1.1      fvdl 		break;
    359   1.1      fvdl 	default:
    360   1.1      fvdl 		return EOPNOTSUPP;
    361   1.1      fvdl 	}
    362   1.1      fvdl 
    363   1.1      fvdl 	SCARG(&fca, fd) = fd;
    364   1.1      fvdl 	SCARG(&fca, cmd) = cmd;
    365   1.1      fvdl 	SCARG(&fca, arg) = arg;
    366  1.12   mycroft 
    367  1.12   mycroft 	return sys_fcntl(p, &fca, retval);
    368   1.1      fvdl }
    369   1.1      fvdl 
    370   1.1      fvdl /*
    371   1.1      fvdl  * Convert a NetBSD stat structure to a Linux stat structure.
    372   1.1      fvdl  * Only the order of the fields and the padding in the structure
    373   1.9      fvdl  * is different. linux_fakedev is a machine-dependent function
    374   1.9      fvdl  * which optionally converts device driver major/minor numbers
    375   1.9      fvdl  * (XXX horrible, but what can you do against code that compares
    376   1.9      fvdl  * things against constant major device numbers? sigh)
    377   1.1      fvdl  */
    378   1.1      fvdl static void
    379   1.1      fvdl bsd_to_linux_stat(bsp, lsp)
    380   1.1      fvdl 	struct stat *bsp;
    381   1.1      fvdl 	struct linux_stat *lsp;
    382   1.1      fvdl {
    383  1.12   mycroft 
    384   1.1      fvdl 	lsp->lst_dev     = bsp->st_dev;
    385   1.1      fvdl 	lsp->lst_ino     = bsp->st_ino;
    386   1.1      fvdl 	lsp->lst_mode    = bsp->st_mode;
    387   1.1      fvdl 	lsp->lst_nlink   = bsp->st_nlink;
    388   1.1      fvdl 	lsp->lst_uid     = bsp->st_uid;
    389   1.1      fvdl 	lsp->lst_gid     = bsp->st_gid;
    390   1.9      fvdl 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev);
    391   1.1      fvdl 	lsp->lst_size    = bsp->st_size;
    392   1.1      fvdl 	lsp->lst_blksize = bsp->st_blksize;
    393   1.1      fvdl 	lsp->lst_blocks  = bsp->st_blocks;
    394   1.1      fvdl 	lsp->lst_atime   = bsp->st_atime;
    395   1.1      fvdl 	lsp->lst_mtime   = bsp->st_mtime;
    396   1.1      fvdl 	lsp->lst_ctime   = bsp->st_ctime;
    397   1.1      fvdl }
    398   1.1      fvdl 
    399   1.1      fvdl /*
    400   1.1      fvdl  * The stat functions below are plain sailing. stat and lstat are handled
    401   1.1      fvdl  * by one function to avoid code duplication.
    402   1.1      fvdl  */
    403   1.1      fvdl int
    404  1.12   mycroft linux_sys_fstat(p, v, retval)
    405   1.1      fvdl 	struct proc *p;
    406  1.11   thorpej 	void *v;
    407  1.11   thorpej 	register_t *retval;
    408  1.11   thorpej {
    409  1.12   mycroft 	struct linux_sys_fstat_args /* {
    410   1.1      fvdl 		syscallarg(int) fd;
    411   1.1      fvdl 		syscallarg(linux_stat *) sp;
    412  1.11   thorpej 	} */ *uap = v;
    413  1.12   mycroft 	struct sys_fstat_args fsa;
    414   1.1      fvdl 	struct linux_stat tmplst;
    415   1.1      fvdl 	struct stat *st,tmpst;
    416   1.1      fvdl 	caddr_t sg;
    417   1.1      fvdl 	int error;
    418   1.1      fvdl 
    419   1.5  christos 	sg = stackgap_init(p->p_emul);
    420   1.1      fvdl 
    421   1.1      fvdl 	st = stackgap_alloc(&sg, sizeof (struct stat));
    422   1.1      fvdl 
    423   1.1      fvdl 	SCARG(&fsa, fd) = SCARG(uap, fd);
    424   1.1      fvdl 	SCARG(&fsa, sb) = st;
    425   1.1      fvdl 
    426  1.12   mycroft 	if ((error = sys_fstat(p, &fsa, retval)))
    427   1.1      fvdl 		return error;
    428   1.1      fvdl 
    429   1.1      fvdl 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    430   1.1      fvdl 		return error;
    431   1.1      fvdl 
    432   1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    433   1.1      fvdl 
    434   1.1      fvdl 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    435   1.1      fvdl 		return error;
    436   1.1      fvdl 
    437   1.1      fvdl 	return 0;
    438   1.1      fvdl }
    439   1.1      fvdl 
    440   1.1      fvdl static int
    441   1.1      fvdl linux_stat1(p, uap, retval, dolstat)
    442   1.1      fvdl 	struct proc *p;
    443  1.12   mycroft 	struct linux_sys_stat_args *uap;
    444   1.1      fvdl 	register_t *retval;
    445   1.1      fvdl 	int dolstat;
    446   1.1      fvdl {
    447  1.12   mycroft 	struct sys_stat_args sa;
    448   1.1      fvdl 	struct linux_stat tmplst;
    449   1.1      fvdl 	struct stat *st, tmpst;
    450   1.1      fvdl 	caddr_t sg;
    451   1.1      fvdl 	int error;
    452   1.1      fvdl 
    453   1.5  christos 	sg = stackgap_init(p->p_emul);
    454   1.1      fvdl 
    455   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    456   1.1      fvdl 
    457   1.1      fvdl 	st = stackgap_alloc(&sg, sizeof (struct stat));
    458   1.1      fvdl 	SCARG(&sa, ub) = st;
    459   1.1      fvdl 	SCARG(&sa, path) = SCARG(uap, path);
    460   1.1      fvdl 
    461  1.12   mycroft 	if ((error = (dolstat ? sys_lstat(p, &sa, retval) :
    462  1.12   mycroft 				sys_stat(p, &sa, retval))))
    463   1.1      fvdl 		return error;
    464   1.1      fvdl 
    465   1.1      fvdl 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    466   1.1      fvdl 		return error;
    467   1.1      fvdl 
    468   1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    469   1.1      fvdl 
    470   1.1      fvdl 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    471   1.1      fvdl 		return error;
    472   1.1      fvdl 
    473   1.1      fvdl 	return 0;
    474   1.1      fvdl }
    475   1.1      fvdl 
    476   1.1      fvdl int
    477  1.12   mycroft linux_sys_stat(p, v, retval)
    478   1.1      fvdl 	struct proc *p;
    479  1.11   thorpej 	void *v;
    480  1.11   thorpej 	register_t *retval;
    481  1.11   thorpej {
    482  1.12   mycroft 	struct linux_sys_stat_args /* {
    483   1.1      fvdl 		syscallarg(char *) path;
    484   1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    485  1.11   thorpej 	} */ *uap = v;
    486  1.11   thorpej 
    487   1.1      fvdl 	return linux_stat1(p, uap, retval, 0);
    488   1.1      fvdl }
    489   1.1      fvdl 
    490   1.1      fvdl int
    491  1.12   mycroft linux_sys_lstat(p, v, retval)
    492   1.1      fvdl 	struct proc *p;
    493  1.11   thorpej 	void *v;
    494  1.11   thorpej 	register_t *retval;
    495  1.11   thorpej {
    496  1.12   mycroft 	struct linux_sys_lstat_args /* {
    497   1.1      fvdl 		syscallarg(char *) path;
    498   1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    499  1.11   thorpej 	} */ *uap = v;
    500  1.11   thorpej 
    501   1.1      fvdl 	return linux_stat1(p, uap, retval, 1);
    502   1.1      fvdl }
    503   1.1      fvdl 
    504   1.1      fvdl /*
    505  1.10      fvdl  * The following syscalls are mostly here because of the alternate path check.
    506   1.1      fvdl  */
    507   1.1      fvdl int
    508  1.12   mycroft linux_sys_access(p, v, retval)
    509   1.1      fvdl 	struct proc *p;
    510  1.11   thorpej 	void *v;
    511  1.11   thorpej 	register_t *retval;
    512  1.11   thorpej {
    513  1.12   mycroft 	struct linux_sys_access_args /* {
    514   1.1      fvdl 		syscallarg(char *) path;
    515   1.1      fvdl 		syscallarg(int) flags;
    516  1.11   thorpej 	} */ *uap = v;
    517   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    518   1.1      fvdl 
    519   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    520   1.1      fvdl 
    521  1.12   mycroft 	return sys_access(p, uap, retval);
    522   1.2      fvdl }
    523   1.2      fvdl 
    524   1.2      fvdl int
    525  1.12   mycroft linux_sys_unlink(p, v, retval)
    526   1.2      fvdl 	struct proc *p;
    527  1.11   thorpej 	void *v;
    528   1.2      fvdl 	register_t *retval;
    529   1.2      fvdl 
    530   1.2      fvdl {
    531  1.12   mycroft 	struct linux_sys_unlink_args /* {
    532  1.11   thorpej 		syscallarg(char *) path;
    533  1.11   thorpej 	} */ *uap = v;
    534   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    535   1.2      fvdl 
    536   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    537   1.2      fvdl 
    538  1.12   mycroft 	return sys_unlink(p, uap, retval);
    539   1.2      fvdl }
    540   1.2      fvdl 
    541  1.10      fvdl int
    542  1.12   mycroft linux_sys_chdir(p, v, retval)
    543   1.2      fvdl 	struct proc *p;
    544  1.11   thorpej 	void *v;
    545  1.11   thorpej 	register_t *retval;
    546  1.11   thorpej {
    547  1.12   mycroft 	struct linux_sys_chdir_args /* {
    548   1.2      fvdl 		syscallarg(char *) path;
    549  1.11   thorpej 	} */ *uap = v;
    550   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    551   1.2      fvdl 
    552   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    553   1.2      fvdl 
    554  1.12   mycroft 	return sys_chdir(p, uap, retval);
    555   1.2      fvdl }
    556   1.2      fvdl 
    557   1.2      fvdl int
    558  1.12   mycroft linux_sys_mknod(p, v, retval)
    559   1.2      fvdl 	struct proc *p;
    560  1.11   thorpej 	void *v;
    561  1.11   thorpej 	register_t *retval;
    562  1.11   thorpej {
    563  1.12   mycroft 	struct linux_sys_mknod_args /* {
    564   1.2      fvdl 		syscallarg(char *) path;
    565   1.2      fvdl 		syscallarg(int) mode;
    566   1.2      fvdl 		syscallarg(int) dev;
    567  1.11   thorpej 	} */ *uap = v;
    568   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    569  1.12   mycroft 	struct sys_mkfifo_args bma;
    570   1.2      fvdl 
    571   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    572   1.2      fvdl 
    573  1.10      fvdl 	/*
    574  1.10      fvdl 	 * BSD handles FIFOs seperately
    575  1.10      fvdl 	 */
    576  1.10      fvdl 	if (SCARG(uap, mode) & S_IFIFO) {
    577  1.10      fvdl 		SCARG(&bma, path) = SCARG(uap, path);
    578  1.10      fvdl 		SCARG(&bma, mode) = SCARG(uap, mode);
    579  1.12   mycroft 		return sys_mkfifo(p, uap, retval);
    580  1.10      fvdl 	} else
    581  1.12   mycroft 		return sys_mknod(p, uap, retval);
    582   1.2      fvdl }
    583   1.2      fvdl 
    584   1.2      fvdl int
    585  1.12   mycroft linux_sys_chmod(p, v, retval)
    586   1.2      fvdl 	struct proc *p;
    587  1.11   thorpej 	void *v;
    588  1.11   thorpej 	register_t *retval;
    589  1.11   thorpej {
    590  1.12   mycroft 	struct linux_sys_chmod_args /* {
    591   1.2      fvdl 		syscallarg(char *) path;
    592   1.2      fvdl 		syscallarg(int) mode;
    593  1.11   thorpej 	} */ *uap = v;
    594   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    595   1.2      fvdl 
    596   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    597   1.2      fvdl 
    598  1.12   mycroft 	return sys_chmod(p, uap, retval);
    599   1.2      fvdl }
    600   1.2      fvdl 
    601   1.2      fvdl int
    602  1.12   mycroft linux_sys_chown(p, v, retval)
    603   1.2      fvdl 	struct proc *p;
    604  1.11   thorpej 	void *v;
    605  1.11   thorpej 	register_t *retval;
    606  1.11   thorpej {
    607  1.12   mycroft 	struct linux_sys_chown_args /* {
    608   1.2      fvdl 		syscallarg(char *) path;
    609   1.2      fvdl 		syscallarg(int) uid;
    610   1.2      fvdl 		syscallarg(int) gid;
    611  1.11   thorpej 	} */ *uap = v;
    612  1.12   mycroft 	struct sys_chown_args bca;
    613   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    614   1.2      fvdl 
    615   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    616   1.2      fvdl 
    617  1.10      fvdl 	SCARG(&bca, path) = SCARG(uap, path);
    618  1.10      fvdl 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    619  1.10      fvdl 		(uid_t)-1 : SCARG(uap, uid);
    620  1.10      fvdl 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    621  1.10      fvdl 		(gid_t)-1 : SCARG(uap, gid);
    622  1.10      fvdl 
    623  1.12   mycroft 	return sys_chown(p, &bca, retval);
    624  1.10      fvdl }
    625  1.10      fvdl 
    626  1.10      fvdl int
    627  1.12   mycroft linux_sys_fchown(p, v, retval)
    628  1.10      fvdl 	struct proc *p;
    629  1.11   thorpej 	void *v;
    630  1.11   thorpej 	register_t *retval;
    631  1.11   thorpej {
    632  1.12   mycroft 	struct linux_sys_fchown_args /* {
    633  1.10      fvdl 		syscallarg(int) fd;
    634  1.10      fvdl 		syscallarg(int) uid;
    635  1.10      fvdl 		syscallarg(int) gid;
    636  1.11   thorpej 	} */ *uap = v;
    637  1.12   mycroft 	struct sys_fchown_args bfa;
    638  1.10      fvdl 
    639  1.10      fvdl 	SCARG(&bfa, fd) = SCARG(uap, fd);
    640  1.10      fvdl 	SCARG(&bfa, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    641  1.10      fvdl 		(uid_t)-1 : SCARG(uap, uid);
    642  1.10      fvdl 	SCARG(&bfa, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    643  1.10      fvdl 		(gid_t)-1 : SCARG(uap, gid);
    644  1.10      fvdl 
    645  1.12   mycroft 	return sys_fchown(p, &bfa, retval);
    646   1.2      fvdl }
    647   1.2      fvdl 
    648   1.2      fvdl int
    649  1.12   mycroft linux_sys_rename(p, v, retval)
    650   1.2      fvdl 	struct proc *p;
    651  1.11   thorpej 	void *v;
    652  1.11   thorpej 	register_t *retval;
    653  1.11   thorpej {
    654  1.12   mycroft 	struct linux_sys_rename_args /* {
    655   1.2      fvdl 		syscallarg(char *) from;
    656   1.2      fvdl 		syscallarg(char *) to;
    657  1.11   thorpej 	} */ *uap = v;
    658   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    659   1.2      fvdl 
    660   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, from));
    661   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
    662   1.2      fvdl 
    663  1.12   mycroft 	return sys_rename(p, uap, retval);
    664   1.2      fvdl }
    665   1.2      fvdl 
    666   1.2      fvdl int
    667  1.12   mycroft linux_sys_mkdir(p, v, retval)
    668   1.2      fvdl 	struct proc *p;
    669  1.11   thorpej 	void *v;
    670  1.11   thorpej 	register_t *retval;
    671  1.11   thorpej {
    672  1.12   mycroft 	struct linux_sys_mkdir_args /* {
    673   1.2      fvdl 		syscallarg(char *) path;
    674   1.7      fvdl 		syscallarg(int) mode;
    675  1.11   thorpej 	} */ *uap = v;
    676   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    677   1.2      fvdl 
    678   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    679  1.12   mycroft 
    680  1.12   mycroft 	return sys_mkdir(p, uap, retval);
    681   1.2      fvdl }
    682   1.2      fvdl 
    683   1.2      fvdl int
    684  1.12   mycroft linux_sys_rmdir(p, v, retval)
    685   1.2      fvdl 	struct proc *p;
    686  1.11   thorpej 	void *v;
    687  1.11   thorpej 	register_t *retval;
    688  1.11   thorpej {
    689  1.12   mycroft 	struct linux_sys_rmdir_args /* {
    690   1.2      fvdl 		syscallarg(char *) path;
    691  1.11   thorpej 	} */ *uap = v;
    692   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    693   1.2      fvdl 
    694   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    695  1.12   mycroft 
    696  1.12   mycroft 	return sys_rmdir(p, uap, retval);
    697   1.2      fvdl }
    698   1.2      fvdl 
    699   1.2      fvdl int
    700  1.12   mycroft linux_sys_symlink(p, v, retval)
    701   1.2      fvdl 	struct proc *p;
    702  1.11   thorpej 	void *v;
    703  1.11   thorpej 	register_t *retval;
    704  1.11   thorpej {
    705  1.12   mycroft 	struct linux_sys_symlink_args /* {
    706   1.2      fvdl 		syscallarg(char *) path;
    707   1.2      fvdl 		syscallarg(char *) to;
    708  1.11   thorpej 	} */ *uap = v;
    709   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    710   1.2      fvdl 
    711   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    712   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
    713   1.2      fvdl 
    714  1.12   mycroft 	return sys_symlink(p, uap, retval);
    715   1.2      fvdl }
    716   1.2      fvdl 
    717   1.2      fvdl int
    718  1.12   mycroft linux_sys_readlink(p, v, retval)
    719   1.2      fvdl 	struct proc *p;
    720  1.11   thorpej 	void *v;
    721  1.11   thorpej 	register_t *retval;
    722  1.11   thorpej {
    723  1.12   mycroft 	struct linux_sys_readlink_args /* {
    724   1.2      fvdl 		syscallarg(char *) name;
    725   1.2      fvdl 		syscallarg(char *) buf;
    726   1.2      fvdl 		syscallarg(int) count;
    727  1.11   thorpej 	} */ *uap = v;
    728   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    729   1.2      fvdl 
    730   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, name));
    731  1.12   mycroft 
    732  1.12   mycroft 	return sys_readlink(p, uap, retval);
    733   1.2      fvdl }
    734   1.2      fvdl 
    735   1.2      fvdl int
    736  1.12   mycroft linux_sys_truncate(p, v, retval)
    737   1.2      fvdl 	struct proc *p;
    738  1.11   thorpej 	void *v;
    739  1.11   thorpej 	register_t *retval;
    740  1.11   thorpej {
    741  1.12   mycroft 	struct linux_sys_truncate_args /* {
    742   1.2      fvdl 		syscallarg(char *) path;
    743   1.2      fvdl 		syscallarg(long) length;
    744  1.11   thorpej 	} */ *uap = v;
    745   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    746   1.2      fvdl 
    747   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    748  1.12   mycroft 
    749  1.12   mycroft 	return compat_43_sys_truncate(p, uap, retval);
    750   1.1      fvdl }
    751