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