Home | History | Annotate | Line # | Download | only in common
vfs_syscalls_43.c revision 1.32.14.2
      1  1.32.14.2        ad /*	$NetBSD: vfs_syscalls_43.c,v 1.32.14.2 2007/01/30 13:51:30 ad 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.32.14.2        ad __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_43.c,v 1.32.14.2 2007/01/30 13:51:30 ad Exp $");
     41        1.9   thorpej 
     42       1.18       mrg #if defined(_KERNEL_OPT)
     43        1.9   thorpej #include "fs_union.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.30  christos #include <sys/sysctl.h>
     65        1.1  christos 
     66        1.1  christos #include <sys/mount.h>
     67        1.1  christos #include <sys/syscallargs.h>
     68        1.1  christos 
     69       1.30  christos #include <compat/sys/stat.h>
     70       1.30  christos #include <compat/sys/mount.h>
     71       1.30  christos 
     72        1.8  christos static void cvtstat __P((struct stat *, struct stat43 *));
     73        1.4  christos 
     74        1.1  christos /*
     75        1.1  christos  * Convert from an old to a new stat structure.
     76        1.1  christos  */
     77        1.4  christos static void
     78        1.1  christos cvtstat(st, ost)
     79        1.1  christos 	struct stat *st;
     80        1.8  christos 	struct stat43 *ost;
     81        1.1  christos {
     82        1.1  christos 
     83        1.1  christos 	ost->st_dev = st->st_dev;
     84        1.1  christos 	ost->st_ino = st->st_ino;
     85       1.11  wrstuden 	ost->st_mode = st->st_mode & 0xffff;
     86        1.1  christos 	ost->st_nlink = st->st_nlink;
     87        1.1  christos 	ost->st_uid = st->st_uid;
     88        1.1  christos 	ost->st_gid = st->st_gid;
     89        1.1  christos 	ost->st_rdev = st->st_rdev;
     90        1.1  christos 	if (st->st_size < (quad_t)1 << 32)
     91        1.1  christos 		ost->st_size = st->st_size;
     92        1.1  christos 	else
     93        1.1  christos 		ost->st_size = -2;
     94        1.1  christos 	ost->st_atime = st->st_atime;
     95        1.1  christos 	ost->st_mtime = st->st_mtime;
     96        1.1  christos 	ost->st_ctime = st->st_ctime;
     97        1.1  christos 	ost->st_blksize = st->st_blksize;
     98        1.1  christos 	ost->st_blocks = st->st_blocks;
     99        1.1  christos 	ost->st_flags = st->st_flags;
    100        1.1  christos 	ost->st_gen = st->st_gen;
    101        1.1  christos }
    102        1.1  christos 
    103        1.1  christos /*
    104        1.1  christos  * Get file status; this version follows links.
    105        1.1  christos  */
    106        1.1  christos /* ARGSUSED */
    107        1.1  christos int
    108       1.22   thorpej compat_43_sys_stat(struct lwp *l, void *v, register_t *retval)
    109        1.2   thorpej {
    110       1.13  augustss 	struct compat_43_sys_stat_args /* {
    111        1.1  christos 		syscallarg(char *) path;
    112        1.8  christos 		syscallarg(struct stat43 *) ub;
    113        1.2   thorpej 	} */ *uap = v;
    114        1.1  christos 	struct stat sb;
    115        1.8  christos 	struct stat43 osb;
    116        1.1  christos 	int error;
    117        1.1  christos 	struct nameidata nd;
    118        1.1  christos 
    119        1.1  christos 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    120       1.31  christos 	    SCARG(uap, path), l);
    121        1.4  christos 	if ((error = namei(&nd)) != 0)
    122        1.1  christos 		return (error);
    123       1.31  christos 	error = vn_stat(nd.ni_vp, &sb, l);
    124        1.1  christos 	vput(nd.ni_vp);
    125        1.1  christos 	if (error)
    126        1.1  christos 		return (error);
    127        1.1  christos 	cvtstat(&sb, &osb);
    128        1.1  christos 	error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
    129        1.1  christos 	return (error);
    130        1.1  christos }
    131        1.1  christos 
    132        1.1  christos /*
    133        1.1  christos  * Get file status; this version does not follow links.
    134        1.1  christos  */
    135        1.1  christos /* ARGSUSED */
    136        1.1  christos int
    137       1.22   thorpej compat_43_sys_lstat(struct lwp *l, void *v, register_t *retval)
    138        1.2   thorpej {
    139       1.13  augustss 	struct compat_43_sys_lstat_args /* {
    140        1.1  christos 		syscallarg(char *) path;
    141       1.10      fvdl 		syscallarg(struct ostat *) ub;
    142        1.2   thorpej 	} */ *uap = v;
    143       1.10      fvdl 	struct vnode *vp, *dvp;
    144       1.10      fvdl 	struct stat sb, sb1;
    145        1.8  christos 	struct stat43 osb;
    146        1.1  christos 	int error;
    147        1.1  christos 	struct nameidata nd;
    148       1.14        pk 	int ndflags;
    149        1.1  christos 
    150       1.14        pk 	ndflags = NOFOLLOW | LOCKLEAF | LOCKPARENT;
    151       1.14        pk again:
    152       1.31  christos 	NDINIT(&nd, LOOKUP, ndflags, UIO_USERSPACE, SCARG(uap, path), l);
    153       1.14        pk 	if ((error = namei(&nd))) {
    154       1.14        pk 		if (error == EISDIR && (ndflags & LOCKPARENT) != 0) {
    155       1.14        pk 			/*
    156       1.14        pk 			 * Should only happen on '/'. Retry without LOCKPARENT;
    157       1.14        pk 			 * this is safe since the vnode won't be a VLNK.
    158       1.14        pk 			 */
    159       1.14        pk 			ndflags &= ~LOCKPARENT;
    160       1.14        pk 			goto again;
    161       1.14        pk 		}
    162        1.5  christos 		return (error);
    163       1.14        pk 	}
    164       1.10      fvdl 	/*
    165       1.10      fvdl 	 * For symbolic links, always return the attributes of its
    166       1.10      fvdl 	 * containing directory, except for mode, size, and links.
    167       1.10      fvdl 	 */
    168       1.10      fvdl 	vp = nd.ni_vp;
    169       1.10      fvdl 	dvp = nd.ni_dvp;
    170       1.10      fvdl 	if (vp->v_type != VLNK) {
    171       1.14        pk 		if ((ndflags & LOCKPARENT) != 0) {
    172       1.14        pk 			if (dvp == vp)
    173       1.14        pk 				vrele(dvp);
    174       1.14        pk 			else
    175       1.14        pk 				vput(dvp);
    176       1.14        pk 		}
    177       1.31  christos 		error = vn_stat(vp, &sb, l);
    178       1.10      fvdl 		vput(vp);
    179       1.10      fvdl 		if (error)
    180       1.10      fvdl 			return (error);
    181       1.10      fvdl 	} else {
    182       1.31  christos 		error = vn_stat(dvp, &sb, l);
    183       1.10      fvdl 		vput(dvp);
    184       1.10      fvdl 		if (error) {
    185       1.10      fvdl 			vput(vp);
    186       1.10      fvdl 			return (error);
    187       1.10      fvdl 		}
    188       1.31  christos 		error = vn_stat(vp, &sb1, l);
    189       1.10      fvdl 		vput(vp);
    190       1.10      fvdl 		if (error)
    191       1.10      fvdl 			return (error);
    192       1.10      fvdl 		sb.st_mode &= ~S_IFDIR;
    193       1.10      fvdl 		sb.st_mode |= S_IFLNK;
    194       1.10      fvdl 		sb.st_nlink = sb1.st_nlink;
    195       1.10      fvdl 		sb.st_size = sb1.st_size;
    196       1.10      fvdl 		sb.st_blocks = sb1.st_blocks;
    197       1.10      fvdl 	}
    198        1.1  christos 	cvtstat(&sb, &osb);
    199       1.10      fvdl 	error = copyout((caddr_t)&osb, (caddr_t)SCARG(uap, ub), sizeof (osb));
    200        1.1  christos 	return (error);
    201        1.1  christos }
    202        1.1  christos 
    203        1.1  christos /*
    204        1.1  christos  * Return status information about a file descriptor.
    205        1.1  christos  */
    206        1.1  christos /* ARGSUSED */
    207        1.4  christos int
    208       1.22   thorpej compat_43_sys_fstat(struct lwp *l, void *v, register_t *retval)
    209        1.2   thorpej {
    210       1.13  augustss 	struct compat_43_sys_fstat_args /* {
    211        1.1  christos 		syscallarg(int) fd;
    212        1.8  christos 		syscallarg(struct stat43 *) sb;
    213        1.2   thorpej 	} */ *uap = v;
    214       1.22   thorpej 	struct proc *p = l->l_proc;
    215        1.1  christos 	int fd = SCARG(uap, fd);
    216       1.13  augustss 	struct filedesc *fdp = p->p_fd;
    217       1.13  augustss 	struct file *fp;
    218        1.1  christos 	struct stat ub;
    219        1.8  christos 	struct stat43 oub;
    220        1.1  christos 	int error;
    221        1.1  christos 
    222       1.19   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    223        1.1  christos 		return (EBADF);
    224        1.1  christos 
    225       1.16  jdolecek 	FILE_USE(fp);
    226       1.31  christos 	error = (*fp->f_ops->fo_stat)(fp, &ub, l);
    227       1.31  christos 	FILE_UNUSE(fp, l);
    228       1.16  jdolecek 
    229       1.16  jdolecek 	if (error == 0) {
    230       1.16  jdolecek 		cvtstat(&ub, &oub);
    231        1.1  christos 		error = copyout((caddr_t)&oub, (caddr_t)SCARG(uap, sb),
    232        1.1  christos 		    sizeof (oub));
    233       1.16  jdolecek 	}
    234       1.16  jdolecek 
    235       1.29     perry 
    236        1.1  christos 	return (error);
    237        1.1  christos }
    238        1.1  christos 
    239        1.1  christos 
    240        1.1  christos /*
    241        1.1  christos  * Truncate a file given a file descriptor.
    242        1.1  christos  */
    243        1.1  christos /* ARGSUSED */
    244        1.1  christos int
    245       1.22   thorpej compat_43_sys_ftruncate(struct lwp *l, void *v, register_t *retval)
    246        1.2   thorpej {
    247       1.13  augustss 	struct compat_43_sys_ftruncate_args /* {
    248        1.1  christos 		syscallarg(int) fd;
    249        1.1  christos 		syscallarg(long) length;
    250        1.2   thorpej 	} */ *uap = v;
    251        1.3   mycroft 	struct sys_ftruncate_args /* {
    252        1.1  christos 		syscallarg(int) fd;
    253        1.1  christos 		syscallarg(int) pad;
    254        1.1  christos 		syscallarg(off_t) length;
    255        1.1  christos 	} */ nuap;
    256        1.1  christos 
    257        1.1  christos 	SCARG(&nuap, fd) = SCARG(uap, fd);
    258        1.1  christos 	SCARG(&nuap, length) = SCARG(uap, length);
    259       1.22   thorpej 	return (sys_ftruncate(l, &nuap, retval));
    260        1.1  christos }
    261        1.1  christos 
    262        1.1  christos /*
    263        1.1  christos  * Truncate a file given its path name.
    264        1.1  christos  */
    265        1.1  christos /* ARGSUSED */
    266        1.1  christos int
    267       1.22   thorpej compat_43_sys_truncate(struct lwp *l, void *v, register_t *retval)
    268        1.2   thorpej {
    269       1.13  augustss 	struct compat_43_sys_truncate_args /* {
    270        1.1  christos 		syscallarg(char *) path;
    271        1.1  christos 		syscallarg(long) length;
    272        1.2   thorpej 	} */ *uap = v;
    273        1.3   mycroft 	struct sys_truncate_args /* {
    274        1.1  christos 		syscallarg(char *) path;
    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, path) = SCARG(uap, path);
    280        1.1  christos 	SCARG(&nuap, length) = SCARG(uap, length);
    281       1.22   thorpej 	return (sys_truncate(l, &nuap, retval));
    282        1.1  christos }
    283        1.1  christos 
    284        1.1  christos 
    285        1.1  christos /*
    286        1.1  christos  * Reposition read/write file offset.
    287        1.1  christos  */
    288        1.1  christos int
    289       1.22   thorpej compat_43_sys_lseek(struct lwp *l, void *v, register_t *retval)
    290        1.2   thorpej {
    291       1.13  augustss 	struct compat_43_sys_lseek_args /* {
    292        1.1  christos 		syscallarg(int) fd;
    293        1.1  christos 		syscallarg(long) offset;
    294        1.1  christos 		syscallarg(int) whence;
    295        1.2   thorpej 	} */ *uap = v;
    296        1.3   mycroft 	struct sys_lseek_args /* {
    297        1.1  christos 		syscallarg(int) fd;
    298        1.1  christos 		syscallarg(int) pad;
    299        1.1  christos 		syscallarg(off_t) offset;
    300        1.1  christos 		syscallarg(int) whence;
    301        1.1  christos 	} */ nuap;
    302        1.1  christos 	off_t qret;
    303        1.1  christos 	int error;
    304        1.1  christos 
    305        1.1  christos 	SCARG(&nuap, fd) = SCARG(uap, fd);
    306        1.1  christos 	SCARG(&nuap, offset) = SCARG(uap, offset);
    307        1.1  christos 	SCARG(&nuap, whence) = SCARG(uap, whence);
    308       1.22   thorpej 	error = sys_lseek(l, &nuap, (void *)&qret);
    309        1.1  christos 	*(long *)retval = qret;
    310        1.1  christos 	return (error);
    311        1.1  christos }
    312        1.1  christos 
    313        1.1  christos 
    314        1.1  christos /*
    315        1.1  christos  * Create a file.
    316        1.1  christos  */
    317        1.1  christos int
    318       1.22   thorpej compat_43_sys_creat(struct lwp *l, void *v, register_t *retval)
    319        1.2   thorpej {
    320       1.13  augustss 	struct compat_43_sys_creat_args /* {
    321        1.1  christos 		syscallarg(char *) path;
    322        1.1  christos 		syscallarg(int) mode;
    323        1.2   thorpej 	} */ *uap = v;
    324        1.3   mycroft 	struct sys_open_args /* {
    325        1.1  christos 		syscallarg(char *) path;
    326        1.1  christos 		syscallarg(int) flags;
    327        1.1  christos 		syscallarg(int) mode;
    328        1.1  christos 	} */ nuap;
    329        1.1  christos 
    330        1.1  christos 	SCARG(&nuap, path) = SCARG(uap, path);
    331        1.1  christos 	SCARG(&nuap, mode) = SCARG(uap, mode);
    332        1.1  christos 	SCARG(&nuap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
    333       1.22   thorpej 	return (sys_open(l, &nuap, retval));
    334        1.1  christos }
    335        1.1  christos 
    336        1.1  christos /*ARGSUSED*/
    337        1.1  christos int
    338  1.32.14.1        ad compat_43_sys_quota(struct lwp *l, void *v,
    339  1.32.14.1        ad     register_t *retval)
    340        1.1  christos {
    341        1.1  christos 
    342        1.1  christos 	return (ENOSYS);
    343        1.1  christos }
    344        1.1  christos 
    345        1.1  christos 
    346        1.1  christos /*
    347        1.1  christos  * Read a block of directory entries in a file system independent format.
    348        1.1  christos  */
    349        1.1  christos int
    350       1.22   thorpej compat_43_sys_getdirentries(struct lwp *l, void *v, register_t *retval)
    351        1.2   thorpej {
    352       1.13  augustss 	struct compat_43_sys_getdirentries_args /* {
    353        1.1  christos 		syscallarg(int) fd;
    354        1.1  christos 		syscallarg(char *) buf;
    355        1.1  christos 		syscallarg(u_int) count;
    356        1.1  christos 		syscallarg(long *) basep;
    357        1.2   thorpej 	} */ *uap = v;
    358       1.22   thorpej 	struct proc *p = l->l_proc;
    359       1.13  augustss 	struct vnode *vp;
    360        1.1  christos 	struct file *fp;
    361        1.1  christos 	struct uio auio, kuio;
    362        1.1  christos 	struct iovec aiov, kiov;
    363        1.1  christos 	struct dirent *dp, *edp;
    364        1.1  christos 	caddr_t dirbuf;
    365       1.27  christos 	size_t count = min(MAXBSIZE, (size_t)SCARG(uap, count));
    366       1.27  christos 
    367        1.1  christos 	int error, eofflag, readcnt;
    368        1.1  christos 	long loff;
    369        1.1  christos 
    370       1.12   thorpej 	/* getvnode() will use the descriptor for us */
    371        1.4  christos 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    372        1.1  christos 		return (error);
    373       1.12   thorpej 	if ((fp->f_flag & FREAD) == 0) {
    374       1.12   thorpej 		error = EBADF;
    375       1.12   thorpej 		goto out;
    376       1.12   thorpej 	}
    377        1.1  christos 	vp = (struct vnode *)fp->f_data;
    378        1.1  christos unionread:
    379       1.12   thorpej 	if (vp->v_type != VDIR) {
    380       1.12   thorpej 		error = EINVAL;
    381       1.12   thorpej 		goto out;
    382       1.12   thorpej 	}
    383        1.1  christos 	aiov.iov_base = SCARG(uap, buf);
    384       1.27  christos 	aiov.iov_len = count;
    385        1.1  christos 	auio.uio_iov = &aiov;
    386        1.1  christos 	auio.uio_iovcnt = 1;
    387        1.1  christos 	auio.uio_rw = UIO_READ;
    388       1.27  christos 	auio.uio_resid = count;
    389       1.32      yamt 	KASSERT(l == curlwp);
    390       1.32      yamt 	auio.uio_vmspace = l->l_proc->p_vmspace;
    391       1.10      fvdl 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    392        1.1  christos 	loff = auio.uio_offset = fp->f_offset;
    393        1.1  christos #	if (BYTE_ORDER != LITTLE_ENDIAN)
    394       1.28   mycroft 		if ((vp->v_mount->mnt_iflag & IMNT_DTYPE) == 0) {
    395        1.1  christos 			error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag,
    396       1.10      fvdl 			    (off_t **)0, (int *)0);
    397        1.1  christos 			fp->f_offset = auio.uio_offset;
    398        1.1  christos 		} else
    399        1.1  christos #	endif
    400        1.1  christos 	{
    401        1.1  christos 		kuio = auio;
    402        1.1  christos 		kuio.uio_iov = &kiov;
    403       1.27  christos 		kiov.iov_len = count;
    404       1.27  christos 		dirbuf = malloc(count, M_TEMP, M_WAITOK);
    405        1.1  christos 		kiov.iov_base = dirbuf;
    406       1.32      yamt 		UIO_SETUP_SYSSPACE(&kuio);
    407        1.1  christos 		error = VOP_READDIR(vp, &kuio, fp->f_cred, &eofflag,
    408       1.10      fvdl 			    (off_t **)0, (int *)0);
    409        1.1  christos 		fp->f_offset = kuio.uio_offset;
    410        1.1  christos 		if (error == 0) {
    411       1.27  christos 			readcnt = count - kuio.uio_resid;
    412        1.1  christos 			edp = (struct dirent *)&dirbuf[readcnt];
    413        1.1  christos 			for (dp = (struct dirent *)dirbuf; dp < edp; ) {
    414        1.1  christos #				if (BYTE_ORDER == LITTLE_ENDIAN)
    415        1.1  christos 					/*
    416        1.1  christos 					 * The expected low byte of
    417        1.1  christos 					 * dp->d_namlen is our dp->d_type.
    418        1.1  christos 					 * The high MBZ byte of dp->d_namlen
    419        1.1  christos 					 * is our dp->d_namlen.
    420        1.1  christos 					 */
    421        1.1  christos 					dp->d_type = dp->d_namlen;
    422        1.1  christos 					dp->d_namlen = 0;
    423        1.1  christos #				else
    424        1.1  christos 					/*
    425        1.1  christos 					 * The dp->d_type is the high byte
    426        1.1  christos 					 * of the expected dp->d_namlen,
    427        1.1  christos 					 * so must be zero'ed.
    428        1.1  christos 					 */
    429        1.1  christos 					dp->d_type = 0;
    430        1.1  christos #				endif
    431        1.1  christos 				if (dp->d_reclen > 0) {
    432        1.1  christos 					dp = (struct dirent *)
    433        1.1  christos 					    ((char *)dp + dp->d_reclen);
    434        1.1  christos 				} else {
    435        1.1  christos 					error = EIO;
    436        1.1  christos 					break;
    437        1.1  christos 				}
    438        1.1  christos 			}
    439        1.1  christos 			if (dp >= edp)
    440        1.1  christos 				error = uiomove(dirbuf, readcnt, &auio);
    441        1.1  christos 		}
    442       1.27  christos 		free(dirbuf, M_TEMP);
    443        1.1  christos 	}
    444       1.10      fvdl 	VOP_UNLOCK(vp, 0);
    445        1.1  christos 	if (error)
    446       1.12   thorpej 		goto out;
    447        1.1  christos 
    448        1.1  christos #ifdef UNION
    449        1.1  christos {
    450        1.4  christos 	extern int (**union_vnodeop_p) __P((void *));
    451        1.1  christos 	extern struct vnode *union_dircache __P((struct vnode *));
    452        1.1  christos 
    453       1.27  christos 	if ((count == auio.uio_resid) &&
    454        1.1  christos 	    (vp->v_op == union_vnodeop_p)) {
    455        1.1  christos 		struct vnode *lvp;
    456        1.1  christos 
    457        1.1  christos 		lvp = union_dircache(vp);
    458        1.1  christos 		if (lvp != NULLVP) {
    459        1.1  christos 			struct vattr va;
    460        1.1  christos 
    461        1.1  christos 			/*
    462        1.1  christos 			 * If the directory is opaque,
    463        1.1  christos 			 * then don't show lower entries
    464        1.1  christos 			 */
    465       1.31  christos 			error = VOP_GETATTR(vp, &va, fp->f_cred, l);
    466        1.1  christos 			if (va.va_flags & OPAQUE) {
    467        1.1  christos 				vput(lvp);
    468        1.1  christos 				lvp = NULL;
    469        1.1  christos 			}
    470        1.1  christos 		}
    471       1.29     perry 
    472        1.1  christos 		if (lvp != NULLVP) {
    473       1.31  christos 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, l);
    474       1.10      fvdl 			VOP_UNLOCK(lvp, 0);
    475        1.1  christos 
    476        1.1  christos 			if (error) {
    477        1.1  christos 				vrele(lvp);
    478       1.12   thorpej 				goto out;
    479        1.1  christos 			}
    480        1.1  christos 			fp->f_data = (caddr_t) lvp;
    481        1.1  christos 			fp->f_offset = 0;
    482       1.31  christos 			error = vn_close(vp, FREAD, fp->f_cred, l);
    483        1.1  christos 			if (error)
    484       1.12   thorpej 				goto out;
    485        1.1  christos 			vp = lvp;
    486        1.1  christos 			goto unionread;
    487        1.1  christos 		}
    488        1.1  christos 	}
    489        1.1  christos }
    490        1.1  christos #endif /* UNION */
    491        1.1  christos 
    492       1.27  christos 	if ((count == auio.uio_resid) &&
    493        1.1  christos 	    (vp->v_flag & VROOT) &&
    494        1.1  christos 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
    495        1.1  christos 		struct vnode *tvp = vp;
    496        1.1  christos 		vp = vp->v_mount->mnt_vnodecovered;
    497        1.1  christos 		VREF(vp);
    498        1.1  christos 		fp->f_data = (caddr_t) vp;
    499        1.1  christos 		fp->f_offset = 0;
    500        1.1  christos 		vrele(tvp);
    501        1.1  christos 		goto unionread;
    502        1.1  christos 	}
    503        1.1  christos 	error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
    504        1.1  christos 	    sizeof(long));
    505       1.27  christos 	*retval = count - auio.uio_resid;
    506       1.12   thorpej  out:
    507       1.31  christos 	FILE_UNUSE(fp, l);
    508        1.1  christos 	return (error);
    509        1.1  christos }
    510       1.30  christos 
    511       1.30  christos /*
    512       1.30  christos  * sysctl helper routine for vfs.generic.conf lookups.
    513       1.30  christos  */
    514       1.30  christos #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44)
    515       1.30  christos static int
    516       1.30  christos sysctl_vfs_generic_conf(SYSCTLFN_ARGS)
    517       1.30  christos {
    518       1.30  christos         struct vfsconf vfc;
    519       1.30  christos         extern const char * const mountcompatnames[];
    520       1.30  christos         extern int nmountcompatnames;
    521       1.30  christos 	struct sysctlnode node;
    522       1.30  christos 	struct vfsops *vfsp;
    523       1.30  christos 	u_int vfsnum;
    524       1.30  christos 
    525       1.30  christos 	if (namelen != 1)
    526       1.30  christos 		return (ENOTDIR);
    527       1.30  christos 	vfsnum = name[0];
    528       1.30  christos 	if (vfsnum >= nmountcompatnames ||
    529       1.30  christos 	    mountcompatnames[vfsnum] == NULL)
    530       1.30  christos 		return (EOPNOTSUPP);
    531       1.30  christos 	vfsp = vfs_getopsbyname(mountcompatnames[vfsnum]);
    532       1.30  christos 	if (vfsp == NULL)
    533       1.30  christos 		return (EOPNOTSUPP);
    534       1.30  christos 
    535       1.30  christos 	vfc.vfc_vfsops = vfsp;
    536       1.30  christos 	strncpy(vfc.vfc_name, vfsp->vfs_name, MFSNAMELEN);
    537       1.30  christos 	vfc.vfc_typenum = vfsnum;
    538       1.30  christos 	vfc.vfc_refcount = vfsp->vfs_refcount;
    539       1.30  christos 	vfc.vfc_flags = 0;
    540       1.30  christos 	vfc.vfc_mountroot = vfsp->vfs_mountroot;
    541       1.30  christos 	vfc.vfc_next = NULL;
    542       1.30  christos 
    543       1.30  christos 	node = *rnode;
    544       1.30  christos 	node.sysctl_data = &vfc;
    545       1.30  christos 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
    546       1.30  christos }
    547       1.30  christos 
    548       1.30  christos /*
    549       1.30  christos  * Top level filesystem related information gathering.
    550       1.30  christos  */
    551       1.30  christos SYSCTL_SETUP(compat_sysctl_vfs_setup, "compat sysctl vfs subtree setup")
    552       1.30  christos {
    553       1.30  christos 	extern int nmountcompatnames;
    554       1.30  christos 
    555       1.30  christos 	sysctl_createv(clog, 0, NULL, NULL,
    556       1.30  christos 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    557       1.30  christos 		       CTLTYPE_INT, "maxtypenum",
    558       1.30  christos 		       SYSCTL_DESCR("Highest valid filesystem type number"),
    559       1.30  christos 		       NULL, nmountcompatnames, NULL, 0,
    560       1.30  christos 		       CTL_VFS, VFS_GENERIC, VFS_MAXTYPENUM, CTL_EOL);
    561       1.30  christos 	sysctl_createv(clog, 0, NULL, NULL,
    562       1.30  christos 		       CTLFLAG_PERMANENT,
    563       1.30  christos 		       CTLTYPE_STRUCT, "conf",
    564       1.30  christos 		       SYSCTL_DESCR("Filesystem configuration information"),
    565       1.30  christos 		       sysctl_vfs_generic_conf, 0, NULL,
    566       1.30  christos 		       sizeof(struct vfsconf),
    567       1.30  christos 		       CTL_VFS, VFS_GENERIC, VFS_CONF, CTL_EOL);
    568       1.30  christos }
    569       1.30  christos #endif
    570