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