Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_fs.c revision 1.43
      1  1.43       dsl /*	$NetBSD: netbsd32_fs.c,v 1.43 2007/05/12 17:28:19 dsl Exp $	*/
      2   1.1       mrg 
      3   1.1       mrg /*
      4   1.1       mrg  * Copyright (c) 1998, 2001 Matthew R. Green
      5   1.1       mrg  * All rights reserved.
      6   1.1       mrg  *
      7   1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8   1.1       mrg  * modification, are permitted provided that the following conditions
      9   1.1       mrg  * are met:
     10   1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11   1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12   1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15   1.1       mrg  * 3. The name of the author may not be used to endorse or promote products
     16   1.1       mrg  *    derived from this software without specific prior written permission.
     17   1.1       mrg  *
     18   1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23   1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24   1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     25   1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     26   1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1       mrg  * SUCH DAMAGE.
     29   1.1       mrg  */
     30   1.7     lukem 
     31   1.7     lukem #include <sys/cdefs.h>
     32  1.43       dsl __KERNEL_RCSID(0, "$NetBSD: netbsd32_fs.c,v 1.43 2007/05/12 17:28:19 dsl Exp $");
     33   1.1       mrg 
     34   1.5       mrg #if defined(_KERNEL_OPT)
     35   1.1       mrg #include "opt_ktrace.h"
     36   1.1       mrg #endif
     37   1.1       mrg 
     38   1.1       mrg #include <sys/param.h>
     39   1.1       mrg #include <sys/systm.h>
     40   1.1       mrg #include <sys/malloc.h>
     41   1.1       mrg #include <sys/mount.h>
     42   1.1       mrg #include <sys/socket.h>
     43   1.1       mrg #include <sys/socketvar.h>
     44   1.1       mrg #include <sys/stat.h>
     45   1.1       mrg #include <sys/time.h>
     46   1.1       mrg #include <sys/ktrace.h>
     47   1.1       mrg #include <sys/resourcevar.h>
     48   1.1       mrg #include <sys/vnode.h>
     49   1.1       mrg #include <sys/file.h>
     50   1.1       mrg #include <sys/filedesc.h>
     51   1.1       mrg #include <sys/namei.h>
     52  1.18      cube #include <sys/statvfs.h>
     53   1.1       mrg #include <sys/syscallargs.h>
     54   1.1       mrg #include <sys/proc.h>
     55  1.22  christos #include <sys/dirent.h>
     56  1.27      elad #include <sys/kauth.h>
     57  1.37       dsl #include <sys/vfs_syscalls.h>
     58   1.1       mrg 
     59   1.1       mrg #include <compat/netbsd32/netbsd32.h>
     60   1.1       mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
     61   1.1       mrg #include <compat/netbsd32/netbsd32_conv.h>
     62  1.28    martin #include <compat/sys/mount.h>
     63   1.1       mrg 
     64   1.1       mrg 
     65  1.23  christos static int dofilereadv32 __P((struct lwp *, int, struct file *, struct netbsd32_iovec *,
     66   1.1       mrg 			      int, off_t *, int, register_t *));
     67  1.23  christos static int dofilewritev32 __P((struct lwp *, int, struct file *, struct netbsd32_iovec *,
     68   1.1       mrg 			       int,  off_t *, int, register_t *));
     69   1.1       mrg 
     70   1.1       mrg int
     71  1.11   thorpej netbsd32_readv(l, v, retval)
     72  1.11   thorpej 	struct lwp *l;
     73   1.1       mrg 	void *v;
     74   1.1       mrg 	register_t *retval;
     75   1.1       mrg {
     76   1.1       mrg 	struct netbsd32_readv_args /* {
     77   1.1       mrg 		syscallarg(int) fd;
     78   1.1       mrg 		syscallarg(const netbsd32_iovecp_t) iovp;
     79   1.1       mrg 		syscallarg(int) iovcnt;
     80   1.1       mrg 	} */ *uap = v;
     81   1.1       mrg 	int fd = SCARG(uap, fd);
     82  1.11   thorpej 	struct proc *p = l->l_proc;
     83   1.1       mrg 	struct file *fp;
     84   1.1       mrg 	struct filedesc *fdp = p->p_fd;
     85   1.1       mrg 
     86   1.6   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
     87   1.6   thorpej 		return (EBADF);
     88   1.6   thorpej 
     89   1.6   thorpej 	if ((fp->f_flag & FREAD) == 0)
     90   1.1       mrg 		return (EBADF);
     91   1.1       mrg 
     92   1.9  jdolecek 	FILE_USE(fp);
     93   1.9  jdolecek 
     94  1.23  christos 	return (dofilereadv32(l, fd, fp,
     95  1.39       dsl 	    (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
     96  1.10       scw 	    SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
     97   1.1       mrg }
     98   1.1       mrg 
     99   1.1       mrg /* Damn thing copies in the iovec! */
    100   1.1       mrg int
    101  1.23  christos dofilereadv32(l, fd, fp, iovp, iovcnt, offset, flags, retval)
    102  1.23  christos 	struct lwp *l;
    103   1.1       mrg 	int fd;
    104   1.1       mrg 	struct file *fp;
    105   1.1       mrg 	struct netbsd32_iovec *iovp;
    106   1.1       mrg 	int iovcnt;
    107   1.1       mrg 	off_t *offset;
    108   1.1       mrg 	int flags;
    109   1.1       mrg 	register_t *retval;
    110   1.1       mrg {
    111   1.1       mrg 	struct uio auio;
    112   1.1       mrg 	struct iovec *iov;
    113   1.1       mrg 	struct iovec *needfree;
    114   1.1       mrg 	struct iovec aiov[UIO_SMALLIOV];
    115   1.1       mrg 	long i, cnt, error = 0;
    116   1.1       mrg 	u_int iovlen;
    117   1.1       mrg #ifdef KTRACE
    118   1.1       mrg 	struct iovec *ktriov = NULL;
    119   1.1       mrg #endif
    120   1.1       mrg 
    121   1.1       mrg 	/* note: can't use iovlen until iovcnt is validated */
    122   1.1       mrg 	iovlen = iovcnt * sizeof(struct iovec);
    123   1.1       mrg 	if ((u_int)iovcnt > UIO_SMALLIOV) {
    124   1.9  jdolecek 		if ((u_int)iovcnt > IOV_MAX) {
    125   1.9  jdolecek 			error = EINVAL;
    126   1.9  jdolecek 			goto out;
    127   1.9  jdolecek 		}
    128   1.9  jdolecek 		iov = malloc(iovlen, M_IOV, M_WAITOK);
    129   1.1       mrg 		needfree = iov;
    130   1.1       mrg 	} else if ((u_int)iovcnt > 0) {
    131   1.1       mrg 		iov = aiov;
    132   1.1       mrg 		needfree = NULL;
    133   1.9  jdolecek 	} else {
    134   1.9  jdolecek 		error = EINVAL;
    135   1.9  jdolecek 		goto out;
    136   1.9  jdolecek 	}
    137   1.1       mrg 
    138   1.1       mrg 	auio.uio_iov = iov;
    139   1.1       mrg 	auio.uio_iovcnt = iovcnt;
    140   1.1       mrg 	auio.uio_rw = UIO_READ;
    141  1.24      yamt 	auio.uio_vmspace = l->l_proc->p_vmspace;
    142   1.1       mrg 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
    143   1.1       mrg 	if (error)
    144   1.1       mrg 		goto done;
    145   1.1       mrg 	auio.uio_resid = 0;
    146   1.1       mrg 	for (i = 0; i < iovcnt; i++) {
    147   1.1       mrg 		auio.uio_resid += iov->iov_len;
    148   1.1       mrg 		/*
    149   1.1       mrg 		 * Reads return ssize_t because -1 is returned on error.
    150   1.1       mrg 		 * Therefore we must restrict the length to SSIZE_MAX to
    151   1.1       mrg 		 * avoid garbage return values.
    152   1.1       mrg 		 */
    153   1.1       mrg 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    154   1.1       mrg 			error = EINVAL;
    155   1.1       mrg 			goto done;
    156   1.1       mrg 		}
    157   1.1       mrg 		iov++;
    158   1.1       mrg 	}
    159   1.1       mrg #ifdef KTRACE
    160   1.1       mrg 	/*
    161   1.1       mrg 	 * if tracing, save a copy of iovec
    162   1.1       mrg 	 */
    163  1.23  christos 	if (KTRPOINT(l->l_proc, KTR_GENIO))  {
    164   1.9  jdolecek 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    165  1.36  christos 		memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
    166   1.1       mrg 	}
    167   1.1       mrg #endif
    168   1.1       mrg 	cnt = auio.uio_resid;
    169   1.1       mrg 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
    170   1.1       mrg 	if (error)
    171   1.1       mrg 		if (auio.uio_resid != cnt && (error == ERESTART ||
    172   1.1       mrg 		    error == EINTR || error == EWOULDBLOCK))
    173   1.1       mrg 			error = 0;
    174   1.1       mrg 	cnt -= auio.uio_resid;
    175   1.1       mrg #ifdef KTRACE
    176  1.23  christos 	if (KTRPOINT(l->l_proc, KTR_GENIO))
    177   1.1       mrg 		if (error == 0) {
    178  1.23  christos 			ktrgenio(l, fd, UIO_READ, ktriov, cnt,
    179   1.1       mrg 			    error);
    180   1.9  jdolecek 		free(ktriov, M_TEMP);
    181   1.1       mrg 	}
    182   1.1       mrg #endif
    183   1.1       mrg 	*retval = cnt;
    184   1.1       mrg done:
    185   1.1       mrg 	if (needfree)
    186   1.9  jdolecek 		free(needfree, M_IOV);
    187   1.9  jdolecek out:
    188  1.23  christos 	FILE_UNUSE(fp, l);
    189   1.1       mrg 	return (error);
    190   1.1       mrg }
    191   1.1       mrg 
    192   1.1       mrg int
    193  1.11   thorpej netbsd32_writev(l, v, retval)
    194  1.11   thorpej 	struct lwp *l;
    195   1.1       mrg 	void *v;
    196   1.1       mrg 	register_t *retval;
    197   1.1       mrg {
    198   1.1       mrg 	struct netbsd32_writev_args /* {
    199   1.1       mrg 		syscallarg(int) fd;
    200   1.1       mrg 		syscallarg(const netbsd32_iovecp_t) iovp;
    201   1.1       mrg 		syscallarg(int) iovcnt;
    202   1.1       mrg 	} */ *uap = v;
    203   1.1       mrg 	int fd = SCARG(uap, fd);
    204   1.1       mrg 	struct file *fp;
    205  1.11   thorpej 	struct proc *p = l->l_proc;
    206   1.1       mrg 	struct filedesc *fdp = p->p_fd;
    207   1.1       mrg 
    208   1.6   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    209   1.6   thorpej 		return (EBADF);
    210   1.6   thorpej 
    211   1.6   thorpej 	if ((fp->f_flag & FWRITE) == 0)
    212   1.1       mrg 		return (EBADF);
    213   1.1       mrg 
    214   1.9  jdolecek 	FILE_USE(fp);
    215   1.9  jdolecek 
    216  1.23  christos 	return (dofilewritev32(l, fd, fp,
    217  1.39       dsl 	    (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
    218  1.10       scw 	    SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    219   1.1       mrg }
    220   1.1       mrg 
    221   1.1       mrg int
    222  1.23  christos dofilewritev32(l, fd, fp, iovp, iovcnt, offset, flags, retval)
    223  1.23  christos 	struct lwp *l;
    224   1.1       mrg 	int fd;
    225   1.1       mrg 	struct file *fp;
    226   1.1       mrg 	struct netbsd32_iovec *iovp;
    227   1.1       mrg 	int iovcnt;
    228   1.1       mrg 	off_t *offset;
    229   1.1       mrg 	int flags;
    230   1.1       mrg 	register_t *retval;
    231   1.1       mrg {
    232   1.1       mrg 	struct uio auio;
    233   1.1       mrg 	struct iovec *iov;
    234   1.1       mrg 	struct iovec *needfree;
    235   1.1       mrg 	struct iovec aiov[UIO_SMALLIOV];
    236  1.23  christos 	struct proc *p = l->l_proc;
    237   1.1       mrg 	long i, cnt, error = 0;
    238   1.1       mrg 	u_int iovlen;
    239   1.1       mrg #ifdef KTRACE
    240   1.1       mrg 	struct iovec *ktriov = NULL;
    241   1.1       mrg #endif
    242   1.1       mrg 
    243   1.1       mrg 	/* note: can't use iovlen until iovcnt is validated */
    244   1.1       mrg 	iovlen = iovcnt * sizeof(struct iovec);
    245   1.1       mrg 	if ((u_int)iovcnt > UIO_SMALLIOV) {
    246   1.9  jdolecek 		if ((u_int)iovcnt > IOV_MAX) {
    247   1.9  jdolecek 			error = EINVAL;
    248   1.9  jdolecek 			goto out;
    249   1.9  jdolecek 		}
    250   1.9  jdolecek 		iov = malloc(iovlen, M_IOV, M_WAITOK);
    251   1.1       mrg 		needfree = iov;
    252   1.1       mrg 	} else if ((u_int)iovcnt > 0) {
    253   1.1       mrg 		iov = aiov;
    254   1.1       mrg 		needfree = NULL;
    255   1.9  jdolecek 	} else {
    256   1.9  jdolecek 		error = EINVAL;
    257   1.9  jdolecek 		goto out;
    258   1.9  jdolecek 	}
    259   1.1       mrg 
    260   1.1       mrg 	auio.uio_iov = iov;
    261   1.1       mrg 	auio.uio_iovcnt = iovcnt;
    262   1.1       mrg 	auio.uio_rw = UIO_WRITE;
    263  1.24      yamt 	auio.uio_vmspace = l->l_proc->p_vmspace;
    264   1.1       mrg 	error = netbsd32_to_iovecin(iovp, iov, iovcnt);
    265   1.1       mrg 	if (error)
    266   1.1       mrg 		goto done;
    267   1.1       mrg 	auio.uio_resid = 0;
    268   1.1       mrg 	for (i = 0; i < iovcnt; i++) {
    269   1.1       mrg 		auio.uio_resid += iov->iov_len;
    270   1.1       mrg 		/*
    271   1.1       mrg 		 * Writes return ssize_t because -1 is returned on error.
    272   1.1       mrg 		 * Therefore we must restrict the length to SSIZE_MAX to
    273   1.1       mrg 		 * avoid garbage return values.
    274   1.1       mrg 		 */
    275   1.1       mrg 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    276   1.1       mrg 			error = EINVAL;
    277   1.1       mrg 			goto done;
    278   1.1       mrg 		}
    279   1.1       mrg 		iov++;
    280   1.1       mrg 	}
    281   1.1       mrg #ifdef KTRACE
    282   1.1       mrg 	/*
    283   1.1       mrg 	 * if tracing, save a copy of iovec
    284   1.1       mrg 	 */
    285  1.14      fvdl 	if (KTRPOINT(p, KTR_GENIO))  {
    286   1.9  jdolecek 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    287  1.36  christos 		memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
    288   1.1       mrg 	}
    289   1.1       mrg #endif
    290   1.1       mrg 	cnt = auio.uio_resid;
    291   1.1       mrg 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
    292   1.1       mrg 	if (error) {
    293   1.1       mrg 		if (auio.uio_resid != cnt && (error == ERESTART ||
    294   1.1       mrg 		    error == EINTR || error == EWOULDBLOCK))
    295   1.1       mrg 			error = 0;
    296   1.1       mrg 		if (error == EPIPE)
    297  1.14      fvdl 			psignal(p, SIGPIPE);
    298   1.1       mrg 	}
    299   1.1       mrg 	cnt -= auio.uio_resid;
    300   1.1       mrg #ifdef KTRACE
    301  1.14      fvdl 	if (KTRPOINT(p, KTR_GENIO))
    302   1.1       mrg 		if (error == 0) {
    303  1.23  christos 			ktrgenio(l, fd, UIO_WRITE, ktriov, cnt,
    304   1.1       mrg 			    error);
    305   1.9  jdolecek 		free(ktriov, M_TEMP);
    306   1.1       mrg 	}
    307   1.1       mrg #endif
    308   1.1       mrg 	*retval = cnt;
    309   1.1       mrg done:
    310   1.1       mrg 	if (needfree)
    311   1.9  jdolecek 		free(needfree, M_IOV);
    312   1.9  jdolecek out:
    313  1.23  christos 	FILE_UNUSE(fp, l);
    314   1.1       mrg 	return (error);
    315   1.1       mrg }
    316   1.1       mrg 
    317  1.43       dsl /*
    318  1.43       dsl  * Common routine to set access and modification times given a vnode.
    319  1.43       dsl  */
    320  1.43       dsl static int
    321  1.43       dsl get_utimes32(const netbsd32_timevalp_t *tptr, struct timeval *tv,
    322  1.43       dsl     struct timeval **tvp)
    323  1.43       dsl {
    324  1.43       dsl 	int error;
    325  1.43       dsl 	struct netbsd32_timeval tv32[2];
    326  1.43       dsl 
    327  1.43       dsl 	if (tptr == NULL) {
    328  1.43       dsl 		*tvp = NULL;
    329  1.43       dsl 		return 0;
    330  1.43       dsl 	}
    331  1.43       dsl 
    332  1.43       dsl 	error = copyin(tptr, tv32, sizeof(tv32));
    333  1.43       dsl 	if (error)
    334  1.43       dsl 		return error;
    335  1.43       dsl 	netbsd32_to_timeval(&tv32[0], &tv[0]);
    336  1.43       dsl 	netbsd32_to_timeval(&tv32[1], &tv[1]);
    337  1.43       dsl 
    338  1.43       dsl 	*tvp = tv;
    339  1.43       dsl 	return 0;
    340  1.43       dsl }
    341  1.43       dsl 
    342   1.1       mrg int
    343  1.11   thorpej netbsd32_utimes(l, v, retval)
    344  1.11   thorpej 	struct lwp *l;
    345   1.1       mrg 	void *v;
    346   1.1       mrg 	register_t *retval;
    347   1.1       mrg {
    348   1.1       mrg 	struct netbsd32_utimes_args /* {
    349   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    350   1.1       mrg 		syscallarg(const netbsd32_timevalp_t) tptr;
    351   1.1       mrg 	} */ *uap = v;
    352   1.1       mrg 	int error;
    353  1.43       dsl 	struct timeval tv[2], *tvp;
    354   1.1       mrg 
    355  1.43       dsl 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    356  1.43       dsl 	if (error != 0)
    357  1.43       dsl 		return error;
    358   1.1       mrg 
    359  1.43       dsl 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
    360  1.43       dsl 			    tvp, UIO_SYSSPACE);
    361   1.1       mrg }
    362   1.1       mrg 
    363  1.42       dsl static int
    364  1.42       dsl netbds32_copyout_statvfs(const void *kp, void *up, size_t len)
    365  1.42       dsl {
    366  1.42       dsl 	struct netbsd32_statvfs *sbuf_32;
    367  1.42       dsl 	int error;
    368  1.42       dsl 
    369  1.42       dsl 	sbuf_32 = malloc(sizeof *sbuf_32, M_TEMP, M_WAITOK);
    370  1.42       dsl 	netbsd32_from_statvfs(kp, sbuf_32);
    371  1.42       dsl 	error = copyout(sbuf_32, up, sizeof(*sbuf_32));
    372  1.42       dsl 	free(sbuf_32, M_TEMP);
    373  1.42       dsl 
    374  1.42       dsl 	return error;
    375  1.42       dsl }
    376  1.42       dsl 
    377   1.1       mrg int
    378  1.18      cube netbsd32_statvfs1(l, v, retval)
    379  1.11   thorpej 	struct lwp *l;
    380   1.1       mrg 	void *v;
    381   1.1       mrg 	register_t *retval;
    382   1.1       mrg {
    383  1.18      cube 	struct netbsd32_statvfs1_args /* {
    384   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    385  1.18      cube 		syscallarg(netbsd32_statvfsp_t) buf;
    386  1.18      cube 		syscallarg(int) flags;
    387   1.1       mrg 	} */ *uap = v;
    388  1.42       dsl 	struct statvfs *sb;
    389   1.1       mrg 	int error;
    390   1.1       mrg 
    391  1.42       dsl 	sb = STATVFSBUF_GET();
    392  1.42       dsl 	error = do_sys_pstatvfs(l, SCARG_P32(uap, path), SCARG(uap, flags), sb);
    393  1.42       dsl 	if (error == 0)
    394  1.42       dsl 		error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
    395  1.42       dsl 	STATVFSBUF_PUT(sb);
    396  1.42       dsl 	return error;
    397   1.1       mrg }
    398   1.1       mrg 
    399   1.1       mrg int
    400  1.18      cube netbsd32_fstatvfs1(l, v, retval)
    401  1.11   thorpej 	struct lwp *l;
    402   1.1       mrg 	void *v;
    403   1.1       mrg 	register_t *retval;
    404   1.1       mrg {
    405  1.18      cube 	struct netbsd32_fstatvfs1_args /* {
    406   1.1       mrg 		syscallarg(int) fd;
    407  1.18      cube 		syscallarg(netbsd32_statvfsp_t) buf;
    408  1.18      cube 		syscallarg(int) flags;
    409   1.1       mrg 	} */ *uap = v;
    410  1.42       dsl 	struct statvfs *sb;
    411   1.1       mrg 	int error;
    412   1.1       mrg 
    413  1.42       dsl 	sb = STATVFSBUF_GET();
    414  1.42       dsl 	error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
    415  1.42       dsl 	if (error == 0)
    416  1.42       dsl 		error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
    417  1.42       dsl 	STATVFSBUF_PUT(sb);
    418  1.18      cube 	return error;
    419  1.18      cube }
    420  1.18      cube 
    421  1.18      cube int
    422  1.18      cube netbsd32_getvfsstat(l, v, retval)
    423  1.18      cube 	struct lwp *l;
    424  1.18      cube 	void *v;
    425  1.18      cube 	register_t *retval;
    426  1.18      cube {
    427  1.18      cube 	struct netbsd32_getvfsstat_args /* {
    428  1.18      cube 		syscallarg(netbsd32_statvfsp_t) buf;
    429  1.18      cube 		syscallarg(netbsd32_size_t) bufsize;
    430  1.18      cube 		syscallarg(int) flags;
    431  1.18      cube 	} */ *uap = v;
    432  1.18      cube 
    433  1.42       dsl 	return do_sys_getvfsstat(l, SCARG_P32(uap, buf), SCARG(uap, bufsize),
    434  1.42       dsl 	    SCARG(uap, flags), netbds32_copyout_statvfs,
    435  1.42       dsl 	    sizeof (struct netbsd32_statvfs), retval);
    436  1.18      cube }
    437  1.18      cube 
    438  1.18      cube int
    439  1.32    martin netbsd32___fhstatvfs140(l, v, retval)
    440  1.18      cube 	struct lwp *l;
    441  1.18      cube 	void *v;
    442  1.18      cube 	register_t *retval;
    443  1.18      cube {
    444  1.32    martin 	struct netbsd32___fhstatvfs140_args /* {
    445  1.32    martin 		syscallarg(const netbsd32_pointer_t) fhp;
    446  1.32    martin 		syscallarg(netbsd32_size_t) fh_size;
    447  1.18      cube 		syscallarg(netbsd32_statvfsp_t) buf;
    448  1.18      cube 		syscallarg(int) flags;
    449  1.18      cube 	} */ *uap = v;
    450  1.42       dsl 	struct statvfs *sb;
    451  1.18      cube 	int error;
    452  1.18      cube 
    453  1.42       dsl 	sb = STATVFSBUF_GET();
    454  1.42       dsl 	error = do_fhstatvfs(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), sb,
    455  1.42       dsl 	    SCARG(uap, flags));
    456  1.42       dsl 
    457  1.42       dsl 	if (error == 0)
    458  1.42       dsl 		error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
    459  1.42       dsl 	STATVFSBUF_PUT(sb);
    460  1.18      cube 
    461  1.42       dsl 	return error;
    462   1.1       mrg }
    463   1.1       mrg 
    464   1.1       mrg int
    465  1.11   thorpej netbsd32_futimes(l, v, retval)
    466  1.11   thorpej 	struct lwp *l;
    467   1.1       mrg 	void *v;
    468   1.1       mrg 	register_t *retval;
    469   1.1       mrg {
    470   1.1       mrg 	struct netbsd32_futimes_args /* {
    471   1.1       mrg 		syscallarg(int) fd;
    472   1.1       mrg 		syscallarg(const netbsd32_timevalp_t) tptr;
    473   1.1       mrg 	} */ *uap = v;
    474   1.1       mrg 	int error;
    475   1.1       mrg 	struct file *fp;
    476  1.43       dsl 	struct timeval tv[2], *tvp;
    477  1.43       dsl 
    478  1.43       dsl 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    479  1.43       dsl 	if (error != 0)
    480  1.43       dsl 		return error;
    481   1.1       mrg 
    482   1.1       mrg 	/* getvnode() will use the descriptor for us */
    483  1.43       dsl 	if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
    484   1.1       mrg 		return (error);
    485   1.1       mrg 
    486  1.43       dsl 	error = do_sys_utimes(l, fp->f_data, NULL, 0, tvp, UIO_SYSSPACE);
    487  1.43       dsl 
    488  1.23  christos 	FILE_UNUSE(fp, l);
    489   1.1       mrg 	return (error);
    490   1.1       mrg }
    491   1.1       mrg 
    492   1.1       mrg int
    493  1.22  christos netbsd32_sys___getdents30(l, v, retval)
    494  1.11   thorpej 	struct lwp *l;
    495   1.1       mrg 	void *v;
    496   1.1       mrg 	register_t *retval;
    497   1.1       mrg {
    498  1.22  christos 	struct netbsd32_sys___getdents30_args /* {
    499   1.1       mrg 		syscallarg(int) fd;
    500   1.1       mrg 		syscallarg(netbsd32_charp) buf;
    501   1.1       mrg 		syscallarg(netbsd32_size_t) count;
    502   1.1       mrg 	} */ *uap = v;
    503   1.1       mrg 	struct file *fp;
    504   1.1       mrg 	int error, done;
    505  1.11   thorpej 	struct proc *p = l->l_proc;
    506   1.1       mrg 
    507   1.1       mrg 	/* getvnode() will use the descriptor for us */
    508   1.1       mrg 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    509   1.1       mrg 		return (error);
    510   1.1       mrg 	if ((fp->f_flag & FREAD) == 0) {
    511   1.1       mrg 		error = EBADF;
    512   1.1       mrg 		goto out;
    513   1.1       mrg 	}
    514  1.39       dsl 	error = vn_readdir(fp, SCARG_P32(uap, buf),
    515  1.23  christos 	    UIO_USERSPACE, SCARG(uap, count), &done, l, 0, 0);
    516   1.1       mrg 	*retval = done;
    517   1.1       mrg  out:
    518  1.23  christos 	FILE_UNUSE(fp, l);
    519   1.1       mrg 	return (error);
    520   1.1       mrg }
    521   1.1       mrg 
    522   1.1       mrg int
    523  1.11   thorpej netbsd32_lutimes(l, v, retval)
    524  1.11   thorpej 	struct lwp *l;
    525   1.1       mrg 	void *v;
    526   1.1       mrg 	register_t *retval;
    527   1.1       mrg {
    528   1.1       mrg 	struct netbsd32_lutimes_args /* {
    529   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    530   1.1       mrg 		syscallarg(const netbsd32_timevalp_t) tptr;
    531   1.1       mrg 	} */ *uap = v;
    532   1.1       mrg 	int error;
    533  1.43       dsl 	struct timeval tv[2], *tvp;
    534   1.1       mrg 
    535  1.43       dsl 	error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
    536  1.43       dsl 	if (error != 0)
    537  1.43       dsl 		return error;
    538   1.1       mrg 
    539  1.43       dsl 	return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
    540  1.43       dsl 			    tvp, UIO_SYSSPACE);
    541   1.1       mrg }
    542   1.1       mrg 
    543   1.1       mrg int
    544  1.22  christos netbsd32_sys___stat30(l, v, retval)
    545  1.11   thorpej 	struct lwp *l;
    546   1.1       mrg 	void *v;
    547   1.1       mrg 	register_t *retval;
    548   1.1       mrg {
    549  1.22  christos 	struct netbsd32_sys___stat30_args /* {
    550   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    551   1.1       mrg 		syscallarg(netbsd32_statp_t) ub;
    552   1.1       mrg 	} */ *uap = v;
    553   1.1       mrg 	struct netbsd32_stat sb32;
    554   1.1       mrg 	struct stat sb;
    555   1.1       mrg 	int error;
    556   1.1       mrg 	const char *path;
    557   1.1       mrg 
    558  1.39       dsl 	path = SCARG_P32(uap, path);
    559   1.1       mrg 
    560  1.37       dsl 	error = do_sys_stat(l, path, FOLLOW, &sb);
    561  1.41       dsl 	if (error)
    562  1.41       dsl 		return (error);
    563  1.22  christos 	netbsd32_from___stat30(&sb, &sb32);
    564  1.39       dsl 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    565   1.1       mrg 	return (error);
    566   1.1       mrg }
    567   1.1       mrg 
    568   1.1       mrg int
    569  1.22  christos netbsd32_sys___fstat30(l, v, retval)
    570  1.11   thorpej 	struct lwp *l;
    571   1.1       mrg 	void *v;
    572   1.1       mrg 	register_t *retval;
    573   1.1       mrg {
    574  1.22  christos 	struct netbsd32_sys___fstat30_args /* {
    575   1.1       mrg 		syscallarg(int) fd;
    576   1.1       mrg 		syscallarg(netbsd32_statp_t) sb;
    577   1.1       mrg 	} */ *uap = v;
    578   1.1       mrg 	int fd = SCARG(uap, fd);
    579  1.14      fvdl 	struct proc *p = l->l_proc;
    580  1.14      fvdl 	struct filedesc *fdp = p->p_fd;
    581   1.1       mrg 	struct file *fp;
    582   1.1       mrg 	struct netbsd32_stat sb32;
    583   1.1       mrg 	struct stat ub;
    584   1.1       mrg 	int error = 0;
    585   1.1       mrg 
    586   1.6   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    587   1.1       mrg 		return (EBADF);
    588   1.1       mrg 
    589   1.3  jdolecek 	FILE_USE(fp);
    590  1.23  christos 	error = (*fp->f_ops->fo_stat)(fp, &ub, l);
    591  1.23  christos 	FILE_UNUSE(fp, l);
    592   1.3  jdolecek 
    593   1.1       mrg 	if (error == 0) {
    594  1.22  christos 		netbsd32_from___stat30(&ub, &sb32);
    595  1.39       dsl 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
    596   1.1       mrg 	}
    597   1.1       mrg 	return (error);
    598   1.1       mrg }
    599   1.1       mrg 
    600   1.1       mrg int
    601  1.22  christos netbsd32_sys___lstat30(l, v, retval)
    602  1.11   thorpej 	struct lwp *l;
    603   1.1       mrg 	void *v;
    604   1.1       mrg 	register_t *retval;
    605   1.1       mrg {
    606  1.22  christos 	struct netbsd32_sys___lstat30_args /* {
    607   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    608   1.1       mrg 		syscallarg(netbsd32_statp_t) ub;
    609   1.1       mrg 	} */ *uap = v;
    610   1.1       mrg 	struct netbsd32_stat sb32;
    611   1.1       mrg 	struct stat sb;
    612   1.1       mrg 	int error;
    613   1.1       mrg 	const char *path;
    614   1.1       mrg 
    615  1.39       dsl 	path = SCARG_P32(uap, path);
    616   1.1       mrg 
    617  1.37       dsl 	error = do_sys_stat(l, path, NOFOLLOW, &sb);
    618   1.1       mrg 	if (error)
    619   1.1       mrg 		return (error);
    620  1.22  christos 	netbsd32_from___stat30(&sb, &sb32);
    621  1.39       dsl 	error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
    622   1.1       mrg 	return (error);
    623   1.1       mrg }
    624   1.1       mrg 
    625  1.32    martin int netbsd32___fhstat40(l, v, retval)
    626  1.26      cube 	struct lwp *l;
    627  1.26      cube 	void *v;
    628  1.26      cube 	register_t *retval;
    629  1.26      cube {
    630  1.32    martin 	struct netbsd32___fhstat40_args /* {
    631  1.32    martin 		syscallarg(const netbsd32_pointer_t) fhp;
    632  1.32    martin 		syscallarg(netbsd32_size_t) fh_size;
    633  1.26      cube 		syscallarg(netbsd32_statp_t) sb;
    634  1.26      cube 	} */ *uap = v;
    635  1.26      cube 	struct stat sb;
    636  1.26      cube 	struct netbsd32_stat sb32;
    637  1.26      cube 	int error;
    638  1.26      cube 
    639  1.42       dsl 	error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
    640  1.42       dsl 	if (error != 0) {
    641  1.42       dsl 		netbsd32_from___stat30(&sb, &sb32);
    642  1.42       dsl 		error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
    643  1.42       dsl 	}
    644  1.29    martin 	return error;
    645  1.26      cube }
    646  1.26      cube 
    647   1.1       mrg int
    648  1.11   thorpej netbsd32_preadv(l, v, retval)
    649  1.11   thorpej 	struct lwp *l;
    650   1.1       mrg 	void *v;
    651   1.1       mrg 	register_t *retval;
    652   1.1       mrg {
    653   1.1       mrg 	struct netbsd32_preadv_args /* {
    654   1.1       mrg 		syscallarg(int) fd;
    655   1.1       mrg 		syscallarg(const netbsd32_iovecp_t) iovp;
    656   1.1       mrg 		syscallarg(int) iovcnt;
    657   1.1       mrg 		syscallarg(int) pad;
    658   1.1       mrg 		syscallarg(off_t) offset;
    659   1.1       mrg 	} */ *uap = v;
    660  1.14      fvdl 	struct proc *p = l->l_proc;
    661  1.14      fvdl 	struct filedesc *fdp = p->p_fd;
    662   1.1       mrg 	struct file *fp;
    663   1.1       mrg 	struct vnode *vp;
    664   1.1       mrg 	off_t offset;
    665   1.1       mrg 	int error, fd = SCARG(uap, fd);
    666   1.1       mrg 
    667   1.6   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    668   1.6   thorpej 		return (EBADF);
    669   1.6   thorpej 
    670   1.6   thorpej 	if ((fp->f_flag & FREAD) == 0)
    671   1.1       mrg 		return (EBADF);
    672   1.1       mrg 
    673   1.9  jdolecek 	FILE_USE(fp);
    674   1.9  jdolecek 
    675   1.1       mrg 	vp = (struct vnode *)fp->f_data;
    676   1.9  jdolecek 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    677   1.9  jdolecek 		error = ESPIPE;
    678   1.9  jdolecek 		goto out;
    679   1.9  jdolecek 	}
    680   1.1       mrg 
    681   1.1       mrg 	offset = SCARG(uap, offset);
    682   1.1       mrg 
    683   1.1       mrg 	/*
    684   1.1       mrg 	 * XXX This works because no file systems actually
    685   1.1       mrg 	 * XXX take any action on the seek operation.
    686   1.1       mrg 	 */
    687   1.1       mrg 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
    688   1.9  jdolecek 		goto out;
    689   1.1       mrg 
    690  1.39       dsl 	return (dofilereadv32(l, fd, fp, SCARG_P32(uap, iovp),
    691  1.10       scw 	    SCARG(uap, iovcnt), &offset, 0, retval));
    692   1.9  jdolecek 
    693   1.9  jdolecek out:
    694  1.23  christos 	FILE_UNUSE(fp, l);
    695   1.9  jdolecek 	return (error);
    696   1.1       mrg }
    697   1.1       mrg 
    698   1.1       mrg int
    699  1.11   thorpej netbsd32_pwritev(l, v, retval)
    700  1.11   thorpej 	struct lwp *l;
    701   1.1       mrg 	void *v;
    702   1.1       mrg 	register_t *retval;
    703   1.1       mrg {
    704   1.1       mrg 	struct netbsd32_pwritev_args /* {
    705   1.1       mrg 		syscallarg(int) fd;
    706   1.1       mrg 		syscallarg(const netbsd32_iovecp_t) iovp;
    707   1.1       mrg 		syscallarg(int) iovcnt;
    708   1.1       mrg 		syscallarg(int) pad;
    709   1.1       mrg 		syscallarg(off_t) offset;
    710   1.1       mrg 	} */ *uap = v;
    711  1.14      fvdl 	struct proc *p = l->l_proc;
    712  1.14      fvdl 	struct filedesc *fdp = p->p_fd;
    713   1.1       mrg 	struct file *fp;
    714   1.1       mrg 	struct vnode *vp;
    715   1.1       mrg 	off_t offset;
    716   1.1       mrg 	int error, fd = SCARG(uap, fd);
    717   1.1       mrg 
    718   1.6   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    719   1.6   thorpej 		return (EBADF);
    720   1.6   thorpej 
    721   1.6   thorpej 	if ((fp->f_flag & FWRITE) == 0)
    722   1.1       mrg 		return (EBADF);
    723   1.1       mrg 
    724   1.9  jdolecek 	FILE_USE(fp);
    725   1.9  jdolecek 
    726   1.1       mrg 	vp = (struct vnode *)fp->f_data;
    727   1.9  jdolecek 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    728   1.9  jdolecek 		error = ESPIPE;
    729   1.9  jdolecek 		goto out;
    730   1.9  jdolecek 	}
    731   1.1       mrg 
    732   1.1       mrg 	offset = SCARG(uap, offset);
    733   1.1       mrg 
    734   1.1       mrg 	/*
    735   1.1       mrg 	 * XXX This works because no file systems actually
    736   1.1       mrg 	 * XXX take any action on the seek operation.
    737   1.1       mrg 	 */
    738   1.1       mrg 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
    739   1.9  jdolecek 		goto out;
    740   1.1       mrg 
    741  1.39       dsl 	return (dofilewritev32(l, fd, fp, SCARG_P32(uap, iovp),
    742  1.10       scw 	    SCARG(uap, iovcnt), &offset, 0, retval));
    743   1.9  jdolecek 
    744   1.9  jdolecek out:
    745  1.23  christos 	FILE_UNUSE(fp, l);
    746   1.9  jdolecek 	return (error);
    747   1.1       mrg }
    748   1.1       mrg 
    749   1.1       mrg /*
    750   1.1       mrg  * Find pathname of process's current directory.
    751   1.1       mrg  *
    752   1.1       mrg  * Use vfs vnode-to-name reverse cache; if that fails, fall back
    753   1.1       mrg  * to reading directory contents.
    754   1.1       mrg  */
    755  1.23  christos /* XXX NH Why does this exist */
    756  1.14      fvdl int
    757  1.14      fvdl getcwd_common __P((struct vnode *, struct vnode *,
    758  1.23  christos 		   char **, char *, int, int, struct lwp *));
    759  1.14      fvdl 
    760  1.19     perry int netbsd32___getcwd(l, v, retval)
    761  1.11   thorpej 	struct lwp *l;
    762   1.1       mrg 	void   *v;
    763   1.1       mrg 	register_t *retval;
    764   1.1       mrg {
    765   1.1       mrg 	struct netbsd32___getcwd_args /* {
    766   1.1       mrg 		syscallarg(char *) bufp;
    767   1.1       mrg 		syscallarg(size_t) length;
    768   1.1       mrg 	} */ *uap = v;
    769  1.11   thorpej 	struct proc *p = l->l_proc;
    770   1.1       mrg 	int     error;
    771   1.1       mrg 	char   *path;
    772   1.1       mrg 	char   *bp, *bend;
    773   1.1       mrg 	int     len = (int)SCARG(uap, length);
    774   1.1       mrg 	int	lenused;
    775   1.1       mrg 
    776   1.1       mrg 	if (len > MAXPATHLEN*4)
    777   1.1       mrg 		len = MAXPATHLEN*4;
    778   1.1       mrg 	else if (len < 2)
    779   1.1       mrg 		return ERANGE;
    780   1.1       mrg 
    781   1.1       mrg 	path = (char *)malloc(len, M_TEMP, M_WAITOK);
    782   1.1       mrg 	if (!path)
    783   1.1       mrg 		return ENOMEM;
    784   1.1       mrg 
    785   1.1       mrg 	bp = &path[len];
    786   1.1       mrg 	bend = bp;
    787   1.1       mrg 	*(--bp) = '\0';
    788   1.1       mrg 
    789   1.1       mrg 	/*
    790   1.1       mrg 	 * 5th argument here is "max number of vnodes to traverse".
    791   1.1       mrg 	 * Since each entry takes up at least 2 bytes in the output buffer,
    792   1.1       mrg 	 * limit it to N/2 vnodes for an N byte buffer.
    793   1.1       mrg 	 */
    794   1.1       mrg #define GETCWD_CHECK_ACCESS 0x0001
    795   1.1       mrg 	error = getcwd_common (p->p_cwdi->cwdi_cdir, NULL, &bp, path, len/2,
    796  1.23  christos 			       GETCWD_CHECK_ACCESS, l);
    797   1.1       mrg 
    798   1.1       mrg 	if (error)
    799   1.1       mrg 		goto out;
    800   1.1       mrg 	lenused = bend - bp;
    801   1.1       mrg 	*retval = lenused;
    802   1.1       mrg 	/* put the result into user buffer */
    803  1.39       dsl 	error = copyout(bp, SCARG_P32(uap, bufp), lenused);
    804   1.1       mrg 
    805   1.1       mrg out:
    806   1.1       mrg 	free(path, M_TEMP);
    807   1.1       mrg 	return error;
    808   1.1       mrg }
    809