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