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