Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_43.c revision 1.64
      1  1.64  pgoyette /*	$NetBSD: vfs_syscalls_43.c,v 1.64 2019/01/27 02:08:39 pgoyette Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*
      4   1.1  christos  * Copyright (c) 1989, 1993
      5   1.1  christos  *	The Regents of the University of California.  All rights reserved.
      6   1.1  christos  * (c) UNIX System Laboratories, Inc.
      7   1.1  christos  * All or some portions of this file are derived from material licensed
      8   1.1  christos  * to the University of California by American Telephone and Telegraph
      9   1.1  christos  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10   1.1  christos  * the permission of UNIX System Laboratories, Inc.
     11   1.1  christos  *
     12   1.1  christos  * Redistribution and use in source and binary forms, with or without
     13   1.1  christos  * modification, are permitted provided that the following conditions
     14   1.1  christos  * are met:
     15   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     16   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     17   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     19   1.1  christos  *    documentation and/or other materials provided with the distribution.
     20  1.25       agc  * 3. Neither the name of the University nor the names of its contributors
     21   1.1  christos  *    may be used to endorse or promote products derived from this software
     22   1.1  christos  *    without specific prior written permission.
     23   1.1  christos  *
     24   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1  christos  * SUCH DAMAGE.
     35   1.1  christos  *
     36   1.1  christos  *	@(#)vfs_syscalls.c	8.28 (Berkeley) 12/10/94
     37   1.1  christos  */
     38  1.20     lukem 
     39  1.20     lukem #include <sys/cdefs.h>
     40  1.64  pgoyette __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.64 2019/01/27 02:08:39 pgoyette Exp $");
     41   1.9   thorpej 
     42  1.18       mrg #if defined(_KERNEL_OPT)
     43  1.50    dyoung #include "opt_compat_netbsd.h"
     44  1.15  jdolecek #endif
     45   1.1  christos 
     46   1.1  christos #include <sys/param.h>
     47   1.1  christos #include <sys/systm.h>
     48   1.1  christos #include <sys/filedesc.h>
     49   1.1  christos #include <sys/kernel.h>
     50   1.1  christos #include <sys/proc.h>
     51   1.1  christos #include <sys/file.h>
     52   1.1  christos #include <sys/vnode.h>
     53   1.1  christos #include <sys/namei.h>
     54   1.1  christos #include <sys/dirent.h>
     55   1.1  christos #include <sys/socket.h>
     56   1.1  christos #include <sys/socketvar.h>
     57   1.1  christos #include <sys/stat.h>
     58  1.27  christos #include <sys/malloc.h>
     59   1.1  christos #include <sys/ioctl.h>
     60   1.1  christos #include <sys/fcntl.h>
     61   1.1  christos #include <sys/syslog.h>
     62   1.1  christos #include <sys/unistd.h>
     63   1.1  christos #include <sys/resourcevar.h>
     64   1.1  christos 
     65   1.1  christos #include <sys/mount.h>
     66  1.64  pgoyette #include <sys/syscall.h>
     67  1.64  pgoyette #include <sys/syscallvar.h>
     68   1.1  christos #include <sys/syscallargs.h>
     69  1.37       dsl #include <sys/vfs_syscalls.h>
     70   1.1  christos 
     71  1.30  christos #include <compat/sys/stat.h>
     72  1.30  christos #include <compat/sys/mount.h>
     73  1.56  christos #include <compat/sys/dirent.h>
     74  1.30  christos 
     75  1.48        ad #include <compat/common/compat_util.h>
     76  1.55  pgoyette #include <compat/common/compat_mod.h>
     77  1.48        ad 
     78  1.58       mrg static void cvttimespec(struct timespec *, struct timespec50 *);
     79  1.42       dsl static void cvtstat(struct stat *, struct stat43 *);
     80   1.4  christos 
     81  1.64  pgoyette static struct syscall_package vfs_syscalls_43_syscalls[] = {
     82  1.64  pgoyette 	{ SYS_compat_43_oquota, 0, (sy_call_t *)compat_43_sys_quota },
     83  1.64  pgoyette 	{ 0, 0, NULL }
     84  1.64  pgoyette };
     85  1.64  pgoyette 
     86   1.1  christos /*
     87  1.58       mrg  * Convert from an old to a new timespec structure.
     88  1.58       mrg  */
     89  1.58       mrg static void
     90  1.58       mrg cvttimespec(struct timespec *ts, struct timespec50 *ots)
     91  1.58       mrg {
     92  1.58       mrg 
     93  1.58       mrg 	if (ts->tv_sec > INT_MAX) {
     94  1.58       mrg #if defined(DEBUG) || 1
     95  1.58       mrg 		static bool first = true;
     96  1.58       mrg 
     97  1.58       mrg 		if (first) {
     98  1.58       mrg 			first = false;
     99  1.58       mrg 			printf("%s[%s:%d]: time_t does not fit\n",
    100  1.58       mrg 			    __func__, curlwp->l_proc->p_comm,
    101  1.58       mrg 			    curlwp->l_lid);
    102  1.58       mrg 		}
    103  1.58       mrg #endif
    104  1.58       mrg 		ots->tv_sec = INT_MAX;
    105  1.58       mrg 	} else
    106  1.58       mrg 		ots->tv_sec = ts->tv_sec;
    107  1.58       mrg 	ots->tv_nsec = ts->tv_nsec;
    108  1.58       mrg }
    109  1.58       mrg 
    110  1.58       mrg /*
    111   1.1  christos  * Convert from an old to a new stat structure.
    112   1.1  christos  */
    113   1.4  christos static void
    114  1.43       dsl cvtstat(struct stat *st, struct stat43 *ost)
    115   1.1  christos {
    116   1.1  christos 
    117  1.58       mrg 	/* Handle any padding. */
    118  1.58       mrg 	memset(ost, 0, sizeof *ost);
    119   1.1  christos 	ost->st_dev = st->st_dev;
    120   1.1  christos 	ost->st_ino = st->st_ino;
    121  1.11  wrstuden 	ost->st_mode = st->st_mode & 0xffff;
    122   1.1  christos 	ost->st_nlink = st->st_nlink;
    123   1.1  christos 	ost->st_uid = st->st_uid;
    124   1.1  christos 	ost->st_gid = st->st_gid;
    125   1.1  christos 	ost->st_rdev = st->st_rdev;
    126   1.1  christos 	if (st->st_size < (quad_t)1 << 32)
    127   1.1  christos 		ost->st_size = st->st_size;
    128   1.1  christos 	else
    129   1.1  christos 		ost->st_size = -2;
    130  1.58       mrg 	cvttimespec(&st->st_atimespec, &ost->st_atimespec);
    131  1.58       mrg 	cvttimespec(&st->st_mtimespec, &ost->st_mtimespec);
    132  1.58       mrg 	cvttimespec(&st->st_ctimespec, &ost->st_ctimespec);
    133   1.1  christos 	ost->st_blksize = st->st_blksize;
    134   1.1  christos 	ost->st_blocks = st->st_blocks;
    135   1.1  christos 	ost->st_flags = st->st_flags;
    136   1.1  christos 	ost->st_gen = st->st_gen;
    137   1.1  christos }
    138   1.1  christos 
    139   1.1  christos /*
    140   1.1  christos  * Get file status; this version follows links.
    141   1.1  christos  */
    142   1.1  christos /* ARGSUSED */
    143   1.1  christos int
    144  1.45       dsl compat_43_sys_stat(struct lwp *l, const struct compat_43_sys_stat_args *uap, register_t *retval)
    145   1.2   thorpej {
    146  1.45       dsl 	/* {
    147   1.1  christos 		syscallarg(char *) path;
    148   1.8  christos 		syscallarg(struct stat43 *) ub;
    149  1.45       dsl 	} */
    150   1.1  christos 	struct stat sb;
    151   1.8  christos 	struct stat43 osb;
    152   1.1  christos 	int error;
    153   1.1  christos 
    154  1.46        ad 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
    155   1.1  christos 	if (error)
    156   1.1  christos 		return (error);
    157   1.1  christos 	cvtstat(&sb, &osb);
    158  1.36  christos 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    159   1.1  christos 	return (error);
    160   1.1  christos }
    161   1.1  christos 
    162   1.1  christos /*
    163   1.1  christos  * Get file status; this version does not follow links.
    164   1.1  christos  */
    165   1.1  christos /* ARGSUSED */
    166   1.1  christos int
    167  1.45       dsl compat_43_sys_lstat(struct lwp *l, const struct compat_43_sys_lstat_args *uap, register_t *retval)
    168   1.2   thorpej {
    169  1.45       dsl 	/* {
    170   1.1  christos 		syscallarg(char *) path;
    171  1.10      fvdl 		syscallarg(struct ostat *) ub;
    172  1.45       dsl 	} */
    173  1.10      fvdl 	struct vnode *vp, *dvp;
    174  1.10      fvdl 	struct stat sb, sb1;
    175   1.8  christos 	struct stat43 osb;
    176   1.1  christos 	int error;
    177  1.54  dholland 	struct pathbuf *pb;
    178   1.1  christos 	struct nameidata nd;
    179  1.14        pk 	int ndflags;
    180   1.1  christos 
    181  1.54  dholland 	error = pathbuf_copyin(SCARG(uap, path), &pb);
    182  1.54  dholland 	if (error) {
    183  1.54  dholland 		return error;
    184  1.54  dholland 	}
    185  1.54  dholland 
    186  1.38       dsl 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT;
    187  1.14        pk again:
    188  1.54  dholland 	NDINIT(&nd, LOOKUP, ndflags, pb);
    189  1.14        pk 	if ((error = namei(&nd))) {
    190  1.14        pk 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
    191  1.14        pk 			/*
    192  1.14        pk 			 * Should only happen on '/'. Retry without LOCKPARENT;
    193  1.14        pk 			 * this is safe since the vnode won't be a VLNK.
    194  1.14        pk 			 */
    195  1.14        pk 			ndflags &= ~LOCKPARENT;
    196  1.14        pk 			goto again;
    197  1.14        pk 		}
    198  1.54  dholland 		pathbuf_destroy(pb);
    199   1.5  christos 		return (error);
    200  1.14        pk 	}
    201  1.10      fvdl 	/*
    202  1.10      fvdl 	 * For symbolic links, always return the attributes of its
    203  1.10      fvdl 	 * containing directory, except for mode, size, and links.
    204  1.10      fvdl 	 */
    205  1.10      fvdl 	vp = nd.ni_vp;
    206  1.10      fvdl 	dvp = nd.ni_dvp;
    207  1.54  dholland 	pathbuf_destroy(pb);
    208  1.10      fvdl 	if (vp->v_type != VLNK) {
    209  1.14        pk 		if ((ndflags & LOCKPARENT) != 0) {
    210  1.14        pk 			if (dvp == vp)
    211  1.14        pk 				vrele(dvp);
    212  1.14        pk 			else
    213  1.14        pk 				vput(dvp);
    214  1.14        pk 		}
    215  1.46        ad 		error = vn_stat(vp, &sb);
    216  1.10      fvdl 		vput(vp);
    217  1.10      fvdl 		if (error)
    218  1.10      fvdl 			return (error);
    219  1.10      fvdl 	} else {
    220  1.46        ad 		error = vn_stat(dvp, &sb);
    221  1.10      fvdl 		vput(dvp);
    222  1.10      fvdl 		if (error) {
    223  1.10      fvdl 			vput(vp);
    224  1.10      fvdl 			return (error);
    225  1.10      fvdl 		}
    226  1.46        ad 		error = vn_stat(vp, &sb1);
    227  1.10      fvdl 		vput(vp);
    228  1.10      fvdl 		if (error)
    229  1.10      fvdl 			return (error);
    230  1.10      fvdl 		sb.st_mode &= ~S_IFDIR;
    231  1.10      fvdl 		sb.st_mode |= S_IFLNK;
    232  1.10      fvdl 		sb.st_nlink = sb1.st_nlink;
    233  1.10      fvdl 		sb.st_size = sb1.st_size;
    234  1.10      fvdl 		sb.st_blocks = sb1.st_blocks;
    235  1.10      fvdl 	}
    236   1.1  christos 	cvtstat(&sb, &osb);
    237  1.36  christos 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    238   1.1  christos 	return (error);
    239   1.1  christos }
    240   1.1  christos 
    241   1.1  christos /*
    242   1.1  christos  * Return status information about a file descriptor.
    243   1.1  christos  */
    244   1.1  christos /* ARGSUSED */
    245   1.4  christos int
    246  1.45       dsl compat_43_sys_fstat(struct lwp *l, const struct compat_43_sys_fstat_args *uap, register_t *retval)
    247   1.2   thorpej {
    248  1.45       dsl 	/* {
    249   1.1  christos 		syscallarg(int) fd;
    250   1.8  christos 		syscallarg(struct stat43 *) sb;
    251  1.45       dsl 	} */
    252   1.1  christos 	struct stat ub;
    253   1.8  christos 	struct stat43 oub;
    254   1.1  christos 	int error;
    255   1.1  christos 
    256  1.49     njoly 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    257  1.16  jdolecek 	if (error == 0) {
    258  1.16  jdolecek 		cvtstat(&ub, &oub);
    259  1.36  christos 		error = copyout((void *)&oub, (void *)SCARG(uap, sb),
    260   1.1  christos 		    sizeof (oub));
    261  1.16  jdolecek 	}
    262  1.16  jdolecek 
    263   1.1  christos 	return (error);
    264   1.1  christos }
    265   1.1  christos 
    266   1.1  christos 
    267   1.1  christos /*
    268   1.1  christos  * Truncate a file given a file descriptor.
    269   1.1  christos  */
    270   1.1  christos /* ARGSUSED */
    271   1.1  christos int
    272  1.45       dsl compat_43_sys_ftruncate(struct lwp *l, const struct compat_43_sys_ftruncate_args *uap, register_t *retval)
    273   1.2   thorpej {
    274  1.45       dsl 	/* {
    275   1.1  christos 		syscallarg(int) fd;
    276   1.1  christos 		syscallarg(long) length;
    277  1.45       dsl 	} */
    278   1.3   mycroft 	struct sys_ftruncate_args /* {
    279   1.1  christos 		syscallarg(int) fd;
    280   1.1  christos 		syscallarg(int) pad;
    281   1.1  christos 		syscallarg(off_t) length;
    282   1.1  christos 	} */ nuap;
    283   1.1  christos 
    284   1.1  christos 	SCARG(&nuap, fd) = SCARG(uap, fd);
    285   1.1  christos 	SCARG(&nuap, length) = SCARG(uap, length);
    286  1.22   thorpej 	return (sys_ftruncate(l, &nuap, retval));
    287   1.1  christos }
    288   1.1  christos 
    289   1.1  christos /*
    290   1.1  christos  * Truncate a file given its path name.
    291   1.1  christos  */
    292   1.1  christos /* ARGSUSED */
    293   1.1  christos int
    294  1.45       dsl compat_43_sys_truncate(struct lwp *l, const struct compat_43_sys_truncate_args *uap, register_t *retval)
    295   1.2   thorpej {
    296  1.45       dsl 	/* {
    297   1.1  christos 		syscallarg(char *) path;
    298   1.1  christos 		syscallarg(long) length;
    299  1.45       dsl 	} */
    300   1.3   mycroft 	struct sys_truncate_args /* {
    301   1.1  christos 		syscallarg(char *) path;
    302   1.1  christos 		syscallarg(int) pad;
    303   1.1  christos 		syscallarg(off_t) length;
    304   1.1  christos 	} */ nuap;
    305   1.1  christos 
    306   1.1  christos 	SCARG(&nuap, path) = SCARG(uap, path);
    307   1.1  christos 	SCARG(&nuap, length) = SCARG(uap, length);
    308  1.22   thorpej 	return (sys_truncate(l, &nuap, retval));
    309   1.1  christos }
    310   1.1  christos 
    311   1.1  christos 
    312   1.1  christos /*
    313   1.1  christos  * Reposition read/write file offset.
    314   1.1  christos  */
    315   1.1  christos int
    316  1.45       dsl compat_43_sys_lseek(struct lwp *l, const struct compat_43_sys_lseek_args *uap, register_t *retval)
    317   1.2   thorpej {
    318  1.45       dsl 	/* {
    319   1.1  christos 		syscallarg(int) fd;
    320   1.1  christos 		syscallarg(long) offset;
    321   1.1  christos 		syscallarg(int) whence;
    322  1.45       dsl 	} */
    323   1.3   mycroft 	struct sys_lseek_args /* {
    324   1.1  christos 		syscallarg(int) fd;
    325   1.1  christos 		syscallarg(int) pad;
    326   1.1  christos 		syscallarg(off_t) offset;
    327   1.1  christos 		syscallarg(int) whence;
    328   1.1  christos 	} */ nuap;
    329   1.1  christos 	off_t qret;
    330   1.1  christos 	int error;
    331   1.1  christos 
    332   1.1  christos 	SCARG(&nuap, fd) = SCARG(uap, fd);
    333   1.1  christos 	SCARG(&nuap, offset) = SCARG(uap, offset);
    334   1.1  christos 	SCARG(&nuap, whence) = SCARG(uap, whence);
    335  1.57      matt 	error = sys_lseek(l, &nuap, (register_t *)&qret);
    336   1.1  christos 	*(long *)retval = qret;
    337   1.1  christos 	return (error);
    338   1.1  christos }
    339   1.1  christos 
    340   1.1  christos 
    341   1.1  christos /*
    342   1.1  christos  * Create a file.
    343   1.1  christos  */
    344   1.1  christos int
    345  1.45       dsl compat_43_sys_creat(struct lwp *l, const struct compat_43_sys_creat_args *uap, register_t *retval)
    346   1.2   thorpej {
    347  1.45       dsl 	/* {
    348   1.1  christos 		syscallarg(char *) path;
    349   1.1  christos 		syscallarg(int) mode;
    350  1.45       dsl 	} */
    351   1.3   mycroft 	struct sys_open_args /* {
    352   1.1  christos 		syscallarg(char *) path;
    353   1.1  christos 		syscallarg(int) flags;
    354   1.1  christos 		syscallarg(int) mode;
    355   1.1  christos 	} */ nuap;
    356   1.1  christos 
    357   1.1  christos 	SCARG(&nuap, path) = SCARG(uap, path);
    358   1.1  christos 	SCARG(&nuap, mode) = SCARG(uap, mode);
    359   1.1  christos 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    360  1.22   thorpej 	return (sys_open(l, &nuap, retval));
    361   1.1  christos }
    362   1.1  christos 
    363   1.1  christos /*ARGSUSED*/
    364   1.1  christos int
    365  1.45       dsl compat_43_sys_quota(struct lwp *l, const void *v, register_t *retval)
    366   1.1  christos {
    367   1.1  christos 
    368   1.1  christos 	return (ENOSYS);
    369   1.1  christos }
    370   1.1  christos 
    371   1.1  christos 
    372   1.1  christos /*
    373   1.1  christos  * Read a block of directory entries in a file system independent format.
    374   1.1  christos  */
    375   1.1  christos int
    376  1.45       dsl compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval)
    377   1.2   thorpej {
    378  1.45       dsl 	/* {
    379   1.1  christos 		syscallarg(int) fd;
    380   1.1  christos 		syscallarg(char *) buf;
    381   1.1  christos 		syscallarg(u_int) count;
    382   1.1  christos 		syscallarg(long *) basep;
    383  1.45       dsl 	} */
    384  1.56  christos 	struct dirent *bdp;
    385  1.13  augustss 	struct vnode *vp;
    386  1.57      matt 	void *tbuf;			/* Current-format */
    387  1.57      matt 	char *inp;			/* Current-format */
    388  1.56  christos 	int len, reclen;		/* Current-format */
    389  1.56  christos 	char *outp;			/* Dirent12-format */
    390  1.56  christos 	int resid, old_reclen = 0;	/* Dirent12-format */
    391   1.1  christos 	struct file *fp;
    392  1.56  christos 	struct uio auio;
    393  1.56  christos 	struct iovec aiov;
    394  1.56  christos 	struct dirent43 idb;
    395  1.56  christos 	off_t off;		/* true file offset */
    396  1.56  christos 	int buflen, error, eofflag, nbytes;
    397  1.56  christos 	struct vattr va;
    398  1.56  christos 	off_t *cookiebuf = NULL, *cookie;
    399  1.56  christos 	int ncookies;
    400   1.1  christos 	long loff;
    401  1.56  christos 
    402  1.47        ad 	/* fd_getvnode() will use the descriptor for us */
    403  1.47        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    404   1.1  christos 		return (error);
    405  1.56  christos 
    406  1.12   thorpej 	if ((fp->f_flag & FREAD) == 0) {
    407  1.12   thorpej 		error = EBADF;
    408  1.56  christos 		goto out1;
    409  1.12   thorpej 	}
    410  1.56  christos 
    411  1.57      matt 	vp = fp->f_vnode;
    412  1.12   thorpej 	if (vp->v_type != VDIR) {
    413  1.56  christos 		error = ENOTDIR;
    414  1.56  christos 		goto out1;
    415  1.12   thorpej 	}
    416  1.56  christos 
    417  1.56  christos 	vn_lock(vp, LK_SHARED | LK_RETRY);
    418  1.56  christos 	error = VOP_GETATTR(vp, &va, l->l_cred);
    419  1.56  christos 	VOP_UNLOCK(vp);
    420  1.56  christos 	if (error)
    421  1.56  christos 		goto out1;
    422  1.56  christos 
    423  1.56  christos 	loff = fp->f_offset;
    424  1.56  christos 	nbytes = SCARG(uap, count);
    425  1.63  riastrad 	buflen = uimin(MAXBSIZE, nbytes);
    426  1.56  christos 	if (buflen < va.va_blocksize)
    427  1.56  christos 		buflen = va.va_blocksize;
    428  1.56  christos 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    429  1.56  christos 
    430  1.56  christos 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    431  1.56  christos 	off = fp->f_offset;
    432  1.56  christos again:
    433  1.56  christos 	aiov.iov_base = tbuf;
    434  1.56  christos 	aiov.iov_len = buflen;
    435   1.1  christos 	auio.uio_iov = &aiov;
    436   1.1  christos 	auio.uio_iovcnt = 1;
    437   1.1  christos 	auio.uio_rw = UIO_READ;
    438  1.56  christos 	auio.uio_resid = buflen;
    439  1.56  christos 	auio.uio_offset = off;
    440  1.56  christos 	UIO_SETUP_SYSSPACE(&auio);
    441  1.56  christos 	/*
    442  1.56  christos          * First we read into the malloc'ed buffer, then
    443  1.56  christos          * we massage it into user space, one record at a time.
    444  1.56  christos          */
    445  1.56  christos 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    446  1.56  christos 	    &ncookies);
    447  1.56  christos 	if (error)
    448  1.56  christos 		goto out;
    449  1.56  christos 
    450  1.57      matt 	inp = (char *)tbuf;
    451  1.56  christos 	outp = SCARG(uap, buf);
    452  1.56  christos 	resid = nbytes;
    453  1.56  christos 	if ((len = buflen - auio.uio_resid) == 0)
    454  1.56  christos 		goto eof;
    455  1.56  christos 
    456  1.56  christos 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    457  1.56  christos 		bdp = (struct dirent *)inp;
    458  1.56  christos 		reclen = bdp->d_reclen;
    459  1.60  riastrad 		if (reclen & 3) {
    460  1.60  riastrad 			error = EIO;
    461  1.60  riastrad 			goto out;
    462  1.60  riastrad 		}
    463  1.56  christos 		if (bdp->d_fileno == 0) {
    464  1.56  christos 			inp += reclen;	/* it is a hole; squish it out */
    465  1.56  christos 			if (cookie)
    466  1.56  christos 				off = *cookie++;
    467  1.56  christos 			else
    468  1.56  christos 				off += reclen;
    469  1.56  christos 			continue;
    470  1.56  christos 		}
    471  1.62  christos 		if (bdp->d_namlen >= sizeof(idb.d_name))
    472  1.62  christos 			idb.d_namlen = sizeof(idb.d_name) - 1;
    473  1.62  christos 		else
    474  1.62  christos 			idb.d_namlen = bdp->d_namlen;
    475  1.56  christos 		old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen);
    476  1.56  christos 		if (reclen > len || resid < old_reclen) {
    477  1.56  christos 			/* entry too big for buffer, so just stop */
    478  1.56  christos 			outp++;
    479  1.56  christos 			break;
    480   1.1  christos 		}
    481  1.56  christos 		/*
    482  1.56  christos 		 * Massage in place to make a Dirent12-shaped dirent (otherwise
    483  1.56  christos 		 * we have to worry about touching user memory outside of
    484  1.56  christos 		 * the copyout() call).
    485  1.56  christos 		 */
    486  1.56  christos 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    487  1.56  christos 		idb.d_reclen = (uint16_t)old_reclen;
    488  1.62  christos 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    489  1.62  christos 		(void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
    490  1.62  christos 		memset(idb.d_name + idb.d_namlen, 0,
    491  1.62  christos 		    idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
    492  1.56  christos 		if ((error = copyout(&idb, outp, old_reclen)))
    493  1.56  christos 			goto out;
    494  1.56  christos 		/* advance past this real entry */
    495  1.56  christos 		inp += reclen;
    496  1.56  christos 		if (cookie)
    497  1.56  christos 			off = *cookie++; /* each entry points to itself */
    498  1.56  christos 		else
    499  1.56  christos 			off += reclen;
    500  1.56  christos 		/* advance output past Dirent12-shaped entry */
    501  1.56  christos 		outp += old_reclen;
    502  1.56  christos 		resid -= old_reclen;
    503   1.1  christos 	}
    504   1.1  christos 
    505  1.56  christos 	/* if we squished out the whole block, try again */
    506  1.56  christos 	if (outp == SCARG(uap, buf)) {
    507  1.56  christos 		if (cookiebuf)
    508  1.56  christos 			free(cookiebuf, M_TEMP);
    509  1.56  christos 		cookiebuf = NULL;
    510  1.56  christos 		goto again;
    511   1.1  christos 	}
    512  1.56  christos 	fp->f_offset = off;	/* update the vnode offset */
    513  1.56  christos 
    514  1.56  christos eof:
    515  1.56  christos 	*retval = nbytes - resid;
    516  1.56  christos out:
    517  1.56  christos 	VOP_UNLOCK(vp);
    518  1.56  christos 	if (cookiebuf)
    519  1.56  christos 		free(cookiebuf, M_TEMP);
    520  1.56  christos 	free(tbuf, M_TEMP);
    521  1.56  christos out1:
    522  1.46        ad 	fd_putfile(SCARG(uap, fd));
    523  1.56  christos 	if (error)
    524  1.56  christos 		return error;
    525  1.56  christos 	return copyout(&loff, SCARG(uap, basep), sizeof(long));
    526   1.1  christos }
    527  1.30  christos 
    528  1.64  pgoyette int
    529  1.64  pgoyette vfs_syscalls_43_init(void)
    530  1.64  pgoyette {
    531  1.64  pgoyette 
    532  1.64  pgoyette 	return syscall_establish(NULL, vfs_syscalls_43_syscalls);
    533  1.64  pgoyette }
    534  1.48        ad 
    535  1.64  pgoyette int
    536  1.64  pgoyette vfs_syscalls_43_fini(void)
    537  1.30  christos {
    538  1.30  christos 
    539  1.64  pgoyette 	return syscall_disestablish(NULL, vfs_syscalls_43_syscalls);
    540  1.30  christos }
    541