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