Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.76
      1 /*	$NetBSD: vfs_syscalls.c,v 1.76 1997/02/13 02:54:06 tls Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)vfs_syscalls.c	8.28 (Berkeley) 12/10/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/namei.h>
     46 #include <sys/filedesc.h>
     47 #include <sys/kernel.h>
     48 #include <sys/file.h>
     49 #include <sys/stat.h>
     50 #include <sys/vnode.h>
     51 #include <sys/mount.h>
     52 #include <sys/proc.h>
     53 #include <sys/uio.h>
     54 #include <sys/malloc.h>
     55 #include <sys/dirent.h>
     56 
     57 #include <sys/syscallargs.h>
     58 
     59 #include <vm/vm.h>
     60 #include <sys/sysctl.h>
     61 
     62 static int change_dir __P((struct nameidata *, struct proc *));
     63 
     64 void checkdirs __P((struct vnode *));
     65 int dounmount __P((struct mount *, int, struct proc *));
     66 
     67 /*
     68  * Virtual File System System Calls
     69  */
     70 
     71 /*
     72  * Mount a file system.
     73  */
     74 /* ARGSUSED */
     75 int
     76 sys_mount(p, v, retval)
     77 	struct proc *p;
     78 	void *v;
     79 	register_t *retval;
     80 {
     81 	register struct sys_mount_args /* {
     82 		syscallarg(const char *) type;
     83 		syscallarg(const char *) path;
     84 		syscallarg(int) flags;
     85 		syscallarg(void *) data;
     86 	} */ *uap = v;
     87 	register struct vnode *vp;
     88 	register struct mount *mp;
     89 	int error, flag = 0;
     90 	u_long fsindex = 0;
     91 	char fstypename[MFSNAMELEN];
     92 	struct vattr va;
     93 	struct nameidata nd;
     94 
     95 	/*
     96 	 * Get vnode to be covered
     97 	 */
     98 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
     99 	    SCARG(uap, path), p);
    100 	if ((error = namei(&nd)) != 0)
    101 		return (error);
    102 	vp = nd.ni_vp;
    103 	if (SCARG(uap, flags) & MNT_UPDATE) {
    104 		if ((vp->v_flag & VROOT) == 0) {
    105 			vput(vp);
    106 			return (EINVAL);
    107 		}
    108 		mp = vp->v_mount;
    109 		flag = mp->mnt_flag;
    110 		/*
    111 		 * We only allow the filesystem to be reloaded if it
    112 		 * is currently mounted read-only.
    113 		 */
    114 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
    115 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
    116 			vput(vp);
    117 			return (EOPNOTSUPP);	/* Needs translation */
    118 		}
    119 		mp->mnt_flag |=
    120 		    SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    121 		/*
    122 		 * Only root, or the user that did the original mount is
    123 		 * permitted to update it.
    124 		 */
    125 		if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
    126 		    (error = suser(p->p_ucred, &p->p_acflag))) {
    127 			vput(vp);
    128 			return (error);
    129 		}
    130 		/*
    131 		 * Do not allow NFS export by non-root users. Silently
    132 		 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
    133 		 */
    134 		if (p->p_ucred->cr_uid != 0) {
    135 			if (SCARG(uap, flags) & MNT_EXPORTED) {
    136 				vput(vp);
    137 				return (EPERM);
    138 			}
    139 			SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    140 		}
    141 		VOP_UNLOCK(vp);
    142 		goto update;
    143 	}
    144 	/*
    145 	 * If the user is not root, ensure that they own the directory
    146 	 * onto which we are attempting to mount.
    147 	 */
    148 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
    149 	    (va.va_uid != p->p_ucred->cr_uid &&
    150 	     (error = suser(p->p_ucred, &p->p_acflag)))) {
    151 		vput(vp);
    152 		return (error);
    153 	}
    154 	/*
    155 	 * Do not allow NFS export by non-root users. Silently
    156 	 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
    157 	 */
    158 	if (p->p_ucred->cr_uid != 0) {
    159 		if (SCARG(uap, flags) & MNT_EXPORTED) {
    160 			vput(vp);
    161 			return (EPERM);
    162 		}
    163 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    164 	}
    165 	if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
    166 		return (error);
    167 	if (vp->v_type != VDIR) {
    168 		vput(vp);
    169 		return (ENOTDIR);
    170 	}
    171 	error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
    172 	if (error) {
    173 #if defined(COMPAT_09) || defined(COMPAT_43)
    174 		/*
    175 		 * Historically filesystem types were identified by number.
    176 		 * If we get an integer for the filesystem type instead of a
    177 		 * string, we check to see if it matches one of the historic
    178 		 * filesystem types.
    179 		 */
    180 		fsindex = (u_long)SCARG(uap, type);
    181 		if (fsindex >= nvfssw || vfssw[fsindex] == NULL) {
    182 			vput(vp);
    183 			return (ENODEV);
    184 		}
    185 		strncpy(fstypename, vfssw[fsindex]->vfs_name, MFSNAMELEN);
    186 #else
    187 		vput(vp);
    188 		return (error);
    189 #endif
    190 	}
    191 #ifdef	COMPAT_10
    192 	/* Accept `ufs' as an alias for `ffs'. */
    193 	if (!strncmp(fstypename, "ufs", MFSNAMELEN))
    194 		strncpy(fstypename, "ffs", MFSNAMELEN);
    195 #endif
    196 	for (fsindex = 0; fsindex < nvfssw; fsindex++)
    197 		if (vfssw[fsindex] != NULL &&
    198 		    !strncmp(vfssw[fsindex]->vfs_name, fstypename, MFSNAMELEN))
    199 			break;
    200 	if (fsindex >= nvfssw) {
    201 		vput(vp);
    202 		return (ENODEV);
    203 	}
    204 	if (vp->v_mountedhere != NULL) {
    205 		vput(vp);
    206 		return (EBUSY);
    207 	}
    208 
    209 	/*
    210 	 * Allocate and initialize the file system.
    211 	 */
    212 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    213 		M_MOUNT, M_WAITOK);
    214 	bzero((char *)mp, (u_long)sizeof(struct mount));
    215 	mp->mnt_op = vfssw[fsindex];
    216 	if ((error = vfs_lock(mp)) != 0) {
    217 		free((caddr_t)mp, M_MOUNT);
    218 		vput(vp);
    219 		return (error);
    220 	}
    221 	/* Do this early in case we block later. */
    222 	vfssw[fsindex]->vfs_refcount++;
    223 	vp->v_mountedhere = mp;
    224 	mp->mnt_vnodecovered = vp;
    225 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
    226 update:
    227 	/*
    228 	 * Set the mount level flags.
    229 	 */
    230 	if (SCARG(uap, flags) & MNT_RDONLY)
    231 		mp->mnt_flag |= MNT_RDONLY;
    232 	else if (mp->mnt_flag & MNT_RDONLY)
    233 		mp->mnt_flag |= MNT_WANTRDWR;
    234 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    235 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP);
    236 	mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
    237 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
    238 	    MNT_NOCOREDUMP);
    239 	/*
    240 	 * Mount the filesystem.
    241 	 */
    242 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
    243 	if (mp->mnt_flag & MNT_UPDATE) {
    244 		vrele(vp);
    245 		if (mp->mnt_flag & MNT_WANTRDWR)
    246 			mp->mnt_flag &= ~MNT_RDONLY;
    247 		mp->mnt_flag &=~
    248 		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
    249 		if (error)
    250 			mp->mnt_flag = flag;
    251 		return (error);
    252 	}
    253 	/*
    254 	 * Put the new filesystem on the mount list after root.
    255 	 */
    256 	cache_purge(vp);
    257 	if (!error) {
    258 		CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    259 		checkdirs(vp);
    260 		VOP_UNLOCK(vp);
    261 		vfs_unlock(mp);
    262 		(void) VFS_STATFS(mp, &mp->mnt_stat, p);
    263 		error = VFS_START(mp, 0, p);
    264 	} else {
    265 		mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
    266 		vfssw[fsindex]->vfs_refcount--;
    267 		vfs_unlock(mp);
    268 		free((caddr_t)mp, M_MOUNT);
    269 		vput(vp);
    270 	}
    271 	return (error);
    272 }
    273 
    274 /*
    275  * Scan all active processes to see if any of them have a current
    276  * or root directory onto which the new filesystem has just been
    277  * mounted. If so, replace them with the new mount point.
    278  */
    279 void
    280 checkdirs(olddp)
    281 	struct vnode *olddp;
    282 {
    283 	struct filedesc *fdp;
    284 	struct vnode *newdp;
    285 	struct proc *p;
    286 
    287 	if (olddp->v_usecount == 1)
    288 		return;
    289 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    290 		panic("mount: lost mount");
    291 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    292 		fdp = p->p_fd;
    293 		if (fdp->fd_cdir == olddp) {
    294 			vrele(fdp->fd_cdir);
    295 			VREF(newdp);
    296 			fdp->fd_cdir = newdp;
    297 		}
    298 		if (fdp->fd_rdir == olddp) {
    299 			vrele(fdp->fd_rdir);
    300 			VREF(newdp);
    301 			fdp->fd_rdir = newdp;
    302 		}
    303 	}
    304 	if (rootvnode == olddp) {
    305 		vrele(rootvnode);
    306 		VREF(newdp);
    307 		rootvnode = newdp;
    308 	}
    309 	vput(newdp);
    310 }
    311 
    312 /*
    313  * Unmount a file system.
    314  *
    315  * Note: unmount takes a path to the vnode mounted on as argument,
    316  * not special file (as before).
    317  */
    318 /* ARGSUSED */
    319 int
    320 sys_unmount(p, v, retval)
    321 	struct proc *p;
    322 	void *v;
    323 	register_t *retval;
    324 {
    325 	register struct sys_unmount_args /* {
    326 		syscallarg(const char *) path;
    327 		syscallarg(int) flags;
    328 	} */ *uap = v;
    329 	register struct vnode *vp;
    330 	struct mount *mp;
    331 	int error;
    332 	struct nameidata nd;
    333 
    334 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    335 	    SCARG(uap, path), p);
    336 	if ((error = namei(&nd)) != 0)
    337 		return (error);
    338 	vp = nd.ni_vp;
    339 	mp = vp->v_mount;
    340 
    341 	/*
    342 	 * Only root, or the user that did the original mount is
    343 	 * permitted to unmount this filesystem.
    344 	 */
    345 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
    346 	    (error = suser(p->p_ucred, &p->p_acflag))) {
    347 		vput(vp);
    348 		return (error);
    349 	}
    350 
    351 	/*
    352 	 * Don't allow unmounting the root file system.
    353 	 */
    354 	if (mp->mnt_flag & MNT_ROOTFS) {
    355 		vput(vp);
    356 		return (EINVAL);
    357 	}
    358 
    359 	/*
    360 	 * Must be the root of the filesystem
    361 	 */
    362 	if ((vp->v_flag & VROOT) == 0) {
    363 		vput(vp);
    364 		return (EINVAL);
    365 	}
    366 	vput(vp);
    367 	return (dounmount(mp, SCARG(uap, flags), p));
    368 }
    369 
    370 /*
    371  * Do the actual file system unmount.
    372  */
    373 int
    374 dounmount(mp, flags, p)
    375 	register struct mount *mp;
    376 	int flags;
    377 	struct proc *p;
    378 {
    379 	struct vnode *coveredvp;
    380 	int error;
    381 
    382 	coveredvp = mp->mnt_vnodecovered;
    383 	if (vfs_busy(mp))
    384 		return (EBUSY);
    385 	mp->mnt_flag |= MNT_UNMOUNT;
    386 	if ((error = vfs_lock(mp)) != 0)
    387 		return (error);
    388 
    389 	mp->mnt_flag &=~ MNT_ASYNC;
    390 	vnode_pager_umount(mp);	/* release cached vnodes */
    391 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    392 	if ((error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0 ||
    393 	    (flags & MNT_FORCE))
    394 		error = VFS_UNMOUNT(mp, flags, p);
    395 	mp->mnt_flag &= ~MNT_UNMOUNT;
    396 	vfs_unbusy(mp);
    397 	if (error) {
    398 		vfs_unlock(mp);
    399 	} else {
    400 		CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
    401 		if (coveredvp != NULLVP) {
    402 			vrele(coveredvp);
    403 			coveredvp->v_mountedhere = (struct mount *)0;
    404 		}
    405 		mp->mnt_op->vfs_refcount--;
    406 		vfs_unlock(mp);
    407 		if (mp->mnt_vnodelist.lh_first != NULL)
    408 			panic("unmount: dangling vnode");
    409 		free((caddr_t)mp, M_MOUNT);
    410 	}
    411 	return (error);
    412 }
    413 
    414 /*
    415  * Sync each mounted filesystem.
    416  */
    417 #ifdef DEBUG
    418 int syncprt = 0;
    419 struct ctldebug debug0 = { "syncprt", &syncprt };
    420 #endif
    421 
    422 /* ARGSUSED */
    423 int
    424 sys_sync(p, v, retval)
    425 	struct proc *p;
    426 	void *v;
    427 	register_t *retval;
    428 {
    429 	register struct mount *mp, *nmp;
    430 	int asyncflag;
    431 
    432 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    433 		/*
    434 		 * Get the next pointer in case we hang on vfs_busy
    435 		 * while we are being unmounted.
    436 		 */
    437 		nmp = mp->mnt_list.cqe_next;
    438 		/*
    439 		 * The lock check below is to avoid races with mount
    440 		 * and unmount.
    441 		 */
    442 		if ((mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY)) == 0 &&
    443 		    !vfs_busy(mp)) {
    444 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    445 			mp->mnt_flag &= ~MNT_ASYNC;
    446 			vnode_pager_sync(mp);
    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(const 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(const 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(sp, 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(sp, 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(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(const 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(const 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(const 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(const 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 #ifndef FIFO
    898 	return (EOPNOTSUPP);
    899 #else
    900 	register struct sys_mkfifo_args /* {
    901 		syscallarg(const char *) path;
    902 		syscallarg(int) mode;
    903 	} */ *uap = v;
    904 	struct vattr vattr;
    905 	int error;
    906 	struct nameidata nd;
    907 
    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(const char *) path;
    940 		syscallarg(const 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 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
    951 	if ((error = namei(&nd)) != 0)
    952 		goto out;
    953 	if (nd.ni_vp) {
    954 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    955 		if (nd.ni_dvp == nd.ni_vp)
    956 			vrele(nd.ni_dvp);
    957 		else
    958 			vput(nd.ni_dvp);
    959 		vrele(nd.ni_vp);
    960 		error = EEXIST;
    961 		goto out;
    962 	}
    963 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    964 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
    965 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
    966 out:
    967 	vrele(vp);
    968 	return (error);
    969 }
    970 
    971 /*
    972  * Make a symbolic link.
    973  */
    974 /* ARGSUSED */
    975 int
    976 sys_symlink(p, v, retval)
    977 	struct proc *p;
    978 	void *v;
    979 	register_t *retval;
    980 {
    981 	register struct sys_symlink_args /* {
    982 		syscallarg(const char *) path;
    983 		syscallarg(const char *) link;
    984 	} */ *uap = v;
    985 	struct vattr vattr;
    986 	char *path;
    987 	int error;
    988 	struct nameidata nd;
    989 
    990 	MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
    991 	error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
    992 	if (error)
    993 		goto out;
    994 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
    995 	if ((error = namei(&nd)) != 0)
    996 		goto out;
    997 	if (nd.ni_vp) {
    998 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    999 		if (nd.ni_dvp == nd.ni_vp)
   1000 			vrele(nd.ni_dvp);
   1001 		else
   1002 			vput(nd.ni_dvp);
   1003 		vrele(nd.ni_vp);
   1004 		error = EEXIST;
   1005 		goto out;
   1006 	}
   1007 	VATTR_NULL(&vattr);
   1008 	vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
   1009 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1010 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   1011 out:
   1012 	FREE(path, M_NAMEI);
   1013 	return (error);
   1014 }
   1015 
   1016 /*
   1017  * Delete a whiteout from the filesystem.
   1018  */
   1019 /* ARGSUSED */
   1020 int
   1021 sys_undelete(p, v, retval)
   1022 	struct proc *p;
   1023 	void *v;
   1024 	register_t *retval;
   1025 {
   1026 	register struct sys_undelete_args /* {
   1027 		syscallarg(const char *) path;
   1028 	} */ *uap = v;
   1029 	int error;
   1030 	struct nameidata nd;
   1031 
   1032 	NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
   1033 	    SCARG(uap, path), p);
   1034 	error = namei(&nd);
   1035 	if (error)
   1036 		return (error);
   1037 
   1038 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   1039 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1040 		if (nd.ni_dvp == nd.ni_vp)
   1041 			vrele(nd.ni_dvp);
   1042 		else
   1043 			vput(nd.ni_dvp);
   1044 		if (nd.ni_vp)
   1045 			vrele(nd.ni_vp);
   1046 		return (EEXIST);
   1047 	}
   1048 
   1049 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1050 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   1051 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1052 	vput(nd.ni_dvp);
   1053 	return (error);
   1054 }
   1055 
   1056 /*
   1057  * Delete a name from the filesystem.
   1058  */
   1059 /* ARGSUSED */
   1060 int
   1061 sys_unlink(p, v, retval)
   1062 	struct proc *p;
   1063 	void *v;
   1064 	register_t *retval;
   1065 {
   1066 	struct sys_unlink_args /* {
   1067 		syscallarg(const char *) path;
   1068 	} */ *uap = v;
   1069 	register struct vnode *vp;
   1070 	int error;
   1071 	struct nameidata nd;
   1072 
   1073 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1074 	    SCARG(uap, path), p);
   1075 	if ((error = namei(&nd)) != 0)
   1076 		return (error);
   1077 	vp = nd.ni_vp;
   1078 
   1079 	/*
   1080 	 * The root of a mounted filesystem cannot be deleted.
   1081 	 */
   1082 	if (vp->v_flag & VROOT) {
   1083 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1084 		if (nd.ni_dvp == vp)
   1085 			vrele(nd.ni_dvp);
   1086 		else
   1087 			vput(nd.ni_dvp);
   1088 		vput(vp);
   1089 		error = EBUSY;
   1090 		goto out;
   1091 	}
   1092 
   1093 	(void)vnode_pager_uncache(vp);
   1094 
   1095 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1096 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1097 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1098 out:
   1099 	return (error);
   1100 }
   1101 
   1102 /*
   1103  * Reposition read/write file offset.
   1104  */
   1105 int
   1106 sys_lseek(p, v, retval)
   1107 	struct proc *p;
   1108 	void *v;
   1109 	register_t *retval;
   1110 {
   1111 	register struct sys_lseek_args /* {
   1112 		syscallarg(int) fd;
   1113 		syscallarg(int) pad;
   1114 		syscallarg(off_t) offset;
   1115 		syscallarg(int) whence;
   1116 	} */ *uap = v;
   1117 	struct ucred *cred = p->p_ucred;
   1118 	register struct filedesc *fdp = p->p_fd;
   1119 	register struct file *fp;
   1120 	struct vattr vattr;
   1121 	int error;
   1122 
   1123 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
   1124 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
   1125 		return (EBADF);
   1126 	if (fp->f_type != DTYPE_VNODE
   1127 #ifdef FIFO
   1128 	    || ((struct vnode *)fp->f_data)->v_type == VFIFO
   1129 #endif
   1130 	)
   1131 		return (ESPIPE);
   1132 	switch (SCARG(uap, whence)) {
   1133 	case L_INCR:
   1134 		fp->f_offset += SCARG(uap, offset);
   1135 		break;
   1136 	case L_XTND:
   1137 		error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr,
   1138 				    cred, p);
   1139 		if (error)
   1140 			return (error);
   1141 		fp->f_offset = SCARG(uap, offset) + vattr.va_size;
   1142 		break;
   1143 	case L_SET:
   1144 		fp->f_offset = SCARG(uap, offset);
   1145 		break;
   1146 	default:
   1147 		return (EINVAL);
   1148 	}
   1149 	*(off_t *)retval = fp->f_offset;
   1150 	return (0);
   1151 }
   1152 
   1153 /*
   1154  * Check access permissions.
   1155  */
   1156 int
   1157 sys_access(p, v, retval)
   1158 	struct proc *p;
   1159 	void *v;
   1160 	register_t *retval;
   1161 {
   1162 	register struct sys_access_args /* {
   1163 		syscallarg(const char *) path;
   1164 		syscallarg(int) flags;
   1165 	} */ *uap = v;
   1166 	register struct ucred *cred = p->p_ucred;
   1167 	register struct vnode *vp;
   1168 	int error, flags, t_gid, t_uid;
   1169 	struct nameidata nd;
   1170 
   1171 	t_uid = cred->cr_uid;
   1172 	t_gid = cred->cr_gid;
   1173 	cred->cr_uid = p->p_cred->p_ruid;
   1174 	cred->cr_gid = p->p_cred->p_rgid;
   1175 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1176 	    SCARG(uap, path), p);
   1177 	if ((error = namei(&nd)) != 0)
   1178 		goto out1;
   1179 	vp = nd.ni_vp;
   1180 
   1181 	/* Flags == 0 means only check for existence. */
   1182 	if (SCARG(uap, flags)) {
   1183 		flags = 0;
   1184 		if (SCARG(uap, flags) & R_OK)
   1185 			flags |= VREAD;
   1186 		if (SCARG(uap, flags) & W_OK)
   1187 			flags |= VWRITE;
   1188 		if (SCARG(uap, flags) & X_OK)
   1189 			flags |= VEXEC;
   1190 		if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
   1191 			error = VOP_ACCESS(vp, flags, cred, p);
   1192 	}
   1193 	vput(vp);
   1194 out1:
   1195 	cred->cr_uid = t_uid;
   1196 	cred->cr_gid = t_gid;
   1197 	return (error);
   1198 }
   1199 
   1200 /*
   1201  * Get file status; this version follows links.
   1202  */
   1203 /* ARGSUSED */
   1204 int
   1205 sys_stat(p, v, retval)
   1206 	struct proc *p;
   1207 	void *v;
   1208 	register_t *retval;
   1209 {
   1210 	register struct sys_stat_args /* {
   1211 		syscallarg(const char *) path;
   1212 		syscallarg(struct stat *) ub;
   1213 	} */ *uap = v;
   1214 	struct stat sb;
   1215 	int error;
   1216 	struct nameidata nd;
   1217 
   1218 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1219 	    SCARG(uap, path), p);
   1220 	if ((error = namei(&nd)) != 0)
   1221 		return (error);
   1222 	error = vn_stat(nd.ni_vp, &sb, p);
   1223 	vput(nd.ni_vp);
   1224 	if (error)
   1225 		return (error);
   1226 	error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
   1227 	return (error);
   1228 }
   1229 
   1230 /*
   1231  * Get file status; this version does not follow links.
   1232  */
   1233 /* ARGSUSED */
   1234 int
   1235 sys_lstat(p, v, retval)
   1236 	struct proc *p;
   1237 	void *v;
   1238 	register_t *retval;
   1239 {
   1240 	register struct sys_lstat_args /* {
   1241 		syscallarg(const char *) path;
   1242 		syscallarg(struct stat *) ub;
   1243 	} */ *uap = v;
   1244 	struct stat sb;
   1245 	int error;
   1246 	struct nameidata nd;
   1247 
   1248 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   1249 	    SCARG(uap, path), p);
   1250 	if ((error = namei(&nd)) != 0)
   1251 		return (error);
   1252 	error = vn_stat(nd.ni_vp, &sb, p);
   1253 	vput(nd.ni_vp);
   1254 	if (error)
   1255 		return (error);
   1256 	error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
   1257 	return (error);
   1258 }
   1259 
   1260 /*
   1261  * Get configurable pathname variables.
   1262  */
   1263 /* ARGSUSED */
   1264 int
   1265 sys_pathconf(p, v, retval)
   1266 	struct proc *p;
   1267 	void *v;
   1268 	register_t *retval;
   1269 {
   1270 	register struct sys_pathconf_args /* {
   1271 		syscallarg(const char *) path;
   1272 		syscallarg(int) name;
   1273 	} */ *uap = v;
   1274 	int error;
   1275 	struct nameidata nd;
   1276 
   1277 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1278 	    SCARG(uap, path), p);
   1279 	if ((error = namei(&nd)) != 0)
   1280 		return (error);
   1281 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   1282 	vput(nd.ni_vp);
   1283 	return (error);
   1284 }
   1285 
   1286 /*
   1287  * Return target name of a symbolic link.
   1288  */
   1289 /* ARGSUSED */
   1290 int
   1291 sys_readlink(p, v, retval)
   1292 	struct proc *p;
   1293 	void *v;
   1294 	register_t *retval;
   1295 {
   1296 	register struct sys_readlink_args /* {
   1297 		syscallarg(const char *) path;
   1298 		syscallarg(char *) buf;
   1299 		syscallarg(int) count;
   1300 	} */ *uap = v;
   1301 	register struct vnode *vp;
   1302 	struct iovec aiov;
   1303 	struct uio auio;
   1304 	int error;
   1305 	struct nameidata nd;
   1306 
   1307 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   1308 	    SCARG(uap, path), p);
   1309 	if ((error = namei(&nd)) != 0)
   1310 		return (error);
   1311 	vp = nd.ni_vp;
   1312 	if (vp->v_type != VLNK)
   1313 		error = EINVAL;
   1314 	else {
   1315 		aiov.iov_base = SCARG(uap, buf);
   1316 		aiov.iov_len = SCARG(uap, count);
   1317 		auio.uio_iov = &aiov;
   1318 		auio.uio_iovcnt = 1;
   1319 		auio.uio_offset = 0;
   1320 		auio.uio_rw = UIO_READ;
   1321 		auio.uio_segflg = UIO_USERSPACE;
   1322 		auio.uio_procp = p;
   1323 		auio.uio_resid = SCARG(uap, count);
   1324 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   1325 	}
   1326 	vput(vp);
   1327 	*retval = SCARG(uap, count) - auio.uio_resid;
   1328 	return (error);
   1329 }
   1330 
   1331 /*
   1332  * Change flags of a file given a path name.
   1333  */
   1334 /* ARGSUSED */
   1335 int
   1336 sys_chflags(p, v, retval)
   1337 	struct proc *p;
   1338 	void *v;
   1339 	register_t *retval;
   1340 {
   1341 	register struct sys_chflags_args /* {
   1342 		syscallarg(const char *) path;
   1343 		syscallarg(u_long) flags;
   1344 	} */ *uap = v;
   1345 	register struct vnode *vp;
   1346 	struct vattr vattr;
   1347 	int error;
   1348 	struct nameidata nd;
   1349 
   1350 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1351 	if ((error = namei(&nd)) != 0)
   1352 		return (error);
   1353 	vp = nd.ni_vp;
   1354 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1355 	VOP_LOCK(vp);
   1356 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1357 		error = EROFS;
   1358 	else {
   1359 		VATTR_NULL(&vattr);
   1360 		vattr.va_flags = SCARG(uap, flags);
   1361 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1362 	}
   1363 	vput(vp);
   1364 	return (error);
   1365 }
   1366 
   1367 /*
   1368  * Change flags of a file given a file descriptor.
   1369  */
   1370 /* ARGSUSED */
   1371 int
   1372 sys_fchflags(p, v, retval)
   1373 	struct proc *p;
   1374 	void *v;
   1375 	register_t *retval;
   1376 {
   1377 	register struct sys_fchflags_args /* {
   1378 		syscallarg(int) fd;
   1379 		syscallarg(u_long) flags;
   1380 	} */ *uap = v;
   1381 	struct vattr vattr;
   1382 	struct vnode *vp;
   1383 	struct file *fp;
   1384 	int error;
   1385 
   1386 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1387 		return (error);
   1388 	vp = (struct vnode *)fp->f_data;
   1389 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1390 	VOP_LOCK(vp);
   1391 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1392 		error = EROFS;
   1393 	else {
   1394 		VATTR_NULL(&vattr);
   1395 		vattr.va_flags = SCARG(uap, flags);
   1396 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1397 	}
   1398 	VOP_UNLOCK(vp);
   1399 	return (error);
   1400 }
   1401 
   1402 /*
   1403  * Change mode of a file given path name.
   1404  */
   1405 /* ARGSUSED */
   1406 int
   1407 sys_chmod(p, v, retval)
   1408 	struct proc *p;
   1409 	void *v;
   1410 	register_t *retval;
   1411 {
   1412 	register struct sys_chmod_args /* {
   1413 		syscallarg(const char *) path;
   1414 		syscallarg(int) mode;
   1415 	} */ *uap = v;
   1416 	register struct vnode *vp;
   1417 	struct vattr vattr;
   1418 	int error;
   1419 	struct nameidata nd;
   1420 
   1421 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1422 	if ((error = namei(&nd)) != 0)
   1423 		return (error);
   1424 	vp = nd.ni_vp;
   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_mode = SCARG(uap, mode) & ALLPERMS;
   1432 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1433 	}
   1434 	vput(vp);
   1435 	return (error);
   1436 }
   1437 
   1438 /*
   1439  * Change mode of a file given a file descriptor.
   1440  */
   1441 /* ARGSUSED */
   1442 int
   1443 sys_fchmod(p, v, retval)
   1444 	struct proc *p;
   1445 	void *v;
   1446 	register_t *retval;
   1447 {
   1448 	register struct sys_fchmod_args /* {
   1449 		syscallarg(int) fd;
   1450 		syscallarg(int) mode;
   1451 	} */ *uap = v;
   1452 	struct vattr vattr;
   1453 	struct vnode *vp;
   1454 	struct file *fp;
   1455 	int error;
   1456 
   1457 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1458 		return (error);
   1459 	vp = (struct vnode *)fp->f_data;
   1460 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1461 	VOP_LOCK(vp);
   1462 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1463 		error = EROFS;
   1464 	else {
   1465 		VATTR_NULL(&vattr);
   1466 		vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
   1467 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1468 	}
   1469 	VOP_UNLOCK(vp);
   1470 	return (error);
   1471 }
   1472 
   1473 /*
   1474  * Set ownership given a path name.
   1475  */
   1476 /* ARGSUSED */
   1477 int
   1478 sys_chown(p, v, retval)
   1479 	struct proc *p;
   1480 	void *v;
   1481 	register_t *retval;
   1482 {
   1483 	register struct sys_chown_args /* {
   1484 		syscallarg(const char *) path;
   1485 		syscallarg(uid_t) uid;
   1486 		syscallarg(gid_t) gid;
   1487 	} */ *uap = v;
   1488 	register struct vnode *vp;
   1489 	struct vattr vattr;
   1490 	int error;
   1491 	struct nameidata nd;
   1492 
   1493 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1494 	if ((error = namei(&nd)) != 0)
   1495 		return (error);
   1496 	vp = nd.ni_vp;
   1497 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1498 	VOP_LOCK(vp);
   1499 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1500 		error = EROFS;
   1501 	else {
   1502 		VATTR_NULL(&vattr);
   1503 		vattr.va_uid = SCARG(uap, uid);
   1504 		vattr.va_gid = SCARG(uap, gid);
   1505 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1506 	}
   1507 	vput(vp);
   1508 	return (error);
   1509 }
   1510 
   1511 /*
   1512  * Set ownership given a file descriptor.
   1513  */
   1514 /* ARGSUSED */
   1515 int
   1516 sys_fchown(p, v, retval)
   1517 	struct proc *p;
   1518 	void *v;
   1519 	register_t *retval;
   1520 {
   1521 	register struct sys_fchown_args /* {
   1522 		syscallarg(int) fd;
   1523 		syscallarg(uid_t) uid;
   1524 		syscallarg(gid_t) gid;
   1525 	} */ *uap = v;
   1526 	register struct vnode *vp;
   1527 	struct vattr vattr;
   1528 	int error;
   1529 	struct file *fp;
   1530 
   1531 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1532 		return (error);
   1533 	vp = (struct vnode *)fp->f_data;
   1534 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1535 	VOP_LOCK(vp);
   1536 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1537 		error = EROFS;
   1538 	else {
   1539 		VATTR_NULL(&vattr);
   1540 		vattr.va_uid = SCARG(uap, uid);
   1541 		vattr.va_gid = SCARG(uap, gid);
   1542 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1543 	}
   1544 	VOP_UNLOCK(vp);
   1545 	return (error);
   1546 }
   1547 
   1548 /*
   1549  * Set the access and modification times given a path name.
   1550  */
   1551 /* ARGSUSED */
   1552 int
   1553 sys_utimes(p, v, retval)
   1554 	struct proc *p;
   1555 	void *v;
   1556 	register_t *retval;
   1557 {
   1558 	register struct sys_utimes_args /* {
   1559 		syscallarg(const char *) path;
   1560 		syscallarg(const struct timeval *) tptr;
   1561 	} */ *uap = v;
   1562 	register struct vnode *vp;
   1563 	struct timeval tv[2];
   1564 	struct vattr vattr;
   1565 	int error;
   1566 	struct nameidata nd;
   1567 
   1568 	VATTR_NULL(&vattr);
   1569 	if (SCARG(uap, tptr) == NULL) {
   1570 		microtime(&tv[0]);
   1571 		tv[1] = tv[0];
   1572 		vattr.va_vaflags |= VA_UTIMES_NULL;
   1573 	} else {
   1574 		error = copyin(SCARG(uap, tptr), tv, sizeof (tv));
   1575 		if (error)
   1576 			return (error);
   1577 	}
   1578 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1579 	if ((error = namei(&nd)) != 0)
   1580 		return (error);
   1581 	vp = nd.ni_vp;
   1582 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1583 	VOP_LOCK(vp);
   1584 	if (vp->v_mount->mnt_flag & MNT_RDONLY)
   1585 		error = EROFS;
   1586 	else {
   1587 		vattr.va_atime.tv_sec = tv[0].tv_sec;
   1588 		vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   1589 		vattr.va_mtime.tv_sec = tv[1].tv_sec;
   1590 		vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   1591 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1592 	}
   1593 	vput(vp);
   1594 	return (error);
   1595 }
   1596 
   1597 /*
   1598  * Set the access and modification times given a file descriptor.
   1599  */
   1600 /* ARGSUSED */
   1601 int
   1602 sys_futimes(p, v, retval)
   1603 	struct proc *p;
   1604 	void *v;
   1605 	register_t *retval;
   1606 {
   1607 	register struct sys_futimes_args /* {
   1608 		syscallarg(int) fd;
   1609 		syscallarg(const struct timeval *) tptr;
   1610 	} */ *uap = v;
   1611 	register struct vnode *vp;
   1612 	struct timeval tv[2];
   1613 	struct vattr vattr;
   1614 	int error;
   1615 	struct file *fp;
   1616 
   1617 	VATTR_NULL(&vattr);
   1618 	if (SCARG(uap, tptr) == NULL) {
   1619 		microtime(&tv[0]);
   1620 		tv[1] = tv[0];
   1621 		vattr.va_vaflags |= VA_UTIMES_NULL;
   1622 	} else {
   1623 		error = copyin(SCARG(uap, tptr), tv, 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(const 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(const char *) from;
   1760 		syscallarg(const 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 			(void)vnode_pager_uncache(tvp);
   1809 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   1810 		}
   1811 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   1812 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   1813 	} else {
   1814 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   1815 		if (tdvp == tvp)
   1816 			vrele(tdvp);
   1817 		else
   1818 			vput(tdvp);
   1819 		if (tvp)
   1820 			vput(tvp);
   1821 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   1822 		vrele(fromnd.ni_dvp);
   1823 		vrele(fvp);
   1824 	}
   1825 	vrele(tond.ni_startdir);
   1826 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   1827 out1:
   1828 	if (fromnd.ni_startdir)
   1829 		vrele(fromnd.ni_startdir);
   1830 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   1831 	if (error == -1)
   1832 		return (0);
   1833 	return (error);
   1834 }
   1835 
   1836 /*
   1837  * Make a directory file.
   1838  */
   1839 /* ARGSUSED */
   1840 int
   1841 sys_mkdir(p, v, retval)
   1842 	struct proc *p;
   1843 	void *v;
   1844 	register_t *retval;
   1845 {
   1846 	register struct sys_mkdir_args /* {
   1847 		syscallarg(const char *) path;
   1848 		syscallarg(int) mode;
   1849 	} */ *uap = v;
   1850 	register struct vnode *vp;
   1851 	struct vattr vattr;
   1852 	int error;
   1853 	struct nameidata nd;
   1854 
   1855 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1856 	if ((error = namei(&nd)) != 0)
   1857 		return (error);
   1858 	vp = nd.ni_vp;
   1859 	if (vp != NULL) {
   1860 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1861 		if (nd.ni_dvp == vp)
   1862 			vrele(nd.ni_dvp);
   1863 		else
   1864 			vput(nd.ni_dvp);
   1865 		vrele(vp);
   1866 		return (EEXIST);
   1867 	}
   1868 	VATTR_NULL(&vattr);
   1869 	vattr.va_type = VDIR;
   1870 	vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
   1871 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1872 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1873 	if (!error)
   1874 		vput(nd.ni_vp);
   1875 	return (error);
   1876 }
   1877 
   1878 /*
   1879  * Remove a directory file.
   1880  */
   1881 /* ARGSUSED */
   1882 int
   1883 sys_rmdir(p, v, retval)
   1884 	struct proc *p;
   1885 	void *v;
   1886 	register_t *retval;
   1887 {
   1888 	struct sys_rmdir_args /* {
   1889 		syscallarg(const char *) path;
   1890 	} */ *uap = v;
   1891 	register struct vnode *vp;
   1892 	int error;
   1893 	struct nameidata nd;
   1894 
   1895 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1896 	    SCARG(uap, path), p);
   1897 	if ((error = namei(&nd)) != 0)
   1898 		return (error);
   1899 	vp = nd.ni_vp;
   1900 	if (vp->v_type != VDIR) {
   1901 		error = ENOTDIR;
   1902 		goto out;
   1903 	}
   1904 	/*
   1905 	 * No rmdir "." please.
   1906 	 */
   1907 	if (nd.ni_dvp == vp) {
   1908 		error = EINVAL;
   1909 		goto out;
   1910 	}
   1911 	/*
   1912 	 * The root of a mounted filesystem cannot be deleted.
   1913 	 */
   1914 	if (vp->v_flag & VROOT)
   1915 		error = EBUSY;
   1916 out:
   1917 	if (!error) {
   1918 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1919 		VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1920 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1921 	} else {
   1922 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1923 		if (nd.ni_dvp == vp)
   1924 			vrele(nd.ni_dvp);
   1925 		else
   1926 			vput(nd.ni_dvp);
   1927 		vput(vp);
   1928 	}
   1929 	return (error);
   1930 }
   1931 
   1932 /*
   1933  * Read a block of directory entries in a file system independent format.
   1934  */
   1935 int
   1936 sys_getdirentries(p, v, retval)
   1937 	struct proc *p;
   1938 	void *v;
   1939 	register_t *retval;
   1940 {
   1941 	register struct sys_getdirentries_args /* {
   1942 		syscallarg(int) fd;
   1943 		syscallarg(char *) buf;
   1944 		syscallarg(u_int) count;
   1945 		syscallarg(long *) basep;
   1946 	} */ *uap = v;
   1947 	register struct vnode *vp;
   1948 	struct file *fp;
   1949 	struct uio auio;
   1950 	struct iovec aiov;
   1951 	long loff;
   1952 	int error, eofflag;
   1953 
   1954 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1955 		return (error);
   1956 	if ((fp->f_flag & FREAD) == 0)
   1957 		return (EBADF);
   1958 	vp = (struct vnode *)fp->f_data;
   1959 unionread:
   1960 	if (vp->v_type != VDIR)
   1961 		return (EINVAL);
   1962 	aiov.iov_base = SCARG(uap, buf);
   1963 	aiov.iov_len = SCARG(uap, count);
   1964 	auio.uio_iov = &aiov;
   1965 	auio.uio_iovcnt = 1;
   1966 	auio.uio_rw = UIO_READ;
   1967 	auio.uio_segflg = UIO_USERSPACE;
   1968 	auio.uio_procp = p;
   1969 	auio.uio_resid = SCARG(uap, count);
   1970 	VOP_LOCK(vp);
   1971 	loff = auio.uio_offset = fp->f_offset;
   1972 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
   1973 	fp->f_offset = auio.uio_offset;
   1974 	VOP_UNLOCK(vp);
   1975 	if (error)
   1976 		return (error);
   1977 
   1978 #ifdef UNION
   1979 {
   1980 	extern int (**union_vnodeop_p) __P((void *));
   1981 	extern struct vnode *union_dircache __P((struct vnode *));
   1982 
   1983 	if ((SCARG(uap, count) == auio.uio_resid) &&
   1984 	    (vp->v_op == union_vnodeop_p)) {
   1985 		struct vnode *lvp;
   1986 
   1987 		lvp = union_dircache(vp);
   1988 		if (lvp != NULLVP) {
   1989 			struct vattr va;
   1990 
   1991 			/*
   1992 			 * If the directory is opaque,
   1993 			 * then don't show lower entries
   1994 			 */
   1995 			error = VOP_GETATTR(vp, &va, fp->f_cred, p);
   1996 			if (va.va_flags & OPAQUE) {
   1997 				vput(lvp);
   1998 				lvp = NULL;
   1999 			}
   2000 		}
   2001 
   2002 		if (lvp != NULLVP) {
   2003 			error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
   2004 			VOP_UNLOCK(lvp);
   2005 
   2006 			if (error) {
   2007 				vrele(lvp);
   2008 				return (error);
   2009 			}
   2010 			fp->f_data = (caddr_t) lvp;
   2011 			fp->f_offset = 0;
   2012 			error = vn_close(vp, FREAD, fp->f_cred, p);
   2013 			if (error)
   2014 				return (error);
   2015 			vp = lvp;
   2016 			goto unionread;
   2017 		}
   2018 	}
   2019 }
   2020 #endif /* UNION */
   2021 
   2022 	if ((SCARG(uap, count) == auio.uio_resid) &&
   2023 	    (vp->v_flag & VROOT) &&
   2024 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
   2025 		struct vnode *tvp = vp;
   2026 		vp = vp->v_mount->mnt_vnodecovered;
   2027 		VREF(vp);
   2028 		fp->f_data = (caddr_t) vp;
   2029 		fp->f_offset = 0;
   2030 		vrele(tvp);
   2031 		goto unionread;
   2032 	}
   2033 	error = copyout(&loff, SCARG(uap, basep), sizeof(long));
   2034 	*retval = SCARG(uap, count) - auio.uio_resid;
   2035 	return (error);
   2036 }
   2037 
   2038 /*
   2039  * Set the mode mask for creation of filesystem nodes.
   2040  */
   2041 int
   2042 sys_umask(p, v, retval)
   2043 	struct proc *p;
   2044 	void *v;
   2045 	register_t *retval;
   2046 {
   2047 	struct sys_umask_args /* {
   2048 		syscallarg(int) newmask;
   2049 	} */ *uap = v;
   2050 	register struct filedesc *fdp;
   2051 
   2052 	fdp = p->p_fd;
   2053 	*retval = fdp->fd_cmask;
   2054 	fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
   2055 	return (0);
   2056 }
   2057 
   2058 /*
   2059  * Void all references to file by ripping underlying filesystem
   2060  * away from vnode.
   2061  */
   2062 /* ARGSUSED */
   2063 int
   2064 sys_revoke(p, v, retval)
   2065 	struct proc *p;
   2066 	void *v;
   2067 	register_t *retval;
   2068 {
   2069 	register struct sys_revoke_args /* {
   2070 		syscallarg(const char *) path;
   2071 	} */ *uap = v;
   2072 	register struct vnode *vp;
   2073 	struct vattr vattr;
   2074 	int error;
   2075 	struct nameidata nd;
   2076 
   2077 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2078 	if ((error = namei(&nd)) != 0)
   2079 		return (error);
   2080 	vp = nd.ni_vp;
   2081 	if (vp->v_type != VCHR && vp->v_type != VBLK) {
   2082 		error = EINVAL;
   2083 		goto out;
   2084 	}
   2085 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2086 		goto out;
   2087 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   2088 	    (error = suser(p->p_ucred, &p->p_acflag)))
   2089 		goto out;
   2090 	if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
   2091 		vgoneall(vp);
   2092 out:
   2093 	vrele(vp);
   2094 	return (error);
   2095 }
   2096 
   2097 /*
   2098  * Convert a user file descriptor to a kernel file entry.
   2099  */
   2100 int
   2101 getvnode(fdp, fd, fpp)
   2102 	struct filedesc *fdp;
   2103 	int fd;
   2104 	struct file **fpp;
   2105 {
   2106 	struct vnode *vp;
   2107 	struct file *fp;
   2108 
   2109 	if ((u_int)fd >= fdp->fd_nfiles ||
   2110 	    (fp = fdp->fd_ofiles[fd]) == NULL)
   2111 		return (EBADF);
   2112 	if (fp->f_type != DTYPE_VNODE)
   2113 		return (EINVAL);
   2114 	vp = (struct vnode *)fp->f_data;
   2115 	if (vp->v_type == VBAD)
   2116 		return (EBADF);
   2117 	*fpp = fp;
   2118 	return (0);
   2119 }
   2120