Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_43.c revision 1.60
      1  1.60  riastrad /*	$NetBSD: vfs_syscalls_43.c,v 1.60 2017/07/28 15:34:06 riastradh 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.60  riastrad __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.60 2017/07/28 15:34:06 riastradh 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.55  pgoyette #include <sys/sysctl.h>
     62   1.1  christos #include <sys/syslog.h>
     63   1.1  christos #include <sys/unistd.h>
     64   1.1  christos #include <sys/resourcevar.h>
     65  1.30  christos #include <sys/sysctl.h>
     66   1.1  christos 
     67   1.1  christos #include <sys/mount.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.1  christos /*
     82  1.58       mrg  * Convert from an old to a new timespec structure.
     83  1.58       mrg  */
     84  1.58       mrg static void
     85  1.58       mrg cvttimespec(struct timespec *ts, struct timespec50 *ots)
     86  1.58       mrg {
     87  1.58       mrg 
     88  1.58       mrg 	if (ts->tv_sec > INT_MAX) {
     89  1.58       mrg #if defined(DEBUG) || 1
     90  1.58       mrg 		static bool first = true;
     91  1.58       mrg 
     92  1.58       mrg 		if (first) {
     93  1.58       mrg 			first = false;
     94  1.58       mrg 			printf("%s[%s:%d]: time_t does not fit\n",
     95  1.58       mrg 			    __func__, curlwp->l_proc->p_comm,
     96  1.58       mrg 			    curlwp->l_lid);
     97  1.58       mrg 		}
     98  1.58       mrg #endif
     99  1.58       mrg 		ots->tv_sec = INT_MAX;
    100  1.58       mrg 	} else
    101  1.58       mrg 		ots->tv_sec = ts->tv_sec;
    102  1.58       mrg 	ots->tv_nsec = ts->tv_nsec;
    103  1.58       mrg }
    104  1.58       mrg 
    105  1.58       mrg /*
    106   1.1  christos  * Convert from an old to a new stat structure.
    107   1.1  christos  */
    108   1.4  christos static void
    109  1.43       dsl cvtstat(struct stat *st, struct stat43 *ost)
    110   1.1  christos {
    111   1.1  christos 
    112  1.58       mrg 	/* Handle any padding. */
    113  1.58       mrg 	memset(ost, 0, sizeof *ost);
    114   1.1  christos 	ost->st_dev = st->st_dev;
    115   1.1  christos 	ost->st_ino = st->st_ino;
    116  1.11  wrstuden 	ost->st_mode = st->st_mode & 0xffff;
    117   1.1  christos 	ost->st_nlink = st->st_nlink;
    118   1.1  christos 	ost->st_uid = st->st_uid;
    119   1.1  christos 	ost->st_gid = st->st_gid;
    120   1.1  christos 	ost->st_rdev = st->st_rdev;
    121   1.1  christos 	if (st->st_size < (quad_t)1 << 32)
    122   1.1  christos 		ost->st_size = st->st_size;
    123   1.1  christos 	else
    124   1.1  christos 		ost->st_size = -2;
    125  1.58       mrg 	cvttimespec(&st->st_atimespec, &ost->st_atimespec);
    126  1.58       mrg 	cvttimespec(&st->st_mtimespec, &ost->st_mtimespec);
    127  1.58       mrg 	cvttimespec(&st->st_ctimespec, &ost->st_ctimespec);
    128   1.1  christos 	ost->st_blksize = st->st_blksize;
    129   1.1  christos 	ost->st_blocks = st->st_blocks;
    130   1.1  christos 	ost->st_flags = st->st_flags;
    131   1.1  christos 	ost->st_gen = st->st_gen;
    132   1.1  christos }
    133   1.1  christos 
    134   1.1  christos /*
    135   1.1  christos  * Get file status; this version follows links.
    136   1.1  christos  */
    137   1.1  christos /* ARGSUSED */
    138   1.1  christos int
    139  1.45       dsl compat_43_sys_stat(struct lwp *l, const struct compat_43_sys_stat_args *uap, register_t *retval)
    140   1.2   thorpej {
    141  1.45       dsl 	/* {
    142   1.1  christos 		syscallarg(char *) path;
    143   1.8  christos 		syscallarg(struct stat43 *) ub;
    144  1.45       dsl 	} */
    145   1.1  christos 	struct stat sb;
    146   1.8  christos 	struct stat43 osb;
    147   1.1  christos 	int error;
    148   1.1  christos 
    149  1.46        ad 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
    150   1.1  christos 	if (error)
    151   1.1  christos 		return (error);
    152   1.1  christos 	cvtstat(&sb, &osb);
    153  1.36  christos 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    154   1.1  christos 	return (error);
    155   1.1  christos }
    156   1.1  christos 
    157   1.1  christos /*
    158   1.1  christos  * Get file status; this version does not follow links.
    159   1.1  christos  */
    160   1.1  christos /* ARGSUSED */
    161   1.1  christos int
    162  1.45       dsl compat_43_sys_lstat(struct lwp *l, const struct compat_43_sys_lstat_args *uap, register_t *retval)
    163   1.2   thorpej {
    164  1.45       dsl 	/* {
    165   1.1  christos 		syscallarg(char *) path;
    166  1.10      fvdl 		syscallarg(struct ostat *) ub;
    167  1.45       dsl 	} */
    168  1.10      fvdl 	struct vnode *vp, *dvp;
    169  1.10      fvdl 	struct stat sb, sb1;
    170   1.8  christos 	struct stat43 osb;
    171   1.1  christos 	int error;
    172  1.54  dholland 	struct pathbuf *pb;
    173   1.1  christos 	struct nameidata nd;
    174  1.14        pk 	int ndflags;
    175   1.1  christos 
    176  1.54  dholland 	error = pathbuf_copyin(SCARG(uap, path), &pb);
    177  1.54  dholland 	if (error) {
    178  1.54  dholland 		return error;
    179  1.54  dholland 	}
    180  1.54  dholland 
    181  1.38       dsl 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT | TRYEMULROOT;
    182  1.14        pk again:
    183  1.54  dholland 	NDINIT(&nd, LOOKUP, ndflags, pb);
    184  1.14        pk 	if ((error = namei(&nd))) {
    185  1.14        pk 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
    186  1.14        pk 			/*
    187  1.14        pk 			 * Should only happen on '/'. Retry without LOCKPARENT;
    188  1.14        pk 			 * this is safe since the vnode won't be a VLNK.
    189  1.14        pk 			 */
    190  1.14        pk 			ndflags &= ~LOCKPARENT;
    191  1.14        pk 			goto again;
    192  1.14        pk 		}
    193  1.54  dholland 		pathbuf_destroy(pb);
    194   1.5  christos 		return (error);
    195  1.14        pk 	}
    196  1.10      fvdl 	/*
    197  1.10      fvdl 	 * For symbolic links, always return the attributes of its
    198  1.10      fvdl 	 * containing directory, except for mode, size, and links.
    199  1.10      fvdl 	 */
    200  1.10      fvdl 	vp = nd.ni_vp;
    201  1.10      fvdl 	dvp = nd.ni_dvp;
    202  1.54  dholland 	pathbuf_destroy(pb);
    203  1.10      fvdl 	if (vp->v_type != VLNK) {
    204  1.14        pk 		if ((ndflags & LOCKPARENT) != 0) {
    205  1.14        pk 			if (dvp == vp)
    206  1.14        pk 				vrele(dvp);
    207  1.14        pk 			else
    208  1.14        pk 				vput(dvp);
    209  1.14        pk 		}
    210  1.46        ad 		error = vn_stat(vp, &sb);
    211  1.10      fvdl 		vput(vp);
    212  1.10      fvdl 		if (error)
    213  1.10      fvdl 			return (error);
    214  1.10      fvdl 	} else {
    215  1.46        ad 		error = vn_stat(dvp, &sb);
    216  1.10      fvdl 		vput(dvp);
    217  1.10      fvdl 		if (error) {
    218  1.10      fvdl 			vput(vp);
    219  1.10      fvdl 			return (error);
    220  1.10      fvdl 		}
    221  1.46        ad 		error = vn_stat(vp, &sb1);
    222  1.10      fvdl 		vput(vp);
    223  1.10      fvdl 		if (error)
    224  1.10      fvdl 			return (error);
    225  1.10      fvdl 		sb.st_mode &= ~S_IFDIR;
    226  1.10      fvdl 		sb.st_mode |= S_IFLNK;
    227  1.10      fvdl 		sb.st_nlink = sb1.st_nlink;
    228  1.10      fvdl 		sb.st_size = sb1.st_size;
    229  1.10      fvdl 		sb.st_blocks = sb1.st_blocks;
    230  1.10      fvdl 	}
    231   1.1  christos 	cvtstat(&sb, &osb);
    232  1.36  christos 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
    233   1.1  christos 	return (error);
    234   1.1  christos }
    235   1.1  christos 
    236   1.1  christos /*
    237   1.1  christos  * Return status information about a file descriptor.
    238   1.1  christos  */
    239   1.1  christos /* ARGSUSED */
    240   1.4  christos int
    241  1.45       dsl compat_43_sys_fstat(struct lwp *l, const struct compat_43_sys_fstat_args *uap, register_t *retval)
    242   1.2   thorpej {
    243  1.45       dsl 	/* {
    244   1.1  christos 		syscallarg(int) fd;
    245   1.8  christos 		syscallarg(struct stat43 *) sb;
    246  1.45       dsl 	} */
    247   1.1  christos 	struct stat ub;
    248   1.8  christos 	struct stat43 oub;
    249   1.1  christos 	int error;
    250   1.1  christos 
    251  1.49     njoly 	error = do_sys_fstat(SCARG(uap, fd), &ub);
    252  1.16  jdolecek 	if (error == 0) {
    253  1.16  jdolecek 		cvtstat(&ub, &oub);
    254  1.36  christos 		error = copyout((void *)&oub, (void *)SCARG(uap, sb),
    255   1.1  christos 		    sizeof (oub));
    256  1.16  jdolecek 	}
    257  1.16  jdolecek 
    258   1.1  christos 	return (error);
    259   1.1  christos }
    260   1.1  christos 
    261   1.1  christos 
    262   1.1  christos /*
    263   1.1  christos  * Truncate a file given a file descriptor.
    264   1.1  christos  */
    265   1.1  christos /* ARGSUSED */
    266   1.1  christos int
    267  1.45       dsl compat_43_sys_ftruncate(struct lwp *l, const struct compat_43_sys_ftruncate_args *uap, register_t *retval)
    268   1.2   thorpej {
    269  1.45       dsl 	/* {
    270   1.1  christos 		syscallarg(int) fd;
    271   1.1  christos 		syscallarg(long) length;
    272  1.45       dsl 	} */
    273   1.3   mycroft 	struct sys_ftruncate_args /* {
    274   1.1  christos 		syscallarg(int) fd;
    275   1.1  christos 		syscallarg(int) pad;
    276   1.1  christos 		syscallarg(off_t) length;
    277   1.1  christos 	} */ nuap;
    278   1.1  christos 
    279   1.1  christos 	SCARG(&nuap, fd) = SCARG(uap, fd);
    280   1.1  christos 	SCARG(&nuap, length) = SCARG(uap, length);
    281  1.22   thorpej 	return (sys_ftruncate(l, &nuap, retval));
    282   1.1  christos }
    283   1.1  christos 
    284   1.1  christos /*
    285   1.1  christos  * Truncate a file given its path name.
    286   1.1  christos  */
    287   1.1  christos /* ARGSUSED */
    288   1.1  christos int
    289  1.45       dsl compat_43_sys_truncate(struct lwp *l, const struct compat_43_sys_truncate_args *uap, register_t *retval)
    290   1.2   thorpej {
    291  1.45       dsl 	/* {
    292   1.1  christos 		syscallarg(char *) path;
    293   1.1  christos 		syscallarg(long) length;
    294  1.45       dsl 	} */
    295   1.3   mycroft 	struct sys_truncate_args /* {
    296   1.1  christos 		syscallarg(char *) path;
    297   1.1  christos 		syscallarg(int) pad;
    298   1.1  christos 		syscallarg(off_t) length;
    299   1.1  christos 	} */ nuap;
    300   1.1  christos 
    301   1.1  christos 	SCARG(&nuap, path) = SCARG(uap, path);
    302   1.1  christos 	SCARG(&nuap, length) = SCARG(uap, length);
    303  1.22   thorpej 	return (sys_truncate(l, &nuap, retval));
    304   1.1  christos }
    305   1.1  christos 
    306   1.1  christos 
    307   1.1  christos /*
    308   1.1  christos  * Reposition read/write file offset.
    309   1.1  christos  */
    310   1.1  christos int
    311  1.45       dsl compat_43_sys_lseek(struct lwp *l, const struct compat_43_sys_lseek_args *uap, register_t *retval)
    312   1.2   thorpej {
    313  1.45       dsl 	/* {
    314   1.1  christos 		syscallarg(int) fd;
    315   1.1  christos 		syscallarg(long) offset;
    316   1.1  christos 		syscallarg(int) whence;
    317  1.45       dsl 	} */
    318   1.3   mycroft 	struct sys_lseek_args /* {
    319   1.1  christos 		syscallarg(int) fd;
    320   1.1  christos 		syscallarg(int) pad;
    321   1.1  christos 		syscallarg(off_t) offset;
    322   1.1  christos 		syscallarg(int) whence;
    323   1.1  christos 	} */ nuap;
    324   1.1  christos 	off_t qret;
    325   1.1  christos 	int error;
    326   1.1  christos 
    327   1.1  christos 	SCARG(&nuap, fd) = SCARG(uap, fd);
    328   1.1  christos 	SCARG(&nuap, offset) = SCARG(uap, offset);
    329   1.1  christos 	SCARG(&nuap, whence) = SCARG(uap, whence);
    330  1.57      matt 	error = sys_lseek(l, &nuap, (register_t *)&qret);
    331   1.1  christos 	*(long *)retval = qret;
    332   1.1  christos 	return (error);
    333   1.1  christos }
    334   1.1  christos 
    335   1.1  christos 
    336   1.1  christos /*
    337   1.1  christos  * Create a file.
    338   1.1  christos  */
    339   1.1  christos int
    340  1.45       dsl compat_43_sys_creat(struct lwp *l, const struct compat_43_sys_creat_args *uap, register_t *retval)
    341   1.2   thorpej {
    342  1.45       dsl 	/* {
    343   1.1  christos 		syscallarg(char *) path;
    344   1.1  christos 		syscallarg(int) mode;
    345  1.45       dsl 	} */
    346   1.3   mycroft 	struct sys_open_args /* {
    347   1.1  christos 		syscallarg(char *) path;
    348   1.1  christos 		syscallarg(int) flags;
    349   1.1  christos 		syscallarg(int) mode;
    350   1.1  christos 	} */ nuap;
    351   1.1  christos 
    352   1.1  christos 	SCARG(&nuap, path) = SCARG(uap, path);
    353   1.1  christos 	SCARG(&nuap, mode) = SCARG(uap, mode);
    354   1.1  christos 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    355  1.22   thorpej 	return (sys_open(l, &nuap, retval));
    356   1.1  christos }
    357   1.1  christos 
    358   1.1  christos /*ARGSUSED*/
    359   1.1  christos int
    360  1.45       dsl compat_43_sys_quota(struct lwp *l, const void *v, register_t *retval)
    361   1.1  christos {
    362   1.1  christos 
    363   1.1  christos 	return (ENOSYS);
    364   1.1  christos }
    365   1.1  christos 
    366   1.1  christos 
    367   1.1  christos /*
    368   1.1  christos  * Read a block of directory entries in a file system independent format.
    369   1.1  christos  */
    370   1.1  christos int
    371  1.45       dsl compat_43_sys_getdirentries(struct lwp *l, const struct compat_43_sys_getdirentries_args *uap, register_t *retval)
    372   1.2   thorpej {
    373  1.45       dsl 	/* {
    374   1.1  christos 		syscallarg(int) fd;
    375   1.1  christos 		syscallarg(char *) buf;
    376   1.1  christos 		syscallarg(u_int) count;
    377   1.1  christos 		syscallarg(long *) basep;
    378  1.45       dsl 	} */
    379  1.56  christos 	struct dirent *bdp;
    380  1.13  augustss 	struct vnode *vp;
    381  1.57      matt 	void *tbuf;			/* Current-format */
    382  1.57      matt 	char *inp;			/* Current-format */
    383  1.56  christos 	int len, reclen;		/* Current-format */
    384  1.56  christos 	char *outp;			/* Dirent12-format */
    385  1.56  christos 	int resid, old_reclen = 0;	/* Dirent12-format */
    386   1.1  christos 	struct file *fp;
    387  1.56  christos 	struct uio auio;
    388  1.56  christos 	struct iovec aiov;
    389  1.56  christos 	struct dirent43 idb;
    390  1.56  christos 	off_t off;		/* true file offset */
    391  1.56  christos 	int buflen, error, eofflag, nbytes;
    392  1.56  christos 	struct vattr va;
    393  1.56  christos 	off_t *cookiebuf = NULL, *cookie;
    394  1.56  christos 	int ncookies;
    395   1.1  christos 	long loff;
    396  1.56  christos 
    397  1.47        ad 	/* fd_getvnode() will use the descriptor for us */
    398  1.47        ad 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
    399   1.1  christos 		return (error);
    400  1.56  christos 
    401  1.12   thorpej 	if ((fp->f_flag & FREAD) == 0) {
    402  1.12   thorpej 		error = EBADF;
    403  1.56  christos 		goto out1;
    404  1.12   thorpej 	}
    405  1.56  christos 
    406  1.57      matt 	vp = fp->f_vnode;
    407  1.12   thorpej 	if (vp->v_type != VDIR) {
    408  1.56  christos 		error = ENOTDIR;
    409  1.56  christos 		goto out1;
    410  1.12   thorpej 	}
    411  1.56  christos 
    412  1.56  christos 	vn_lock(vp, LK_SHARED | LK_RETRY);
    413  1.56  christos 	error = VOP_GETATTR(vp, &va, l->l_cred);
    414  1.56  christos 	VOP_UNLOCK(vp);
    415  1.56  christos 	if (error)
    416  1.56  christos 		goto out1;
    417  1.56  christos 
    418  1.56  christos 	loff = fp->f_offset;
    419  1.56  christos 	nbytes = SCARG(uap, count);
    420  1.56  christos 	buflen = min(MAXBSIZE, nbytes);
    421  1.56  christos 	if (buflen < va.va_blocksize)
    422  1.56  christos 		buflen = va.va_blocksize;
    423  1.56  christos 	tbuf = malloc(buflen, M_TEMP, M_WAITOK);
    424  1.56  christos 
    425  1.56  christos 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    426  1.56  christos 	off = fp->f_offset;
    427  1.56  christos again:
    428  1.56  christos 	aiov.iov_base = tbuf;
    429  1.56  christos 	aiov.iov_len = buflen;
    430   1.1  christos 	auio.uio_iov = &aiov;
    431   1.1  christos 	auio.uio_iovcnt = 1;
    432   1.1  christos 	auio.uio_rw = UIO_READ;
    433  1.56  christos 	auio.uio_resid = buflen;
    434  1.56  christos 	auio.uio_offset = off;
    435  1.56  christos 	UIO_SETUP_SYSSPACE(&auio);
    436  1.56  christos 	/*
    437  1.56  christos          * First we read into the malloc'ed buffer, then
    438  1.56  christos          * we massage it into user space, one record at a time.
    439  1.56  christos          */
    440  1.56  christos 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    441  1.56  christos 	    &ncookies);
    442  1.56  christos 	if (error)
    443  1.56  christos 		goto out;
    444  1.56  christos 
    445  1.57      matt 	inp = (char *)tbuf;
    446  1.56  christos 	outp = SCARG(uap, buf);
    447  1.56  christos 	resid = nbytes;
    448  1.56  christos 	if ((len = buflen - auio.uio_resid) == 0)
    449  1.56  christos 		goto eof;
    450  1.56  christos 
    451  1.56  christos 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    452  1.56  christos 		bdp = (struct dirent *)inp;
    453  1.56  christos 		reclen = bdp->d_reclen;
    454  1.60  riastrad 		if (reclen & 3) {
    455  1.60  riastrad 			error = EIO;
    456  1.60  riastrad 			goto out;
    457  1.60  riastrad 		}
    458  1.56  christos 		if (bdp->d_fileno == 0) {
    459  1.56  christos 			inp += reclen;	/* it is a hole; squish it out */
    460  1.56  christos 			if (cookie)
    461  1.56  christos 				off = *cookie++;
    462  1.56  christos 			else
    463  1.56  christos 				off += reclen;
    464  1.56  christos 			continue;
    465  1.56  christos 		}
    466  1.56  christos 		old_reclen = _DIRENT_RECLEN(&idb, bdp->d_namlen);
    467  1.56  christos 		if (reclen > len || resid < old_reclen) {
    468  1.56  christos 			/* entry too big for buffer, so just stop */
    469  1.56  christos 			outp++;
    470  1.56  christos 			break;
    471   1.1  christos 		}
    472  1.56  christos 		/*
    473  1.56  christos 		 * Massage in place to make a Dirent12-shaped dirent (otherwise
    474  1.56  christos 		 * we have to worry about touching user memory outside of
    475  1.56  christos 		 * the copyout() call).
    476  1.56  christos 		 */
    477  1.56  christos 		idb.d_fileno = (uint32_t)bdp->d_fileno;
    478  1.56  christos 		idb.d_reclen = (uint16_t)old_reclen;
    479  1.56  christos 		idb.d_namlen = (uint16_t)bdp->d_namlen;
    480  1.59  christos 		memcpy(idb.d_name, bdp->d_name, MIN(sizeof(idb.d_name),
    481  1.59  christos 		    idb.d_namlen));
    482  1.56  christos 		if ((error = copyout(&idb, outp, old_reclen)))
    483  1.56  christos 			goto out;
    484  1.56  christos 		/* advance past this real entry */
    485  1.56  christos 		inp += reclen;
    486  1.56  christos 		if (cookie)
    487  1.56  christos 			off = *cookie++; /* each entry points to itself */
    488  1.56  christos 		else
    489  1.56  christos 			off += reclen;
    490  1.56  christos 		/* advance output past Dirent12-shaped entry */
    491  1.56  christos 		outp += old_reclen;
    492  1.56  christos 		resid -= old_reclen;
    493   1.1  christos 	}
    494   1.1  christos 
    495  1.56  christos 	/* if we squished out the whole block, try again */
    496  1.56  christos 	if (outp == SCARG(uap, buf)) {
    497  1.56  christos 		if (cookiebuf)
    498  1.56  christos 			free(cookiebuf, M_TEMP);
    499  1.56  christos 		cookiebuf = NULL;
    500  1.56  christos 		goto again;
    501   1.1  christos 	}
    502  1.56  christos 	fp->f_offset = off;	/* update the vnode offset */
    503  1.56  christos 
    504  1.56  christos eof:
    505  1.56  christos 	*retval = nbytes - resid;
    506  1.56  christos out:
    507  1.56  christos 	VOP_UNLOCK(vp);
    508  1.56  christos 	if (cookiebuf)
    509  1.56  christos 		free(cookiebuf, M_TEMP);
    510  1.56  christos 	free(tbuf, M_TEMP);
    511  1.56  christos out1:
    512  1.46        ad 	fd_putfile(SCARG(uap, fd));
    513  1.56  christos 	if (error)
    514  1.56  christos 		return error;
    515  1.56  christos 	return copyout(&loff, SCARG(uap, basep), sizeof(long));
    516   1.1  christos }
    517  1.30  christos 
    518  1.30  christos /*
    519  1.30  christos  * sysctl helper routine for vfs.generic.conf lookups.
    520  1.30  christos  */
    521  1.30  christos #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44)
    522  1.48        ad 
    523  1.30  christos static int
    524  1.30  christos sysctl_vfs_generic_conf(SYSCTLFN_ARGS)
    525  1.30  christos {
    526  1.30  christos         struct vfsconf vfc;
    527  1.30  christos         extern const char * const mountcompatnames[];
    528  1.30  christos         extern int nmountcompatnames;
    529  1.30  christos 	struct sysctlnode node;
    530  1.30  christos 	struct vfsops *vfsp;
    531  1.30  christos 	u_int vfsnum;
    532  1.30  christos 
    533  1.30  christos 	if (namelen != 1)
    534  1.30  christos 		return (ENOTDIR);
    535  1.30  christos 	vfsnum = name[0];
    536  1.30  christos 	if (vfsnum >= nmountcompatnames ||
    537  1.30  christos 	    mountcompatnames[vfsnum] == NULL)
    538  1.30  christos 		return (EOPNOTSUPP);
    539  1.30  christos 	vfsp = vfs_getopsbyname(mountcompatnames[vfsnum]);
    540  1.30  christos 	if (vfsp == NULL)
    541  1.30  christos 		return (EOPNOTSUPP);
    542  1.30  christos 
    543  1.30  christos 	vfc.vfc_vfsops = vfsp;
    544  1.39  christos 	strncpy(vfc.vfc_name, vfsp->vfs_name, sizeof(vfc.vfc_name));
    545  1.30  christos 	vfc.vfc_typenum = vfsnum;
    546  1.30  christos 	vfc.vfc_refcount = vfsp->vfs_refcount;
    547  1.30  christos 	vfc.vfc_flags = 0;
    548  1.30  christos 	vfc.vfc_mountroot = vfsp->vfs_mountroot;
    549  1.30  christos 	vfc.vfc_next = NULL;
    550  1.40        ad 	vfs_delref(vfsp);
    551  1.30  christos 
    552  1.30  christos 	node = *rnode;
    553  1.30  christos 	node.sysctl_data = &vfc;
    554  1.30  christos 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    555  1.30  christos }
    556  1.30  christos 
    557  1.30  christos /*
    558  1.30  christos  * Top level filesystem related information gathering.
    559  1.30  christos  */
    560  1.48        ad void
    561  1.55  pgoyette compat_sysctl_vfs(struct sysctllog **clog)
    562  1.30  christos {
    563  1.30  christos 	extern int nmountcompatnames;
    564  1.30  christos 
    565  1.55  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    566  1.30  christos 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    567  1.30  christos 		       CTLTYPE_INT, "maxtypenum",
    568  1.30  christos 		       SYSCTL_DESCR("Highest valid filesystem type number"),
    569  1.30  christos 		       NULL, nmountcompatnames, NULL, 0,
    570  1.30  christos 		       CTL_VFS, VFS_GENERIC, VFS_MAXTYPENUM, CTL_EOL);
    571  1.55  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    572  1.30  christos 		       CTLFLAG_PERMANENT,
    573  1.30  christos 		       CTLTYPE_STRUCT, "conf",
    574  1.30  christos 		       SYSCTL_DESCR("Filesystem configuration information"),
    575  1.30  christos 		       sysctl_vfs_generic_conf, 0, NULL,
    576  1.30  christos 		       sizeof(struct vfsconf),
    577  1.30  christos 		       CTL_VFS, VFS_GENERIC, VFS_CONF, CTL_EOL);
    578  1.30  christos }
    579  1.30  christos #endif
    580