Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.63
      1 /*	$NetBSD: vfs_syscalls.c,v 1.63 1996/02/04 02:18:37 christos 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 	/*
   1261 	 * For symbolic links, always return the attributes of its
   1262 	 * containing directory, except for mode, size, and links.
   1263 	 */
   1264 	vp = nd.ni_vp;
   1265 	dvp = nd.ni_dvp;
   1266 	if (vp->v_type != VLNK) {
   1267 		if (dvp == vp)
   1268 			vrele(dvp);
   1269 		else
   1270 			vput(dvp);
   1271 		error = vn_stat(vp, &sb, p);
   1272 		vput(vp);
   1273 		if (error)
   1274 			return (error);
   1275 	} else {
   1276 		error = vn_stat(dvp, &sb, p);
   1277 		vput(dvp);
   1278 		if (error) {
   1279 			vput(vp);
   1280 			return (error);
   1281 		}
   1282 		error = vn_stat(vp, &sb1, p);
   1283 		vput(vp);
   1284 		if (error)
   1285 			return (error);
   1286 		sb.st_mode &= ~S_IFDIR;
   1287 		sb.st_mode |= S_IFLNK;
   1288 		sb.st_nlink = sb1.st_nlink;
   1289 		sb.st_size = sb1.st_size;
   1290 		sb.st_blocks = sb1.st_blocks;
   1291 	}
   1292 	error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
   1293 	return (error);
   1294 }
   1295 
   1296 /*
   1297  * Get configurable pathname variables.
   1298  */
   1299 /* ARGSUSED */
   1300 int
   1301 sys_pathconf(p, v, retval)
   1302 	struct proc *p;
   1303 	void *v;
   1304 	register_t *retval;
   1305 {
   1306 	register struct sys_pathconf_args /* {
   1307 		syscallarg(char *) path;
   1308 		syscallarg(int) name;
   1309 	} */ *uap = v;
   1310 	int error;
   1311 	struct nameidata nd;
   1312 
   1313 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1314 	    SCARG(uap, path), p);
   1315 	if ((error = namei(&nd)) != 0)
   1316 		return (error);
   1317 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   1318 	vput(nd.ni_vp);
   1319 	return (error);
   1320 }
   1321 
   1322 /*
   1323  * Return target name of a symbolic link.
   1324  */
   1325 /* ARGSUSED */
   1326 int
   1327 sys_readlink(p, v, retval)
   1328 	struct proc *p;
   1329 	void *v;
   1330 	register_t *retval;
   1331 {
   1332 	register struct sys_readlink_args /* {
   1333 		syscallarg(char *) path;
   1334 		syscallarg(char *) buf;
   1335 		syscallarg(int) count;
   1336 	} */ *uap = v;
   1337 	register struct vnode *vp;
   1338 	struct iovec aiov;
   1339 	struct uio auio;
   1340 	int error;
   1341 	struct nameidata nd;
   1342 
   1343 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   1344 	    SCARG(uap, path), p);
   1345 	if ((error = namei(&nd)) != 0)
   1346 		return (error);
   1347 	vp = nd.ni_vp;
   1348 	if (vp->v_type != VLNK)
   1349 		error = EINVAL;
   1350 	else {
   1351 		aiov.iov_base = SCARG(uap, buf);
   1352 		aiov.iov_len = SCARG(uap, count);
   1353 		auio.uio_iov = &aiov;
   1354 		auio.uio_iovcnt = 1;
   1355 		auio.uio_offset = 0;
   1356 		auio.uio_rw = UIO_READ;
   1357 		auio.uio_segflg = UIO_USERSPACE;
   1358 		auio.uio_procp = p;
   1359 		auio.uio_resid = SCARG(uap, count);
   1360 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   1361 	}
   1362 	vput(vp);
   1363 	*retval = SCARG(uap, count) - auio.uio_resid;
   1364 	return (error);
   1365 }
   1366 
   1367 /*
   1368  * Change flags of a file given a path name.
   1369  */
   1370 /* ARGSUSED */
   1371 int
   1372 sys_chflags(p, v, retval)
   1373 	struct proc *p;
   1374 	void *v;
   1375 	register_t *retval;
   1376 {
   1377 	register struct sys_chflags_args /* {
   1378 		syscallarg(char *) path;
   1379 		syscallarg(int) flags;
   1380 	} */ *uap = v;
   1381 	register struct vnode *vp;
   1382 	struct vattr vattr;
   1383 	int error;
   1384 	struct nameidata nd;
   1385 
   1386 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1387 	if ((error = namei(&nd)) != 0)
   1388 		return (error);
   1389 	vp = nd.ni_vp;
   1390 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1391 	VOP_LOCK(vp);
   1392 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1393 		error = EROFS;
   1394 	else {
   1395 		VATTR_NULL(&vattr);
   1396 		vattr.va_flags = SCARG(uap, flags);
   1397 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1398 	}
   1399 	vput(vp);
   1400 	return (error);
   1401 }
   1402 
   1403 /*
   1404  * Change flags of a file given a file descriptor.
   1405  */
   1406 /* ARGSUSED */
   1407 int
   1408 sys_fchflags(p, v, retval)
   1409 	struct proc *p;
   1410 	void *v;
   1411 	register_t *retval;
   1412 {
   1413 	register struct sys_fchflags_args /* {
   1414 		syscallarg(int) fd;
   1415 		syscallarg(int) flags;
   1416 	} */ *uap = v;
   1417 	struct vattr vattr;
   1418 	struct vnode *vp;
   1419 	struct file *fp;
   1420 	int error;
   1421 
   1422 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1423 		return (error);
   1424 	vp = (struct vnode *)fp->f_data;
   1425 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1426 	VOP_LOCK(vp);
   1427 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1428 		error = EROFS;
   1429 	else {
   1430 		VATTR_NULL(&vattr);
   1431 		vattr.va_flags = SCARG(uap, flags);
   1432 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1433 	}
   1434 	VOP_UNLOCK(vp);
   1435 	return (error);
   1436 }
   1437 
   1438 /*
   1439  * Change mode of a file given path name.
   1440  */
   1441 /* ARGSUSED */
   1442 int
   1443 sys_chmod(p, v, retval)
   1444 	struct proc *p;
   1445 	void *v;
   1446 	register_t *retval;
   1447 {
   1448 	register struct sys_chmod_args /* {
   1449 		syscallarg(char *) path;
   1450 		syscallarg(int) mode;
   1451 	} */ *uap = v;
   1452 	register struct vnode *vp;
   1453 	struct vattr vattr;
   1454 	int error;
   1455 	struct nameidata nd;
   1456 
   1457 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1458 	if ((error = namei(&nd)) != 0)
   1459 		return (error);
   1460 	vp = nd.ni_vp;
   1461 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1462 	VOP_LOCK(vp);
   1463 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1464 		error = EROFS;
   1465 	else {
   1466 		VATTR_NULL(&vattr);
   1467 		vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
   1468 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1469 	}
   1470 	vput(vp);
   1471 	return (error);
   1472 }
   1473 
   1474 /*
   1475  * Change mode of a file given a file descriptor.
   1476  */
   1477 /* ARGSUSED */
   1478 int
   1479 sys_fchmod(p, v, retval)
   1480 	struct proc *p;
   1481 	void *v;
   1482 	register_t *retval;
   1483 {
   1484 	register struct sys_fchmod_args /* {
   1485 		syscallarg(int) fd;
   1486 		syscallarg(int) mode;
   1487 	} */ *uap = v;
   1488 	struct vattr vattr;
   1489 	struct vnode *vp;
   1490 	struct file *fp;
   1491 	int error;
   1492 
   1493 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1494 		return (error);
   1495 	vp = (struct vnode *)fp->f_data;
   1496 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1497 	VOP_LOCK(vp);
   1498 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1499 		error = EROFS;
   1500 	else {
   1501 		VATTR_NULL(&vattr);
   1502 		vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
   1503 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1504 	}
   1505 	VOP_UNLOCK(vp);
   1506 	return (error);
   1507 }
   1508 
   1509 /*
   1510  * Set ownership given a path name.
   1511  */
   1512 /* ARGSUSED */
   1513 int
   1514 sys_chown(p, v, retval)
   1515 	struct proc *p;
   1516 	void *v;
   1517 	register_t *retval;
   1518 {
   1519 	register struct sys_chown_args /* {
   1520 		syscallarg(char *) path;
   1521 		syscallarg(int) uid;
   1522 		syscallarg(int) gid;
   1523 	} */ *uap = v;
   1524 	register struct vnode *vp;
   1525 	struct vattr vattr;
   1526 	int error;
   1527 	struct nameidata nd;
   1528 
   1529 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1530 	if ((error = namei(&nd)) != 0)
   1531 		return (error);
   1532 	vp = nd.ni_vp;
   1533 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1534 	VOP_LOCK(vp);
   1535 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1536 		error = EROFS;
   1537 	else {
   1538 		VATTR_NULL(&vattr);
   1539 		vattr.va_uid = SCARG(uap, uid);
   1540 		vattr.va_gid = SCARG(uap, gid);
   1541 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1542 	}
   1543 	vput(vp);
   1544 	return (error);
   1545 }
   1546 
   1547 /*
   1548  * Set ownership given a file descriptor.
   1549  */
   1550 /* ARGSUSED */
   1551 int
   1552 sys_fchown(p, v, retval)
   1553 	struct proc *p;
   1554 	void *v;
   1555 	register_t *retval;
   1556 {
   1557 	register struct sys_fchown_args /* {
   1558 		syscallarg(int) fd;
   1559 		syscallarg(int) uid;
   1560 		syscallarg(int) gid;
   1561 	} */ *uap = v;
   1562 	struct vattr vattr;
   1563 	struct vnode *vp;
   1564 	struct file *fp;
   1565 	int error;
   1566 
   1567 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1568 		return (error);
   1569 	vp = (struct vnode *)fp->f_data;
   1570 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1571 	VOP_LOCK(vp);
   1572 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1573 		error = EROFS;
   1574 	else {
   1575 		VATTR_NULL(&vattr);
   1576 		vattr.va_uid = SCARG(uap, uid);
   1577 		vattr.va_gid = SCARG(uap, gid);
   1578 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1579 	}
   1580 	VOP_UNLOCK(vp);
   1581 	return (error);
   1582 }
   1583 
   1584 /*
   1585  * Set the access and modification times of a file.
   1586  */
   1587 /* ARGSUSED */
   1588 int
   1589 sys_utimes(p, v, retval)
   1590 	struct proc *p;
   1591 	void *v;
   1592 	register_t *retval;
   1593 {
   1594 	register struct sys_utimes_args /* {
   1595 		syscallarg(char *) path;
   1596 		syscallarg(struct timeval *) tptr;
   1597 	} */ *uap = v;
   1598 	register struct vnode *vp;
   1599 	struct timeval tv[2];
   1600 	struct vattr vattr;
   1601 	int error;
   1602 	struct nameidata nd;
   1603 
   1604 	VATTR_NULL(&vattr);
   1605 	if (SCARG(uap, tptr) == NULL) {
   1606 		microtime(&tv[0]);
   1607 		tv[1] = tv[0];
   1608 		vattr.va_vaflags |= VA_UTIMES_NULL;
   1609 	}
   1610 	else {
   1611 		error = copyin((caddr_t)SCARG(uap, tptr), (caddr_t)tv,
   1612 			       sizeof (tv));
   1613 		if (error)
   1614 			return (error);
   1615 	}
   1616 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1617 	if ((error = namei(&nd)) != 0)
   1618 		return (error);
   1619 	vp = nd.ni_vp;
   1620 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1621 	VOP_LOCK(vp);
   1622 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1623 		error = EROFS;
   1624 	else {
   1625 		vattr.va_atime.tv_sec = tv[0].tv_sec;
   1626 		vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   1627 		vattr.va_mtime.tv_sec = tv[1].tv_sec;
   1628 		vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   1629 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1630 	}
   1631 	vput(vp);
   1632 	return (error);
   1633 }
   1634 
   1635 /*
   1636  * Truncate a file given its path name.
   1637  */
   1638 /* ARGSUSED */
   1639 int
   1640 sys_truncate(p, v, retval)
   1641 	struct proc *p;
   1642 	void *v;
   1643 	register_t *retval;
   1644 {
   1645 	register struct sys_truncate_args /* {
   1646 		syscallarg(char *) path;
   1647 		syscallarg(int) pad;
   1648 		syscallarg(off_t) length;
   1649 	} */ *uap = v;
   1650 	register struct vnode *vp;
   1651 	struct vattr vattr;
   1652 	int error;
   1653 	struct nameidata nd;
   1654 
   1655 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1656 	if ((error = namei(&nd)) != 0)
   1657 		return (error);
   1658 	vp = nd.ni_vp;
   1659 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1660 	VOP_LOCK(vp);
   1661 	if (vp->v_type == VDIR)
   1662 		error = EISDIR;
   1663 	else if ((error = vn_writechk(vp)) == 0 &&
   1664 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   1665 		VATTR_NULL(&vattr);
   1666 		vattr.va_size = SCARG(uap, length);
   1667 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1668 	}
   1669 	vput(vp);
   1670 	return (error);
   1671 }
   1672 
   1673 /*
   1674  * Truncate a file given a file descriptor.
   1675  */
   1676 /* ARGSUSED */
   1677 int
   1678 sys_ftruncate(p, v, retval)
   1679 	struct proc *p;
   1680 	void *v;
   1681 	register_t *retval;
   1682 {
   1683 	register struct sys_ftruncate_args /* {
   1684 		syscallarg(int) fd;
   1685 		syscallarg(int) pad;
   1686 		syscallarg(off_t) length;
   1687 	} */ *uap = v;
   1688 	struct vattr vattr;
   1689 	struct vnode *vp;
   1690 	struct file *fp;
   1691 	int error;
   1692 
   1693 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1694 		return (error);
   1695 	if ((fp->f_flag & FWRITE) == 0)
   1696 		return (EINVAL);
   1697 	vp = (struct vnode *)fp->f_data;
   1698 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1699 	VOP_LOCK(vp);
   1700 	if (vp->v_type == VDIR)
   1701 		error = EISDIR;
   1702 	else if ((error = vn_writechk(vp)) == 0) {
   1703 		VATTR_NULL(&vattr);
   1704 		vattr.va_size = SCARG(uap, length);
   1705 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   1706 	}
   1707 	VOP_UNLOCK(vp);
   1708 	return (error);
   1709 }
   1710 
   1711 /*
   1712  * Sync an open file.
   1713  */
   1714 /* ARGSUSED */
   1715 int
   1716 sys_fsync(p, v, retval)
   1717 	struct proc *p;
   1718 	void *v;
   1719 	register_t *retval;
   1720 {
   1721 	struct sys_fsync_args /* {
   1722 		syscallarg(int) fd;
   1723 	} */ *uap = v;
   1724 	register struct vnode *vp;
   1725 	struct file *fp;
   1726 	int error;
   1727 
   1728 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1729 		return (error);
   1730 	vp = (struct vnode *)fp->f_data;
   1731 	VOP_LOCK(vp);
   1732 	error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
   1733 	VOP_UNLOCK(vp);
   1734 	return (error);
   1735 }
   1736 
   1737 /*
   1738  * Rename files.  Source and destination must either both be directories,
   1739  * or both not be directories.  If target is a directory, it must be empty.
   1740  */
   1741 /* ARGSUSED */
   1742 int
   1743 sys_rename(p, v, retval)
   1744 	struct proc *p;
   1745 	void *v;
   1746 	register_t *retval;
   1747 {
   1748 	register struct sys_rename_args /* {
   1749 		syscallarg(char *) from;
   1750 		syscallarg(char *) to;
   1751 	} */ *uap = v;
   1752 	register struct vnode *tvp, *fvp, *tdvp;
   1753 	struct nameidata fromnd, tond;
   1754 	int error;
   1755 
   1756 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   1757 	    SCARG(uap, from), p);
   1758 	if ((error = namei(&fromnd)) != 0)
   1759 		return (error);
   1760 	fvp = fromnd.ni_vp;
   1761 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
   1762 	    UIO_USERSPACE, SCARG(uap, to), p);
   1763 	if ((error = namei(&tond)) != 0) {
   1764 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1765 		vrele(fromnd.ni_dvp);
   1766 		vrele(fvp);
   1767 		goto out1;
   1768 	}
   1769 	tdvp = tond.ni_dvp;
   1770 	tvp = tond.ni_vp;
   1771 	if (tvp != NULL) {
   1772 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   1773 			error = ENOTDIR;
   1774 			goto out;
   1775 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   1776 			error = EISDIR;
   1777 			goto out;
   1778 		}
   1779 	}
   1780 	if (fvp == tdvp)
   1781 		error = EINVAL;
   1782 	/*
   1783 	 * If source is the same as the destination (that is the
   1784 	 * same inode number with the same name in the same directory),
   1785 	 * then there is nothing to do.
   1786 	 */
   1787 	if (fvp == tvp && fromnd.ni_dvp == tdvp &&
   1788 	    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   1789 	    !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
   1790 	      fromnd.ni_cnd.cn_namelen))
   1791 		error = -1;
   1792 out:
   1793 	if (!error) {
   1794 		VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
   1795 		if (fromnd.ni_dvp != tdvp)
   1796 			VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1797 		if (tvp)
   1798 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   1799 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   1800 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   1801 	} else {
   1802 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   1803 		if (tdvp == tvp)
   1804 			vrele(tdvp);
   1805 		else
   1806 			vput(tdvp);
   1807 		if (tvp)
   1808 			vput(tvp);
   1809 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1810 		vrele(fromnd.ni_dvp);
   1811 		vrele(fvp);
   1812 	}
   1813 	vrele(tond.ni_startdir);
   1814 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1815 out1:
   1816 	if (fromnd.ni_startdir)
   1817 		vrele(fromnd.ni_startdir);
   1818 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1819 	if (error == -1)
   1820 		return (0);
   1821 	return (error);
   1822 }
   1823 
   1824 /*
   1825  * Make a directory file.
   1826  */
   1827 /* ARGSUSED */
   1828 int
   1829 sys_mkdir(p, v, retval)
   1830 	struct proc *p;
   1831 	void *v;
   1832 	register_t *retval;
   1833 {
   1834 	register struct sys_mkdir_args /* {
   1835 		syscallarg(char *) path;
   1836 		syscallarg(int) mode;
   1837 	} */ *uap = v;
   1838 	register struct vnode *vp;
   1839 	struct vattr vattr;
   1840 	int error;
   1841 	struct nameidata nd;
   1842 
   1843 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1844 	if ((error = namei(&nd)) != 0)
   1845 		return (error);
   1846 	vp = nd.ni_vp;
   1847 	if (vp != NULL) {
   1848 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1849 		if (nd.ni_dvp == vp)
   1850 			vrele(nd.ni_dvp);
   1851 		else
   1852 			vput(nd.ni_dvp);
   1853 		vrele(vp);
   1854 		return (EEXIST);
   1855 	}
   1856 	VATTR_NULL(&vattr);
   1857 	vattr.va_type = VDIR;
   1858 	vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
   1859 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1860 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1861 	if (!error)
   1862 		vput(nd.ni_vp);
   1863 	return (error);
   1864 }
   1865 
   1866 /*
   1867  * Remove a directory file.
   1868  */
   1869 /* ARGSUSED */
   1870 int
   1871 sys_rmdir(p, v, retval)
   1872 	struct proc *p;
   1873 	void *v;
   1874 	register_t *retval;
   1875 {
   1876 	struct sys_rmdir_args /* {
   1877 		syscallarg(char *) path;
   1878 	} */ *uap = v;
   1879 	register struct vnode *vp;
   1880 	int error;
   1881 	struct nameidata nd;
   1882 
   1883 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1884 	    SCARG(uap, path), p);
   1885 	if ((error = namei(&nd)) != 0)
   1886 		return (error);
   1887 	vp = nd.ni_vp;
   1888 	if (vp->v_type != VDIR) {
   1889 		error = ENOTDIR;
   1890 		goto out;
   1891 	}
   1892 	/*
   1893 	 * No rmdir "." please.
   1894 	 */
   1895 	if (nd.ni_dvp == vp) {
   1896 		error = EINVAL;
   1897 		goto out;
   1898 	}
   1899 	/*
   1900 	 * The root of a mounted filesystem cannot be deleted.
   1901 	 */
   1902 	if (vp->v_flag & VROOT)
   1903 		error = EBUSY;
   1904 out:
   1905 	if (!error) {
   1906 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1907 		VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1908 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1909 	} else {
   1910 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1911 		if (nd.ni_dvp == vp)
   1912 			vrele(nd.ni_dvp);
   1913 		else
   1914 			vput(nd.ni_dvp);
   1915 		vput(vp);
   1916 	}
   1917 	return (error);
   1918 }
   1919 
   1920 /*
   1921  * Read a block of directory entries in a file system independent format.
   1922  */
   1923 int
   1924 sys_getdirentries(p, v, retval)
   1925 	struct proc *p;
   1926 	void *v;
   1927 	register_t *retval;
   1928 {
   1929 	register struct sys_getdirentries_args /* {
   1930 		syscallarg(int) fd;
   1931 		syscallarg(char *) buf;
   1932 		syscallarg(u_int) count;
   1933 		syscallarg(long *) basep;
   1934 	} */ *uap = v;
   1935 	register struct vnode *vp;
   1936 	struct file *fp;
   1937 	struct uio auio;
   1938 	struct iovec aiov;
   1939 	long loff;
   1940 	int error, eofflag;
   1941 
   1942 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1943 		return (error);
   1944 	if ((fp->f_flag & FREAD) == 0)
   1945 		return (EBADF);
   1946 	vp = (struct vnode *)fp->f_data;
   1947 unionread:
   1948 	if (vp->v_type != VDIR)
   1949 		return (EINVAL);
   1950 	aiov.iov_base = SCARG(uap, buf);
   1951 	aiov.iov_len = SCARG(uap, count);
   1952 	auio.uio_iov = &aiov;
   1953 	auio.uio_iovcnt = 1;
   1954 	auio.uio_rw = UIO_READ;
   1955 	auio.uio_segflg = UIO_USERSPACE;
   1956 	auio.uio_procp = p;
   1957 	auio.uio_resid = SCARG(uap, count);
   1958 	VOP_LOCK(vp);
   1959 	loff = auio.uio_offset = fp->f_offset;
   1960 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
   1961 	fp->f_offset = auio.uio_offset;
   1962 	VOP_UNLOCK(vp);
   1963 	if (error)
   1964 		return (error);
   1965 
   1966 #ifdef UNION
   1967 {
   1968 	extern int (**union_vnodeop_p) __P((void *));
   1969 	extern struct vnode *union_dircache __P((struct vnode *));
   1970 
   1971 	if ((SCARG(uap, count) == auio.uio_resid) &&
   1972 	    (vp->v_op == union_vnodeop_p)) {
   1973 		struct vnode *lvp;
   1974 
   1975 		lvp = union_dircache(vp);
   1976 		if (lvp != NULLVP) {
   1977 			struct vattr va;
   1978 
   1979 			/*
   1980 			 * If the directory is opaque,
   1981 			 * then don't show lower entries
   1982 			 */
   1983 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
   1984 			if (va.va_flags & OPAQUE) {
   1985 				vput(lvp);
   1986 				lvp = NULL;
   1987 			}
   1988 		}
   1989 
   1990 		if (lvp != NULLVP) {
   1991 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
   1992 			VOP_UNLOCK(lvp);
   1993 
   1994 			if (error) {
   1995 				vrele(lvp);
   1996 				return (error);
   1997 			}
   1998 			fp->f_data = (caddr_t) lvp;
   1999 			fp->f_offset = 0;
   2000 			error = vn_close(vp, FREAD, fp->f_cred, p);
   2001 			if (error)
   2002 				return (error);
   2003 			vp = lvp;
   2004 			goto unionread;
   2005 		}
   2006 	}
   2007 }
   2008 #endif /* UNION */
   2009 
   2010 	if ((SCARG(uap, count) == auio.uio_resid) &&
   2011 	    (vp->v_flag & VROOT) &&
   2012 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
   2013 		struct vnode *tvp = vp;
   2014 		vp = vp->v_mount->mnt_vnodecovered;
   2015 		VREF(vp);
   2016 		fp->f_data = (caddr_t) vp;
   2017 		fp->f_offset = 0;
   2018 		vrele(tvp);
   2019 		goto unionread;
   2020 	}
   2021 	error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
   2022 	    sizeof(long));
   2023 	*retval = SCARG(uap, count) - auio.uio_resid;
   2024 	return (error);
   2025 }
   2026 
   2027 /*
   2028  * Set the mode mask for creation of filesystem nodes.
   2029  */
   2030 int
   2031 sys_umask(p, v, retval)
   2032 	struct proc *p;
   2033 	void *v;
   2034 	register_t *retval;
   2035 {
   2036 	struct sys_umask_args /* {
   2037 		syscallarg(int) newmask;
   2038 	} */ *uap = v;
   2039 	register struct filedesc *fdp;
   2040 
   2041 	fdp = p->p_fd;
   2042 	*retval = fdp->fd_cmask;
   2043 	fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
   2044 	return (0);
   2045 }
   2046 
   2047 /*
   2048  * Void all references to file by ripping underlying filesystem
   2049  * away from vnode.
   2050  */
   2051 /* ARGSUSED */
   2052 int
   2053 sys_revoke(p, v, retval)
   2054 	struct proc *p;
   2055 	void *v;
   2056 	register_t *retval;
   2057 {
   2058 	register struct sys_revoke_args /* {
   2059 		syscallarg(char *) path;
   2060 	} */ *uap = v;
   2061 	register struct vnode *vp;
   2062 	struct vattr vattr;
   2063 	int error;
   2064 	struct nameidata nd;
   2065 
   2066 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2067 	if ((error = namei(&nd)) != 0)
   2068 		return (error);
   2069 	vp = nd.ni_vp;
   2070 	if (vp->v_type != VCHR && vp->v_type != VBLK) {
   2071 		error = EINVAL;
   2072 		goto out;
   2073 	}
   2074 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2075 		goto out;
   2076 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   2077 	    (error = suser(p->p_ucred, &p->p_acflag)))
   2078 		goto out;
   2079 	if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
   2080 		vgoneall(vp);
   2081 out:
   2082 	vrele(vp);
   2083 	return (error);
   2084 }
   2085 
   2086 /*
   2087  * Convert a user file descriptor to a kernel file entry.
   2088  */
   2089 int
   2090 getvnode(fdp, fd, fpp)
   2091 	struct filedesc *fdp;
   2092 	int fd;
   2093 	struct file **fpp;
   2094 {
   2095 	struct vnode *vp;
   2096 	struct file *fp;
   2097 
   2098 	if ((u_int)fd >= fdp->fd_nfiles ||
   2099 	    (fp = fdp->fd_ofiles[fd]) == NULL)
   2100 		return (EBADF);
   2101 	if (fp->f_type != DTYPE_VNODE)
   2102 		return (EINVAL);
   2103 	vp = (struct vnode *)fp->f_data;
   2104 	if (vp->v_type == VBAD)
   2105 		return (EBADF);
   2106 	*fpp = fp;
   2107 	return (0);
   2108 }
   2109