Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.82
      1 /*	$NetBSD: vfs_syscalls.c,v 1.82 1997/04/07 00:04:16 kleink Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)vfs_syscalls.c	8.28 (Berkeley) 12/10/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/namei.h>
     46 #include <sys/filedesc.h>
     47 #include <sys/kernel.h>
     48 #include <sys/file.h>
     49 #include <sys/stat.h>
     50 #include <sys/vnode.h>
     51 #include <sys/mount.h>
     52 #include <sys/proc.h>
     53 #include <sys/uio.h>
     54 #include <sys/malloc.h>
     55 #include <sys/dirent.h>
     56 
     57 #include <sys/syscallargs.h>
     58 
     59 #include <vm/vm.h>
     60 #include <sys/sysctl.h>
     61 
     62 static int change_dir __P((struct nameidata *, struct proc *));
     63 
     64 void checkdirs __P((struct vnode *));
     65 int dounmount __P((struct mount *, int, struct proc *));
     66 
     67 /*
     68  * Virtual File System System Calls
     69  */
     70 
     71 /*
     72  * Mount a file system.
     73  */
     74 /* ARGSUSED */
     75 int
     76 sys_mount(p, v, retval)
     77 	struct proc *p;
     78 	void *v;
     79 	register_t *retval;
     80 {
     81 	register struct sys_mount_args /* {
     82 		syscallarg(const char *) type;
     83 		syscallarg(const char *) path;
     84 		syscallarg(int) flags;
     85 		syscallarg(void *) data;
     86 	} */ *uap = v;
     87 	register struct vnode *vp;
     88 	register struct mount *mp;
     89 	int error, flag = 0;
     90 	u_long fsindex = 0;
     91 	char fstypename[MFSNAMELEN];
     92 	struct vattr va;
     93 	struct nameidata nd;
     94 
     95 	/*
     96 	 * Get vnode to be covered
     97 	 */
     98 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
     99 	    SCARG(uap, path), p);
    100 	if ((error = namei(&nd)) != 0)
    101 		return (error);
    102 	vp = nd.ni_vp;
    103 	if (SCARG(uap, flags) & MNT_UPDATE) {
    104 		if ((vp->v_flag & VROOT) == 0) {
    105 			vput(vp);
    106 			return (EINVAL);
    107 		}
    108 		mp = vp->v_mount;
    109 		flag = mp->mnt_flag;
    110 		/*
    111 		 * We only allow the filesystem to be reloaded if it
    112 		 * is currently mounted read-only.
    113 		 */
    114 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
    115 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
    116 			vput(vp);
    117 			return (EOPNOTSUPP);	/* Needs translation */
    118 		}
    119 		mp->mnt_flag |=
    120 		    SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    121 		/*
    122 		 * Only root, or the user that did the original mount is
    123 		 * permitted to update it.
    124 		 */
    125 		if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
    126 		    (error = suser(p->p_ucred, &p->p_acflag))) {
    127 			vput(vp);
    128 			return (error);
    129 		}
    130 		/*
    131 		 * Do not allow NFS export by non-root users. Silently
    132 		 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
    133 		 */
    134 		if (p->p_ucred->cr_uid != 0) {
    135 			if (SCARG(uap, flags) & MNT_EXPORTED) {
    136 				vput(vp);
    137 				return (EPERM);
    138 			}
    139 			SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    140 		}
    141 		VOP_UNLOCK(vp);
    142 		goto update;
    143 	}
    144 	/*
    145 	 * If the user is not root, ensure that they own the directory
    146 	 * onto which we are attempting to mount.
    147 	 */
    148 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
    149 	    (va.va_uid != p->p_ucred->cr_uid &&
    150 	     (error = suser(p->p_ucred, &p->p_acflag)))) {
    151 		vput(vp);
    152 		return (error);
    153 	}
    154 	/*
    155 	 * Do not allow NFS export by non-root users. Silently
    156 	 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
    157 	 */
    158 	if (p->p_ucred->cr_uid != 0) {
    159 		if (SCARG(uap, flags) & MNT_EXPORTED) {
    160 			vput(vp);
    161 			return (EPERM);
    162 		}
    163 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    164 	}
    165 	if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
    166 		return (error);
    167 	if (vp->v_type != VDIR) {
    168 		vput(vp);
    169 		return (ENOTDIR);
    170 	}
    171 	error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
    172 	if (error) {
    173 #if defined(COMPAT_09) || defined(COMPAT_43)
    174 		/*
    175 		 * Historically filesystem types were identified by number.
    176 		 * If we get an integer for the filesystem type instead of a
    177 		 * string, we check to see if it matches one of the historic
    178 		 * filesystem types.
    179 		 */
    180 		fsindex = (u_long)SCARG(uap, type);
    181 		if (fsindex >= nvfssw || vfssw[fsindex] == NULL) {
    182 			vput(vp);
    183 			return (ENODEV);
    184 		}
    185 		strncpy(fstypename, vfssw[fsindex]->vfs_name, MFSNAMELEN);
    186 #else
    187 		vput(vp);
    188 		return (error);
    189 #endif
    190 	}
    191 #ifdef	COMPAT_10
    192 	/* Accept `ufs' as an alias for `ffs'. */
    193 	if (!strncmp(fstypename, "ufs", MFSNAMELEN))
    194 		strncpy(fstypename, "ffs", MFSNAMELEN);
    195 #endif
    196 	for (fsindex = 0; fsindex < nvfssw; fsindex++)
    197 		if (vfssw[fsindex] != NULL &&
    198 		    !strncmp(vfssw[fsindex]->vfs_name, fstypename, MFSNAMELEN))
    199 			break;
    200 	if (fsindex >= nvfssw) {
    201 		vput(vp);
    202 		return (ENODEV);
    203 	}
    204 	if (vp->v_mountedhere != NULL) {
    205 		vput(vp);
    206 		return (EBUSY);
    207 	}
    208 
    209 	/*
    210 	 * Allocate and initialize the file system.
    211 	 */
    212 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    213 		M_MOUNT, M_WAITOK);
    214 	bzero((char *)mp, (u_long)sizeof(struct mount));
    215 	mp->mnt_op = vfssw[fsindex];
    216 	if ((error = vfs_lock(mp)) != 0) {
    217 		free((caddr_t)mp, M_MOUNT);
    218 		vput(vp);
    219 		return (error);
    220 	}
    221 	/* Do this early in case we block later. */
    222 	vfssw[fsindex]->vfs_refcount++;
    223 	vp->v_mountedhere = mp;
    224 	mp->mnt_vnodecovered = vp;
    225 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
    226 update:
    227 	/*
    228 	 * Set the mount level flags.
    229 	 */
    230 	if (SCARG(uap, flags) & MNT_RDONLY)
    231 		mp->mnt_flag |= MNT_RDONLY;
    232 	else if (mp->mnt_flag & MNT_RDONLY)
    233 		mp->mnt_flag |= MNT_WANTRDWR;
    234 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    235 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    236 	    MNT_NOATIME);
    237 	mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
    238 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
    239 	    MNT_NOCOREDUMP | MNT_NOATIME);
    240 	/*
    241 	 * Mount the filesystem.
    242 	 */
    243 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
    244 	if (mp->mnt_flag & MNT_UPDATE) {
    245 		vrele(vp);
    246 		if (mp->mnt_flag & MNT_WANTRDWR)
    247 			mp->mnt_flag &= ~MNT_RDONLY;
    248 		mp->mnt_flag &=~
    249 		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
    250 		if (error)
    251 			mp->mnt_flag = flag;
    252 		return (error);
    253 	}
    254 	/*
    255 	 * Put the new filesystem on the mount list after root.
    256 	 */
    257 	cache_purge(vp);
    258 	if (!error) {
    259 		CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    260 		checkdirs(vp);
    261 		VOP_UNLOCK(vp);
    262 		vfs_unlock(mp);
    263 		(void) VFS_STATFS(mp, &mp->mnt_stat, p);
    264 		error = VFS_START(mp, 0, p);
    265 	} else {
    266 		mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
    267 		vfssw[fsindex]->vfs_refcount--;
    268 		vfs_unlock(mp);
    269 		free((caddr_t)mp, M_MOUNT);
    270 		vput(vp);
    271 	}
    272 	return (error);
    273 }
    274 
    275 /*
    276  * Scan all active processes to see if any of them have a current
    277  * or root directory onto which the new filesystem has just been
    278  * mounted. If so, replace them with the new mount point.
    279  */
    280 void
    281 checkdirs(olddp)
    282 	struct vnode *olddp;
    283 {
    284 	struct filedesc *fdp;
    285 	struct vnode *newdp;
    286 	struct proc *p;
    287 
    288 	if (olddp->v_usecount == 1)
    289 		return;
    290 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    291 		panic("mount: lost mount");
    292 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    293 		fdp = p->p_fd;
    294 		if (fdp->fd_cdir == olddp) {
    295 			vrele(fdp->fd_cdir);
    296 			VREF(newdp);
    297 			fdp->fd_cdir = newdp;
    298 		}
    299 		if (fdp->fd_rdir == olddp) {
    300 			vrele(fdp->fd_rdir);
    301 			VREF(newdp);
    302 			fdp->fd_rdir = newdp;
    303 		}
    304 	}
    305 	if (rootvnode == olddp) {
    306 		vrele(rootvnode);
    307 		VREF(newdp);
    308 		rootvnode = newdp;
    309 	}
    310 	vput(newdp);
    311 }
    312 
    313 /*
    314  * Unmount a file system.
    315  *
    316  * Note: unmount takes a path to the vnode mounted on as argument,
    317  * not special file (as before).
    318  */
    319 /* ARGSUSED */
    320 int
    321 sys_unmount(p, v, retval)
    322 	struct proc *p;
    323 	void *v;
    324 	register_t *retval;
    325 {
    326 	register struct sys_unmount_args /* {
    327 		syscallarg(const char *) path;
    328 		syscallarg(int) flags;
    329 	} */ *uap = v;
    330 	register struct vnode *vp;
    331 	struct mount *mp;
    332 	int error;
    333 	struct nameidata nd;
    334 
    335 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    336 	    SCARG(uap, path), p);
    337 	if ((error = namei(&nd)) != 0)
    338 		return (error);
    339 	vp = nd.ni_vp;
    340 	mp = vp->v_mount;
    341 
    342 	/*
    343 	 * Only root, or the user that did the original mount is
    344 	 * permitted to unmount this filesystem.
    345 	 */
    346 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
    347 	    (error = suser(p->p_ucred, &p->p_acflag))) {
    348 		vput(vp);
    349 		return (error);
    350 	}
    351 
    352 	/*
    353 	 * Don't allow unmounting the root file system.
    354 	 */
    355 	if (mp->mnt_flag & MNT_ROOTFS) {
    356 		vput(vp);
    357 		return (EINVAL);
    358 	}
    359 
    360 	/*
    361 	 * Must be the root of the filesystem
    362 	 */
    363 	if ((vp->v_flag & VROOT) == 0) {
    364 		vput(vp);
    365 		return (EINVAL);
    366 	}
    367 	vput(vp);
    368 
    369 	if (vfs_busy(mp))
    370 		return (EBUSY);
    371 
    372 	return (dounmount(mp, SCARG(uap, flags), p));
    373 }
    374 
    375 /*
    376  * Do the actual file system unmount. File system is assumed to have been
    377  * marked busy by the caller.
    378  */
    379 int
    380 dounmount(mp, flags, p)
    381 	register struct mount *mp;
    382 	int flags;
    383 	struct proc *p;
    384 {
    385 	struct vnode *coveredvp;
    386 	int error;
    387 
    388 	coveredvp = mp->mnt_vnodecovered;
    389 	mp->mnt_flag |= MNT_UNMOUNT;
    390 	if ((error = vfs_lock(mp)) != 0) {
    391 		vfs_unbusy(mp);
    392 		return (error);
    393 	}
    394 
    395 	mp->mnt_flag &=~ MNT_ASYNC;
    396 	vnode_pager_umount(mp);	/* release cached vnodes */
    397 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    398 	if ((error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0 ||
    399 	    (flags & MNT_FORCE))
    400 		error = VFS_UNMOUNT(mp, flags, p);
    401 	mp->mnt_flag &= ~MNT_UNMOUNT;
    402 	vfs_unbusy(mp);
    403 	if (error) {
    404 		vfs_unlock(mp);
    405 	} else {
    406 		CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
    407 		if (coveredvp != NULLVP) {
    408 			vrele(coveredvp);
    409 			coveredvp->v_mountedhere = (struct mount *)0;
    410 		}
    411 		mp->mnt_op->vfs_refcount--;
    412 		vfs_unlock(mp);
    413 		if (mp->mnt_vnodelist.lh_first != NULL)
    414 			panic("unmount: dangling vnode");
    415 		free((caddr_t)mp, M_MOUNT);
    416 	}
    417 	return (error);
    418 }
    419 
    420 /*
    421  * Sync each mounted filesystem.
    422  */
    423 #ifdef DEBUG
    424 int syncprt = 0;
    425 struct ctldebug debug0 = { "syncprt", &syncprt };
    426 #endif
    427 
    428 /* ARGSUSED */
    429 int
    430 sys_sync(p, v, retval)
    431 	struct proc *p;
    432 	void *v;
    433 	register_t *retval;
    434 {
    435 	register struct mount *mp, *nmp;
    436 	int asyncflag;
    437 
    438 	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
    439 		/*
    440 		 * Get the prev pointer in case we hang on vfs_busy
    441 		 * while we are being unmounted.
    442 		 */
    443 		nmp = mp->mnt_list.cqe_prev;
    444 		/*
    445 		 * The lock check below is to avoid races with mount
    446 		 * and unmount.
    447 		 */
    448 		if ((mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY)) == 0 &&
    449 		    !vfs_busy(mp)) {
    450 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    451 			mp->mnt_flag &= ~MNT_ASYNC;
    452 			vnode_pager_sync(mp);
    453 			VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
    454 			if (asyncflag)
    455 				mp->mnt_flag |= MNT_ASYNC;
    456 			/*
    457 			 * Get the prev pointer again, as the prior filesystem
    458 			 * might have been unmounted while we were sync'ing.
    459 			 */
    460 			nmp = mp->mnt_list.cqe_prev;
    461 			vfs_unbusy(mp);
    462 		}
    463 	}
    464 #ifdef DEBUG
    465 	if (syncprt)
    466 		vfs_bufstats();
    467 #endif /* DEBUG */
    468 	return (0);
    469 }
    470 
    471 /*
    472  * Change filesystem quotas.
    473  */
    474 /* ARGSUSED */
    475 int
    476 sys_quotactl(p, v, retval)
    477 	struct proc *p;
    478 	void *v;
    479 	register_t *retval;
    480 {
    481 	register struct sys_quotactl_args /* {
    482 		syscallarg(const char *) path;
    483 		syscallarg(int) cmd;
    484 		syscallarg(int) uid;
    485 		syscallarg(caddr_t) arg;
    486 	} */ *uap = v;
    487 	register struct mount *mp;
    488 	int error;
    489 	struct nameidata nd;
    490 
    491 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    492 	if ((error = namei(&nd)) != 0)
    493 		return (error);
    494 	mp = nd.ni_vp->v_mount;
    495 	vrele(nd.ni_vp);
    496 	return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
    497 	    SCARG(uap, arg), p));
    498 }
    499 
    500 /*
    501  * Get filesystem statistics.
    502  */
    503 /* ARGSUSED */
    504 int
    505 sys_statfs(p, v, retval)
    506 	struct proc *p;
    507 	void *v;
    508 	register_t *retval;
    509 {
    510 	register struct sys_statfs_args /* {
    511 		syscallarg(const char *) path;
    512 		syscallarg(struct statfs *) buf;
    513 	} */ *uap = v;
    514 	register struct mount *mp;
    515 	register struct statfs *sp;
    516 	int error;
    517 	struct nameidata nd;
    518 
    519 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    520 	if ((error = namei(&nd)) != 0)
    521 		return (error);
    522 	mp = nd.ni_vp->v_mount;
    523 	sp = &mp->mnt_stat;
    524 	vrele(nd.ni_vp);
    525 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    526 		return (error);
    527 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    528 	return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
    529 }
    530 
    531 /*
    532  * Get filesystem statistics.
    533  */
    534 /* ARGSUSED */
    535 int
    536 sys_fstatfs(p, v, retval)
    537 	struct proc *p;
    538 	void *v;
    539 	register_t *retval;
    540 {
    541 	register struct sys_fstatfs_args /* {
    542 		syscallarg(int) fd;
    543 		syscallarg(struct statfs *) buf;
    544 	} */ *uap = v;
    545 	struct file *fp;
    546 	struct mount *mp;
    547 	register struct statfs *sp;
    548 	int error;
    549 
    550 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    551 		return (error);
    552 	mp = ((struct vnode *)fp->f_data)->v_mount;
    553 	sp = &mp->mnt_stat;
    554 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    555 		return (error);
    556 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    557 	return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
    558 }
    559 
    560 /*
    561  * Get statistics on all filesystems.
    562  */
    563 int
    564 sys_getfsstat(p, v, retval)
    565 	struct proc *p;
    566 	void *v;
    567 	register_t *retval;
    568 {
    569 	register struct sys_getfsstat_args /* {
    570 		syscallarg(struct statfs *) buf;
    571 		syscallarg(long) bufsize;
    572 		syscallarg(int) flags;
    573 	} */ *uap = v;
    574 	register struct mount *mp, *nmp;
    575 	register struct statfs *sp;
    576 	caddr_t sfsp;
    577 	long count, maxcount, error;
    578 
    579 	maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
    580 	sfsp = (caddr_t)SCARG(uap, buf);
    581 	for (count = 0,
    582 	     mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    583 		nmp = mp->mnt_list.cqe_next;
    584 		if (sfsp && count < maxcount &&
    585 		    ((mp->mnt_flag & MNT_MLOCK) == 0)) {
    586 			sp = &mp->mnt_stat;
    587 			/*
    588 			 * If MNT_NOWAIT is specified, do not refresh the
    589 			 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
    590 			 */
    591 			if (((SCARG(uap, flags) & MNT_NOWAIT) == 0 ||
    592 			    (SCARG(uap, flags) & MNT_WAIT)) &&
    593 			    (error = VFS_STATFS(mp, sp, p)))
    594 				continue;
    595 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    596 			error = copyout(sp, sfsp, sizeof(*sp));
    597 			if (error)
    598 				return (error);
    599 			sfsp += sizeof(*sp);
    600 		}
    601 		count++;
    602 	}
    603 	if (sfsp && count > maxcount)
    604 		*retval = maxcount;
    605 	else
    606 		*retval = count;
    607 	return (0);
    608 }
    609 
    610 /*
    611  * Change current working directory to a given file descriptor.
    612  */
    613 /* ARGSUSED */
    614 int
    615 sys_fchdir(p, v, retval)
    616 	struct proc *p;
    617 	void *v;
    618 	register_t *retval;
    619 {
    620 	struct sys_fchdir_args /* {
    621 		syscallarg(int) fd;
    622 	} */ *uap = v;
    623 	register struct filedesc *fdp = p->p_fd;
    624 	struct vnode *vp, *tdp;
    625 	struct mount *mp;
    626 	struct file *fp;
    627 	int error;
    628 
    629 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    630 		return (error);
    631 	vp = (struct vnode *)fp->f_data;
    632 	VREF(vp);
    633 	VOP_LOCK(vp);
    634 	if (vp->v_type != VDIR)
    635 		error = ENOTDIR;
    636 	else
    637 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    638 	while (!error && (mp = vp->v_mountedhere) != NULL) {
    639 		if (mp->mnt_flag & MNT_MLOCK) {
    640 			mp->mnt_flag |= MNT_MWAIT;
    641 			sleep((caddr_t)mp, PVFS);
    642 			continue;
    643 		}
    644 		if ((error = VFS_ROOT(mp, &tdp)) != 0)
    645 			break;
    646 		vput(vp);
    647 		vp = tdp;
    648 	}
    649 	VOP_UNLOCK(vp);
    650 	if (error) {
    651 		vrele(vp);
    652 		return (error);
    653 	}
    654 	vrele(fdp->fd_cdir);
    655 	fdp->fd_cdir = vp;
    656 	return (0);
    657 }
    658 
    659 /*
    660  * Change current working directory (``.'').
    661  */
    662 /* ARGSUSED */
    663 int
    664 sys_chdir(p, v, retval)
    665 	struct proc *p;
    666 	void *v;
    667 	register_t *retval;
    668 {
    669 	struct sys_chdir_args /* {
    670 		syscallarg(const char *) path;
    671 	} */ *uap = v;
    672 	register struct filedesc *fdp = p->p_fd;
    673 	int error;
    674 	struct nameidata nd;
    675 
    676 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    677 	    SCARG(uap, path), p);
    678 	if ((error = change_dir(&nd, p)) != 0)
    679 		return (error);
    680 	vrele(fdp->fd_cdir);
    681 	fdp->fd_cdir = nd.ni_vp;
    682 	return (0);
    683 }
    684 
    685 /*
    686  * Change notion of root (``/'') directory.
    687  */
    688 /* ARGSUSED */
    689 int
    690 sys_chroot(p, v, retval)
    691 	struct proc *p;
    692 	void *v;
    693 	register_t *retval;
    694 {
    695 	struct sys_chroot_args /* {
    696 		syscallarg(const char *) path;
    697 	} */ *uap = v;
    698 	register struct filedesc *fdp = p->p_fd;
    699 	int error;
    700 	struct nameidata nd;
    701 
    702 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    703 		return (error);
    704 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    705 	    SCARG(uap, path), p);
    706 	if ((error = change_dir(&nd, p)) != 0)
    707 		return (error);
    708 	if (fdp->fd_rdir != NULL)
    709 		vrele(fdp->fd_rdir);
    710 	fdp->fd_rdir = nd.ni_vp;
    711 	return (0);
    712 }
    713 
    714 /*
    715  * Common routine for chroot and chdir.
    716  */
    717 static int
    718 change_dir(ndp, p)
    719 	register struct nameidata *ndp;
    720 	struct proc *p;
    721 {
    722 	struct vnode *vp;
    723 	int error;
    724 
    725 	if ((error = namei(ndp)) != 0)
    726 		return (error);
    727 	vp = ndp->ni_vp;
    728 	if (vp->v_type != VDIR)
    729 		error = ENOTDIR;
    730 	else
    731 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    732 	VOP_UNLOCK(vp);
    733 	if (error)
    734 		vrele(vp);
    735 	return (error);
    736 }
    737 
    738 /*
    739  * Check permissions, allocate an open file structure,
    740  * and call the device open routine if any.
    741  */
    742 int
    743 sys_open(p, v, retval)
    744 	struct proc *p;
    745 	void *v;
    746 	register_t *retval;
    747 {
    748 	register struct sys_open_args /* {
    749 		syscallarg(const char *) path;
    750 		syscallarg(int) flags;
    751 		syscallarg(int) mode;
    752 	} */ *uap = v;
    753 	register struct filedesc *fdp = p->p_fd;
    754 	register struct file *fp;
    755 	register struct vnode *vp;
    756 	int flags, cmode;
    757 	struct file *nfp;
    758 	int type, indx, error;
    759 	struct flock lf;
    760 	struct nameidata nd;
    761 	extern struct fileops vnops;
    762 
    763 	if ((error = falloc(p, &nfp, &indx)) != 0)
    764 		return (error);
    765 	fp = nfp;
    766 	flags = FFLAGS(SCARG(uap, flags));
    767 	cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
    768 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    769 	p->p_dupfd = -indx - 1;			/* XXX check for fdopen */
    770 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
    771 		ffree(fp);
    772 		if ((error == ENODEV || error == ENXIO) &&
    773 		    p->p_dupfd >= 0 &&			/* XXX from fdopen */
    774 		    (error =
    775 			dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
    776 			*retval = indx;
    777 			return (0);
    778 		}
    779 		if (error == ERESTART)
    780 			error = EINTR;
    781 		fdp->fd_ofiles[indx] = NULL;
    782 		return (error);
    783 	}
    784 	p->p_dupfd = 0;
    785 	vp = nd.ni_vp;
    786 	fp->f_flag = flags & FMASK;
    787 	fp->f_type = DTYPE_VNODE;
    788 	fp->f_ops = &vnops;
    789 	fp->f_data = (caddr_t)vp;
    790 	if (flags & (O_EXLOCK | O_SHLOCK)) {
    791 		lf.l_whence = SEEK_SET;
    792 		lf.l_start = 0;
    793 		lf.l_len = 0;
    794 		if (flags & O_EXLOCK)
    795 			lf.l_type = F_WRLCK;
    796 		else
    797 			lf.l_type = F_RDLCK;
    798 		type = F_FLOCK;
    799 		if ((flags & FNONBLOCK) == 0)
    800 			type |= F_WAIT;
    801 		VOP_UNLOCK(vp);
    802 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
    803 		if (error) {
    804 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
    805 			ffree(fp);
    806 			fdp->fd_ofiles[indx] = NULL;
    807 			return (error);
    808 		}
    809 		VOP_LOCK(vp);
    810 		fp->f_flag |= FHASLOCK;
    811 	}
    812 	VOP_UNLOCK(vp);
    813 	*retval = indx;
    814 	return (0);
    815 }
    816 
    817 /*
    818  * Create a special file.
    819  */
    820 /* ARGSUSED */
    821 int
    822 sys_mknod(p, v, retval)
    823 	struct proc *p;
    824 	void *v;
    825 	register_t *retval;
    826 {
    827 	register struct sys_mknod_args /* {
    828 		syscallarg(const char *) path;
    829 		syscallarg(int) mode;
    830 		syscallarg(int) dev;
    831 	} */ *uap = v;
    832 	register struct vnode *vp;
    833 	struct vattr vattr;
    834 	int error;
    835 	int whiteout = 0;
    836 	struct nameidata nd;
    837 
    838 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    839 		return (error);
    840 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
    841 	if ((error = namei(&nd)) != 0)
    842 		return (error);
    843 	vp = nd.ni_vp;
    844 	if (vp != NULL)
    845 		error = EEXIST;
    846 	else {
    847 		VATTR_NULL(&vattr);
    848 		vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
    849 		vattr.va_rdev = SCARG(uap, dev);
    850 		whiteout = 0;
    851 
    852 		switch (SCARG(uap, mode) & S_IFMT) {
    853 		case S_IFMT:	/* used by badsect to flag bad sectors */
    854 			vattr.va_type = VBAD;
    855 			break;
    856 		case S_IFCHR:
    857 			vattr.va_type = VCHR;
    858 			break;
    859 		case S_IFBLK:
    860 			vattr.va_type = VBLK;
    861 			break;
    862 		case S_IFWHT:
    863 			whiteout = 1;
    864 			break;
    865 		default:
    866 			error = EINVAL;
    867 			break;
    868 		}
    869 	}
    870 	if (!error) {
    871 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    872 		if (whiteout) {
    873 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
    874 			if (error)
    875 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    876 			vput(nd.ni_dvp);
    877 		} else {
    878 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
    879 						&nd.ni_cnd, &vattr);
    880 		}
    881 	} else {
    882 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    883 		if (nd.ni_dvp == vp)
    884 			vrele(nd.ni_dvp);
    885 		else
    886 			vput(nd.ni_dvp);
    887 		if (vp)
    888 			vrele(vp);
    889 	}
    890 	return (error);
    891 }
    892 
    893 /*
    894  * Create a named pipe.
    895  */
    896 /* ARGSUSED */
    897 int
    898 sys_mkfifo(p, v, retval)
    899 	struct proc *p;
    900 	void *v;
    901 	register_t *retval;
    902 {
    903 #ifndef FIFO
    904 	return (EOPNOTSUPP);
    905 #else
    906 	register struct sys_mkfifo_args /* {
    907 		syscallarg(const char *) path;
    908 		syscallarg(int) mode;
    909 	} */ *uap = v;
    910 	struct vattr vattr;
    911 	int error;
    912 	struct nameidata nd;
    913 
    914 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
    915 	if ((error = namei(&nd)) != 0)
    916 		return (error);
    917 	if (nd.ni_vp != NULL) {
    918 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    919 		if (nd.ni_dvp == nd.ni_vp)
    920 			vrele(nd.ni_dvp);
    921 		else
    922 			vput(nd.ni_dvp);
    923 		vrele(nd.ni_vp);
    924 		return (EEXIST);
    925 	}
    926 	VATTR_NULL(&vattr);
    927 	vattr.va_type = VFIFO;
    928 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
    929 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    930 	return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
    931 #endif /* FIFO */
    932 }
    933 
    934 /*
    935  * Make a hard file link.
    936  */
    937 /* ARGSUSED */
    938 int
    939 sys_link(p, v, retval)
    940 	struct proc *p;
    941 	void *v;
    942 	register_t *retval;
    943 {
    944 	register struct sys_link_args /* {
    945 		syscallarg(const char *) path;
    946 		syscallarg(const char *) link;
    947 	} */ *uap = v;
    948 	register struct vnode *vp;
    949 	struct nameidata nd;
    950 	int error;
    951 
    952 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    953 	if ((error = namei(&nd)) != 0)
    954 		return (error);
    955 	vp = nd.ni_vp;
    956 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
    957 	if ((error = namei(&nd)) != 0)
    958 		goto out;
    959 	if (nd.ni_vp) {
    960 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    961 		if (nd.ni_dvp == nd.ni_vp)
    962 			vrele(nd.ni_dvp);
    963 		else
    964 			vput(nd.ni_dvp);
    965 		vrele(nd.ni_vp);
    966 		error = EEXIST;
    967 		goto out;
    968 	}
    969 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    970 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
    971 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
    972 out:
    973 	vrele(vp);
    974 	return (error);
    975 }
    976 
    977 /*
    978  * Make a symbolic link.
    979  */
    980 /* ARGSUSED */
    981 int
    982 sys_symlink(p, v, retval)
    983 	struct proc *p;
    984 	void *v;
    985 	register_t *retval;
    986 {
    987 	register struct sys_symlink_args /* {
    988 		syscallarg(const char *) path;
    989 		syscallarg(const char *) link;
    990 	} */ *uap = v;
    991 	struct vattr vattr;
    992 	char *path;
    993 	int error;
    994 	struct nameidata nd;
    995 
    996 	MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
    997 	error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
    998 	if (error)
    999 		goto out;
   1000 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1001 	if ((error = namei(&nd)) != 0)
   1002 		goto out;
   1003 	if (nd.ni_vp) {
   1004 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1005 		if (nd.ni_dvp == nd.ni_vp)
   1006 			vrele(nd.ni_dvp);
   1007 		else
   1008 			vput(nd.ni_dvp);
   1009 		vrele(nd.ni_vp);
   1010 		error = EEXIST;
   1011 		goto out;
   1012 	}
   1013 	VATTR_NULL(&vattr);
   1014 	vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
   1015 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1016 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   1017 out:
   1018 	FREE(path, M_NAMEI);
   1019 	return (error);
   1020 }
   1021 
   1022 /*
   1023  * Delete a whiteout from the filesystem.
   1024  */
   1025 /* ARGSUSED */
   1026 int
   1027 sys_undelete(p, v, retval)
   1028 	struct proc *p;
   1029 	void *v;
   1030 	register_t *retval;
   1031 {
   1032 	register struct sys_undelete_args /* {
   1033 		syscallarg(const char *) path;
   1034 	} */ *uap = v;
   1035 	int error;
   1036 	struct nameidata nd;
   1037 
   1038 	NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
   1039 	    SCARG(uap, path), p);
   1040 	error = namei(&nd);
   1041 	if (error)
   1042 		return (error);
   1043 
   1044 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   1045 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1046 		if (nd.ni_dvp == nd.ni_vp)
   1047 			vrele(nd.ni_dvp);
   1048 		else
   1049 			vput(nd.ni_dvp);
   1050 		if (nd.ni_vp)
   1051 			vrele(nd.ni_vp);
   1052 		return (EEXIST);
   1053 	}
   1054 
   1055 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1056 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   1057 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1058 	vput(nd.ni_dvp);
   1059 	return (error);
   1060 }
   1061 
   1062 /*
   1063  * Delete a name from the filesystem.
   1064  */
   1065 /* ARGSUSED */
   1066 int
   1067 sys_unlink(p, v, retval)
   1068 	struct proc *p;
   1069 	void *v;
   1070 	register_t *retval;
   1071 {
   1072 	struct sys_unlink_args /* {
   1073 		syscallarg(const char *) path;
   1074 	} */ *uap = v;
   1075 	register struct vnode *vp;
   1076 	int error;
   1077 	struct nameidata nd;
   1078 
   1079 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1080 	    SCARG(uap, path), p);
   1081 	if ((error = namei(&nd)) != 0)
   1082 		return (error);
   1083 	vp = nd.ni_vp;
   1084 
   1085 	/*
   1086 	 * The root of a mounted filesystem cannot be deleted.
   1087 	 */
   1088 	if (vp->v_flag & VROOT) {
   1089 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1090 		if (nd.ni_dvp == vp)
   1091 			vrele(nd.ni_dvp);
   1092 		else
   1093 			vput(nd.ni_dvp);
   1094 		vput(vp);
   1095 		error = EBUSY;
   1096 		goto out;
   1097 	}
   1098 
   1099 	(void)vnode_pager_uncache(vp);
   1100 
   1101 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1102 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1103 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1104 out:
   1105 	return (error);
   1106 }
   1107 
   1108 /*
   1109  * Reposition read/write file offset.
   1110  */
   1111 int
   1112 sys_lseek(p, v, retval)
   1113 	struct proc *p;
   1114 	void *v;
   1115 	register_t *retval;
   1116 {
   1117 	register struct sys_lseek_args /* {
   1118 		syscallarg(int) fd;
   1119 		syscallarg(int) pad;
   1120 		syscallarg(off_t) offset;
   1121 		syscallarg(int) whence;
   1122 	} */ *uap = v;
   1123 	struct ucred *cred = p->p_ucred;
   1124 	register struct filedesc *fdp = p->p_fd;
   1125 	register struct file *fp;
   1126 	struct vattr vattr;
   1127 	register off_t newoff;
   1128 	int error;
   1129 
   1130 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
   1131 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
   1132 		return (EBADF);
   1133 	if (fp->f_type != DTYPE_VNODE
   1134 #ifdef FIFO
   1135 	    || ((struct vnode *)fp->f_data)->v_type == VFIFO
   1136 #endif
   1137 	)
   1138 		return (ESPIPE);
   1139 	switch (SCARG(uap, whence)) {
   1140 	case L_INCR:
   1141 		newoff = fp->f_offset + SCARG(uap, offset);
   1142 		break;
   1143 	case L_XTND:
   1144 		error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr,
   1145 				    cred, p);
   1146 		if (error)
   1147 			return (error);
   1148 		newoff = SCARG(uap, offset) + vattr.va_size;
   1149 		break;
   1150 	case L_SET:
   1151 		newoff = SCARG(uap, offset);
   1152 		break;
   1153 	default:
   1154 		return (EINVAL);
   1155 	}
   1156 	if (newoff < 0)
   1157 		return (EINVAL);
   1158 
   1159 	*(off_t *)retval = fp->f_offset = newoff;
   1160 	return (0);
   1161 }
   1162 
   1163 /*
   1164  * Check access permissions.
   1165  */
   1166 int
   1167 sys_access(p, v, retval)
   1168 	struct proc *p;
   1169 	void *v;
   1170 	register_t *retval;
   1171 {
   1172 	register struct sys_access_args /* {
   1173 		syscallarg(const char *) path;
   1174 		syscallarg(int) flags;
   1175 	} */ *uap = v;
   1176 	register struct ucred *cred = p->p_ucred;
   1177 	register struct vnode *vp;
   1178 	int error, flags, t_gid, t_uid;
   1179 	struct nameidata nd;
   1180 
   1181 	t_uid = cred->cr_uid;
   1182 	t_gid = cred->cr_gid;
   1183 	cred->cr_uid = p->p_cred->p_ruid;
   1184 	cred->cr_gid = p->p_cred->p_rgid;
   1185 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1186 	    SCARG(uap, path), p);
   1187 	if ((error = namei(&nd)) != 0)
   1188 		goto out1;
   1189 	vp = nd.ni_vp;
   1190 
   1191 	/* Flags == 0 means only check for existence. */
   1192 	if (SCARG(uap, flags)) {
   1193 		flags = 0;
   1194 		if (SCARG(uap, flags) & R_OK)
   1195 			flags |= VREAD;
   1196 		if (SCARG(uap, flags) & W_OK)
   1197 			flags |= VWRITE;
   1198 		if (SCARG(uap, flags) & X_OK)
   1199 			flags |= VEXEC;
   1200 		if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
   1201 			error = VOP_ACCESS(vp, flags, cred, p);
   1202 	}
   1203 	vput(vp);
   1204 out1:
   1205 	cred->cr_uid = t_uid;
   1206 	cred->cr_gid = t_gid;
   1207 	return (error);
   1208 }
   1209 
   1210 /*
   1211  * Get file status; this version follows links.
   1212  */
   1213 /* ARGSUSED */
   1214 int
   1215 sys_stat(p, v, retval)
   1216 	struct proc *p;
   1217 	void *v;
   1218 	register_t *retval;
   1219 {
   1220 	register struct sys_stat_args /* {
   1221 		syscallarg(const char *) path;
   1222 		syscallarg(struct stat *) ub;
   1223 	} */ *uap = v;
   1224 	struct stat sb;
   1225 	int error;
   1226 	struct nameidata nd;
   1227 
   1228 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1229 	    SCARG(uap, path), p);
   1230 	if ((error = namei(&nd)) != 0)
   1231 		return (error);
   1232 	error = vn_stat(nd.ni_vp, &sb, p);
   1233 	vput(nd.ni_vp);
   1234 	if (error)
   1235 		return (error);
   1236 	error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
   1237 	return (error);
   1238 }
   1239 
   1240 /*
   1241  * Get file status; this version does not follow links.
   1242  */
   1243 /* ARGSUSED */
   1244 int
   1245 sys_lstat(p, v, retval)
   1246 	struct proc *p;
   1247 	void *v;
   1248 	register_t *retval;
   1249 {
   1250 	register struct sys_lstat_args /* {
   1251 		syscallarg(const char *) path;
   1252 		syscallarg(struct stat *) ub;
   1253 	} */ *uap = v;
   1254 	struct stat sb;
   1255 	int error;
   1256 	struct nameidata nd;
   1257 
   1258 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   1259 	    SCARG(uap, path), p);
   1260 	if ((error = namei(&nd)) != 0)
   1261 		return (error);
   1262 	error = vn_stat(nd.ni_vp, &sb, p);
   1263 	vput(nd.ni_vp);
   1264 	if (error)
   1265 		return (error);
   1266 	error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
   1267 	return (error);
   1268 }
   1269 
   1270 /*
   1271  * Get configurable pathname variables.
   1272  */
   1273 /* ARGSUSED */
   1274 int
   1275 sys_pathconf(p, v, retval)
   1276 	struct proc *p;
   1277 	void *v;
   1278 	register_t *retval;
   1279 {
   1280 	register struct sys_pathconf_args /* {
   1281 		syscallarg(const char *) path;
   1282 		syscallarg(int) name;
   1283 	} */ *uap = v;
   1284 	int error;
   1285 	struct nameidata nd;
   1286 
   1287 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1288 	    SCARG(uap, path), p);
   1289 	if ((error = namei(&nd)) != 0)
   1290 		return (error);
   1291 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   1292 	vput(nd.ni_vp);
   1293 	return (error);
   1294 }
   1295 
   1296 /*
   1297  * Return target name of a symbolic link.
   1298  */
   1299 /* ARGSUSED */
   1300 int
   1301 sys_readlink(p, v, retval)
   1302 	struct proc *p;
   1303 	void *v;
   1304 	register_t *retval;
   1305 {
   1306 	register struct sys_readlink_args /* {
   1307 		syscallarg(const char *) path;
   1308 		syscallarg(char *) buf;
   1309 		syscallarg(int) count;
   1310 	} */ *uap = v;
   1311 	register struct vnode *vp;
   1312 	struct iovec aiov;
   1313 	struct uio auio;
   1314 	int error;
   1315 	struct nameidata nd;
   1316 
   1317 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   1318 	    SCARG(uap, path), p);
   1319 	if ((error = namei(&nd)) != 0)
   1320 		return (error);
   1321 	vp = nd.ni_vp;
   1322 	if (vp->v_type != VLNK)
   1323 		error = EINVAL;
   1324 	else {
   1325 		aiov.iov_base = SCARG(uap, buf);
   1326 		aiov.iov_len = SCARG(uap, count);
   1327 		auio.uio_iov = &aiov;
   1328 		auio.uio_iovcnt = 1;
   1329 		auio.uio_offset = 0;
   1330 		auio.uio_rw = UIO_READ;
   1331 		auio.uio_segflg = UIO_USERSPACE;
   1332 		auio.uio_procp = p;
   1333 		auio.uio_resid = SCARG(uap, count);
   1334 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   1335 	}
   1336 	vput(vp);
   1337 	*retval = SCARG(uap, count) - auio.uio_resid;
   1338 	return (error);
   1339 }
   1340 
   1341 /*
   1342  * Change flags of a file given a path name.
   1343  */
   1344 /* ARGSUSED */
   1345 int
   1346 sys_chflags(p, v, retval)
   1347 	struct proc *p;
   1348 	void *v;
   1349 	register_t *retval;
   1350 {
   1351 	register struct sys_chflags_args /* {
   1352 		syscallarg(const char *) path;
   1353 		syscallarg(u_long) flags;
   1354 	} */ *uap = v;
   1355 	register struct vnode *vp;
   1356 	struct vattr vattr;
   1357 	int error;
   1358 	struct nameidata nd;
   1359 
   1360 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1361 	if ((error = namei(&nd)) != 0)
   1362 		return (error);
   1363 	vp = nd.ni_vp;
   1364 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1365 	VOP_LOCK(vp);
   1366 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1367 		error = EROFS;
   1368 	else {
   1369 		VATTR_NULL(&vattr);
   1370 		vattr.va_flags = SCARG(uap, flags);
   1371 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1372 	}
   1373 	vput(vp);
   1374 	return (error);
   1375 }
   1376 
   1377 /*
   1378  * Change flags of a file given a file descriptor.
   1379  */
   1380 /* ARGSUSED */
   1381 int
   1382 sys_fchflags(p, v, retval)
   1383 	struct proc *p;
   1384 	void *v;
   1385 	register_t *retval;
   1386 {
   1387 	register struct sys_fchflags_args /* {
   1388 		syscallarg(int) fd;
   1389 		syscallarg(u_long) flags;
   1390 	} */ *uap = v;
   1391 	struct vattr vattr;
   1392 	struct vnode *vp;
   1393 	struct file *fp;
   1394 	int error;
   1395 
   1396 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1397 		return (error);
   1398 	vp = (struct vnode *)fp->f_data;
   1399 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1400 	VOP_LOCK(vp);
   1401 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1402 		error = EROFS;
   1403 	else {
   1404 		VATTR_NULL(&vattr);
   1405 		vattr.va_flags = SCARG(uap, flags);
   1406 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1407 	}
   1408 	VOP_UNLOCK(vp);
   1409 	return (error);
   1410 }
   1411 
   1412 /*
   1413  * Change mode of a file given path name.
   1414  */
   1415 /* ARGSUSED */
   1416 int
   1417 sys_chmod(p, v, retval)
   1418 	struct proc *p;
   1419 	void *v;
   1420 	register_t *retval;
   1421 {
   1422 	register struct sys_chmod_args /* {
   1423 		syscallarg(const char *) path;
   1424 		syscallarg(int) mode;
   1425 	} */ *uap = v;
   1426 	register struct vnode *vp;
   1427 	struct vattr vattr;
   1428 	int error;
   1429 	struct nameidata nd;
   1430 
   1431 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1432 	if ((error = namei(&nd)) != 0)
   1433 		return (error);
   1434 	vp = nd.ni_vp;
   1435 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1436 	VOP_LOCK(vp);
   1437 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1438 		error = EROFS;
   1439 	else {
   1440 		VATTR_NULL(&vattr);
   1441 		vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
   1442 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1443 	}
   1444 	vput(vp);
   1445 	return (error);
   1446 }
   1447 
   1448 /*
   1449  * Change mode of a file given a file descriptor.
   1450  */
   1451 /* ARGSUSED */
   1452 int
   1453 sys_fchmod(p, v, retval)
   1454 	struct proc *p;
   1455 	void *v;
   1456 	register_t *retval;
   1457 {
   1458 	register struct sys_fchmod_args /* {
   1459 		syscallarg(int) fd;
   1460 		syscallarg(int) mode;
   1461 	} */ *uap = v;
   1462 	struct vattr vattr;
   1463 	struct vnode *vp;
   1464 	struct file *fp;
   1465 	int error;
   1466 
   1467 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1468 		return (error);
   1469 	vp = (struct vnode *)fp->f_data;
   1470 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1471 	VOP_LOCK(vp);
   1472 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1473 		error = EROFS;
   1474 	else {
   1475 		VATTR_NULL(&vattr);
   1476 		vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
   1477 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1478 	}
   1479 	VOP_UNLOCK(vp);
   1480 	return (error);
   1481 }
   1482 
   1483 /*
   1484  * Set ownership given a path name.
   1485  */
   1486 /* ARGSUSED */
   1487 int
   1488 sys_chown(p, v, retval)
   1489 	struct proc *p;
   1490 	void *v;
   1491 	register_t *retval;
   1492 {
   1493 	register struct sys_chown_args /* {
   1494 		syscallarg(const char *) path;
   1495 		syscallarg(uid_t) uid;
   1496 		syscallarg(gid_t) gid;
   1497 	} */ *uap = v;
   1498 	register struct vnode *vp;
   1499 	struct vattr vattr;
   1500 	int error;
   1501 	struct nameidata nd;
   1502 
   1503 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1504 	if ((error = namei(&nd)) != 0)
   1505 		return (error);
   1506 	vp = nd.ni_vp;
   1507 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1508 	VOP_LOCK(vp);
   1509 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1510 		error = EROFS;
   1511 	else {
   1512 		VATTR_NULL(&vattr);
   1513 		vattr.va_uid = SCARG(uap, uid);
   1514 		vattr.va_gid = SCARG(uap, gid);
   1515 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1516 	}
   1517 	vput(vp);
   1518 	return (error);
   1519 }
   1520 
   1521 /*
   1522  * Set ownership given a file descriptor.
   1523  */
   1524 /* ARGSUSED */
   1525 int
   1526 sys_fchown(p, v, retval)
   1527 	struct proc *p;
   1528 	void *v;
   1529 	register_t *retval;
   1530 {
   1531 	register struct sys_fchown_args /* {
   1532 		syscallarg(int) fd;
   1533 		syscallarg(uid_t) uid;
   1534 		syscallarg(gid_t) gid;
   1535 	} */ *uap = v;
   1536 	register struct vnode *vp;
   1537 	struct vattr vattr;
   1538 	int error;
   1539 	struct file *fp;
   1540 
   1541 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1542 		return (error);
   1543 	vp = (struct vnode *)fp->f_data;
   1544 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1545 	VOP_LOCK(vp);
   1546 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1547 		error = EROFS;
   1548 	else {
   1549 		VATTR_NULL(&vattr);
   1550 		vattr.va_uid = SCARG(uap, uid);
   1551 		vattr.va_gid = SCARG(uap, gid);
   1552 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1553 	}
   1554 	VOP_UNLOCK(vp);
   1555 	return (error);
   1556 }
   1557 
   1558 /*
   1559  * Set the access and modification times given a path name.
   1560  */
   1561 /* ARGSUSED */
   1562 int
   1563 sys_utimes(p, v, retval)
   1564 	struct proc *p;
   1565 	void *v;
   1566 	register_t *retval;
   1567 {
   1568 	register struct sys_utimes_args /* {
   1569 		syscallarg(const char *) path;
   1570 		syscallarg(const struct timeval *) tptr;
   1571 	} */ *uap = v;
   1572 	register struct vnode *vp;
   1573 	struct timeval tv[2];
   1574 	struct vattr vattr;
   1575 	int error;
   1576 	struct nameidata nd;
   1577 
   1578 	VATTR_NULL(&vattr);
   1579 	if (SCARG(uap, tptr) == NULL) {
   1580 		microtime(&tv[0]);
   1581 		tv[1] = tv[0];
   1582 		vattr.va_vaflags |= VA_UTIMES_NULL;
   1583 	} else {
   1584 		error = copyin(SCARG(uap, tptr), tv, sizeof (tv));
   1585 		if (error)
   1586 			return (error);
   1587 	}
   1588 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1589 	if ((error = namei(&nd)) != 0)
   1590 		return (error);
   1591 	vp = nd.ni_vp;
   1592 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1593 	VOP_LOCK(vp);
   1594 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1595 		error = EROFS;
   1596 	else {
   1597 		vattr.va_atime.tv_sec = tv[0].tv_sec;
   1598 		vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   1599 		vattr.va_mtime.tv_sec = tv[1].tv_sec;
   1600 		vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   1601 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1602 	}
   1603 	vput(vp);
   1604 	return (error);
   1605 }
   1606 
   1607 /*
   1608  * Set the access and modification times given a file descriptor.
   1609  */
   1610 /* ARGSUSED */
   1611 int
   1612 sys_futimes(p, v, retval)
   1613 	struct proc *p;
   1614 	void *v;
   1615 	register_t *retval;
   1616 {
   1617 	register struct sys_futimes_args /* {
   1618 		syscallarg(int) fd;
   1619 		syscallarg(const struct timeval *) tptr;
   1620 	} */ *uap = v;
   1621 	register struct vnode *vp;
   1622 	struct timeval tv[2];
   1623 	struct vattr vattr;
   1624 	int error;
   1625 	struct file *fp;
   1626 
   1627 	VATTR_NULL(&vattr);
   1628 	if (SCARG(uap, tptr) == NULL) {
   1629 		microtime(&tv[0]);
   1630 		tv[1] = tv[0];
   1631 		vattr.va_vaflags |= VA_UTIMES_NULL;
   1632 	} else {
   1633 		error = copyin(SCARG(uap, tptr), tv, sizeof (tv));
   1634 		if (error)
   1635 			return (error);
   1636 	}
   1637 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1638 		return (error);
   1639 	vp = (struct vnode *)fp->f_data;
   1640 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1641 	VOP_LOCK(vp);
   1642 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1643 		error = EROFS;
   1644 	else {
   1645 		vattr.va_atime.tv_sec = tv[0].tv_sec;
   1646 		vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   1647 		vattr.va_mtime.tv_sec = tv[1].tv_sec;
   1648 		vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   1649 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1650 	}
   1651 	VOP_UNLOCK(vp);
   1652 	return (error);
   1653 }
   1654 
   1655 /*
   1656  * Truncate a file given its path name.
   1657  */
   1658 /* ARGSUSED */
   1659 int
   1660 sys_truncate(p, v, retval)
   1661 	struct proc *p;
   1662 	void *v;
   1663 	register_t *retval;
   1664 {
   1665 	register struct sys_truncate_args /* {
   1666 		syscallarg(const char *) path;
   1667 		syscallarg(int) pad;
   1668 		syscallarg(off_t) length;
   1669 	} */ *uap = v;
   1670 	register struct vnode *vp;
   1671 	struct vattr vattr;
   1672 	int error;
   1673 	struct nameidata nd;
   1674 
   1675 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1676 	if ((error = namei(&nd)) != 0)
   1677 		return (error);
   1678 	vp = nd.ni_vp;
   1679 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1680 	VOP_LOCK(vp);
   1681 	if (vp->v_type == VDIR)
   1682 		error = EISDIR;
   1683 	else if ((error = vn_writechk(vp)) == 0 &&
   1684 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   1685 		VATTR_NULL(&vattr);
   1686 		vattr.va_size = SCARG(uap, length);
   1687 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1688 	}
   1689 	vput(vp);
   1690 	return (error);
   1691 }
   1692 
   1693 /*
   1694  * Truncate a file given a file descriptor.
   1695  */
   1696 /* ARGSUSED */
   1697 int
   1698 sys_ftruncate(p, v, retval)
   1699 	struct proc *p;
   1700 	void *v;
   1701 	register_t *retval;
   1702 {
   1703 	register struct sys_ftruncate_args /* {
   1704 		syscallarg(int) fd;
   1705 		syscallarg(int) pad;
   1706 		syscallarg(off_t) length;
   1707 	} */ *uap = v;
   1708 	struct vattr vattr;
   1709 	struct vnode *vp;
   1710 	struct file *fp;
   1711 	int error;
   1712 
   1713 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1714 		return (error);
   1715 	if ((fp->f_flag & FWRITE) == 0)
   1716 		return (EINVAL);
   1717 	vp = (struct vnode *)fp->f_data;
   1718 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1719 	VOP_LOCK(vp);
   1720 	if (vp->v_type == VDIR)
   1721 		error = EISDIR;
   1722 	else if ((error = vn_writechk(vp)) == 0) {
   1723 		VATTR_NULL(&vattr);
   1724 		vattr.va_size = SCARG(uap, length);
   1725 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   1726 	}
   1727 	VOP_UNLOCK(vp);
   1728 	return (error);
   1729 }
   1730 
   1731 /*
   1732  * Sync an open file.
   1733  */
   1734 /* ARGSUSED */
   1735 int
   1736 sys_fsync(p, v, retval)
   1737 	struct proc *p;
   1738 	void *v;
   1739 	register_t *retval;
   1740 {
   1741 	struct sys_fsync_args /* {
   1742 		syscallarg(int) fd;
   1743 	} */ *uap = v;
   1744 	register struct vnode *vp;
   1745 	struct file *fp;
   1746 	int error;
   1747 
   1748 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1749 		return (error);
   1750 	vp = (struct vnode *)fp->f_data;
   1751 	VOP_LOCK(vp);
   1752 	error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
   1753 	VOP_UNLOCK(vp);
   1754 	return (error);
   1755 }
   1756 
   1757 /*
   1758  * Rename files.  Source and destination must either both be directories,
   1759  * or both not be directories.  If target is a directory, it must be empty.
   1760  */
   1761 /* ARGSUSED */
   1762 int
   1763 sys_rename(p, v, retval)
   1764 	struct proc *p;
   1765 	void *v;
   1766 	register_t *retval;
   1767 {
   1768 	register struct sys_rename_args /* {
   1769 		syscallarg(const char *) from;
   1770 		syscallarg(const char *) to;
   1771 	} */ *uap = v;
   1772 	register struct vnode *tvp, *fvp, *tdvp;
   1773 	struct nameidata fromnd, tond;
   1774 	int error;
   1775 
   1776 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   1777 	    SCARG(uap, from), p);
   1778 	if ((error = namei(&fromnd)) != 0)
   1779 		return (error);
   1780 	fvp = fromnd.ni_vp;
   1781 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
   1782 	    UIO_USERSPACE, SCARG(uap, to), p);
   1783 	if ((error = namei(&tond)) != 0) {
   1784 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1785 		vrele(fromnd.ni_dvp);
   1786 		vrele(fvp);
   1787 		goto out1;
   1788 	}
   1789 	tdvp = tond.ni_dvp;
   1790 	tvp = tond.ni_vp;
   1791 	if (tvp != NULL) {
   1792 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   1793 			error = ENOTDIR;
   1794 			goto out;
   1795 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   1796 			error = EISDIR;
   1797 			goto out;
   1798 		}
   1799 	}
   1800 	if (fvp == tdvp)
   1801 		error = EINVAL;
   1802 	/*
   1803 	 * If source is the same as the destination (that is the
   1804 	 * same inode number with the same name in the same directory),
   1805 	 * then there is nothing to do.
   1806 	 */
   1807 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
   1808 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   1809 	    !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
   1810 	      fromnd.ni_cnd.cn_namelen))
   1811 		error = -1;
   1812 out:
   1813 	if (!error) {
   1814 		VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
   1815 		if (fromnd.ni_dvp != tdvp)
   1816 			VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1817 		if (tvp) {
   1818 			(void)vnode_pager_uncache(tvp);
   1819 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   1820 		}
   1821 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   1822 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   1823 	} else {
   1824 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   1825 		if (tdvp == tvp)
   1826 			vrele(tdvp);
   1827 		else
   1828 			vput(tdvp);
   1829 		if (tvp)
   1830 			vput(tvp);
   1831 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1832 		vrele(fromnd.ni_dvp);
   1833 		vrele(fvp);
   1834 	}
   1835 	vrele(tond.ni_startdir);
   1836 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1837 out1:
   1838 	if (fromnd.ni_startdir)
   1839 		vrele(fromnd.ni_startdir);
   1840 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1841 	return (error == -1 ? 0 : error);
   1842 }
   1843 
   1844 /*
   1845  * Make a directory file.
   1846  */
   1847 /* ARGSUSED */
   1848 int
   1849 sys_mkdir(p, v, retval)
   1850 	struct proc *p;
   1851 	void *v;
   1852 	register_t *retval;
   1853 {
   1854 	register struct sys_mkdir_args /* {
   1855 		syscallarg(const char *) path;
   1856 		syscallarg(int) mode;
   1857 	} */ *uap = v;
   1858 	register struct vnode *vp;
   1859 	struct vattr vattr;
   1860 	int error;
   1861 	struct nameidata nd;
   1862 
   1863 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1864 	if ((error = namei(&nd)) != 0)
   1865 		return (error);
   1866 	vp = nd.ni_vp;
   1867 	if (vp != NULL) {
   1868 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1869 		if (nd.ni_dvp == vp)
   1870 			vrele(nd.ni_dvp);
   1871 		else
   1872 			vput(nd.ni_dvp);
   1873 		vrele(vp);
   1874 		return (EEXIST);
   1875 	}
   1876 	VATTR_NULL(&vattr);
   1877 	vattr.va_type = VDIR;
   1878 	vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
   1879 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1880 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1881 	if (!error)
   1882 		vput(nd.ni_vp);
   1883 	return (error);
   1884 }
   1885 
   1886 /*
   1887  * Remove a directory file.
   1888  */
   1889 /* ARGSUSED */
   1890 int
   1891 sys_rmdir(p, v, retval)
   1892 	struct proc *p;
   1893 	void *v;
   1894 	register_t *retval;
   1895 {
   1896 	struct sys_rmdir_args /* {
   1897 		syscallarg(const char *) path;
   1898 	} */ *uap = v;
   1899 	register struct vnode *vp;
   1900 	int error;
   1901 	struct nameidata nd;
   1902 
   1903 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1904 	    SCARG(uap, path), p);
   1905 	if ((error = namei(&nd)) != 0)
   1906 		return (error);
   1907 	vp = nd.ni_vp;
   1908 	if (vp->v_type != VDIR) {
   1909 		error = ENOTDIR;
   1910 		goto out;
   1911 	}
   1912 	/*
   1913 	 * No rmdir "." please.
   1914 	 */
   1915 	if (nd.ni_dvp == vp) {
   1916 		error = EINVAL;
   1917 		goto out;
   1918 	}
   1919 	/*
   1920 	 * The root of a mounted filesystem cannot be deleted.
   1921 	 */
   1922 	if (vp->v_flag & VROOT)
   1923 		error = EBUSY;
   1924 out:
   1925 	if (!error) {
   1926 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1927 		VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1928 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1929 	} else {
   1930 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1931 		if (nd.ni_dvp == vp)
   1932 			vrele(nd.ni_dvp);
   1933 		else
   1934 			vput(nd.ni_dvp);
   1935 		vput(vp);
   1936 	}
   1937 	return (error);
   1938 }
   1939 
   1940 /*
   1941  * Read a block of directory entries in a file system independent format.
   1942  */
   1943 int
   1944 sys_getdirentries(p, v, retval)
   1945 	struct proc *p;
   1946 	void *v;
   1947 	register_t *retval;
   1948 {
   1949 	register struct sys_getdirentries_args /* {
   1950 		syscallarg(int) fd;
   1951 		syscallarg(char *) buf;
   1952 		syscallarg(u_int) count;
   1953 		syscallarg(long *) basep;
   1954 	} */ *uap = v;
   1955 	register struct vnode *vp;
   1956 	struct file *fp;
   1957 	struct uio auio;
   1958 	struct iovec aiov;
   1959 	long loff;
   1960 	int error, eofflag;
   1961 
   1962 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1963 		return (error);
   1964 	if ((fp->f_flag & FREAD) == 0)
   1965 		return (EBADF);
   1966 	vp = (struct vnode *)fp->f_data;
   1967 unionread:
   1968 	if (vp->v_type != VDIR)
   1969 		return (EINVAL);
   1970 	aiov.iov_base = SCARG(uap, buf);
   1971 	aiov.iov_len = SCARG(uap, count);
   1972 	auio.uio_iov = &aiov;
   1973 	auio.uio_iovcnt = 1;
   1974 	auio.uio_rw = UIO_READ;
   1975 	auio.uio_segflg = UIO_USERSPACE;
   1976 	auio.uio_procp = p;
   1977 	auio.uio_resid = SCARG(uap, count);
   1978 	VOP_LOCK(vp);
   1979 	loff = auio.uio_offset = fp->f_offset;
   1980 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
   1981 	fp->f_offset = auio.uio_offset;
   1982 	VOP_UNLOCK(vp);
   1983 	if (error)
   1984 		return (error);
   1985 
   1986 #ifdef UNION
   1987 {
   1988 	extern int (**union_vnodeop_p) __P((void *));
   1989 	extern struct vnode *union_dircache __P((struct vnode *));
   1990 
   1991 	if ((SCARG(uap, count) == auio.uio_resid) &&
   1992 	    (vp->v_op == union_vnodeop_p)) {
   1993 		struct vnode *lvp;
   1994 
   1995 		lvp = union_dircache(vp);
   1996 		if (lvp != NULLVP) {
   1997 			struct vattr va;
   1998 
   1999 			/*
   2000 			 * If the directory is opaque,
   2001 			 * then don't show lower entries
   2002 			 */
   2003 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
   2004 			if (va.va_flags & OPAQUE) {
   2005 				vput(lvp);
   2006 				lvp = NULL;
   2007 			}
   2008 		}
   2009 
   2010 		if (lvp != NULLVP) {
   2011 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
   2012 			VOP_UNLOCK(lvp);
   2013 
   2014 			if (error) {
   2015 				vrele(lvp);
   2016 				return (error);
   2017 			}
   2018 			fp->f_data = (caddr_t) lvp;
   2019 			fp->f_offset = 0;
   2020 			error = vn_close(vp, FREAD, fp->f_cred, p);
   2021 			if (error)
   2022 				return (error);
   2023 			vp = lvp;
   2024 			goto unionread;
   2025 		}
   2026 	}
   2027 }
   2028 #endif /* UNION */
   2029 
   2030 	if ((SCARG(uap, count) == auio.uio_resid) &&
   2031 	    (vp->v_flag & VROOT) &&
   2032 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
   2033 		struct vnode *tvp = vp;
   2034 		vp = vp->v_mount->mnt_vnodecovered;
   2035 		VREF(vp);
   2036 		fp->f_data = (caddr_t) vp;
   2037 		fp->f_offset = 0;
   2038 		vrele(tvp);
   2039 		goto unionread;
   2040 	}
   2041 	error = copyout(&loff, SCARG(uap, basep), sizeof(long));
   2042 	*retval = SCARG(uap, count) - auio.uio_resid;
   2043 	return (error);
   2044 }
   2045 
   2046 /*
   2047  * Set the mode mask for creation of filesystem nodes.
   2048  */
   2049 int
   2050 sys_umask(p, v, retval)
   2051 	struct proc *p;
   2052 	void *v;
   2053 	register_t *retval;
   2054 {
   2055 	struct sys_umask_args /* {
   2056 		syscallarg(int) newmask;
   2057 	} */ *uap = v;
   2058 	register struct filedesc *fdp;
   2059 
   2060 	fdp = p->p_fd;
   2061 	*retval = fdp->fd_cmask;
   2062 	fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
   2063 	return (0);
   2064 }
   2065 
   2066 /*
   2067  * Void all references to file by ripping underlying filesystem
   2068  * away from vnode.
   2069  */
   2070 /* ARGSUSED */
   2071 int
   2072 sys_revoke(p, v, retval)
   2073 	struct proc *p;
   2074 	void *v;
   2075 	register_t *retval;
   2076 {
   2077 	register struct sys_revoke_args /* {
   2078 		syscallarg(const char *) path;
   2079 	} */ *uap = v;
   2080 	register struct vnode *vp;
   2081 	struct vattr vattr;
   2082 	int error;
   2083 	struct nameidata nd;
   2084 
   2085 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2086 	if ((error = namei(&nd)) != 0)
   2087 		return (error);
   2088 	vp = nd.ni_vp;
   2089 	if (vp->v_type != VCHR && vp->v_type != VBLK) {
   2090 		error = EINVAL;
   2091 		goto out;
   2092 	}
   2093 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2094 		goto out;
   2095 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   2096 	    (error = suser(p->p_ucred, &p->p_acflag)))
   2097 		goto out;
   2098 	if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
   2099 		vgoneall(vp);
   2100 out:
   2101 	vrele(vp);
   2102 	return (error);
   2103 }
   2104 
   2105 /*
   2106  * Convert a user file descriptor to a kernel file entry.
   2107  */
   2108 int
   2109 getvnode(fdp, fd, fpp)
   2110 	struct filedesc *fdp;
   2111 	int fd;
   2112 	struct file **fpp;
   2113 {
   2114 	struct vnode *vp;
   2115 	struct file *fp;
   2116 
   2117 	if ((u_int)fd >= fdp->fd_nfiles ||
   2118 	    (fp = fdp->fd_ofiles[fd]) == NULL)
   2119 		return (EBADF);
   2120 	if (fp->f_type != DTYPE_VNODE)
   2121 		return (EINVAL);
   2122 	vp = (struct vnode *)fp->f_data;
   2123 	if (vp->v_type == VBAD)
   2124 		return (EBADF);
   2125 	*fpp = fp;
   2126 	return (0);
   2127 }
   2128