Home | History | Annotate | Line # | Download | only in common
linux_llseek.c revision 1.10
      1  1.10      fvdl /*	$NetBSD: linux_llseek.c,v 1.10 1995/09/07 21:48:59 fvdl 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.1      fvdl linux_creat(p, uap, retval)
    111   1.1      fvdl 	struct proc *p;
    112   1.1      fvdl 	struct linux_creat_args /* {
    113   1.1      fvdl 		syscallarg(char *) path;
    114   1.1      fvdl 		syscallarg(int) mode;
    115   1.1      fvdl 	} */ *uap;
    116   1.1      fvdl 	register_t *retval;
    117   1.1      fvdl {
    118   1.1      fvdl 	struct open_args oa;
    119   1.1      fvdl 	caddr_t sg;
    120   1.1      fvdl 
    121   1.5  christos 	sg = stackgap_init(p->p_emul);
    122   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    123   1.1      fvdl 
    124   1.1      fvdl 	SCARG(&oa, path) = SCARG(uap, path);
    125   1.1      fvdl 	SCARG(&oa, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    126   1.1      fvdl 	SCARG(&oa, mode) = SCARG(uap, mode);
    127   1.1      fvdl 	return open(p, &oa, retval);
    128   1.1      fvdl }
    129   1.1      fvdl 
    130   1.1      fvdl /*
    131   1.1      fvdl  * open(2). Take care of the different flag values, and let the
    132   1.1      fvdl  * NetBSD syscall do the real work. See if this operation
    133   1.1      fvdl  * gives the current process a controlling terminal.
    134   1.1      fvdl  * (XXX is this necessary?)
    135   1.1      fvdl  */
    136   1.1      fvdl int
    137   1.1      fvdl linux_open(p, uap, retval)
    138   1.1      fvdl 	struct proc *p;
    139   1.1      fvdl 	struct linux_open_args /* {
    140   1.1      fvdl 		syscallarg(char *) path;
    141   1.1      fvdl 		syscallarg(int) flags;
    142   1.1      fvdl 		syscallarg(int) mode;
    143   1.1      fvdl 	} */ *uap;
    144   1.1      fvdl 	register_t *retval;
    145   1.1      fvdl {
    146   1.1      fvdl 	int error, fl;
    147   1.1      fvdl 	struct open_args boa;
    148   1.1      fvdl 	caddr_t sg;
    149   1.1      fvdl 
    150   1.5  christos 	sg = stackgap_init(p->p_emul);
    151   1.1      fvdl 
    152   1.2      fvdl 	fl = linux_to_bsd_ioflags(SCARG(uap, flags));
    153   1.1      fvdl 
    154   1.2      fvdl 	if (fl & O_CREAT)
    155   1.5  christos 		LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    156   1.2      fvdl 	else
    157   1.5  christos 		LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    158   1.1      fvdl 
    159   1.1      fvdl 	SCARG(&boa, path) = SCARG(uap, path);
    160   1.1      fvdl 	SCARG(&boa, flags) = fl;
    161   1.1      fvdl 	SCARG(&boa, mode) = SCARG(uap, mode);
    162   1.2      fvdl 
    163   1.1      fvdl 	if ((error = open(p, &boa, retval)))
    164   1.1      fvdl 		return error;
    165   1.1      fvdl 
    166   1.1      fvdl 	/*
    167   1.1      fvdl 	 * this bit from sunos_misc.c (and svr4_fcntl.c).
    168   1.1      fvdl 	 * If we are a session leader, and we don't have a controlling
    169   1.1      fvdl 	 * terminal yet, and the O_NOCTTY flag is not set, try to make
    170   1.1      fvdl 	 * this the controlling terminal.
    171   1.1      fvdl 	 */
    172   1.1      fvdl         if (!(fl & O_NOCTTY) && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
    173   1.1      fvdl                 struct filedesc *fdp = p->p_fd;
    174   1.1      fvdl                 struct file     *fp = fdp->fd_ofiles[*retval];
    175   1.1      fvdl 
    176   1.1      fvdl                 /* ignore any error, just give it a try */
    177   1.1      fvdl                 if (fp->f_type == DTYPE_VNODE)
    178   1.1      fvdl                         (fp->f_ops->fo_ioctl) (fp, TIOCSCTTY, (caddr_t) 0, p);
    179   1.1      fvdl         }
    180   1.1      fvdl 	return 0;
    181   1.1      fvdl }
    182   1.1      fvdl 
    183   1.1      fvdl /*
    184   1.2      fvdl  * This appears to be part of a Linux attempt to switch to 64 bits file sizes.
    185   1.2      fvdl  */
    186   1.2      fvdl int
    187   1.2      fvdl linux_llseek(p, uap, retval)
    188   1.2      fvdl 	struct proc *p;
    189   1.2      fvdl 	struct linux_llseek_args /* {
    190   1.2      fvdl 		syscallarg(int) fd;
    191   1.2      fvdl 		syscallarg(uint32_t) ohigh;
    192   1.2      fvdl 		syscallarg(uint32_t) olow;
    193   1.2      fvdl 		syscallarg(caddr_t) res;
    194   1.2      fvdl 		syscallarg(int) whence;
    195   1.2      fvdl 	} */ *uap;
    196   1.2      fvdl 	register_t *retval;
    197   1.2      fvdl {
    198   1.2      fvdl 	struct lseek_args bla;
    199   1.2      fvdl 	int error;
    200   1.2      fvdl 	off_t off;
    201   1.2      fvdl 
    202   1.2      fvdl 	off = SCARG(uap, olow) | (((off_t) SCARG(uap, ohigh)) << 32);
    203   1.2      fvdl 
    204   1.2      fvdl 	SCARG(&bla, fd) = SCARG(uap, fd);
    205   1.2      fvdl 	SCARG(&bla, offset) = off;
    206   1.2      fvdl 	SCARG(&bla, whence) = SCARG(uap, whence);
    207   1.2      fvdl 
    208   1.2      fvdl 	if ((error = lseek(p, &bla, retval)))
    209   1.2      fvdl 		return error;
    210   1.2      fvdl 
    211   1.2      fvdl 	if ((error = copyout(retval, SCARG(uap, res), sizeof (off_t))))
    212   1.2      fvdl 		return error;
    213   1.2      fvdl 
    214   1.2      fvdl 	retval[0] = 0;
    215   1.2      fvdl 	return 0;
    216   1.2      fvdl }
    217   1.2      fvdl 
    218   1.2      fvdl /*
    219   1.1      fvdl  * The next two functions take care of converting the flock
    220   1.1      fvdl  * structure back and forth between Linux and NetBSD format.
    221   1.1      fvdl  * The only difference in the structures is the order of
    222   1.1      fvdl  * the fields, and the 'whence' value.
    223   1.1      fvdl  */
    224   1.1      fvdl static void
    225   1.1      fvdl bsd_to_linux_flock(bfp, lfp)
    226   1.1      fvdl 	struct flock *bfp;
    227   1.1      fvdl 	struct linux_flock *lfp;
    228   1.1      fvdl {
    229   1.1      fvdl 	lfp->l_start = bfp->l_start;
    230   1.1      fvdl 	lfp->l_len = bfp->l_len;
    231   1.1      fvdl 	lfp->l_pid = bfp->l_pid;
    232   1.3   mycroft 	lfp->l_whence = bfp->l_whence;
    233   1.3   mycroft 	switch (bfp->l_type) {
    234   1.1      fvdl 	case F_RDLCK:
    235   1.3   mycroft 		lfp->l_type = LINUX_F_RDLCK;
    236   1.1      fvdl 		break;
    237   1.1      fvdl 	case F_UNLCK:
    238   1.3   mycroft 		lfp->l_type = LINUX_F_UNLCK;
    239   1.1      fvdl 		break;
    240   1.1      fvdl 	case F_WRLCK:
    241   1.3   mycroft 		lfp->l_type = LINUX_F_WRLCK;
    242   1.1      fvdl 		break;
    243   1.1      fvdl 	}
    244   1.1      fvdl }
    245   1.1      fvdl 
    246   1.1      fvdl static void
    247   1.1      fvdl linux_to_bsd_flock(lfp, bfp)
    248   1.1      fvdl 	struct linux_flock *lfp;
    249   1.1      fvdl 	struct flock *bfp;
    250   1.1      fvdl {
    251   1.1      fvdl 	bfp->l_start = lfp->l_start;
    252   1.1      fvdl 	bfp->l_len = lfp->l_len;
    253   1.1      fvdl 	bfp->l_pid = lfp->l_pid;
    254   1.3   mycroft 	bfp->l_whence = lfp->l_whence;
    255   1.3   mycroft 	switch (lfp->l_type) {
    256   1.1      fvdl 	case LINUX_F_RDLCK:
    257   1.3   mycroft 		bfp->l_type = F_RDLCK;
    258   1.1      fvdl 		break;
    259   1.1      fvdl 	case LINUX_F_UNLCK:
    260   1.3   mycroft 		bfp->l_type = F_UNLCK;
    261   1.1      fvdl 		break;
    262   1.1      fvdl 	case LINUX_F_WRLCK:
    263   1.3   mycroft 		bfp->l_type = F_WRLCK;
    264   1.1      fvdl 		break;
    265   1.1      fvdl 	}
    266   1.1      fvdl }
    267   1.1      fvdl 
    268   1.1      fvdl /*
    269   1.1      fvdl  * Most actions in the fcntl() call are straightforward; simply
    270   1.1      fvdl  * pass control to the NetBSD system call. A few commands need
    271   1.1      fvdl  * conversions after the actual system call has done its work,
    272   1.1      fvdl  * because the flag values and lock structure are different.
    273   1.1      fvdl  */
    274   1.1      fvdl int
    275   1.1      fvdl linux_fcntl(p, uap, retval)
    276   1.1      fvdl 	struct proc *p;
    277   1.1      fvdl 	struct linux_fcntl_args /* {
    278   1.1      fvdl 		syscallarg(int) fd;
    279   1.1      fvdl 		syscallarg(int) cmd;
    280   1.1      fvdl 		syscallarg(void *) arg;
    281   1.1      fvdl 	} */ *uap;
    282   1.1      fvdl 	register_t *retval;
    283   1.1      fvdl {
    284   1.6      fvdl 	int fd, cmd, error, val;
    285   1.1      fvdl 	caddr_t arg, sg;
    286   1.1      fvdl 	struct linux_flock lfl;
    287   1.1      fvdl 	struct flock *bfp, bfl;
    288   1.1      fvdl 	struct fcntl_args fca;
    289   1.1      fvdl 
    290   1.1      fvdl 	fd = SCARG(uap, fd);
    291   1.1      fvdl 	cmd = SCARG(uap, cmd);
    292   1.1      fvdl 	arg = (caddr_t) SCARG(uap, arg);
    293   1.1      fvdl 
    294   1.1      fvdl 	switch (cmd) {
    295   1.1      fvdl 	case LINUX_F_DUPFD:
    296   1.1      fvdl 		cmd = F_DUPFD;
    297   1.1      fvdl 		break;
    298   1.1      fvdl 	case LINUX_F_GETFD:
    299   1.1      fvdl 		cmd = F_GETFD;
    300   1.1      fvdl 		break;
    301   1.1      fvdl 	case LINUX_F_SETFD:
    302   1.1      fvdl 		cmd = F_SETFD;
    303   1.1      fvdl 		break;
    304   1.1      fvdl 	case LINUX_F_GETFL:
    305   1.1      fvdl 		SCARG(&fca, fd) = fd;
    306   1.1      fvdl 		SCARG(&fca, cmd) = F_GETFL;
    307   1.1      fvdl 		SCARG(&fca, arg) = arg;
    308   1.1      fvdl 		if ((error = fcntl(p, &fca, retval)))
    309   1.1      fvdl 			return error;
    310   1.1      fvdl 		retval[0] = bsd_to_linux_ioflags(retval[0]);
    311   1.1      fvdl 		return 0;
    312   1.1      fvdl 	case LINUX_F_SETFL:
    313   1.6      fvdl 		val = linux_to_bsd_ioflags((int)SCARG(uap, arg));
    314   1.1      fvdl 		SCARG(&fca, fd) = fd;
    315   1.1      fvdl 		SCARG(&fca, cmd) = F_SETFL;
    316   1.6      fvdl 		SCARG(&fca, arg) = (caddr_t) val;
    317   1.1      fvdl 		return fcntl(p, &fca, retval);
    318   1.1      fvdl 	case LINUX_F_GETLK:
    319   1.5  christos 		sg = stackgap_init(p->p_emul);
    320   1.1      fvdl 		bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
    321   1.1      fvdl 		SCARG(&fca, fd) = fd;
    322   1.1      fvdl 		SCARG(&fca, cmd) = F_GETLK;
    323   1.1      fvdl 		SCARG(&fca, arg) = bfp;
    324   1.1      fvdl 		if ((error = fcntl(p, &fca, retval)))
    325   1.1      fvdl 			return error;
    326   1.1      fvdl 		if ((error = copyin(bfp, &bfl, sizeof bfl)))
    327   1.1      fvdl 			return error;
    328   1.1      fvdl 		bsd_to_linux_flock(&bfl, &lfl);
    329   1.1      fvdl 		return copyout(&lfl, arg, sizeof lfl);
    330   1.1      fvdl 		break;
    331   1.1      fvdl 	case LINUX_F_SETLK:
    332   1.1      fvdl 	case LINUX_F_SETLKW:
    333   1.1      fvdl 		cmd = (cmd == LINUX_F_SETLK ? F_SETLK : F_SETLKW);
    334   1.1      fvdl 		if ((error = copyin(arg, &lfl, sizeof lfl)))
    335   1.1      fvdl 			return error;
    336   1.1      fvdl 		linux_to_bsd_flock(&lfl, &bfl);
    337   1.5  christos 		sg = stackgap_init(p->p_emul);
    338   1.1      fvdl 		bfp = (struct flock *) stackgap_alloc(&sg, sizeof *bfp);
    339   1.1      fvdl 		if ((error = copyout(&bfl, bfp, sizeof bfl)))
    340   1.1      fvdl 			return error;
    341   1.1      fvdl 		SCARG(&fca, fd) = fd;
    342   1.1      fvdl 		SCARG(&fca, cmd) = cmd;
    343   1.1      fvdl 		SCARG(&fca, arg) = bfp;
    344   1.1      fvdl 		return fcntl(p, &fca, retval);
    345   1.1      fvdl 		break;
    346   1.1      fvdl 	case LINUX_F_SETOWN:
    347   1.1      fvdl 		cmd = F_SETOWN;
    348   1.1      fvdl 		break;
    349   1.1      fvdl 	case LINUX_F_GETOWN:
    350   1.1      fvdl 		cmd = F_GETOWN;
    351   1.1      fvdl 		break;
    352   1.1      fvdl 	default:
    353   1.1      fvdl 		return EOPNOTSUPP;
    354   1.1      fvdl 	}
    355   1.1      fvdl 
    356   1.1      fvdl 	SCARG(&fca, fd) = fd;
    357   1.1      fvdl 	SCARG(&fca, cmd) = cmd;
    358   1.1      fvdl 	SCARG(&fca, arg) = arg;
    359   1.6      fvdl 	return fcntl(p, &fca, retval);
    360   1.1      fvdl }
    361   1.1      fvdl 
    362   1.1      fvdl /*
    363   1.1      fvdl  * Convert a NetBSD stat structure to a Linux stat structure.
    364   1.1      fvdl  * Only the order of the fields and the padding in the structure
    365   1.9      fvdl  * is different. linux_fakedev is a machine-dependent function
    366   1.9      fvdl  * which optionally converts device driver major/minor numbers
    367   1.9      fvdl  * (XXX horrible, but what can you do against code that compares
    368   1.9      fvdl  * things against constant major device numbers? sigh)
    369   1.1      fvdl  */
    370   1.1      fvdl static void
    371   1.1      fvdl bsd_to_linux_stat(bsp, lsp)
    372   1.1      fvdl 	struct stat *bsp;
    373   1.1      fvdl 	struct linux_stat *lsp;
    374   1.1      fvdl {
    375   1.1      fvdl 	lsp->lst_dev     = bsp->st_dev;
    376   1.1      fvdl 	lsp->lst_ino     = bsp->st_ino;
    377   1.1      fvdl 	lsp->lst_mode    = bsp->st_mode;
    378   1.1      fvdl 	lsp->lst_nlink   = bsp->st_nlink;
    379   1.1      fvdl 	lsp->lst_uid     = bsp->st_uid;
    380   1.1      fvdl 	lsp->lst_gid     = bsp->st_gid;
    381   1.9      fvdl 	lsp->lst_rdev    = linux_fakedev(bsp->st_rdev);
    382   1.1      fvdl 	lsp->lst_size    = bsp->st_size;
    383   1.1      fvdl 	lsp->lst_blksize = bsp->st_blksize;
    384   1.1      fvdl 	lsp->lst_blocks  = bsp->st_blocks;
    385   1.1      fvdl 	lsp->lst_atime   = bsp->st_atime;
    386   1.1      fvdl 	lsp->lst_mtime   = bsp->st_mtime;
    387   1.1      fvdl 	lsp->lst_ctime   = bsp->st_ctime;
    388   1.1      fvdl }
    389   1.1      fvdl 
    390   1.1      fvdl /*
    391   1.1      fvdl  * The stat functions below are plain sailing. stat and lstat are handled
    392   1.1      fvdl  * by one function to avoid code duplication.
    393   1.1      fvdl  */
    394   1.1      fvdl int
    395   1.1      fvdl linux_fstat(p, uap, retval)
    396   1.1      fvdl 	struct proc *p;
    397   1.1      fvdl 	struct linux_fstat_args /* {
    398   1.1      fvdl 		syscallarg(int) fd;
    399   1.1      fvdl 		syscallarg(linux_stat *) sp;
    400   1.1      fvdl 	} */ *uap;
    401   1.1      fvdl 	register_t *retval;
    402   1.1      fvdl {
    403   1.1      fvdl 	struct fstat_args fsa;
    404   1.1      fvdl 	struct linux_stat tmplst;
    405   1.1      fvdl 	struct stat *st,tmpst;
    406   1.1      fvdl 	caddr_t sg;
    407   1.1      fvdl 	int error;
    408   1.1      fvdl 
    409   1.5  christos 	sg = stackgap_init(p->p_emul);
    410   1.1      fvdl 
    411   1.1      fvdl 	st = stackgap_alloc(&sg, sizeof (struct stat));
    412   1.1      fvdl 
    413   1.1      fvdl 	SCARG(&fsa, fd) = SCARG(uap, fd);
    414   1.1      fvdl 	SCARG(&fsa, sb) = st;
    415   1.1      fvdl 
    416   1.1      fvdl 	if ((error = fstat(p, &fsa, retval)))
    417   1.1      fvdl 		return error;
    418   1.1      fvdl 
    419   1.1      fvdl 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    420   1.1      fvdl 		return error;
    421   1.1      fvdl 
    422   1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    423   1.1      fvdl 
    424   1.1      fvdl 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    425   1.1      fvdl 		return error;
    426   1.1      fvdl 
    427   1.1      fvdl 	return 0;
    428   1.1      fvdl }
    429   1.1      fvdl 
    430   1.1      fvdl static int
    431   1.1      fvdl linux_stat1(p, uap, retval, dolstat)
    432   1.1      fvdl 	struct proc *p;
    433   1.1      fvdl 	struct linux_stat_args *uap;
    434   1.1      fvdl 	register_t *retval;
    435   1.1      fvdl 	int dolstat;
    436   1.1      fvdl {
    437   1.1      fvdl 	struct stat_args sa;
    438   1.1      fvdl 	struct linux_stat tmplst;
    439   1.1      fvdl 	struct stat *st, tmpst;
    440   1.1      fvdl 	caddr_t sg;
    441   1.1      fvdl 	int error;
    442   1.1      fvdl 
    443   1.5  christos 	sg = stackgap_init(p->p_emul);
    444   1.1      fvdl 
    445   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    446   1.1      fvdl 
    447   1.1      fvdl 	st = stackgap_alloc(&sg, sizeof (struct stat));
    448   1.1      fvdl 	SCARG(&sa, ub) = st;
    449   1.1      fvdl 	SCARG(&sa, path) = SCARG(uap, path);
    450   1.1      fvdl 
    451   1.1      fvdl 	if ((error = (dolstat ? lstat(p, &sa, retval) : stat(p, &sa, retval))))
    452   1.1      fvdl 		return error;
    453   1.1      fvdl 
    454   1.1      fvdl 	if ((error = copyin(st, &tmpst, sizeof tmpst)))
    455   1.1      fvdl 		return error;
    456   1.1      fvdl 
    457   1.1      fvdl 	bsd_to_linux_stat(&tmpst, &tmplst);
    458   1.1      fvdl 
    459   1.1      fvdl 	if ((error = copyout(&tmplst, SCARG(uap, sp), sizeof tmplst)))
    460   1.1      fvdl 		return error;
    461   1.1      fvdl 
    462   1.1      fvdl 	return 0;
    463   1.1      fvdl }
    464   1.1      fvdl 
    465   1.1      fvdl int
    466   1.1      fvdl linux_stat(p, uap, retval)
    467   1.1      fvdl 	struct proc *p;
    468   1.1      fvdl 	struct linux_stat_args /* {
    469   1.1      fvdl 		syscallarg(char *) path;
    470   1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    471   1.1      fvdl 	} */ *uap;
    472   1.1      fvdl 	register_t *retval;
    473   1.1      fvdl {
    474   1.1      fvdl 	return linux_stat1(p, uap, retval, 0);
    475   1.1      fvdl }
    476   1.1      fvdl 
    477   1.1      fvdl int
    478   1.1      fvdl linux_lstat(p, uap, retval)
    479   1.1      fvdl 	struct proc *p;
    480   1.1      fvdl 	struct linux_lstat_args /* {
    481   1.1      fvdl 		syscallarg(char *) path;
    482   1.1      fvdl 		syscallarg(struct linux_stat *) sp;
    483   1.1      fvdl 	} */ *uap;
    484   1.1      fvdl 	register_t *retval;
    485   1.1      fvdl {
    486   1.1      fvdl 	return linux_stat1(p, uap, retval, 1);
    487   1.1      fvdl }
    488   1.1      fvdl 
    489   1.1      fvdl /*
    490  1.10      fvdl  * The following syscalls are mostly here because of the alternate path check.
    491   1.1      fvdl  */
    492   1.1      fvdl int
    493   1.1      fvdl linux_access(p, uap, retval)
    494   1.1      fvdl 	struct proc *p;
    495   1.1      fvdl 	struct linux_access_args /* {
    496   1.1      fvdl 		syscallarg(char *) path;
    497   1.1      fvdl 		syscallarg(int) flags;
    498   1.1      fvdl 	} */ *uap;
    499   1.1      fvdl 	register_t *retval;
    500   1.1      fvdl {
    501   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    502   1.1      fvdl 
    503   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    504   1.1      fvdl 
    505   1.1      fvdl 	return access(p, uap, retval);
    506   1.2      fvdl }
    507   1.2      fvdl 
    508   1.2      fvdl int
    509   1.2      fvdl linux_unlink(p, uap, retval)
    510   1.2      fvdl 	struct proc *p;
    511   1.2      fvdl 	struct linux_unlink_args /* {
    512   1.2      fvdl 		syscallarg(char *) path;
    513   1.2      fvdl 	} */ *uap;
    514   1.2      fvdl 	register_t *retval;
    515   1.2      fvdl 
    516   1.2      fvdl {
    517   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    518   1.2      fvdl 
    519   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    520   1.2      fvdl 
    521   1.2      fvdl 	return unlink(p, uap, retval);
    522   1.2      fvdl }
    523   1.2      fvdl 
    524  1.10      fvdl int
    525  1.10      fvdl linux_chdir(p, uap, retval)
    526   1.2      fvdl 	struct proc *p;
    527   1.2      fvdl 	struct linux_chdir_args /* {
    528   1.2      fvdl 		syscallarg(char *) path;
    529   1.2      fvdl 	} */ *uap;
    530   1.2      fvdl 	register_t *retval;
    531   1.2      fvdl {
    532   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    533   1.2      fvdl 
    534   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    535   1.2      fvdl 
    536   1.2      fvdl 	return chdir(p, uap, retval);
    537   1.2      fvdl }
    538   1.2      fvdl 
    539   1.2      fvdl int
    540   1.2      fvdl linux_mknod(p, uap, retval)
    541   1.2      fvdl 	struct proc *p;
    542   1.2      fvdl 	struct linux_mknod_args /* {
    543   1.2      fvdl 		syscallarg(char *) path;
    544   1.2      fvdl 		syscallarg(int) mode;
    545   1.2      fvdl 		syscallarg(int) dev;
    546   1.2      fvdl 	} */ *uap;
    547   1.2      fvdl 	register_t *retval;
    548   1.2      fvdl {
    549   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    550  1.10      fvdl 	struct mkfifo_args bma;
    551   1.2      fvdl 
    552   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    553   1.2      fvdl 
    554  1.10      fvdl 	/*
    555  1.10      fvdl 	 * BSD handles FIFOs seperately
    556  1.10      fvdl 	 */
    557  1.10      fvdl 	if (SCARG(uap, mode) & S_IFIFO) {
    558  1.10      fvdl 		SCARG(&bma, path) = SCARG(uap, path);
    559  1.10      fvdl 		SCARG(&bma, mode) = SCARG(uap, mode);
    560  1.10      fvdl 		return mkfifo(p, uap, retval);
    561  1.10      fvdl 	} else
    562  1.10      fvdl 		return mknod(p, uap, retval);
    563   1.2      fvdl }
    564   1.2      fvdl 
    565   1.2      fvdl int
    566   1.2      fvdl linux_chmod(p, uap, retval)
    567   1.2      fvdl 	struct proc *p;
    568   1.2      fvdl 	struct linux_chmod_args /* {
    569   1.2      fvdl 		syscallarg(char *) path;
    570   1.2      fvdl 		syscallarg(int) mode;
    571   1.2      fvdl 	} */ *uap;
    572   1.2      fvdl 	register_t *retval;
    573   1.2      fvdl {
    574   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    575   1.2      fvdl 
    576   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    577   1.2      fvdl 
    578   1.2      fvdl 	return chmod(p, uap, retval);
    579   1.2      fvdl }
    580   1.2      fvdl 
    581   1.2      fvdl int
    582   1.2      fvdl linux_chown(p, uap, retval)
    583   1.2      fvdl 	struct proc *p;
    584   1.2      fvdl 	struct linux_chown_args /* {
    585   1.2      fvdl 		syscallarg(char *) path;
    586   1.2      fvdl 		syscallarg(int) uid;
    587   1.2      fvdl 		syscallarg(int) gid;
    588   1.2      fvdl 	} */ *uap;
    589   1.2      fvdl 	register_t *retval;
    590   1.2      fvdl {
    591  1.10      fvdl 	struct chown_args bca;
    592   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    593   1.2      fvdl 
    594   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    595   1.2      fvdl 
    596  1.10      fvdl 	SCARG(&bca, path) = SCARG(uap, path);
    597  1.10      fvdl 	SCARG(&bca, uid) = ((linux_uid_t)SCARG(uap, uid) == (linux_uid_t)-1) ?
    598  1.10      fvdl 		(uid_t)-1 : SCARG(uap, uid);
    599  1.10      fvdl 	SCARG(&bca, gid) = ((linux_gid_t)SCARG(uap, gid) == (linux_gid_t)-1) ?
    600  1.10      fvdl 		(gid_t)-1 : SCARG(uap, gid);
    601  1.10      fvdl 
    602  1.10      fvdl 	return chown(p, &bca, retval);
    603  1.10      fvdl }
    604  1.10      fvdl 
    605  1.10      fvdl int
    606  1.10      fvdl linux_fchown(p, uap, retval)
    607  1.10      fvdl 	struct proc *p;
    608  1.10      fvdl 	struct linux_fchown_args /* {
    609  1.10      fvdl 		syscallarg(int) fd;
    610  1.10      fvdl 		syscallarg(int) uid;
    611  1.10      fvdl 		syscallarg(int) gid;
    612  1.10      fvdl 	} */ *uap;
    613  1.10      fvdl 	register_t *retval;
    614  1.10      fvdl {
    615  1.10      fvdl 	struct fchown_args bfa;
    616  1.10      fvdl 
    617  1.10      fvdl 	SCARG(&bfa, fd) = SCARG(uap, fd);
    618  1.10      fvdl 	SCARG(&bfa, 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(&bfa, 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.10      fvdl 	return fchown(p, &bfa, retval);
    624   1.2      fvdl }
    625   1.2      fvdl 
    626   1.2      fvdl int
    627   1.2      fvdl linux_rename(p, uap, retval)
    628   1.2      fvdl 	struct proc *p;
    629   1.2      fvdl 	struct linux_rename_args /* {
    630   1.2      fvdl 		syscallarg(char *) from;
    631   1.2      fvdl 		syscallarg(char *) to;
    632   1.2      fvdl 	} */ *uap;
    633   1.2      fvdl 	register_t *retval;
    634   1.2      fvdl {
    635   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    636   1.2      fvdl 
    637   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, from));
    638   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
    639   1.2      fvdl 
    640   1.2      fvdl 	return rename(p, uap, retval);
    641   1.2      fvdl }
    642   1.2      fvdl 
    643   1.2      fvdl int
    644   1.2      fvdl linux_mkdir(p, uap, retval)
    645   1.2      fvdl 	struct proc *p;
    646   1.2      fvdl 	struct linux_mkdir_args /* {
    647   1.2      fvdl 		syscallarg(char *) path;
    648   1.7      fvdl 		syscallarg(int) mode;
    649   1.2      fvdl 	} */ *uap;
    650   1.2      fvdl 	register_t *retval;
    651   1.2      fvdl {
    652   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    653   1.2      fvdl 
    654   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
    655   1.2      fvdl 	return mkdir(p, uap, retval);
    656   1.2      fvdl }
    657   1.2      fvdl 
    658   1.2      fvdl int
    659   1.2      fvdl linux_rmdir(p, uap, retval)
    660   1.2      fvdl 	struct proc *p;
    661   1.2      fvdl 	struct linux_rmdir_args /* {
    662   1.2      fvdl 		syscallarg(char *) path;
    663   1.2      fvdl 	} */ *uap;
    664   1.2      fvdl 	register_t *retval;
    665   1.2      fvdl {
    666   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    667   1.2      fvdl 
    668   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    669   1.2      fvdl 	return rmdir(p, uap, retval);
    670   1.2      fvdl }
    671   1.2      fvdl 
    672   1.2      fvdl int
    673   1.2      fvdl linux_symlink(p, uap, retval)
    674   1.2      fvdl 	struct proc *p;
    675   1.2      fvdl 	struct linux_symlink_args /* {
    676   1.2      fvdl 		syscallarg(char *) path;
    677   1.2      fvdl 		syscallarg(char *) to;
    678   1.2      fvdl 	} */ *uap;
    679   1.2      fvdl 	register_t *retval;
    680   1.2      fvdl {
    681   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    682   1.2      fvdl 
    683   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    684   1.5  christos 	LINUX_CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
    685   1.2      fvdl 
    686   1.2      fvdl 	return symlink(p, uap, retval);
    687   1.2      fvdl }
    688   1.2      fvdl 
    689   1.2      fvdl int
    690   1.2      fvdl linux_readlink(p, uap, retval)
    691   1.2      fvdl 	struct proc *p;
    692   1.2      fvdl 	struct linux_readlink_args /* {
    693   1.2      fvdl 		syscallarg(char *) name;
    694   1.2      fvdl 		syscallarg(char *) buf;
    695   1.2      fvdl 		syscallarg(int) count;
    696   1.2      fvdl 	} */ *uap;
    697   1.2      fvdl {
    698   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    699   1.2      fvdl 
    700   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, name));
    701   1.2      fvdl 	return readlink(p, uap, retval);
    702   1.2      fvdl }
    703   1.2      fvdl 
    704   1.2      fvdl int
    705   1.2      fvdl linux_truncate(p, uap, retval)
    706   1.2      fvdl 	struct proc *p;
    707   1.2      fvdl 	struct linux_truncate_args /* {
    708   1.2      fvdl 		syscallarg(char *) path;
    709   1.2      fvdl 		syscallarg(long) length;
    710   1.2      fvdl 	} */ *uap;
    711   1.2      fvdl {
    712   1.5  christos 	caddr_t sg = stackgap_init(p->p_emul);
    713   1.2      fvdl 
    714   1.5  christos 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    715   1.2      fvdl 	return compat_43_truncate(p, uap, retval);
    716   1.1      fvdl }
    717