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