Home | History | Annotate | Line # | Download | only in netbsd32
netbsd32_fs.c revision 1.36
      1  1.36  christos /*	$NetBSD: netbsd32_fs.c,v 1.36 2007/03/04 06:01:26 christos 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.36  christos __KERNEL_RCSID(0, "$NetBSD: netbsd32_fs.c,v 1.36 2007/03/04 06:01:26 christos 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.1       mrg 
     58   1.1       mrg #include <compat/netbsd32/netbsd32.h>
     59   1.1       mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
     60   1.1       mrg #include <compat/netbsd32/netbsd32_conv.h>
     61  1.28    martin #include <compat/sys/mount.h>
     62   1.1       mrg 
     63   1.1       mrg 
     64  1.23  christos static int dofilereadv32 __P((struct lwp *, int, struct file *, struct netbsd32_iovec *,
     65   1.1       mrg 			      int, off_t *, int, register_t *));
     66  1.23  christos static int dofilewritev32 __P((struct lwp *, int, struct file *, struct netbsd32_iovec *,
     67   1.1       mrg 			       int,  off_t *, int, register_t *));
     68  1.23  christos static int change_utimes32 __P((struct vnode *, netbsd32_timevalp_t, struct lwp *));
     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.19     perry 	    (struct netbsd32_iovec *)NETBSD32PTR64(SCARG(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.10       scw 	    (struct netbsd32_iovec *)NETBSD32PTR64(SCARG(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.1       mrg int
    318  1.11   thorpej netbsd32_utimes(l, v, retval)
    319  1.11   thorpej 	struct lwp *l;
    320   1.1       mrg 	void *v;
    321   1.1       mrg 	register_t *retval;
    322   1.1       mrg {
    323   1.1       mrg 	struct netbsd32_utimes_args /* {
    324   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    325   1.1       mrg 		syscallarg(const netbsd32_timevalp_t) tptr;
    326   1.1       mrg 	} */ *uap = v;
    327   1.1       mrg 	int error;
    328   1.1       mrg 	struct nameidata nd;
    329   1.1       mrg 
    330  1.10       scw 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
    331  1.23  christos 	    (char *)NETBSD32PTR64(SCARG(uap, path)), l);
    332   1.1       mrg 	if ((error = namei(&nd)) != 0)
    333   1.1       mrg 		return (error);
    334   1.1       mrg 
    335  1.23  christos 	error = change_utimes32(nd.ni_vp, SCARG(uap, tptr), l);
    336   1.1       mrg 
    337   1.1       mrg 	vrele(nd.ni_vp);
    338   1.1       mrg 	return (error);
    339   1.1       mrg }
    340   1.1       mrg 
    341   1.1       mrg /*
    342   1.1       mrg  * Common routine to set access and modification times given a vnode.
    343   1.1       mrg  */
    344   1.1       mrg static int
    345  1.23  christos change_utimes32(vp, tptr, l)
    346   1.1       mrg 	struct vnode *vp;
    347   1.1       mrg 	netbsd32_timevalp_t tptr;
    348  1.23  christos 	struct lwp *l;
    349   1.1       mrg {
    350   1.1       mrg 	struct netbsd32_timeval tv32[2];
    351   1.1       mrg 	struct timeval tv[2];
    352   1.1       mrg 	struct vattr vattr;
    353   1.1       mrg 	int error;
    354   1.1       mrg 
    355   1.1       mrg 	VATTR_NULL(&vattr);
    356  1.15      fvdl 	if (tptr == 0) {
    357   1.1       mrg 		microtime(&tv[0]);
    358   1.1       mrg 		tv[1] = tv[0];
    359   1.1       mrg 		vattr.va_vaflags |= VA_UTIMES_NULL;
    360   1.1       mrg 	} else {
    361  1.36  christos 		error = copyin((void *)NETBSD32PTR64(tptr), tv32,
    362  1.10       scw 		    sizeof(tv32));
    363   1.1       mrg 		if (error)
    364   1.1       mrg 			return (error);
    365   1.1       mrg 		netbsd32_to_timeval(&tv32[0], &tv[0]);
    366   1.1       mrg 		netbsd32_to_timeval(&tv32[1], &tv[1]);
    367   1.1       mrg 	}
    368  1.31        ad 	VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
    369   1.1       mrg 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    370   1.1       mrg 	vattr.va_atime.tv_sec = tv[0].tv_sec;
    371   1.1       mrg 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
    372   1.1       mrg 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
    373   1.1       mrg 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
    374  1.31        ad 	error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
    375   1.1       mrg 	VOP_UNLOCK(vp, 0);
    376   1.1       mrg 	return (error);
    377   1.1       mrg }
    378   1.1       mrg 
    379   1.1       mrg int
    380  1.18      cube netbsd32_statvfs1(l, v, retval)
    381  1.11   thorpej 	struct lwp *l;
    382   1.1       mrg 	void *v;
    383   1.1       mrg 	register_t *retval;
    384   1.1       mrg {
    385  1.18      cube 	struct netbsd32_statvfs1_args /* {
    386   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    387  1.18      cube 		syscallarg(netbsd32_statvfsp_t) buf;
    388  1.18      cube 		syscallarg(int) flags;
    389   1.1       mrg 	} */ *uap = v;
    390   1.1       mrg 	struct mount *mp;
    391  1.18      cube 	struct statvfs *sbuf;
    392  1.18      cube 	struct netbsd32_statvfs *s32;
    393  1.18      cube 	struct nameidata nd;
    394   1.1       mrg 	int error;
    395   1.1       mrg 
    396  1.10       scw 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
    397  1.23  christos 	    (char *)NETBSD32PTR64(SCARG(uap, path)), l);
    398   1.1       mrg 	if ((error = namei(&nd)) != 0)
    399   1.1       mrg 		return (error);
    400  1.18      cube 	/* Allocating on the stack would blow it up */
    401  1.18      cube 	sbuf = (struct statvfs *)malloc(sizeof(struct statvfs), M_TEMP,
    402  1.18      cube 	    M_WAITOK);
    403   1.1       mrg 	mp = nd.ni_vp->v_mount;
    404   1.1       mrg 	vrele(nd.ni_vp);
    405  1.23  christos 	if ((error = dostatvfs(mp, sbuf, l, SCARG(uap, flags), 1)) != 0)
    406  1.18      cube 		goto out;
    407  1.18      cube 	s32 = (struct netbsd32_statvfs *)
    408  1.18      cube 	    malloc(sizeof(struct netbsd32_statvfs), M_TEMP, M_WAITOK);
    409  1.18      cube 	netbsd32_from_statvfs(sbuf, s32);
    410  1.36  christos 	error = copyout(s32, (void *)NETBSD32PTR64(SCARG(uap, buf)),
    411  1.18      cube 	    sizeof(struct netbsd32_statvfs));
    412  1.18      cube 	free(s32, M_TEMP);
    413  1.18      cube out:
    414  1.18      cube 	free(sbuf, M_TEMP);
    415  1.18      cube 	return (error);
    416   1.1       mrg }
    417   1.1       mrg 
    418   1.1       mrg int
    419  1.18      cube netbsd32_fstatvfs1(l, v, retval)
    420  1.11   thorpej 	struct lwp *l;
    421   1.1       mrg 	void *v;
    422   1.1       mrg 	register_t *retval;
    423   1.1       mrg {
    424  1.18      cube 	struct netbsd32_fstatvfs1_args /* {
    425   1.1       mrg 		syscallarg(int) fd;
    426  1.18      cube 		syscallarg(netbsd32_statvfsp_t) buf;
    427  1.18      cube 		syscallarg(int) flags;
    428   1.1       mrg 	} */ *uap = v;
    429  1.18      cube 	struct proc *p = l->l_proc;
    430   1.1       mrg 	struct file *fp;
    431   1.1       mrg 	struct mount *mp;
    432  1.18      cube 	struct statvfs *sbuf;
    433  1.18      cube 	struct netbsd32_statvfs *s32;
    434   1.1       mrg 	int error;
    435   1.1       mrg 
    436   1.1       mrg 	/* getvnode() will use the descriptor for us */
    437  1.14      fvdl 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    438   1.1       mrg 		return (error);
    439   1.1       mrg 	mp = ((struct vnode *)fp->f_data)->v_mount;
    440  1.18      cube 	sbuf = (struct statvfs *)malloc(sizeof(struct statvfs), M_TEMP,
    441  1.18      cube 	    M_WAITOK);
    442  1.23  christos 	if ((error = dostatvfs(mp, sbuf, l, SCARG(uap, flags), 1)) != 0)
    443   1.1       mrg 		goto out;
    444  1.18      cube 	s32 = (struct netbsd32_statvfs *)
    445  1.18      cube 	    malloc(sizeof(struct netbsd32_statvfs), M_TEMP, M_WAITOK);
    446  1.18      cube 	netbsd32_from_statvfs(sbuf, s32);
    447  1.36  christos 	error = copyout(s32, (void *)NETBSD32PTR64(SCARG(uap, buf)),
    448  1.18      cube 	    sizeof(struct netbsd32_statvfs));
    449  1.18      cube 	free(s32, M_TEMP);
    450   1.1       mrg  out:
    451  1.18      cube 	free(sbuf, M_TEMP);
    452  1.23  christos 	FILE_UNUSE(fp, l);
    453  1.18      cube 	return error;
    454  1.18      cube }
    455  1.18      cube 
    456  1.18      cube int
    457  1.18      cube netbsd32_getvfsstat(l, v, retval)
    458  1.18      cube 	struct lwp *l;
    459  1.18      cube 	void *v;
    460  1.18      cube 	register_t *retval;
    461  1.18      cube {
    462  1.18      cube 	struct netbsd32_getvfsstat_args /* {
    463  1.18      cube 		syscallarg(netbsd32_statvfsp_t) buf;
    464  1.18      cube 		syscallarg(netbsd32_size_t) bufsize;
    465  1.18      cube 		syscallarg(int) flags;
    466  1.18      cube 	} */ *uap = v;
    467  1.18      cube 	int root = 0;
    468  1.18      cube 	struct proc *p = l->l_proc;
    469  1.18      cube 	struct mount *mp, *nmp;
    470  1.18      cube 	struct statvfs *sbuf;
    471  1.18      cube 	struct netbsd32_statvfs *sfsp;
    472  1.18      cube 	struct netbsd32_statvfs *s32;
    473  1.18      cube 	size_t count, maxcount;
    474  1.18      cube 	int error = 0;
    475  1.18      cube 
    476  1.18      cube 	maxcount = SCARG(uap, bufsize) / sizeof(struct netbsd32_statvfs);
    477  1.18      cube 	sfsp = (struct netbsd32_statvfs *)NETBSD32PTR64(SCARG(uap, buf));
    478  1.18      cube 	sbuf = (struct statvfs *)malloc(sizeof(struct statvfs), M_TEMP,
    479  1.18      cube 	    M_WAITOK);
    480  1.18      cube 	s32 = (struct netbsd32_statvfs *)
    481  1.18      cube 	    malloc(sizeof(struct netbsd32_statvfs), M_TEMP, M_WAITOK);
    482  1.25      cube 	simple_lock(&mountlist_slock);
    483  1.25      cube 	count = 0;
    484  1.18      cube 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    485  1.18      cube 	     mp = nmp) {
    486  1.18      cube 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    487  1.18      cube 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    488  1.18      cube 			continue;
    489  1.18      cube 		}
    490  1.18      cube 		if (sfsp && count < maxcount) {
    491  1.23  christos 			error = dostatvfs(mp, sbuf, l, SCARG(uap, flags), 0);
    492  1.18      cube 			if (error) {
    493  1.18      cube 				simple_lock(&mountlist_slock);
    494  1.18      cube 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
    495  1.18      cube 				vfs_unbusy(mp);
    496  1.18      cube 				continue;
    497  1.18      cube 			}
    498  1.18      cube 			netbsd32_from_statvfs(sbuf, s32);
    499  1.18      cube 			error = copyout(s32, sfsp, sizeof(*sfsp));
    500  1.18      cube 			if (error) {
    501  1.18      cube 				vfs_unbusy(mp);
    502  1.18      cube 				goto out;
    503  1.18      cube 			}
    504  1.18      cube 			sfsp++;
    505  1.18      cube 			root |= strcmp(sbuf->f_mntonname, "/") == 0;
    506  1.18      cube 		}
    507  1.18      cube 		count++;
    508  1.18      cube 		simple_lock(&mountlist_slock);
    509  1.18      cube 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
    510  1.18      cube 		vfs_unbusy(mp);
    511  1.18      cube 	}
    512  1.18      cube 	simple_unlock(&mountlist_slock);
    513  1.18      cube 	if (root == 0 && p->p_cwdi->cwdi_rdir) {
    514  1.18      cube 		/*
    515  1.18      cube 		 * fake a root entry
    516  1.18      cube 		 */
    517  1.23  christos 		if ((error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount, sbuf, l,
    518  1.18      cube 		    SCARG(uap, flags), 1)) != 0)
    519  1.18      cube 			goto out;
    520  1.18      cube 		if (sfsp) {
    521  1.18      cube 			netbsd32_from_statvfs(sbuf, s32);
    522  1.18      cube 			error = copyout(s32, sfsp, sizeof(*sfsp));
    523  1.18      cube 		}
    524  1.18      cube 		count++;
    525  1.18      cube 	}
    526  1.18      cube 	if (sfsp && count > maxcount)
    527  1.18      cube 		*retval = maxcount;
    528  1.18      cube 	else
    529  1.18      cube 		*retval = count;
    530  1.18      cube 
    531  1.18      cube out:
    532  1.18      cube 	free(s32, M_TEMP);
    533  1.18      cube 	free(sbuf, M_TEMP);
    534  1.18      cube 	return (error);
    535  1.18      cube }
    536  1.18      cube 
    537  1.18      cube int
    538  1.32    martin netbsd32___fhstatvfs140(l, v, retval)
    539  1.18      cube 	struct lwp *l;
    540  1.18      cube 	void *v;
    541  1.18      cube 	register_t *retval;
    542  1.18      cube {
    543  1.32    martin 	struct netbsd32___fhstatvfs140_args /* {
    544  1.32    martin 		syscallarg(const netbsd32_pointer_t) fhp;
    545  1.32    martin 		syscallarg(netbsd32_size_t) fh_size;
    546  1.18      cube 		syscallarg(netbsd32_statvfsp_t) buf;
    547  1.18      cube 		syscallarg(int) flags;
    548  1.18      cube 	} */ *uap = v;
    549  1.18      cube 	struct statvfs *sbuf;
    550  1.18      cube 	struct netbsd32_statvfs *s32;
    551  1.29    martin 	fhandle_t *fh;
    552  1.18      cube 	struct vnode *vp;
    553  1.18      cube 	int error;
    554  1.18      cube 
    555  1.18      cube 	/*
    556  1.18      cube 	 * Must be super user
    557  1.18      cube 	 */
    558  1.34      elad 	if ((error = kauth_authorize_system(l->l_cred,
    559  1.34      elad 	    KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
    560  1.18      cube 		return error;
    561  1.18      cube 
    562  1.33      yamt 	if ((error = vfs_copyinfh_alloc(NETBSD32PTR64(SCARG(uap, fhp)),
    563  1.32    martin 	    SCARG(uap, fh_size), &fh)) != 0)
    564  1.29    martin 		goto bad;
    565  1.29    martin 	if ((error = vfs_fhtovp(fh, &vp)) != 0)
    566  1.29    martin 		goto bad;
    567  1.18      cube 
    568  1.18      cube 	sbuf = (struct statvfs *)malloc(sizeof(struct statvfs), M_TEMP,
    569  1.18      cube 	    M_WAITOK);
    570  1.29    martin 	error = dostatvfs(vp->v_mount, sbuf, l, SCARG(uap, flags), 1);
    571  1.29    martin 	vput(vp);
    572  1.29    martin 	if (error != 0)
    573  1.18      cube 		goto out;
    574  1.18      cube 
    575  1.18      cube 	s32 = (struct netbsd32_statvfs *)
    576  1.18      cube 	    malloc(sizeof(struct netbsd32_statvfs), M_TEMP, M_WAITOK);
    577  1.18      cube 	netbsd32_from_statvfs(sbuf, s32);
    578  1.36  christos 	error = copyout(s32, (void *)NETBSD32PTR64(SCARG(uap, buf)),
    579  1.18      cube 	    sizeof(struct netbsd32_statvfs));
    580  1.18      cube 	free(s32, M_TEMP);
    581  1.18      cube 
    582  1.18      cube out:
    583  1.18      cube 	free(sbuf, M_TEMP);
    584  1.29    martin bad:
    585  1.29    martin 	vfs_copyinfh_free(fh);
    586   1.1       mrg 	return (error);
    587   1.1       mrg }
    588   1.1       mrg 
    589   1.1       mrg int
    590  1.11   thorpej netbsd32_futimes(l, v, retval)
    591  1.11   thorpej 	struct lwp *l;
    592   1.1       mrg 	void *v;
    593   1.1       mrg 	register_t *retval;
    594   1.1       mrg {
    595   1.1       mrg 	struct netbsd32_futimes_args /* {
    596   1.1       mrg 		syscallarg(int) fd;
    597   1.1       mrg 		syscallarg(const netbsd32_timevalp_t) tptr;
    598   1.1       mrg 	} */ *uap = v;
    599   1.1       mrg 	int error;
    600   1.1       mrg 	struct file *fp;
    601  1.14      fvdl 	struct proc *p = l->l_proc;
    602   1.1       mrg 
    603   1.1       mrg 	/* getvnode() will use the descriptor for us */
    604  1.14      fvdl 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    605   1.1       mrg 		return (error);
    606   1.1       mrg 
    607  1.19     perry 	error = change_utimes32((struct vnode *)fp->f_data,
    608  1.23  christos 				SCARG(uap, tptr), l);
    609  1.23  christos 	FILE_UNUSE(fp, l);
    610   1.1       mrg 	return (error);
    611   1.1       mrg }
    612   1.1       mrg 
    613   1.1       mrg int
    614  1.22  christos netbsd32_sys___getdents30(l, v, retval)
    615  1.11   thorpej 	struct lwp *l;
    616   1.1       mrg 	void *v;
    617   1.1       mrg 	register_t *retval;
    618   1.1       mrg {
    619  1.22  christos 	struct netbsd32_sys___getdents30_args /* {
    620   1.1       mrg 		syscallarg(int) fd;
    621   1.1       mrg 		syscallarg(netbsd32_charp) buf;
    622   1.1       mrg 		syscallarg(netbsd32_size_t) count;
    623   1.1       mrg 	} */ *uap = v;
    624   1.1       mrg 	struct file *fp;
    625   1.1       mrg 	int error, done;
    626  1.11   thorpej 	struct proc *p = l->l_proc;
    627   1.1       mrg 
    628   1.1       mrg 	/* getvnode() will use the descriptor for us */
    629   1.1       mrg 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    630   1.1       mrg 		return (error);
    631   1.1       mrg 	if ((fp->f_flag & FREAD) == 0) {
    632   1.1       mrg 		error = EBADF;
    633   1.1       mrg 		goto out;
    634   1.1       mrg 	}
    635  1.36  christos 	error = vn_readdir(fp, (void *)NETBSD32PTR64(SCARG(uap, buf)),
    636  1.23  christos 	    UIO_USERSPACE, SCARG(uap, count), &done, l, 0, 0);
    637   1.1       mrg 	*retval = done;
    638   1.1       mrg  out:
    639  1.23  christos 	FILE_UNUSE(fp, l);
    640   1.1       mrg 	return (error);
    641   1.1       mrg }
    642   1.1       mrg 
    643   1.1       mrg int
    644  1.11   thorpej netbsd32_lutimes(l, v, retval)
    645  1.11   thorpej 	struct lwp *l;
    646   1.1       mrg 	void *v;
    647   1.1       mrg 	register_t *retval;
    648   1.1       mrg {
    649   1.1       mrg 	struct netbsd32_lutimes_args /* {
    650   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    651   1.1       mrg 		syscallarg(const netbsd32_timevalp_t) tptr;
    652   1.1       mrg 	} */ *uap = v;
    653   1.1       mrg 	int error;
    654   1.1       mrg 	struct nameidata nd;
    655   1.1       mrg 
    656  1.10       scw 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE,
    657  1.36  christos 	    (void *)NETBSD32PTR64(SCARG(uap, path)), l);
    658   1.1       mrg 	if ((error = namei(&nd)) != 0)
    659   1.1       mrg 		return (error);
    660   1.1       mrg 
    661  1.23  christos 	error = change_utimes32(nd.ni_vp, SCARG(uap, tptr), l);
    662   1.1       mrg 
    663   1.1       mrg 	vrele(nd.ni_vp);
    664   1.1       mrg 	return (error);
    665   1.1       mrg }
    666   1.1       mrg 
    667   1.1       mrg int
    668  1.22  christos netbsd32_sys___stat30(l, v, retval)
    669  1.11   thorpej 	struct lwp *l;
    670   1.1       mrg 	void *v;
    671   1.1       mrg 	register_t *retval;
    672   1.1       mrg {
    673  1.22  christos 	struct netbsd32_sys___stat30_args /* {
    674   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    675   1.1       mrg 		syscallarg(netbsd32_statp_t) ub;
    676   1.1       mrg 	} */ *uap = v;
    677   1.1       mrg 	struct netbsd32_stat sb32;
    678   1.1       mrg 	struct stat sb;
    679   1.1       mrg 	int error;
    680   1.1       mrg 	struct nameidata nd;
    681  1.36  christos 	void *sg;
    682   1.1       mrg 	const char *path;
    683  1.14      fvdl 	struct proc *p = l->l_proc;
    684   1.1       mrg 
    685  1.10       scw 	path = (char *)NETBSD32PTR64(SCARG(uap, path));
    686  1.14      fvdl 	sg = stackgap_init(p, 0);
    687  1.23  christos 	CHECK_ALT_EXIST(l, &sg, path);
    688   1.1       mrg 
    689  1.23  christos 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, path, l);
    690   1.1       mrg 	if ((error = namei(&nd)) != 0)
    691   1.1       mrg 		return (error);
    692  1.23  christos 	error = vn_stat(nd.ni_vp, &sb, l);
    693   1.1       mrg 	vput(nd.ni_vp);
    694   1.1       mrg 	if (error)
    695   1.1       mrg 		return (error);
    696  1.22  christos 	netbsd32_from___stat30(&sb, &sb32);
    697  1.36  christos 	error = copyout(&sb32, (void *)NETBSD32PTR64(SCARG(uap, ub)),
    698  1.10       scw 	    sizeof(sb32));
    699   1.1       mrg 	return (error);
    700   1.1       mrg }
    701   1.1       mrg 
    702   1.1       mrg int
    703  1.22  christos netbsd32_sys___fstat30(l, v, retval)
    704  1.11   thorpej 	struct lwp *l;
    705   1.1       mrg 	void *v;
    706   1.1       mrg 	register_t *retval;
    707   1.1       mrg {
    708  1.22  christos 	struct netbsd32_sys___fstat30_args /* {
    709   1.1       mrg 		syscallarg(int) fd;
    710   1.1       mrg 		syscallarg(netbsd32_statp_t) sb;
    711   1.1       mrg 	} */ *uap = v;
    712   1.1       mrg 	int fd = SCARG(uap, fd);
    713  1.14      fvdl 	struct proc *p = l->l_proc;
    714  1.14      fvdl 	struct filedesc *fdp = p->p_fd;
    715   1.1       mrg 	struct file *fp;
    716   1.1       mrg 	struct netbsd32_stat sb32;
    717   1.1       mrg 	struct stat ub;
    718   1.1       mrg 	int error = 0;
    719   1.1       mrg 
    720   1.6   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    721   1.1       mrg 		return (EBADF);
    722   1.1       mrg 
    723   1.3  jdolecek 	FILE_USE(fp);
    724  1.23  christos 	error = (*fp->f_ops->fo_stat)(fp, &ub, l);
    725  1.23  christos 	FILE_UNUSE(fp, l);
    726   1.3  jdolecek 
    727   1.1       mrg 	if (error == 0) {
    728  1.22  christos 		netbsd32_from___stat30(&ub, &sb32);
    729  1.36  christos 		error = copyout(&sb32, (void *)NETBSD32PTR64(SCARG(uap, sb)),
    730  1.10       scw 		    sizeof(sb32));
    731   1.1       mrg 	}
    732   1.1       mrg 	return (error);
    733   1.1       mrg }
    734   1.1       mrg 
    735   1.1       mrg int
    736  1.22  christos netbsd32_sys___lstat30(l, v, retval)
    737  1.11   thorpej 	struct lwp *l;
    738   1.1       mrg 	void *v;
    739   1.1       mrg 	register_t *retval;
    740   1.1       mrg {
    741  1.22  christos 	struct netbsd32_sys___lstat30_args /* {
    742   1.1       mrg 		syscallarg(const netbsd32_charp) path;
    743   1.1       mrg 		syscallarg(netbsd32_statp_t) ub;
    744   1.1       mrg 	} */ *uap = v;
    745   1.1       mrg 	struct netbsd32_stat sb32;
    746   1.1       mrg 	struct stat sb;
    747   1.1       mrg 	int error;
    748   1.1       mrg 	struct nameidata nd;
    749  1.36  christos 	void *sg;
    750   1.1       mrg 	const char *path;
    751  1.14      fvdl 	struct proc *p = l->l_proc;
    752   1.1       mrg 
    753  1.10       scw 	path = (char *)NETBSD32PTR64(SCARG(uap, path));
    754  1.14      fvdl 	sg = stackgap_init(p, 0);
    755  1.23  christos 	CHECK_ALT_EXIST(l, &sg, path);
    756   1.1       mrg 
    757  1.23  christos 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE, path, l);
    758   1.1       mrg 	if ((error = namei(&nd)) != 0)
    759   1.1       mrg 		return (error);
    760  1.23  christos 	error = vn_stat(nd.ni_vp, &sb, l);
    761   1.1       mrg 	vput(nd.ni_vp);
    762   1.1       mrg 	if (error)
    763   1.1       mrg 		return (error);
    764  1.22  christos 	netbsd32_from___stat30(&sb, &sb32);
    765  1.36  christos 	error = copyout(&sb32, (void *)NETBSD32PTR64(SCARG(uap, ub)),
    766  1.10       scw 	    sizeof(sb32));
    767   1.1       mrg 	return (error);
    768   1.1       mrg }
    769   1.1       mrg 
    770  1.32    martin int netbsd32___fhstat40(l, v, retval)
    771  1.26      cube 	struct lwp *l;
    772  1.26      cube 	void *v;
    773  1.26      cube 	register_t *retval;
    774  1.26      cube {
    775  1.32    martin 	struct netbsd32___fhstat40_args /* {
    776  1.32    martin 		syscallarg(const netbsd32_pointer_t) fhp;
    777  1.32    martin 		syscallarg(netbsd32_size_t) fh_size;
    778  1.26      cube 		syscallarg(netbsd32_statp_t) sb;
    779  1.26      cube 	} */ *uap = v;
    780  1.26      cube 	struct stat sb;
    781  1.26      cube 	struct netbsd32_stat sb32;
    782  1.26      cube 	int error;
    783  1.29    martin 	fhandle_t *fh;
    784  1.26      cube 	struct vnode *vp;
    785  1.26      cube 
    786  1.26      cube 	/*
    787  1.26      cube 	 * Must be super user
    788  1.26      cube 	 */
    789  1.34      elad 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
    790  1.34      elad 	    0, NULL, NULL, NULL)))
    791  1.29    martin 		return error;
    792  1.29    martin 
    793  1.33      yamt 	if ((error = vfs_copyinfh_alloc(NETBSD32PTR64(SCARG(uap, fhp)),
    794  1.33      yamt 	    SCARG(uap, fh_size), &fh)) != 0)
    795  1.29    martin 		goto bad;
    796  1.26      cube 
    797  1.29    martin 	if ((error = vfs_fhtovp(fh, &vp)) != 0)
    798  1.29    martin 		goto bad;
    799  1.26      cube 
    800  1.26      cube 	error = vn_stat(vp, &sb, l);
    801  1.26      cube 	vput(vp);
    802  1.26      cube 	if (error)
    803  1.29    martin 		goto bad;
    804  1.26      cube 	netbsd32_from___stat30(&sb, &sb32);
    805  1.26      cube 	error = copyout(&sb32, NETBSD32PTR64(SCARG(uap, sb)), sizeof(sb));
    806  1.29    martin bad:
    807  1.29    martin 	vfs_copyinfh_free(fh);
    808  1.29    martin 	return error;
    809  1.26      cube }
    810  1.26      cube 
    811   1.1       mrg int
    812  1.11   thorpej netbsd32_preadv(l, v, retval)
    813  1.11   thorpej 	struct lwp *l;
    814   1.1       mrg 	void *v;
    815   1.1       mrg 	register_t *retval;
    816   1.1       mrg {
    817   1.1       mrg 	struct netbsd32_preadv_args /* {
    818   1.1       mrg 		syscallarg(int) fd;
    819   1.1       mrg 		syscallarg(const netbsd32_iovecp_t) iovp;
    820   1.1       mrg 		syscallarg(int) iovcnt;
    821   1.1       mrg 		syscallarg(int) pad;
    822   1.1       mrg 		syscallarg(off_t) offset;
    823   1.1       mrg 	} */ *uap = v;
    824  1.14      fvdl 	struct proc *p = l->l_proc;
    825  1.14      fvdl 	struct filedesc *fdp = p->p_fd;
    826   1.1       mrg 	struct file *fp;
    827   1.1       mrg 	struct vnode *vp;
    828   1.1       mrg 	off_t offset;
    829   1.1       mrg 	int error, fd = SCARG(uap, fd);
    830   1.1       mrg 
    831   1.6   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    832   1.6   thorpej 		return (EBADF);
    833   1.6   thorpej 
    834   1.6   thorpej 	if ((fp->f_flag & FREAD) == 0)
    835   1.1       mrg 		return (EBADF);
    836   1.1       mrg 
    837   1.9  jdolecek 	FILE_USE(fp);
    838   1.9  jdolecek 
    839   1.1       mrg 	vp = (struct vnode *)fp->f_data;
    840   1.9  jdolecek 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    841   1.9  jdolecek 		error = ESPIPE;
    842   1.9  jdolecek 		goto out;
    843   1.9  jdolecek 	}
    844   1.1       mrg 
    845   1.1       mrg 	offset = SCARG(uap, offset);
    846   1.1       mrg 
    847   1.1       mrg 	/*
    848   1.1       mrg 	 * XXX This works because no file systems actually
    849   1.1       mrg 	 * XXX take any action on the seek operation.
    850   1.1       mrg 	 */
    851   1.1       mrg 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
    852   1.9  jdolecek 		goto out;
    853   1.1       mrg 
    854  1.23  christos 	return (dofilereadv32(l, fd, fp,
    855  1.10       scw 	    (struct netbsd32_iovec *)NETBSD32PTR64(SCARG(uap, iovp)),
    856  1.10       scw 	    SCARG(uap, iovcnt), &offset, 0, retval));
    857   1.9  jdolecek 
    858   1.9  jdolecek out:
    859  1.23  christos 	FILE_UNUSE(fp, l);
    860   1.9  jdolecek 	return (error);
    861   1.1       mrg }
    862   1.1       mrg 
    863   1.1       mrg int
    864  1.11   thorpej netbsd32_pwritev(l, v, retval)
    865  1.11   thorpej 	struct lwp *l;
    866   1.1       mrg 	void *v;
    867   1.1       mrg 	register_t *retval;
    868   1.1       mrg {
    869   1.1       mrg 	struct netbsd32_pwritev_args /* {
    870   1.1       mrg 		syscallarg(int) fd;
    871   1.1       mrg 		syscallarg(const netbsd32_iovecp_t) iovp;
    872   1.1       mrg 		syscallarg(int) iovcnt;
    873   1.1       mrg 		syscallarg(int) pad;
    874   1.1       mrg 		syscallarg(off_t) offset;
    875   1.1       mrg 	} */ *uap = v;
    876  1.14      fvdl 	struct proc *p = l->l_proc;
    877  1.14      fvdl 	struct filedesc *fdp = p->p_fd;
    878   1.1       mrg 	struct file *fp;
    879   1.1       mrg 	struct vnode *vp;
    880   1.1       mrg 	off_t offset;
    881   1.1       mrg 	int error, fd = SCARG(uap, fd);
    882   1.1       mrg 
    883   1.6   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    884   1.6   thorpej 		return (EBADF);
    885   1.6   thorpej 
    886   1.6   thorpej 	if ((fp->f_flag & FWRITE) == 0)
    887   1.1       mrg 		return (EBADF);
    888   1.1       mrg 
    889   1.9  jdolecek 	FILE_USE(fp);
    890   1.9  jdolecek 
    891   1.1       mrg 	vp = (struct vnode *)fp->f_data;
    892   1.9  jdolecek 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    893   1.9  jdolecek 		error = ESPIPE;
    894   1.9  jdolecek 		goto out;
    895   1.9  jdolecek 	}
    896   1.1       mrg 
    897   1.1       mrg 	offset = SCARG(uap, offset);
    898   1.1       mrg 
    899   1.1       mrg 	/*
    900   1.1       mrg 	 * XXX This works because no file systems actually
    901   1.1       mrg 	 * XXX take any action on the seek operation.
    902   1.1       mrg 	 */
    903   1.1       mrg 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
    904   1.9  jdolecek 		goto out;
    905   1.1       mrg 
    906  1.23  christos 	return (dofilewritev32(l, fd, fp,
    907  1.10       scw 	    (struct netbsd32_iovec *)NETBSD32PTR64(SCARG(uap, iovp)),
    908  1.10       scw 	    SCARG(uap, iovcnt), &offset, 0, retval));
    909   1.9  jdolecek 
    910   1.9  jdolecek out:
    911  1.23  christos 	FILE_UNUSE(fp, l);
    912   1.9  jdolecek 	return (error);
    913   1.1       mrg }
    914   1.1       mrg 
    915   1.1       mrg /*
    916   1.1       mrg  * Find pathname of process's current directory.
    917   1.1       mrg  *
    918   1.1       mrg  * Use vfs vnode-to-name reverse cache; if that fails, fall back
    919   1.1       mrg  * to reading directory contents.
    920   1.1       mrg  */
    921  1.23  christos /* XXX NH Why does this exist */
    922  1.14      fvdl int
    923  1.14      fvdl getcwd_common __P((struct vnode *, struct vnode *,
    924  1.23  christos 		   char **, char *, int, int, struct lwp *));
    925  1.14      fvdl 
    926  1.19     perry int netbsd32___getcwd(l, v, retval)
    927  1.11   thorpej 	struct lwp *l;
    928   1.1       mrg 	void   *v;
    929   1.1       mrg 	register_t *retval;
    930   1.1       mrg {
    931   1.1       mrg 	struct netbsd32___getcwd_args /* {
    932   1.1       mrg 		syscallarg(char *) bufp;
    933   1.1       mrg 		syscallarg(size_t) length;
    934   1.1       mrg 	} */ *uap = v;
    935  1.11   thorpej 	struct proc *p = l->l_proc;
    936   1.1       mrg 	int     error;
    937   1.1       mrg 	char   *path;
    938   1.1       mrg 	char   *bp, *bend;
    939   1.1       mrg 	int     len = (int)SCARG(uap, length);
    940   1.1       mrg 	int	lenused;
    941   1.1       mrg 
    942   1.1       mrg 	if (len > MAXPATHLEN*4)
    943   1.1       mrg 		len = MAXPATHLEN*4;
    944   1.1       mrg 	else if (len < 2)
    945   1.1       mrg 		return ERANGE;
    946   1.1       mrg 
    947   1.1       mrg 	path = (char *)malloc(len, M_TEMP, M_WAITOK);
    948   1.1       mrg 	if (!path)
    949   1.1       mrg 		return ENOMEM;
    950   1.1       mrg 
    951   1.1       mrg 	bp = &path[len];
    952   1.1       mrg 	bend = bp;
    953   1.1       mrg 	*(--bp) = '\0';
    954   1.1       mrg 
    955   1.1       mrg 	/*
    956   1.1       mrg 	 * 5th argument here is "max number of vnodes to traverse".
    957   1.1       mrg 	 * Since each entry takes up at least 2 bytes in the output buffer,
    958   1.1       mrg 	 * limit it to N/2 vnodes for an N byte buffer.
    959   1.1       mrg 	 */
    960   1.1       mrg #define GETCWD_CHECK_ACCESS 0x0001
    961   1.1       mrg 	error = getcwd_common (p->p_cwdi->cwdi_cdir, NULL, &bp, path, len/2,
    962  1.23  christos 			       GETCWD_CHECK_ACCESS, l);
    963   1.1       mrg 
    964   1.1       mrg 	if (error)
    965   1.1       mrg 		goto out;
    966   1.1       mrg 	lenused = bend - bp;
    967   1.1       mrg 	*retval = lenused;
    968   1.1       mrg 	/* put the result into user buffer */
    969  1.36  christos 	error = copyout(bp, (void *)NETBSD32PTR64(SCARG(uap, bufp)), lenused);
    970   1.1       mrg 
    971   1.1       mrg out:
    972   1.1       mrg 	free(path, M_TEMP);
    973   1.1       mrg 	return error;
    974   1.1       mrg }
    975