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