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