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