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