Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.164.2.12
      1 /*	$NetBSD: vfs_syscalls.c,v 1.164.2.12 2002/09/17 21:22:31 nathanw 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.42 (Berkeley) 7/31/95
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.164.2.12 2002/09/17 21:22:31 nathanw Exp $");
     45 
     46 #include "opt_compat_netbsd.h"
     47 #include "opt_compat_43.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/namei.h>
     52 #include <sys/filedesc.h>
     53 #include <sys/kernel.h>
     54 #include <sys/file.h>
     55 #include <sys/stat.h>
     56 #include <sys/vnode.h>
     57 #include <sys/mount.h>
     58 #include <sys/proc.h>
     59 #include <sys/uio.h>
     60 #include <sys/malloc.h>
     61 #include <sys/dirent.h>
     62 #include <sys/sysctl.h>
     63 #include <sys/sa.h>
     64 #include <sys/syscallargs.h>
     65 
     66 #include <miscfs/genfs/genfs.h>
     67 #include <miscfs/syncfs/syncfs.h>
     68 
     69 static int change_dir __P((struct nameidata *, struct proc *));
     70 static int change_flags __P((struct vnode *, u_long, struct proc *));
     71 static int change_mode __P((struct vnode *, int, struct proc *p));
     72 static int change_owner __P((struct vnode *, uid_t, gid_t, struct proc *,
     73     int));
     74 static int change_utimes __P((struct vnode *vp, const struct timeval *,
     75 	       struct proc *p));
     76 static int rename_files __P((const char *, const char *, struct proc *, int));
     77 
     78 void checkdirs __P((struct vnode *));
     79 
     80 int dovfsusermount = 0;
     81 
     82 /*
     83  * Virtual File System System Calls
     84  */
     85 
     86 /*
     87  * Mount a file system.
     88  */
     89 
     90 #if defined(COMPAT_09) || defined(COMPAT_43)
     91 /*
     92  * This table is used to maintain compatibility with 4.3BSD
     93  * and NetBSD 0.9 mount syscalls.  Note, the order is important!
     94  *
     95  * Do not modify this table. It should only contain filesystems
     96  * supported by NetBSD 0.9 and 4.3BSD.
     97  */
     98 const char * const mountcompatnames[] = {
     99 	NULL,		/* 0 = MOUNT_NONE */
    100 	MOUNT_FFS,	/* 1 = MOUNT_UFS */
    101 	MOUNT_NFS,	/* 2 */
    102 	MOUNT_MFS,	/* 3 */
    103 	MOUNT_MSDOS,	/* 4 */
    104 	MOUNT_CD9660,	/* 5 = MOUNT_ISOFS */
    105 	MOUNT_FDESC,	/* 6 */
    106 	MOUNT_KERNFS,	/* 7 */
    107 	NULL,		/* 8 = MOUNT_DEVFS */
    108 	MOUNT_AFS,	/* 9 */
    109 };
    110 const int nmountcompatnames = sizeof(mountcompatnames) /
    111     sizeof(mountcompatnames[0]);
    112 #endif /* COMPAT_09 || COMPAT_43 */
    113 
    114 /* ARGSUSED */
    115 int
    116 sys_mount(l, v, retval)
    117 	struct lwp *l;
    118 	void *v;
    119 	register_t *retval;
    120 {
    121 	struct sys_mount_args /* {
    122 		syscallarg(const char *) type;
    123 		syscallarg(const char *) path;
    124 		syscallarg(int) flags;
    125 		syscallarg(void *) data;
    126 	} */ *uap = v;
    127 	struct proc *p = l->l_proc;
    128 	struct vnode *vp;
    129 	struct mount *mp;
    130 	int error, flag = 0;
    131 	char fstypename[MFSNAMELEN];
    132 	struct vattr va;
    133 	struct nameidata nd;
    134 	struct vfsops *vfs;
    135 
    136 	if (dovfsusermount == 0 && (error = suser(p->p_ucred, &p->p_acflag)))
    137 		return (error);
    138 	/*
    139 	 * Get vnode to be covered
    140 	 */
    141 	NDINIT(&nd, LOOKUP, FOLLOW , UIO_USERSPACE,
    142 	    SCARG(uap, path), p);
    143 	if ((error = namei(&nd)) != 0)
    144 		return (error);
    145 	vp = nd.ni_vp;
    146 	/*
    147 	 * A lookup in VFS_MOUNT might result in an attempt to
    148 	 * lock this vnode again, so make the lock resursive.
    149 	 */
    150 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
    151 	if (SCARG(uap, flags) & MNT_UPDATE) {
    152 		if ((vp->v_flag & VROOT) == 0) {
    153 			vput(vp);
    154 			return (EINVAL);
    155 		}
    156 		mp = vp->v_mount;
    157 		flag = mp->mnt_flag;
    158 		vfs = mp->mnt_op;
    159 		/*
    160 		 * We only allow the filesystem to be reloaded if it
    161 		 * is currently mounted read-only.
    162 		 */
    163 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
    164 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
    165 			vput(vp);
    166 			return (EOPNOTSUPP);	/* Needs translation */
    167 		}
    168 		/*
    169 		 * In "highly secure" mode, don't let the caller do anything
    170 		 * but downgrade a filesystem from read-write to read-only.
    171 		 * (see also below; MNT_UPDATE is required.)
    172 		 */
    173 		if (securelevel >= 2 &&
    174 		    (SCARG(uap, flags) !=
    175 		    (mp->mnt_flag | MNT_RDONLY |
    176 		    MNT_RELOAD | MNT_FORCE | MNT_UPDATE))) {
    177 			vput(vp);
    178 			return (EPERM);
    179 		}
    180 		mp->mnt_flag |=
    181 		    SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    182 		/*
    183 		 * Only root, or the user that did the original mount is
    184 		 * permitted to update it.
    185 		 */
    186 		if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
    187 		    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    188 			vput(vp);
    189 			return (error);
    190 		}
    191 		/*
    192 		 * Do not allow NFS export by non-root users. For non-root
    193 		 * users, silently enforce MNT_NOSUID and MNT_NODEV, and
    194 		 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
    195 		 */
    196 		if (p->p_ucred->cr_uid != 0) {
    197 			if (SCARG(uap, flags) & MNT_EXPORTED) {
    198 				vput(vp);
    199 				return (EPERM);
    200 			}
    201 			SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    202 			if (flag & MNT_NOEXEC)
    203 				SCARG(uap, flags) |= MNT_NOEXEC;
    204 		}
    205 		if (vfs_busy(mp, LK_NOWAIT, 0)) {
    206 			vput(vp);
    207 			return (EPERM);
    208 		}
    209 		goto update;
    210 	} else {
    211 		if (securelevel >= 2) {
    212 			vput(vp);
    213 			return (EPERM);
    214 		}
    215 	}
    216 	/*
    217 	 * If the user is not root, ensure that they own the directory
    218 	 * onto which we are attempting to mount.
    219 	 */
    220 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
    221 	    (va.va_uid != p->p_ucred->cr_uid &&
    222 		(error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
    223 		vput(vp);
    224 		return (error);
    225 	}
    226 	/*
    227 	 * Do not allow NFS export by non-root users. For non-root users,
    228 	 * silently enforce MNT_NOSUID and MNT_NODEV, and MNT_NOEXEC if the
    229 	 * mount point is already MNT_NOEXEC.
    230 	 */
    231 	if (p->p_ucred->cr_uid != 0) {
    232 		if (SCARG(uap, flags) & MNT_EXPORTED) {
    233 			vput(vp);
    234 			return (EPERM);
    235 		}
    236 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    237 		if (vp->v_mount->mnt_flag & MNT_NOEXEC)
    238 			SCARG(uap, flags) |= MNT_NOEXEC;
    239 	}
    240 	if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
    241 		return (error);
    242 	if (vp->v_type != VDIR) {
    243 		vput(vp);
    244 		return (ENOTDIR);
    245 	}
    246 	error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
    247 	if (error) {
    248 #if defined(COMPAT_09) || defined(COMPAT_43)
    249 		/*
    250 		 * Historically filesystem types were identified by number.
    251 		 * If we get an integer for the filesystem type instead of a
    252 		 * string, we check to see if it matches one of the historic
    253 		 * filesystem types.
    254 		 */
    255 		u_long fsindex = (u_long)SCARG(uap, type);
    256 		if (fsindex >= nmountcompatnames ||
    257 		    mountcompatnames[fsindex] == NULL) {
    258 			vput(vp);
    259 			return (ENODEV);
    260 		}
    261 		strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
    262 #else
    263 		vput(vp);
    264 		return (error);
    265 #endif
    266 	}
    267 #ifdef	COMPAT_10
    268 	/* Accept `ufs' as an alias for `ffs'. */
    269 	if (!strncmp(fstypename, "ufs", MFSNAMELEN))
    270 		strncpy(fstypename, "ffs", MFSNAMELEN);
    271 #endif
    272 	if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
    273 		vput(vp);
    274 		return (ENODEV);
    275 	}
    276 	if (vp->v_mountedhere != NULL) {
    277 		vput(vp);
    278 		return (EBUSY);
    279 	}
    280 
    281 	/*
    282 	 * Allocate and initialize the file system.
    283 	 */
    284 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    285 		M_MOUNT, M_WAITOK);
    286 	memset((char *)mp, 0, (u_long)sizeof(struct mount));
    287 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
    288 	(void)vfs_busy(mp, LK_NOWAIT, 0);
    289 	mp->mnt_op = vfs;
    290 	vfs->vfs_refcount++;
    291 	mp->mnt_vnodecovered = vp;
    292 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
    293 	mp->mnt_unmounter = NULL;
    294 update:
    295 	/*
    296 	 * Set the mount level flags.
    297 	 */
    298 	if (SCARG(uap, flags) & MNT_RDONLY)
    299 		mp->mnt_flag |= MNT_RDONLY;
    300 	else if (mp->mnt_flag & MNT_RDONLY)
    301 		mp->mnt_flag |= MNT_WANTRDWR;
    302 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    303 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    304 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP);
    305 	mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
    306 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
    307 	    MNT_NOCOREDUMP | MNT_IGNORE | MNT_NOATIME | MNT_NODEVMTIME |
    308 	    MNT_SYMPERM | MNT_SOFTDEP);
    309 	/*
    310 	 * Mount the filesystem.
    311 	 */
    312 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
    313 	if (mp->mnt_flag & MNT_UPDATE) {
    314 		if (mp->mnt_flag & MNT_WANTRDWR)
    315 			mp->mnt_flag &= ~MNT_RDONLY;
    316 		mp->mnt_flag &=~
    317 		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
    318 		if (error)
    319 			mp->mnt_flag = flag;
    320 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
    321 			if (mp->mnt_syncer == NULL)
    322 				error = vfs_allocate_syncvnode(mp);
    323 		} else {
    324 			if (mp->mnt_syncer != NULL)
    325 				vfs_deallocate_syncvnode(mp);
    326 		}
    327 		vfs_unbusy(mp);
    328 		VOP_UNLOCK(vp, 0);
    329 		vrele(vp);
    330 		return (error);
    331 	}
    332 	/*
    333 	 * Put the new filesystem on the mount list after root.
    334 	 */
    335 	cache_purge(vp);
    336 	if (!error) {
    337 		vp->v_mountedhere = mp;
    338 		simple_lock(&mountlist_slock);
    339 		CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    340 		simple_unlock(&mountlist_slock);
    341 		checkdirs(vp);
    342 		VOP_UNLOCK(vp, 0);
    343 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    344 			error = vfs_allocate_syncvnode(mp);
    345 		vfs_unbusy(mp);
    346 		(void) VFS_STATFS(mp, &mp->mnt_stat, p);
    347 		if ((error = VFS_START(mp, 0, p)))
    348 			vrele(vp);
    349 	} else {
    350 		vp->v_mountedhere = (struct mount *)0;
    351 		vfs->vfs_refcount--;
    352 		vfs_unbusy(mp);
    353 		free((caddr_t)mp, M_MOUNT);
    354 		vput(vp);
    355 	}
    356 	return (error);
    357 }
    358 
    359 /*
    360  * Scan all active processes to see if any of them have a current
    361  * or root directory onto which the new filesystem has just been
    362  * mounted. If so, replace them with the new mount point.
    363  */
    364 void
    365 checkdirs(olddp)
    366 	struct vnode *olddp;
    367 {
    368 	struct cwdinfo *cwdi;
    369 	struct vnode *newdp;
    370 	struct proc *p;
    371 
    372 	if (olddp->v_usecount == 1)
    373 		return;
    374 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    375 		panic("mount: lost mount");
    376 	proclist_lock_read();
    377 	LIST_FOREACH(p, &allproc, p_list) {
    378 		cwdi = p->p_cwdi;
    379 		if (cwdi->cwdi_cdir == olddp) {
    380 			vrele(cwdi->cwdi_cdir);
    381 			VREF(newdp);
    382 			cwdi->cwdi_cdir = newdp;
    383 		}
    384 		if (cwdi->cwdi_rdir == olddp) {
    385 			vrele(cwdi->cwdi_rdir);
    386 			VREF(newdp);
    387 			cwdi->cwdi_rdir = newdp;
    388 		}
    389 	}
    390 	proclist_unlock_read();
    391 	if (rootvnode == olddp) {
    392 		vrele(rootvnode);
    393 		VREF(newdp);
    394 		rootvnode = newdp;
    395 	}
    396 	vput(newdp);
    397 }
    398 
    399 /*
    400  * Unmount a file system.
    401  *
    402  * Note: unmount takes a path to the vnode mounted on as argument,
    403  * not special file (as before).
    404  */
    405 /* ARGSUSED */
    406 int
    407 sys_unmount(l, v, retval)
    408 	struct lwp *l;
    409 	void *v;
    410 	register_t *retval;
    411 {
    412 	struct sys_unmount_args /* {
    413 		syscallarg(const char *) path;
    414 		syscallarg(int) flags;
    415 	} */ *uap = v;
    416 	struct proc *p = l->l_proc;
    417 	struct vnode *vp;
    418 	struct mount *mp;
    419 	int error;
    420 	struct nameidata nd;
    421 
    422 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    423 	    SCARG(uap, path), p);
    424 	if ((error = namei(&nd)) != 0)
    425 		return (error);
    426 	vp = nd.ni_vp;
    427 	mp = vp->v_mount;
    428 
    429 	/*
    430 	 * Only root, or the user that did the original mount is
    431 	 * permitted to unmount this filesystem.
    432 	 */
    433 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
    434 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    435 		vput(vp);
    436 		return (error);
    437 	}
    438 
    439 	/*
    440 	 * Don't allow unmounting the root file system.
    441 	 */
    442 	if (mp->mnt_flag & MNT_ROOTFS) {
    443 		vput(vp);
    444 		return (EINVAL);
    445 	}
    446 
    447 	/*
    448 	 * Must be the root of the filesystem
    449 	 */
    450 	if ((vp->v_flag & VROOT) == 0) {
    451 		vput(vp);
    452 		return (EINVAL);
    453 	}
    454 	vput(vp);
    455 
    456 	/*
    457 	 * XXX Freeze syncer.  Must do this before locking the
    458 	 * mount point.  See dounmount() for details.
    459 	 */
    460 	lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
    461 
    462 	if (vfs_busy(mp, 0, 0)) {
    463 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    464 		return (EBUSY);
    465 	}
    466 
    467 	return (dounmount(mp, SCARG(uap, flags), p));
    468 }
    469 
    470 /*
    471  * Do the actual file system unmount. File system is assumed to have been
    472  * marked busy by the caller.
    473  */
    474 int
    475 dounmount(mp, flags, p)
    476 	struct mount *mp;
    477 	int flags;
    478 	struct proc *p;
    479 {
    480 	struct vnode *coveredvp;
    481 	int error;
    482 	int async;
    483 	int used_syncer;
    484 
    485 	simple_lock(&mountlist_slock);
    486 	vfs_unbusy(mp);
    487 	used_syncer = (mp->mnt_syncer != NULL);
    488 
    489 	/*
    490 	 * XXX Syncer must be frozen when we get here.  This should really
    491 	 * be done on a per-mountpoint basis, but especially the softdep
    492 	 * code possibly called from the syncer doens't exactly work on a
    493 	 * per-mountpoint basis, so the softdep code would become a maze
    494 	 * of vfs_busy() calls.
    495 	 *
    496 	 * The caller of dounmount() must acquire syncer_lock because
    497 	 * the syncer itself acquires locks in syncer_lock -> vfs_busy
    498 	 * order, and we must preserve that order to avoid deadlock.
    499 	 *
    500 	 * So, if the file system did not use the syncer, now is
    501 	 * the time to release the syncer_lock.
    502 	 */
    503 	if (used_syncer == 0)
    504 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    505 
    506 	mp->mnt_flag |= MNT_UNMOUNT;
    507 	mp->mnt_unmounter = p;
    508 	lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
    509 	if (mp->mnt_flag & MNT_EXPUBLIC)
    510 		vfs_setpublicfs(NULL, NULL, NULL);
    511 	async = mp->mnt_flag & MNT_ASYNC;
    512 	mp->mnt_flag &= ~MNT_ASYNC;
    513 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    514 	if (mp->mnt_syncer != NULL)
    515 		vfs_deallocate_syncvnode(mp);
    516 	if (((mp->mnt_flag & MNT_RDONLY) ||
    517 	    (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
    518 	    (flags & MNT_FORCE))
    519 		error = VFS_UNMOUNT(mp, flags, p);
    520 	simple_lock(&mountlist_slock);
    521 	if (error) {
    522 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    523 			(void) vfs_allocate_syncvnode(mp);
    524 		mp->mnt_flag &= ~MNT_UNMOUNT;
    525 		mp->mnt_unmounter = NULL;
    526 		mp->mnt_flag |= async;
    527 		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
    528 		    &mountlist_slock);
    529 		if (used_syncer)
    530 			lockmgr(&syncer_lock, LK_RELEASE, NULL);
    531 		while (mp->mnt_wcnt > 0) {
    532 			wakeup((caddr_t)mp);
    533 			tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1", 0);
    534 		}
    535 		return (error);
    536 	}
    537 	CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
    538 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    539 		coveredvp->v_mountedhere = NULL;
    540 		vrele(coveredvp);
    541 	}
    542 	mp->mnt_op->vfs_refcount--;
    543 	if (LIST_FIRST(&mp->mnt_vnodelist) != NULL)
    544 		panic("unmount: dangling vnode");
    545 	mp->mnt_flag |= MNT_GONE;
    546 	lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
    547 	if (used_syncer)
    548 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    549 	while(mp->mnt_wcnt > 0) {
    550 		wakeup((caddr_t)mp);
    551 		tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0);
    552 	}
    553 	free((caddr_t)mp, M_MOUNT);
    554 	return (0);
    555 }
    556 
    557 /*
    558  * Sync each mounted filesystem.
    559  */
    560 #ifdef DEBUG
    561 int syncprt = 0;
    562 struct ctldebug debug0 = { "syncprt", &syncprt };
    563 #endif
    564 
    565 /* ARGSUSED */
    566 int
    567 sys_sync(l, v, retval)
    568 	struct lwp *l;
    569 	void *v;
    570 	register_t *retval;
    571 {
    572 	struct mount *mp, *nmp;
    573 	int asyncflag;
    574 	struct proc *p = l->l_proc;
    575 
    576 	simple_lock(&mountlist_slock);
    577 	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
    578 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    579 			nmp = mp->mnt_list.cqe_prev;
    580 			continue;
    581 		}
    582 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    583 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    584 			mp->mnt_flag &= ~MNT_ASYNC;
    585 			VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
    586 			if (asyncflag)
    587 				 mp->mnt_flag |= MNT_ASYNC;
    588 		}
    589 		simple_lock(&mountlist_slock);
    590 		nmp = mp->mnt_list.cqe_prev;
    591 		vfs_unbusy(mp);
    592 
    593 	}
    594 	simple_unlock(&mountlist_slock);
    595 #ifdef DEBUG
    596 	if (syncprt)
    597 		vfs_bufstats();
    598 #endif /* DEBUG */
    599 	return (0);
    600 }
    601 
    602 /*
    603  * Change filesystem quotas.
    604  */
    605 /* ARGSUSED */
    606 int
    607 sys_quotactl(l, v, retval)
    608 	struct lwp *l;
    609 	void *v;
    610 	register_t *retval;
    611 {
    612 	struct sys_quotactl_args /* {
    613 		syscallarg(const char *) path;
    614 		syscallarg(int) cmd;
    615 		syscallarg(int) uid;
    616 		syscallarg(caddr_t) arg;
    617 	} */ *uap = v;
    618 	struct proc *p = l->l_proc;
    619 	struct mount *mp;
    620 	int error;
    621 	struct nameidata nd;
    622 
    623 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    624 	if ((error = namei(&nd)) != 0)
    625 		return (error);
    626 	mp = nd.ni_vp->v_mount;
    627 	vrele(nd.ni_vp);
    628 	return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
    629 	    SCARG(uap, arg), p));
    630 }
    631 
    632 /*
    633  * Get filesystem statistics.
    634  */
    635 /* ARGSUSED */
    636 int
    637 sys_statfs(l, v, retval)
    638 	struct lwp *l;
    639 	void *v;
    640 	register_t *retval;
    641 {
    642 	struct sys_statfs_args /* {
    643 		syscallarg(const char *) path;
    644 		syscallarg(struct statfs *) buf;
    645 	} */ *uap = v;
    646 	struct proc *p = l->l_proc;
    647 	struct mount *mp;
    648 	struct statfs *sp;
    649 	int error;
    650 	struct nameidata nd;
    651 
    652 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    653 	if ((error = namei(&nd)) != 0)
    654 		return (error);
    655 	mp = nd.ni_vp->v_mount;
    656 	sp = &mp->mnt_stat;
    657 	vrele(nd.ni_vp);
    658 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    659 		return (error);
    660 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    661 	sp->f_oflags = sp->f_flags & 0xffff;
    662 	return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
    663 }
    664 
    665 /*
    666  * Get filesystem statistics.
    667  */
    668 /* ARGSUSED */
    669 int
    670 sys_fstatfs(l, v, retval)
    671 	struct lwp *l;
    672 	void *v;
    673 	register_t *retval;
    674 {
    675 	struct sys_fstatfs_args /* {
    676 		syscallarg(int) fd;
    677 		syscallarg(struct statfs *) buf;
    678 	} */ *uap = v;
    679 	struct proc *p = l->l_proc;
    680 	struct file *fp;
    681 	struct mount *mp;
    682 	struct statfs *sp;
    683 	int error;
    684 
    685 	/* getvnode() will use the descriptor for us */
    686 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    687 		return (error);
    688 	mp = ((struct vnode *)fp->f_data)->v_mount;
    689 	sp = &mp->mnt_stat;
    690 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    691 		goto out;
    692 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    693 	sp->f_oflags = sp->f_flags & 0xffff;
    694 	error = copyout(sp, SCARG(uap, buf), sizeof(*sp));
    695  out:
    696 	FILE_UNUSE(fp, p);
    697 	return (error);
    698 }
    699 
    700 /*
    701  * Get statistics on all filesystems.
    702  */
    703 int
    704 sys_getfsstat(l, v, retval)
    705 	struct lwp *l;
    706 	void *v;
    707 	register_t *retval;
    708 {
    709 	struct sys_getfsstat_args /* {
    710 		syscallarg(struct statfs *) buf;
    711 		syscallarg(long) bufsize;
    712 		syscallarg(int) flags;
    713 	} */ *uap = v;
    714 	struct proc *p = l->l_proc;
    715 	struct mount *mp, *nmp;
    716 	struct statfs *sp;
    717 	caddr_t sfsp;
    718 	long count, maxcount, error;
    719 
    720 	maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
    721 	sfsp = (caddr_t)SCARG(uap, buf);
    722 	simple_lock(&mountlist_slock);
    723 	count = 0;
    724 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    725 	     mp = nmp) {
    726 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    727 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    728 			continue;
    729 		}
    730 		if (sfsp && count < maxcount) {
    731 			sp = &mp->mnt_stat;
    732 			/*
    733 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    734 			 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
    735 			 * overrides MNT_NOWAIT.
    736 			 */
    737 			if (SCARG(uap, flags) != MNT_NOWAIT &&
    738 			    SCARG(uap, flags) != MNT_LAZY &&
    739 			    (SCARG(uap, flags) == MNT_WAIT ||
    740 			     SCARG(uap, flags) == 0) &&
    741 			    (error = VFS_STATFS(mp, sp, p)) != 0) {
    742 				simple_lock(&mountlist_slock);
    743 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
    744 				vfs_unbusy(mp);
    745 				continue;
    746 			}
    747 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    748 			sp->f_oflags = sp->f_flags & 0xffff;
    749 			error = copyout(sp, sfsp, sizeof(*sp));
    750 			if (error) {
    751 				vfs_unbusy(mp);
    752 				return (error);
    753 			}
    754 			sfsp += sizeof(*sp);
    755 		}
    756 		count++;
    757 		simple_lock(&mountlist_slock);
    758 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
    759 		vfs_unbusy(mp);
    760 	}
    761 	simple_unlock(&mountlist_slock);
    762 	if (sfsp && count > maxcount)
    763 		*retval = maxcount;
    764 	else
    765 		*retval = count;
    766 	return (0);
    767 }
    768 
    769 /*
    770  * Change current working directory to a given file descriptor.
    771  */
    772 /* ARGSUSED */
    773 int
    774 sys_fchdir(l, v, retval)
    775 	struct lwp *l;
    776 	void *v;
    777 	register_t *retval;
    778 {
    779 	struct sys_fchdir_args /* {
    780 		syscallarg(int) fd;
    781 	} */ *uap = v;
    782 	struct proc *p = l->l_proc;
    783 	struct filedesc *fdp = p->p_fd;
    784 	struct cwdinfo *cwdi = p->p_cwdi;
    785 	struct vnode *vp, *tdp;
    786 	struct mount *mp;
    787 	struct file *fp;
    788 	int error;
    789 
    790 	/* getvnode() will use the descriptor for us */
    791 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    792 		return (error);
    793 	vp = (struct vnode *)fp->f_data;
    794 
    795 	VREF(vp);
    796 	vn_lock(vp,  LK_EXCLUSIVE | LK_RETRY);
    797 	if (vp->v_type != VDIR)
    798 		error = ENOTDIR;
    799 	else
    800 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    801 	while (!error && (mp = vp->v_mountedhere) != NULL) {
    802 		if (vfs_busy(mp, 0, 0))
    803 			continue;
    804 		error = VFS_ROOT(mp, &tdp);
    805 		vfs_unbusy(mp);
    806 		if (error)
    807 			break;
    808 		vput(vp);
    809 		vp = tdp;
    810 	}
    811 	if (error) {
    812 		vput(vp);
    813 		goto out;
    814 	}
    815 	VOP_UNLOCK(vp, 0);
    816 
    817 	/*
    818 	 * Disallow changing to a directory not under the process's
    819 	 * current root directory (if there is one).
    820 	 */
    821 	if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, p)) {
    822 		vrele(vp);
    823 		error = EPERM;	/* operation not permitted */
    824 		goto out;
    825 	}
    826 
    827 	vrele(cwdi->cwdi_cdir);
    828 	cwdi->cwdi_cdir = vp;
    829  out:
    830 	FILE_UNUSE(fp, p);
    831 	return (error);
    832 }
    833 
    834 /*
    835  * Change this process's notion of the root directory to a given file descriptor.
    836  */
    837 
    838 int
    839 sys_fchroot(l, v, retval)
    840 	struct lwp *l;
    841 	void *v;
    842 	register_t *retval;
    843 {
    844 	struct sys_fchroot_args *uap = v;
    845 	struct proc *p = l->l_proc;
    846 	struct filedesc *fdp = p->p_fd;
    847 	struct cwdinfo *cwdi = p->p_cwdi;
    848 	struct vnode	*vp;
    849 	struct file	*fp;
    850 	int		 error;
    851 
    852 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    853 		return error;
    854 	/* getvnode() will use the descriptor for us */
    855 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    856 		return error;
    857 	vp = (struct vnode *) fp->f_data;
    858 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    859 	if (vp->v_type != VDIR)
    860 		error = ENOTDIR;
    861 	else
    862 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    863 	VOP_UNLOCK(vp, 0);
    864 	if (error)
    865 		goto out;
    866 	VREF(vp);
    867 
    868 	/*
    869 	 * Prevent escaping from chroot by putting the root under
    870 	 * the working directory.  Silently chdir to / if we aren't
    871 	 * already there.
    872 	 */
    873 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
    874 		/*
    875 		 * XXX would be more failsafe to change directory to a
    876 		 * deadfs node here instead
    877 		 */
    878 		vrele(cwdi->cwdi_cdir);
    879 		VREF(vp);
    880 		cwdi->cwdi_cdir = vp;
    881 	}
    882 
    883 	if (cwdi->cwdi_rdir != NULL)
    884 		vrele(cwdi->cwdi_rdir);
    885 	cwdi->cwdi_rdir = vp;
    886  out:
    887 	FILE_UNUSE(fp, p);
    888 	return (error);
    889 }
    890 
    891 
    892 
    893 /*
    894  * Change current working directory (``.'').
    895  */
    896 /* ARGSUSED */
    897 int
    898 sys_chdir(l, v, retval)
    899 	struct lwp *l;
    900 	void *v;
    901 	register_t *retval;
    902 {
    903 	struct sys_chdir_args /* {
    904 		syscallarg(const char *) path;
    905 	} */ *uap = v;
    906 	struct proc *p = l->l_proc;
    907 	struct cwdinfo *cwdi = p->p_cwdi;
    908 	int error;
    909 	struct nameidata nd;
    910 
    911 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    912 	    SCARG(uap, path), p);
    913 	if ((error = change_dir(&nd, p)) != 0)
    914 		return (error);
    915 	vrele(cwdi->cwdi_cdir);
    916 	cwdi->cwdi_cdir = nd.ni_vp;
    917 	return (0);
    918 }
    919 
    920 /*
    921  * Change notion of root (``/'') directory.
    922  */
    923 /* ARGSUSED */
    924 int
    925 sys_chroot(l, v, retval)
    926 	struct lwp *l;
    927 	void *v;
    928 	register_t *retval;
    929 {
    930 	struct sys_chroot_args /* {
    931 		syscallarg(const char *) path;
    932 	} */ *uap = v;
    933 	struct proc *p = l->l_proc;
    934 	struct cwdinfo *cwdi = p->p_cwdi;
    935 	struct vnode *vp;
    936 	int error;
    937 	struct nameidata nd;
    938 
    939 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    940 		return (error);
    941 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    942 	    SCARG(uap, path), p);
    943 	if ((error = change_dir(&nd, p)) != 0)
    944 		return (error);
    945 	if (cwdi->cwdi_rdir != NULL)
    946 		vrele(cwdi->cwdi_rdir);
    947 	vp = nd.ni_vp;
    948 	cwdi->cwdi_rdir = vp;
    949 
    950 	/*
    951 	 * Prevent escaping from chroot by putting the root under
    952 	 * the working directory.  Silently chdir to / if we aren't
    953 	 * already there.
    954 	 */
    955 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
    956 		/*
    957 		 * XXX would be more failsafe to change directory to a
    958 		 * deadfs node here instead
    959 		 */
    960 		vrele(cwdi->cwdi_cdir);
    961 		VREF(vp);
    962 		cwdi->cwdi_cdir = vp;
    963 	}
    964 
    965 	return (0);
    966 }
    967 
    968 /*
    969  * Common routine for chroot and chdir.
    970  */
    971 static int
    972 change_dir(ndp, p)
    973 	struct nameidata *ndp;
    974 	struct proc *p;
    975 {
    976 	struct vnode *vp;
    977 	int error;
    978 
    979 	if ((error = namei(ndp)) != 0)
    980 		return (error);
    981 	vp = ndp->ni_vp;
    982 	if (vp->v_type != VDIR)
    983 		error = ENOTDIR;
    984 	else
    985 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    986 
    987 	if (error)
    988 		vput(vp);
    989 	else
    990 		VOP_UNLOCK(vp, 0);
    991 	return (error);
    992 }
    993 
    994 /*
    995  * Check permissions, allocate an open file structure,
    996  * and call the device open routine if any.
    997  */
    998 int
    999 sys_open(l, v, retval)
   1000 	struct lwp *l;
   1001 	void *v;
   1002 	register_t *retval;
   1003 {
   1004 	struct sys_open_args /* {
   1005 		syscallarg(const char *) path;
   1006 		syscallarg(int) flags;
   1007 		syscallarg(int) mode;
   1008 	} */ *uap = v;
   1009 	struct proc *p = l->l_proc;
   1010 	struct cwdinfo *cwdi = p->p_cwdi;
   1011 	struct filedesc *fdp = p->p_fd;
   1012 	struct file *fp;
   1013 	struct vnode *vp;
   1014 	int flags, cmode;
   1015 	int type, indx, error;
   1016 	struct flock lf;
   1017 	struct nameidata nd;
   1018 
   1019 	flags = FFLAGS(SCARG(uap, flags));
   1020 	if ((flags & (FREAD | FWRITE)) == 0)
   1021 		return (EINVAL);
   1022 	/* falloc() will use the file descriptor for us */
   1023 	if ((error = falloc(p, &fp, &indx)) != 0)
   1024 		return (error);
   1025 	cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
   1026 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1027 	p->p_dupfd = -indx - 1;			/* XXX check for fdopen */
   1028 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
   1029 		FILE_UNUSE(fp, p);
   1030 		ffree(fp);
   1031 		if ((error == ENODEV || error == ENXIO) &&
   1032 		    p->p_dupfd >= 0 &&			/* XXX from fdopen */
   1033 		    (error =
   1034 			dupfdopen(p, indx, p->p_dupfd, flags, error)) == 0) {
   1035 			*retval = indx;
   1036 			return (0);
   1037 		}
   1038 		if (error == ERESTART)
   1039 			error = EINTR;
   1040 		fdremove(fdp, indx);
   1041 		return (error);
   1042 	}
   1043 	p->p_dupfd = 0;
   1044 	vp = nd.ni_vp;
   1045 	fp->f_flag = flags & FMASK;
   1046 	fp->f_type = DTYPE_VNODE;
   1047 	fp->f_ops = &vnops;
   1048 	fp->f_data = (caddr_t)vp;
   1049 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1050 		lf.l_whence = SEEK_SET;
   1051 		lf.l_start = 0;
   1052 		lf.l_len = 0;
   1053 		if (flags & O_EXLOCK)
   1054 			lf.l_type = F_WRLCK;
   1055 		else
   1056 			lf.l_type = F_RDLCK;
   1057 		type = F_FLOCK;
   1058 		if ((flags & FNONBLOCK) == 0)
   1059 			type |= F_WAIT;
   1060 		VOP_UNLOCK(vp, 0);
   1061 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
   1062 		if (error) {
   1063 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1064 			FILE_UNUSE(fp, p);
   1065 			ffree(fp);
   1066 			fdremove(fdp, indx);
   1067 			return (error);
   1068 		}
   1069 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1070 		fp->f_flag |= FHASLOCK;
   1071 	}
   1072 	VOP_UNLOCK(vp, 0);
   1073 	*retval = indx;
   1074 	FILE_SET_MATURE(fp);
   1075 	FILE_UNUSE(fp, p);
   1076 	return (0);
   1077 }
   1078 
   1079 /*
   1080  * Get file handle system call
   1081  */
   1082 int
   1083 sys_getfh(l, v, retval)
   1084 	struct lwp *l;
   1085 	void *v;
   1086 	register_t *retval;
   1087 {
   1088 	struct sys_getfh_args /* {
   1089 		syscallarg(char *) fname;
   1090 		syscallarg(fhandle_t *) fhp;
   1091 	} */ *uap = v;
   1092 	struct proc *p = l->l_proc;
   1093 	struct vnode *vp;
   1094 	fhandle_t fh;
   1095 	int error;
   1096 	struct nameidata nd;
   1097 
   1098 	/*
   1099 	 * Must be super user
   1100 	 */
   1101 	error = suser(p->p_ucred, &p->p_acflag);
   1102 	if (error)
   1103 		return (error);
   1104 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1105 	    SCARG(uap, fname), p);
   1106 	error = namei(&nd);
   1107 	if (error)
   1108 		return (error);
   1109 	vp = nd.ni_vp;
   1110 	memset((caddr_t)&fh, 0, sizeof(fh));
   1111 	fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1112 	error = VFS_VPTOFH(vp, &fh.fh_fid);
   1113 	vput(vp);
   1114 	if (error)
   1115 		return (error);
   1116 	error = copyout((caddr_t)&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh));
   1117 	return (error);
   1118 }
   1119 
   1120 /*
   1121  * Open a file given a file handle.
   1122  *
   1123  * Check permissions, allocate an open file structure,
   1124  * and call the device open routine if any.
   1125  */
   1126 int
   1127 sys_fhopen(l, v, retval)
   1128 	struct lwp *l;
   1129 	void *v;
   1130 	register_t *retval;
   1131 {
   1132 	struct sys_fhopen_args /* {
   1133 		syscallarg(const fhandle_t *) fhp;
   1134 		syscallarg(int) flags;
   1135 	} */ *uap = v;
   1136 	struct proc *p = l->l_proc;
   1137 	struct filedesc *fdp = p->p_fd;
   1138 	struct file *fp;
   1139 	struct vnode *vp = NULL;
   1140 	struct mount *mp;
   1141 	struct ucred *cred = p->p_ucred;
   1142 	int flags;
   1143 	struct file *nfp;
   1144 	int type, indx, error=0;
   1145 	struct flock lf;
   1146 	struct vattr va;
   1147 	fhandle_t fh;
   1148 
   1149 	/*
   1150 	 * Must be super user
   1151 	 */
   1152 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1153 		return (error);
   1154 
   1155 	flags = FFLAGS(SCARG(uap, flags));
   1156 	if ((flags & (FREAD | FWRITE)) == 0)
   1157 		return (EINVAL);
   1158 	if ((flags & O_CREAT))
   1159 		return (EINVAL);
   1160 	/* falloc() will use the file descriptor for us */
   1161 	if ((error = falloc(p, &nfp, &indx)) != 0)
   1162 		return (error);
   1163 	fp = nfp;
   1164 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1165 		goto bad;
   1166 
   1167 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
   1168 		error = ESTALE;
   1169 		goto bad;
   1170 	}
   1171 
   1172 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) {
   1173 		vp = NULL;	/* most likely unnecessary sanity for bad: */
   1174 		goto bad;
   1175 	}
   1176 
   1177 	/* Now do an effective vn_open */
   1178 
   1179 	if (vp->v_type == VSOCK) {
   1180 		error = EOPNOTSUPP;
   1181 		goto bad;
   1182 	}
   1183 	if (flags & FREAD) {
   1184 		if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
   1185 			goto bad;
   1186 	}
   1187 	if (flags & (FWRITE | O_TRUNC)) {
   1188 		if (vp->v_type == VDIR) {
   1189 			error = EISDIR;
   1190 			goto bad;
   1191 		}
   1192 		if ((error = vn_writechk(vp)) != 0 ||
   1193 		    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
   1194 			goto bad;
   1195 	}
   1196 	if (flags & O_TRUNC) {
   1197 		VOP_UNLOCK(vp, 0);			/* XXX */
   1198 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1199 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
   1200 		VATTR_NULL(&va);
   1201 		va.va_size = 0;
   1202 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
   1203 			goto bad;
   1204 	}
   1205 	if ((error = VOP_OPEN(vp, flags, cred, p)) != 0)
   1206 		goto bad;
   1207 	if (vp->v_type == VREG &&
   1208 	    uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
   1209 		error = EIO;
   1210 		goto bad;
   1211 	}
   1212 	if (flags & FWRITE)
   1213 		vp->v_writecount++;
   1214 
   1215 	/* done with modified vn_open, now finish what sys_open does. */
   1216 
   1217 	fp->f_flag = flags & FMASK;
   1218 	fp->f_type = DTYPE_VNODE;
   1219 	fp->f_ops = &vnops;
   1220 	fp->f_data = (caddr_t)vp;
   1221 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1222 		lf.l_whence = SEEK_SET;
   1223 		lf.l_start = 0;
   1224 		lf.l_len = 0;
   1225 		if (flags & O_EXLOCK)
   1226 			lf.l_type = F_WRLCK;
   1227 		else
   1228 			lf.l_type = F_RDLCK;
   1229 		type = F_FLOCK;
   1230 		if ((flags & FNONBLOCK) == 0)
   1231 			type |= F_WAIT;
   1232 		VOP_UNLOCK(vp, 0);
   1233 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
   1234 		if (error) {
   1235 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1236 			FILE_UNUSE(fp, p);
   1237 			ffree(fp);
   1238 			fdremove(fdp, indx);
   1239 			return (error);
   1240 		}
   1241 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1242 		fp->f_flag |= FHASLOCK;
   1243 	}
   1244 	VOP_UNLOCK(vp, 0);
   1245 	*retval = indx;
   1246 	FILE_SET_MATURE(fp);
   1247 	FILE_UNUSE(fp, p);
   1248 	return (0);
   1249 
   1250 bad:
   1251 	FILE_UNUSE(fp, p);
   1252 	ffree(fp);
   1253 	fdremove(fdp, indx);
   1254 	if (vp != NULL)
   1255 		vput(vp);
   1256 	return (error);
   1257 }
   1258 
   1259 /* ARGSUSED */
   1260 int
   1261 sys_fhstat(l, v, retval)
   1262 	struct lwp *l;
   1263 	void *v;
   1264 	register_t *retval;
   1265 {
   1266 	struct sys_fhstat_args /* {
   1267 		syscallarg(const fhandle_t *) fhp;
   1268 		syscallarg(struct stat *) sb;
   1269 	} */ *uap = v;
   1270 	struct proc *p = l->l_proc;
   1271 	struct stat sb;
   1272 	int error;
   1273 	fhandle_t fh;
   1274 	struct mount *mp;
   1275 	struct vnode *vp;
   1276 
   1277 	/*
   1278 	 * Must be super user
   1279 	 */
   1280 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1281 		return (error);
   1282 
   1283 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1284 		return (error);
   1285 
   1286 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1287 		return (ESTALE);
   1288 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1289 		return (error);
   1290 	error = vn_stat(vp, &sb, p);
   1291 	vput(vp);
   1292 	if (error)
   1293 		return (error);
   1294 	error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
   1295 	return (error);
   1296 }
   1297 
   1298 /* ARGSUSED */
   1299 int
   1300 sys_fhstatfs(l, v, retval)
   1301 	struct lwp *l;
   1302 	void *v;
   1303 	register_t *retval;
   1304 {
   1305 	struct sys_fhstatfs_args /*
   1306 		syscallarg(const fhandle_t *) fhp;
   1307 		syscallarg(struct statfs *) buf;
   1308 	} */ *uap = v;
   1309 	struct proc *p = l->l_proc;
   1310 	struct statfs sp;
   1311 	fhandle_t fh;
   1312 	struct mount *mp;
   1313 	struct vnode *vp;
   1314 	int error;
   1315 
   1316 	/*
   1317 	 * Must be super user
   1318 	 */
   1319 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1320 		return (error);
   1321 
   1322 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1323 		return (error);
   1324 
   1325 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1326 		return (ESTALE);
   1327 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1328 		return (error);
   1329 	mp = vp->v_mount;
   1330 	vput(vp);
   1331 	if ((error = VFS_STATFS(mp, &sp, p)) != 0)
   1332 		return (error);
   1333 	sp.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   1334 	sp.f_oflags = sp.f_flags & 0xffff;
   1335 	return (copyout(&sp, SCARG(uap, buf), sizeof(sp)));
   1336 }
   1337 
   1338 /*
   1339  * Create a special file.
   1340  */
   1341 /* ARGSUSED */
   1342 int
   1343 sys_mknod(l, v, retval)
   1344 	struct lwp *l;
   1345 	void *v;
   1346 	register_t *retval;
   1347 {
   1348 	struct sys_mknod_args /* {
   1349 		syscallarg(const char *) path;
   1350 		syscallarg(int) mode;
   1351 		syscallarg(int) dev;
   1352 	} */ *uap = v;
   1353 	struct proc *p = l->l_proc;
   1354 	struct vnode *vp;
   1355 	struct vattr vattr;
   1356 	int error;
   1357 	int whiteout = 0;
   1358 	struct nameidata nd;
   1359 
   1360 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1361 		return (error);
   1362 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1363 	if ((error = namei(&nd)) != 0)
   1364 		return (error);
   1365 	vp = nd.ni_vp;
   1366 	if (vp != NULL)
   1367 		error = EEXIST;
   1368 	else {
   1369 		VATTR_NULL(&vattr);
   1370 		vattr.va_mode =
   1371 		    (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1372 		vattr.va_rdev = SCARG(uap, dev);
   1373 		whiteout = 0;
   1374 
   1375 		switch (SCARG(uap, mode) & S_IFMT) {
   1376 		case S_IFMT:	/* used by badsect to flag bad sectors */
   1377 			vattr.va_type = VBAD;
   1378 			break;
   1379 		case S_IFCHR:
   1380 			vattr.va_type = VCHR;
   1381 			break;
   1382 		case S_IFBLK:
   1383 			vattr.va_type = VBLK;
   1384 			break;
   1385 		case S_IFWHT:
   1386 			whiteout = 1;
   1387 			break;
   1388 		default:
   1389 			error = EINVAL;
   1390 			break;
   1391 		}
   1392 	}
   1393 	if (!error) {
   1394 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1395 		if (whiteout) {
   1396 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
   1397 			if (error)
   1398 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1399 			vput(nd.ni_dvp);
   1400 		} else {
   1401 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
   1402 						&nd.ni_cnd, &vattr);
   1403 			if (error == 0)
   1404 				vput(nd.ni_vp);
   1405 		}
   1406 	} else {
   1407 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1408 		if (nd.ni_dvp == vp)
   1409 			vrele(nd.ni_dvp);
   1410 		else
   1411 			vput(nd.ni_dvp);
   1412 		if (vp)
   1413 			vrele(vp);
   1414 	}
   1415 	return (error);
   1416 }
   1417 
   1418 /*
   1419  * Create a named pipe.
   1420  */
   1421 /* ARGSUSED */
   1422 int
   1423 sys_mkfifo(l, v, retval)
   1424 	struct lwp *l;
   1425 	void *v;
   1426 	register_t *retval;
   1427 {
   1428 	struct sys_mkfifo_args /* {
   1429 		syscallarg(const char *) path;
   1430 		syscallarg(int) mode;
   1431 	} */ *uap = v;
   1432 	struct proc *p = l->l_proc;
   1433 	struct vattr vattr;
   1434 	int error;
   1435 	struct nameidata nd;
   1436 
   1437 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1438 	if ((error = namei(&nd)) != 0)
   1439 		return (error);
   1440 	if (nd.ni_vp != NULL) {
   1441 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1442 		if (nd.ni_dvp == nd.ni_vp)
   1443 			vrele(nd.ni_dvp);
   1444 		else
   1445 			vput(nd.ni_dvp);
   1446 		vrele(nd.ni_vp);
   1447 		return (EEXIST);
   1448 	}
   1449 	VATTR_NULL(&vattr);
   1450 	vattr.va_type = VFIFO;
   1451 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1452 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1453 	error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1454 	if (error == 0)
   1455 		vput(nd.ni_vp);
   1456 	return (error);
   1457 }
   1458 
   1459 /*
   1460  * Make a hard file link.
   1461  */
   1462 /* ARGSUSED */
   1463 int
   1464 sys_link(l, v, retval)
   1465 	struct lwp *l;
   1466 	void *v;
   1467 	register_t *retval;
   1468 {
   1469 	struct sys_link_args /* {
   1470 		syscallarg(const char *) path;
   1471 		syscallarg(const char *) link;
   1472 	} */ *uap = v;
   1473 	struct proc *p = l->l_proc;
   1474 	struct vnode *vp;
   1475 	struct nameidata nd;
   1476 	int error;
   1477 
   1478 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1479 	if ((error = namei(&nd)) != 0)
   1480 		return (error);
   1481 	vp = nd.ni_vp;
   1482 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1483 	if ((error = namei(&nd)) != 0)
   1484 		goto out;
   1485 	if (nd.ni_vp) {
   1486 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1487 		if (nd.ni_dvp == nd.ni_vp)
   1488 			vrele(nd.ni_dvp);
   1489 		else
   1490 			vput(nd.ni_dvp);
   1491 		vrele(nd.ni_vp);
   1492 		error = EEXIST;
   1493 		goto out;
   1494 	}
   1495 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1496 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1497 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1498 out:
   1499 	vrele(vp);
   1500 	return (error);
   1501 }
   1502 
   1503 /*
   1504  * Make a symbolic link.
   1505  */
   1506 /* ARGSUSED */
   1507 int
   1508 sys_symlink(l, v, retval)
   1509 	struct lwp *l;
   1510 	void *v;
   1511 	register_t *retval;
   1512 {
   1513 	struct sys_symlink_args /* {
   1514 		syscallarg(const char *) path;
   1515 		syscallarg(const char *) link;
   1516 	} */ *uap = v;
   1517 	struct proc *p = l->l_proc;
   1518 	struct vattr vattr;
   1519 	char *path;
   1520 	int error;
   1521 	struct nameidata nd;
   1522 
   1523 	path = PNBUF_GET();
   1524 	error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
   1525 	if (error)
   1526 		goto out;
   1527 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1528 	if ((error = namei(&nd)) != 0)
   1529 		goto out;
   1530 	if (nd.ni_vp) {
   1531 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1532 		if (nd.ni_dvp == nd.ni_vp)
   1533 			vrele(nd.ni_dvp);
   1534 		else
   1535 			vput(nd.ni_dvp);
   1536 		vrele(nd.ni_vp);
   1537 		error = EEXIST;
   1538 		goto out;
   1539 	}
   1540 	VATTR_NULL(&vattr);
   1541 	vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
   1542 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1543 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   1544 	if (error == 0)
   1545 		vput(nd.ni_vp);
   1546 out:
   1547 	PNBUF_PUT(path);
   1548 	return (error);
   1549 }
   1550 
   1551 /*
   1552  * Delete a whiteout from the filesystem.
   1553  */
   1554 /* ARGSUSED */
   1555 int
   1556 sys_undelete(l, v, retval)
   1557 	struct lwp *l;
   1558 	void *v;
   1559 	register_t *retval;
   1560 {
   1561 	struct sys_undelete_args /* {
   1562 		syscallarg(const char *) path;
   1563 	} */ *uap = v;
   1564 	struct proc *p = l->l_proc;
   1565 	int error;
   1566 	struct nameidata nd;
   1567 
   1568 	NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
   1569 	    SCARG(uap, path), p);
   1570 	error = namei(&nd);
   1571 	if (error)
   1572 		return (error);
   1573 
   1574 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   1575 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1576 		if (nd.ni_dvp == nd.ni_vp)
   1577 			vrele(nd.ni_dvp);
   1578 		else
   1579 			vput(nd.ni_dvp);
   1580 		if (nd.ni_vp)
   1581 			vrele(nd.ni_vp);
   1582 		return (EEXIST);
   1583 	}
   1584 
   1585 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1586 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   1587 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1588 	vput(nd.ni_dvp);
   1589 	return (error);
   1590 }
   1591 
   1592 /*
   1593  * Delete a name from the filesystem.
   1594  */
   1595 /* ARGSUSED */
   1596 int
   1597 sys_unlink(l, v, retval)
   1598 	struct lwp *l;
   1599 	void *v;
   1600 	register_t *retval;
   1601 {
   1602 	struct sys_unlink_args /* {
   1603 		syscallarg(const char *) path;
   1604 	} */ *uap = v;
   1605 	struct proc *p = l->l_proc;
   1606 	struct vnode *vp;
   1607 	int error;
   1608 	struct nameidata nd;
   1609 
   1610 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1611 	    SCARG(uap, path), p);
   1612 	if ((error = namei(&nd)) != 0)
   1613 		return (error);
   1614 	vp = nd.ni_vp;
   1615 
   1616 	/*
   1617 	 * The root of a mounted filesystem cannot be deleted.
   1618 	 */
   1619 	if (vp->v_flag & VROOT) {
   1620 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1621 		if (nd.ni_dvp == vp)
   1622 			vrele(nd.ni_dvp);
   1623 		else
   1624 			vput(nd.ni_dvp);
   1625 		vput(vp);
   1626 		error = EBUSY;
   1627 		goto out;
   1628 	}
   1629 
   1630 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1631 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1632 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1633 out:
   1634 	return (error);
   1635 }
   1636 
   1637 /*
   1638  * Reposition read/write file offset.
   1639  */
   1640 int
   1641 sys_lseek(l, v, retval)
   1642 	struct lwp *l;
   1643 	void *v;
   1644 	register_t *retval;
   1645 {
   1646 	struct sys_lseek_args /* {
   1647 		syscallarg(int) fd;
   1648 		syscallarg(int) pad;
   1649 		syscallarg(off_t) offset;
   1650 		syscallarg(int) whence;
   1651 	} */ *uap = v;
   1652 	struct proc *p = l->l_proc;
   1653 	struct ucred *cred = p->p_ucred;
   1654 	struct filedesc *fdp = p->p_fd;
   1655 	struct file *fp;
   1656 	struct vnode *vp;
   1657 	struct vattr vattr;
   1658 	off_t newoff;
   1659 	int error;
   1660 
   1661 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
   1662 		return (EBADF);
   1663 
   1664 	FILE_USE(fp);
   1665 
   1666 	vp = (struct vnode *)fp->f_data;
   1667 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1668 		error = ESPIPE;
   1669 		goto out;
   1670 	}
   1671 
   1672 	switch (SCARG(uap, whence)) {
   1673 	case SEEK_CUR:
   1674 		newoff = fp->f_offset + SCARG(uap, offset);
   1675 		break;
   1676 	case SEEK_END:
   1677 		error = VOP_GETATTR(vp, &vattr, cred, p);
   1678 		if (error)
   1679 			goto out;
   1680 		newoff = SCARG(uap, offset) + vattr.va_size;
   1681 		break;
   1682 	case SEEK_SET:
   1683 		newoff = SCARG(uap, offset);
   1684 		break;
   1685 	default:
   1686 		error = EINVAL;
   1687 		goto out;
   1688 	}
   1689 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
   1690 		goto out;
   1691 
   1692 	*(off_t *)retval = fp->f_offset = newoff;
   1693  out:
   1694 	FILE_UNUSE(fp, p);
   1695 	return (error);
   1696 }
   1697 
   1698 /*
   1699  * Positional read system call.
   1700  */
   1701 int
   1702 sys_pread(l, v, retval)
   1703 	struct lwp *l;
   1704 	void *v;
   1705 	register_t *retval;
   1706 {
   1707 	struct sys_pread_args /* {
   1708 		syscallarg(int) fd;
   1709 		syscallarg(void *) buf;
   1710 		syscallarg(size_t) nbyte;
   1711 		syscallarg(off_t) offset;
   1712 	} */ *uap = v;
   1713 	struct proc *p = l->l_proc;
   1714 	struct filedesc *fdp = p->p_fd;
   1715 	struct file *fp;
   1716 	struct vnode *vp;
   1717 	off_t offset;
   1718 	int error, fd = SCARG(uap, fd);
   1719 
   1720 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1721 		return (EBADF);
   1722 
   1723 	if ((fp->f_flag & FREAD) == 0)
   1724 		return (EBADF);
   1725 
   1726 	FILE_USE(fp);
   1727 
   1728 	vp = (struct vnode *)fp->f_data;
   1729 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1730 		error = ESPIPE;
   1731 		goto out;
   1732 	}
   1733 
   1734 	offset = SCARG(uap, offset);
   1735 
   1736 	/*
   1737 	 * XXX This works because no file systems actually
   1738 	 * XXX take any action on the seek operation.
   1739 	 */
   1740 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1741 		goto out;
   1742 
   1743 	/* dofileread() will unuse the descriptor for us */
   1744 	return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   1745 	    &offset, 0, retval));
   1746 
   1747  out:
   1748 	FILE_UNUSE(fp, p);
   1749 	return (error);
   1750 }
   1751 
   1752 /*
   1753  * Positional scatter read system call.
   1754  */
   1755 int
   1756 sys_preadv(l, v, retval)
   1757 	struct lwp *l;
   1758 	void *v;
   1759 	register_t *retval;
   1760 {
   1761 	struct sys_preadv_args /* {
   1762 		syscallarg(int) fd;
   1763 		syscallarg(const struct iovec *) iovp;
   1764 		syscallarg(int) iovcnt;
   1765 		syscallarg(off_t) offset;
   1766 	} */ *uap = v;
   1767 	struct proc *p = l->l_proc;
   1768 	struct filedesc *fdp = p->p_fd;
   1769 	struct file *fp;
   1770 	struct vnode *vp;
   1771 	off_t offset;
   1772 	int error, fd = SCARG(uap, fd);
   1773 
   1774 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1775 		return (EBADF);
   1776 
   1777 	if ((fp->f_flag & FREAD) == 0)
   1778 		return (EBADF);
   1779 
   1780 	FILE_USE(fp);
   1781 
   1782 	vp = (struct vnode *)fp->f_data;
   1783 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1784 		error = ESPIPE;
   1785 		goto out;
   1786 	}
   1787 
   1788 	offset = SCARG(uap, offset);
   1789 
   1790 	/*
   1791 	 * XXX This works because no file systems actually
   1792 	 * XXX take any action on the seek operation.
   1793 	 */
   1794 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1795 		goto out;
   1796 
   1797 	/* dofilereadv() will unuse the descriptor for us */
   1798 	return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   1799 	    &offset, 0, retval));
   1800 
   1801  out:
   1802 	FILE_UNUSE(fp, p);
   1803 	return (error);
   1804 }
   1805 
   1806 /*
   1807  * Positional write system call.
   1808  */
   1809 int
   1810 sys_pwrite(l, v, retval)
   1811 	struct lwp *l;
   1812 	void *v;
   1813 	register_t *retval;
   1814 {
   1815 	struct sys_pwrite_args /* {
   1816 		syscallarg(int) fd;
   1817 		syscallarg(const void *) buf;
   1818 		syscallarg(size_t) nbyte;
   1819 		syscallarg(off_t) offset;
   1820 	} */ *uap = v;
   1821 	struct proc *p = l->l_proc;
   1822 	struct filedesc *fdp = p->p_fd;
   1823 	struct file *fp;
   1824 	struct vnode *vp;
   1825 	off_t offset;
   1826 	int error, fd = SCARG(uap, fd);
   1827 
   1828 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1829 		return (EBADF);
   1830 
   1831 	if ((fp->f_flag & FWRITE) == 0)
   1832 		return (EBADF);
   1833 
   1834 	FILE_USE(fp);
   1835 
   1836 	vp = (struct vnode *)fp->f_data;
   1837 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1838 		error = ESPIPE;
   1839 		goto out;
   1840 	}
   1841 
   1842 	offset = SCARG(uap, offset);
   1843 
   1844 	/*
   1845 	 * XXX This works because no file systems actually
   1846 	 * XXX take any action on the seek operation.
   1847 	 */
   1848 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1849 		goto out;
   1850 
   1851 	/* dofilewrite() will unuse the descriptor for us */
   1852 	return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   1853 	    &offset, 0, retval));
   1854 
   1855  out:
   1856 	FILE_UNUSE(fp, p);
   1857 	return (error);
   1858 }
   1859 
   1860 /*
   1861  * Positional gather write system call.
   1862  */
   1863 int
   1864 sys_pwritev(l, v, retval)
   1865 	struct lwp *l;
   1866 	void *v;
   1867 	register_t *retval;
   1868 {
   1869 	struct sys_pwritev_args /* {
   1870 		syscallarg(int) fd;
   1871 		syscallarg(const struct iovec *) iovp;
   1872 		syscallarg(int) iovcnt;
   1873 		syscallarg(off_t) offset;
   1874 	} */ *uap = v;
   1875 	struct proc *p = l->l_proc;
   1876 	struct filedesc *fdp = p->p_fd;
   1877 	struct file *fp;
   1878 	struct vnode *vp;
   1879 	off_t offset;
   1880 	int error, fd = SCARG(uap, fd);
   1881 
   1882 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1883 		return (EBADF);
   1884 
   1885 	if ((fp->f_flag & FWRITE) == 0)
   1886 		return (EBADF);
   1887 
   1888 	FILE_USE(fp);
   1889 
   1890 	vp = (struct vnode *)fp->f_data;
   1891 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1892 		error = ESPIPE;
   1893 		goto out;
   1894 	}
   1895 
   1896 	offset = SCARG(uap, offset);
   1897 
   1898 	/*
   1899 	 * XXX This works because no file systems actually
   1900 	 * XXX take any action on the seek operation.
   1901 	 */
   1902 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1903 		goto out;
   1904 
   1905 	/* dofilewritev() will unuse the descriptor for us */
   1906 	return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   1907 	    &offset, 0, retval));
   1908 
   1909  out:
   1910 	FILE_UNUSE(fp, p);
   1911 	return (error);
   1912 }
   1913 
   1914 /*
   1915  * Check access permissions.
   1916  */
   1917 int
   1918 sys_access(l, v, retval)
   1919 	struct lwp *l;
   1920 	void *v;
   1921 	register_t *retval;
   1922 {
   1923 	struct sys_access_args /* {
   1924 		syscallarg(const char *) path;
   1925 		syscallarg(int) flags;
   1926 	} */ *uap = v;
   1927 	struct proc *p = l->l_proc;
   1928 	struct ucred *cred = crget();
   1929 	struct vnode *vp;
   1930 	int error, flags;
   1931 	struct nameidata nd;
   1932 
   1933 	(void)memcpy(cred, p->p_ucred, sizeof(*cred));
   1934 	cred->cr_ref = 1;
   1935 	cred->cr_uid = p->p_cred->p_ruid;
   1936 	cred->cr_gid = p->p_cred->p_rgid;
   1937 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1938 	    SCARG(uap, path), p);
   1939 	/* Override default credentials */
   1940 	nd.ni_cnd.cn_cred = cred;
   1941 	if ((error = namei(&nd)) != 0)
   1942 		goto out;
   1943 	vp = nd.ni_vp;
   1944 
   1945 	/* Flags == 0 means only check for existence. */
   1946 	if (SCARG(uap, flags)) {
   1947 		flags = 0;
   1948 		if (SCARG(uap, flags) & R_OK)
   1949 			flags |= VREAD;
   1950 		if (SCARG(uap, flags) & W_OK)
   1951 			flags |= VWRITE;
   1952 		if (SCARG(uap, flags) & X_OK)
   1953 			flags |= VEXEC;
   1954 
   1955 		error = VOP_ACCESS(vp, flags, cred, p);
   1956 		if (!error && (flags & VWRITE))
   1957 			error = vn_writechk(vp);
   1958 	}
   1959 	vput(vp);
   1960 out:
   1961 	crfree(cred);
   1962 	return (error);
   1963 }
   1964 
   1965 /*
   1966  * Get file status; this version follows links.
   1967  */
   1968 /* ARGSUSED */
   1969 int
   1970 sys___stat13(l, v, retval)
   1971 	struct lwp *l;
   1972 	void *v;
   1973 	register_t *retval;
   1974 {
   1975 	struct sys___stat13_args /* {
   1976 		syscallarg(const char *) path;
   1977 		syscallarg(struct stat *) ub;
   1978 	} */ *uap = v;
   1979 	struct proc *p = l->l_proc;
   1980 	struct stat sb;
   1981 	int error;
   1982 	struct nameidata nd;
   1983 
   1984 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1985 	    SCARG(uap, path), p);
   1986 	if ((error = namei(&nd)) != 0)
   1987 		return (error);
   1988 	error = vn_stat(nd.ni_vp, &sb, p);
   1989 	vput(nd.ni_vp);
   1990 	if (error)
   1991 		return (error);
   1992 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   1993 	return (error);
   1994 }
   1995 
   1996 /*
   1997  * Get file status; this version does not follow links.
   1998  */
   1999 /* ARGSUSED */
   2000 int
   2001 sys___lstat13(l, v, retval)
   2002 	struct lwp *l;
   2003 	void *v;
   2004 	register_t *retval;
   2005 {
   2006 	struct sys___lstat13_args /* {
   2007 		syscallarg(const char *) path;
   2008 		syscallarg(struct stat *) ub;
   2009 	} */ *uap = v;
   2010 	struct proc *p = l->l_proc;
   2011 	struct stat sb;
   2012 	int error;
   2013 	struct nameidata nd;
   2014 
   2015 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2016 	    SCARG(uap, path), p);
   2017 	if ((error = namei(&nd)) != 0)
   2018 		return (error);
   2019 	error = vn_stat(nd.ni_vp, &sb, p);
   2020 	vput(nd.ni_vp);
   2021 	if (error)
   2022 		return (error);
   2023 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2024 	return (error);
   2025 }
   2026 
   2027 /*
   2028  * Get configurable pathname variables.
   2029  */
   2030 /* ARGSUSED */
   2031 int
   2032 sys_pathconf(l, v, retval)
   2033 	struct lwp *l;
   2034 	void *v;
   2035 	register_t *retval;
   2036 {
   2037 	struct sys_pathconf_args /* {
   2038 		syscallarg(const char *) path;
   2039 		syscallarg(int) name;
   2040 	} */ *uap = v;
   2041 	struct proc *p = l->l_proc;
   2042 	int error;
   2043 	struct nameidata nd;
   2044 
   2045 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   2046 	    SCARG(uap, path), p);
   2047 	if ((error = namei(&nd)) != 0)
   2048 		return (error);
   2049 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   2050 	vput(nd.ni_vp);
   2051 	return (error);
   2052 }
   2053 
   2054 /*
   2055  * Return target name of a symbolic link.
   2056  */
   2057 /* ARGSUSED */
   2058 int
   2059 sys_readlink(l, v, retval)
   2060 	struct lwp *l;
   2061 	void *v;
   2062 	register_t *retval;
   2063 {
   2064 	struct sys_readlink_args /* {
   2065 		syscallarg(const char *) path;
   2066 		syscallarg(char *) buf;
   2067 		syscallarg(size_t) count;
   2068 	} */ *uap = v;
   2069 	struct proc *p = l->l_proc;
   2070 	struct vnode *vp;
   2071 	struct iovec aiov;
   2072 	struct uio auio;
   2073 	int error;
   2074 	struct nameidata nd;
   2075 
   2076 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2077 	    SCARG(uap, path), p);
   2078 	if ((error = namei(&nd)) != 0)
   2079 		return (error);
   2080 	vp = nd.ni_vp;
   2081 	if (vp->v_type != VLNK)
   2082 		error = EINVAL;
   2083 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   2084 	    (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
   2085 		aiov.iov_base = SCARG(uap, buf);
   2086 		aiov.iov_len = SCARG(uap, count);
   2087 		auio.uio_iov = &aiov;
   2088 		auio.uio_iovcnt = 1;
   2089 		auio.uio_offset = 0;
   2090 		auio.uio_rw = UIO_READ;
   2091 		auio.uio_segflg = UIO_USERSPACE;
   2092 		auio.uio_procp = p;
   2093 		auio.uio_resid = SCARG(uap, count);
   2094 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   2095 	}
   2096 	vput(vp);
   2097 	*retval = SCARG(uap, count) - auio.uio_resid;
   2098 	return (error);
   2099 }
   2100 
   2101 /*
   2102  * Change flags of a file given a path name.
   2103  */
   2104 /* ARGSUSED */
   2105 int
   2106 sys_chflags(l, v, retval)
   2107 	struct lwp *l;
   2108 	void *v;
   2109 	register_t *retval;
   2110 {
   2111 	struct sys_chflags_args /* {
   2112 		syscallarg(const char *) path;
   2113 		syscallarg(u_long) flags;
   2114 	} */ *uap = v;
   2115 	struct proc *p = l->l_proc;
   2116 	struct vnode *vp;
   2117 	int error;
   2118 	struct nameidata nd;
   2119 
   2120 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2121 	if ((error = namei(&nd)) != 0)
   2122 		return (error);
   2123 	vp = nd.ni_vp;
   2124 	error = change_flags(vp, SCARG(uap, flags), p);
   2125 	vput(vp);
   2126 	return (error);
   2127 }
   2128 
   2129 /*
   2130  * Change flags of a file given a file descriptor.
   2131  */
   2132 /* ARGSUSED */
   2133 int
   2134 sys_fchflags(l, v, retval)
   2135 	struct lwp *l;
   2136 	void *v;
   2137 	register_t *retval;
   2138 {
   2139 	struct sys_fchflags_args /* {
   2140 		syscallarg(int) fd;
   2141 		syscallarg(u_long) flags;
   2142 	} */ *uap = v;
   2143 	struct proc *p = l->l_proc;
   2144 	struct vnode *vp;
   2145 	struct file *fp;
   2146 	int error;
   2147 
   2148 	/* getvnode() will use the descriptor for us */
   2149 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2150 		return (error);
   2151 	vp = (struct vnode *)fp->f_data;
   2152 	error = change_flags(vp, SCARG(uap, flags), p);
   2153 	VOP_UNLOCK(vp, 0);
   2154 	FILE_UNUSE(fp, p);
   2155 	return (error);
   2156 }
   2157 
   2158 /*
   2159  * Change flags of a file given a path name; this version does
   2160  * not follow links.
   2161  */
   2162 int
   2163 sys_lchflags(l, v, retval)
   2164 	struct lwp *l;
   2165 	void *v;
   2166 	register_t *retval;
   2167 {
   2168 	struct sys_lchflags_args /* {
   2169 		syscallarg(const char *) path;
   2170 		syscallarg(u_long) flags;
   2171 	} */ *uap = v;
   2172 	struct proc *p = l->l_proc;
   2173 	struct vnode *vp;
   2174 	int error;
   2175 	struct nameidata nd;
   2176 
   2177 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2178 	if ((error = namei(&nd)) != 0)
   2179 		return (error);
   2180 	vp = nd.ni_vp;
   2181 	error = change_flags(vp, SCARG(uap, flags), p);
   2182 	vput(vp);
   2183 	return (error);
   2184 }
   2185 
   2186 /*
   2187  * Common routine to change flags of a file.
   2188  */
   2189 int
   2190 change_flags(vp, flags, p)
   2191 	struct vnode *vp;
   2192 	u_long flags;
   2193 	struct proc *p;
   2194 {
   2195 	struct vattr vattr;
   2196 	int error;
   2197 
   2198 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2199 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2200 	/*
   2201 	 * Non-superusers cannot change the flags on devices, even if they
   2202 	 * own them.
   2203 	 */
   2204 	if (suser(p->p_ucred, &p->p_acflag) != 0) {
   2205 		if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2206 			goto out;
   2207 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2208 			error = EINVAL;
   2209 			goto out;
   2210 		}
   2211 	}
   2212 	VATTR_NULL(&vattr);
   2213 	vattr.va_flags = flags;
   2214 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2215 out:
   2216 	return (error);
   2217 }
   2218 
   2219 /*
   2220  * Change mode of a file given path name; this version follows links.
   2221  */
   2222 /* ARGSUSED */
   2223 int
   2224 sys_chmod(l, v, retval)
   2225 	struct lwp *l;
   2226 	void *v;
   2227 	register_t *retval;
   2228 {
   2229 	struct sys_chmod_args /* {
   2230 		syscallarg(const char *) path;
   2231 		syscallarg(int) mode;
   2232 	} */ *uap = v;
   2233 	struct proc *p = l->l_proc;
   2234 	int error;
   2235 	struct nameidata nd;
   2236 
   2237 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2238 	if ((error = namei(&nd)) != 0)
   2239 		return (error);
   2240 
   2241 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2242 
   2243 	vrele(nd.ni_vp);
   2244 	return (error);
   2245 }
   2246 
   2247 /*
   2248  * Change mode of a file given a file descriptor.
   2249  */
   2250 /* ARGSUSED */
   2251 int
   2252 sys_fchmod(l, v, retval)
   2253 	struct lwp *l;
   2254 	void *v;
   2255 	register_t *retval;
   2256 {
   2257 	struct sys_fchmod_args /* {
   2258 		syscallarg(int) fd;
   2259 		syscallarg(int) mode;
   2260 	} */ *uap = v;
   2261 	struct proc *p = l->l_proc;
   2262 	struct file *fp;
   2263 	int error;
   2264 
   2265 	/* getvnode() will use the descriptor for us */
   2266 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2267 		return (error);
   2268 
   2269 	error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
   2270 	FILE_UNUSE(fp, p);
   2271 	return (error);
   2272 }
   2273 
   2274 /*
   2275  * Change mode of a file given path name; this version does not follow links.
   2276  */
   2277 /* ARGSUSED */
   2278 int
   2279 sys_lchmod(l, v, retval)
   2280 	struct lwp *l;
   2281 	void *v;
   2282 	register_t *retval;
   2283 {
   2284 	struct sys_lchmod_args /* {
   2285 		syscallarg(const char *) path;
   2286 		syscallarg(int) mode;
   2287 	} */ *uap = v;
   2288 	struct proc *p = l->l_proc;
   2289 	int error;
   2290 	struct nameidata nd;
   2291 
   2292 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2293 	if ((error = namei(&nd)) != 0)
   2294 		return (error);
   2295 
   2296 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2297 
   2298 	vrele(nd.ni_vp);
   2299 	return (error);
   2300 }
   2301 
   2302 /*
   2303  * Common routine to set mode given a vnode.
   2304  */
   2305 static int
   2306 change_mode(vp, mode, p)
   2307 	struct vnode *vp;
   2308 	int mode;
   2309 	struct proc *p;
   2310 {
   2311 	struct vattr vattr;
   2312 	int error;
   2313 
   2314 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2315 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2316 	VATTR_NULL(&vattr);
   2317 	vattr.va_mode = mode & ALLPERMS;
   2318 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2319 	VOP_UNLOCK(vp, 0);
   2320 	return (error);
   2321 }
   2322 
   2323 /*
   2324  * Set ownership given a path name; this version follows links.
   2325  */
   2326 /* ARGSUSED */
   2327 int
   2328 sys_chown(l, v, retval)
   2329 	struct lwp *l;
   2330 	void *v;
   2331 	register_t *retval;
   2332 {
   2333 	struct sys_chown_args /* {
   2334 		syscallarg(const char *) path;
   2335 		syscallarg(uid_t) uid;
   2336 		syscallarg(gid_t) gid;
   2337 	} */ *uap = v;
   2338 	struct proc *p = l->l_proc;
   2339 	int error;
   2340 	struct nameidata nd;
   2341 
   2342 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2343 	if ((error = namei(&nd)) != 0)
   2344 		return (error);
   2345 
   2346 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2347 
   2348 	vrele(nd.ni_vp);
   2349 	return (error);
   2350 }
   2351 
   2352 /*
   2353  * Set ownership given a path name; this version follows links.
   2354  * Provides POSIX semantics.
   2355  */
   2356 /* ARGSUSED */
   2357 int
   2358 sys___posix_chown(l, v, retval)
   2359 	struct lwp *l;
   2360 	void *v;
   2361 	register_t *retval;
   2362 {
   2363 	struct sys_chown_args /* {
   2364 		syscallarg(const char *) path;
   2365 		syscallarg(uid_t) uid;
   2366 		syscallarg(gid_t) gid;
   2367 	} */ *uap = v;
   2368 	struct proc *p = l->l_proc;
   2369 	int error;
   2370 	struct nameidata nd;
   2371 
   2372 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2373 	if ((error = namei(&nd)) != 0)
   2374 		return (error);
   2375 
   2376 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2377 
   2378 	vrele(nd.ni_vp);
   2379 	return (error);
   2380 }
   2381 
   2382 /*
   2383  * Set ownership given a file descriptor.
   2384  */
   2385 /* ARGSUSED */
   2386 int
   2387 sys_fchown(l, v, retval)
   2388 	struct lwp *l;
   2389 	void *v;
   2390 	register_t *retval;
   2391 {
   2392 	struct sys_fchown_args /* {
   2393 		syscallarg(int) fd;
   2394 		syscallarg(uid_t) uid;
   2395 		syscallarg(gid_t) gid;
   2396 	} */ *uap = v;
   2397 	struct proc *p = l->l_proc;
   2398 	int error;
   2399 	struct file *fp;
   2400 
   2401 	/* getvnode() will use the descriptor for us */
   2402 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2403 		return (error);
   2404 
   2405 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2406 	    SCARG(uap, gid), p, 0);
   2407 	FILE_UNUSE(fp, p);
   2408 	return (error);
   2409 }
   2410 
   2411 /*
   2412  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   2413  */
   2414 /* ARGSUSED */
   2415 int
   2416 sys___posix_fchown(l, v, retval)
   2417 	struct lwp *l;
   2418 	void *v;
   2419 	register_t *retval;
   2420 {
   2421 	struct sys_fchown_args /* {
   2422 		syscallarg(int) fd;
   2423 		syscallarg(uid_t) uid;
   2424 		syscallarg(gid_t) gid;
   2425 	} */ *uap = v;
   2426 	struct proc *p = l->l_proc;
   2427 	int error;
   2428 	struct file *fp;
   2429 
   2430 	/* getvnode() will use the descriptor for us */
   2431 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2432 		return (error);
   2433 
   2434 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2435 	    SCARG(uap, gid), p, 1);
   2436 	FILE_UNUSE(fp, p);
   2437 	return (error);
   2438 }
   2439 
   2440 /*
   2441  * Set ownership given a path name; this version does not follow links.
   2442  */
   2443 /* ARGSUSED */
   2444 int
   2445 sys_lchown(l, v, retval)
   2446 	struct lwp *l;
   2447 	void *v;
   2448 	register_t *retval;
   2449 {
   2450 	struct sys_lchown_args /* {
   2451 		syscallarg(const char *) path;
   2452 		syscallarg(uid_t) uid;
   2453 		syscallarg(gid_t) gid;
   2454 	} */ *uap = v;
   2455 	struct proc *p = l->l_proc;
   2456 	int error;
   2457 	struct nameidata nd;
   2458 
   2459 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2460 	if ((error = namei(&nd)) != 0)
   2461 		return (error);
   2462 
   2463 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2464 
   2465 	vrele(nd.ni_vp);
   2466 	return (error);
   2467 }
   2468 
   2469 /*
   2470  * Set ownership given a path name; this version does not follow links.
   2471  * Provides POSIX/XPG semantics.
   2472  */
   2473 /* ARGSUSED */
   2474 int
   2475 sys___posix_lchown(l, v, retval)
   2476 	struct lwp *l;
   2477 	void *v;
   2478 	register_t *retval;
   2479 {
   2480 	struct sys_lchown_args /* {
   2481 		syscallarg(const char *) path;
   2482 		syscallarg(uid_t) uid;
   2483 		syscallarg(gid_t) gid;
   2484 	} */ *uap = v;
   2485 	struct proc *p = l->l_proc;
   2486 	int error;
   2487 	struct nameidata nd;
   2488 
   2489 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2490 	if ((error = namei(&nd)) != 0)
   2491 		return (error);
   2492 
   2493 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2494 
   2495 	vrele(nd.ni_vp);
   2496 	return (error);
   2497 }
   2498 
   2499 /*
   2500  * Common routine to set ownership given a vnode.
   2501  */
   2502 static int
   2503 change_owner(vp, uid, gid, p, posix_semantics)
   2504 	struct vnode *vp;
   2505 	uid_t uid;
   2506 	gid_t gid;
   2507 	struct proc *p;
   2508 	int posix_semantics;
   2509 {
   2510 	struct vattr vattr;
   2511 	mode_t newmode;
   2512 	int error;
   2513 
   2514 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2515 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2516 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2517 		goto out;
   2518 
   2519 #define CHANGED(x) ((int)(x) != -1)
   2520 	newmode = vattr.va_mode;
   2521 	if (posix_semantics) {
   2522 		/*
   2523 		 * POSIX/XPG semantics: if the caller is not the super-user,
   2524 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   2525 		 * the XPG consider the behaviour for calls by the super-user
   2526 		 * implementation-defined; we leave the set-user-id and set-
   2527 		 * group-id settings intact in that case.
   2528 		 */
   2529 		if (suser(p->p_ucred, NULL) != 0)
   2530 			newmode &= ~(S_ISUID | S_ISGID);
   2531 	} else {
   2532 		/*
   2533 		 * NetBSD semantics: when changing owner and/or group,
   2534 		 * clear the respective bit(s).
   2535 		 */
   2536 		if (CHANGED(uid))
   2537 			newmode &= ~S_ISUID;
   2538 		if (CHANGED(gid))
   2539 			newmode &= ~S_ISGID;
   2540 	}
   2541 	/* Update va_mode iff altered. */
   2542 	if (vattr.va_mode == newmode)
   2543 		newmode = VNOVAL;
   2544 
   2545 	VATTR_NULL(&vattr);
   2546 	vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
   2547 	vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
   2548 	vattr.va_mode = newmode;
   2549 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2550 #undef CHANGED
   2551 
   2552 out:
   2553 	VOP_UNLOCK(vp, 0);
   2554 	return (error);
   2555 }
   2556 
   2557 /*
   2558  * Set the access and modification times given a path name; this
   2559  * version follows links.
   2560  */
   2561 /* ARGSUSED */
   2562 int
   2563 sys_utimes(l, v, retval)
   2564 	struct lwp *l;
   2565 	void *v;
   2566 	register_t *retval;
   2567 {
   2568 	struct sys_utimes_args /* {
   2569 		syscallarg(const char *) path;
   2570 		syscallarg(const struct timeval *) tptr;
   2571 	} */ *uap = v;
   2572 	struct proc *p = l->l_proc;
   2573 	int error;
   2574 	struct nameidata nd;
   2575 
   2576 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2577 	if ((error = namei(&nd)) != 0)
   2578 		return (error);
   2579 
   2580 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2581 
   2582 	vrele(nd.ni_vp);
   2583 	return (error);
   2584 }
   2585 
   2586 /*
   2587  * Set the access and modification times given a file descriptor.
   2588  */
   2589 /* ARGSUSED */
   2590 int
   2591 sys_futimes(l, v, retval)
   2592 	struct lwp *l;
   2593 	void *v;
   2594 	register_t *retval;
   2595 {
   2596 	struct sys_futimes_args /* {
   2597 		syscallarg(int) fd;
   2598 		syscallarg(const struct timeval *) tptr;
   2599 	} */ *uap = v;
   2600 	struct proc *p = l->l_proc;
   2601 	int error;
   2602 	struct file *fp;
   2603 
   2604 	/* getvnode() will use the descriptor for us */
   2605 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2606 		return (error);
   2607 
   2608 	error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
   2609 	FILE_UNUSE(fp, p);
   2610 	return (error);
   2611 }
   2612 
   2613 /*
   2614  * Set the access and modification times given a path name; this
   2615  * version does not follow links.
   2616  */
   2617 /* ARGSUSED */
   2618 int
   2619 sys_lutimes(l, v, retval)
   2620 	struct lwp *l;
   2621 	void *v;
   2622 	register_t *retval;
   2623 {
   2624 	struct sys_lutimes_args /* {
   2625 		syscallarg(const char *) path;
   2626 		syscallarg(const struct timeval *) tptr;
   2627 	} */ *uap = v;
   2628 	struct proc *p = l->l_proc;
   2629 	int error;
   2630 	struct nameidata nd;
   2631 
   2632 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2633 	if ((error = namei(&nd)) != 0)
   2634 		return (error);
   2635 
   2636 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2637 
   2638 	vrele(nd.ni_vp);
   2639 	return (error);
   2640 }
   2641 
   2642 /*
   2643  * Common routine to set access and modification times given a vnode.
   2644  */
   2645 static int
   2646 change_utimes(vp, tptr, p)
   2647 	struct vnode *vp;
   2648 	const struct timeval *tptr;
   2649 	struct proc *p;
   2650 {
   2651 	struct timeval tv[2];
   2652 	struct vattr vattr;
   2653 	int error;
   2654 
   2655 	VATTR_NULL(&vattr);
   2656 	if (tptr == NULL) {
   2657 		microtime(&tv[0]);
   2658 		tv[1] = tv[0];
   2659 		vattr.va_vaflags |= VA_UTIMES_NULL;
   2660 	} else {
   2661 		error = copyin(tptr, tv, sizeof(tv));
   2662 		if (error)
   2663 			return (error);
   2664 	}
   2665 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2666 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2667 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   2668 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   2669 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   2670 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   2671 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2672 	VOP_UNLOCK(vp, 0);
   2673 	return (error);
   2674 }
   2675 
   2676 /*
   2677  * Truncate a file given its path name.
   2678  */
   2679 /* ARGSUSED */
   2680 int
   2681 sys_truncate(l, v, retval)
   2682 	struct lwp *l;
   2683 	void *v;
   2684 	register_t *retval;
   2685 {
   2686 	struct sys_truncate_args /* {
   2687 		syscallarg(const char *) path;
   2688 		syscallarg(int) pad;
   2689 		syscallarg(off_t) length;
   2690 	} */ *uap = v;
   2691 	struct proc *p = l->l_proc;
   2692 	struct vnode *vp;
   2693 	struct vattr vattr;
   2694 	int error;
   2695 	struct nameidata nd;
   2696 
   2697 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2698 	if ((error = namei(&nd)) != 0)
   2699 		return (error);
   2700 	vp = nd.ni_vp;
   2701 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2702 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2703 	if (vp->v_type == VDIR)
   2704 		error = EISDIR;
   2705 	else if ((error = vn_writechk(vp)) == 0 &&
   2706 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   2707 		VATTR_NULL(&vattr);
   2708 		vattr.va_size = SCARG(uap, length);
   2709 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2710 	}
   2711 	vput(vp);
   2712 	return (error);
   2713 }
   2714 
   2715 /*
   2716  * Truncate a file given a file descriptor.
   2717  */
   2718 /* ARGSUSED */
   2719 int
   2720 sys_ftruncate(l, v, retval)
   2721 	struct lwp *l;
   2722 	void *v;
   2723 	register_t *retval;
   2724 {
   2725 	struct sys_ftruncate_args /* {
   2726 		syscallarg(int) fd;
   2727 		syscallarg(int) pad;
   2728 		syscallarg(off_t) length;
   2729 	} */ *uap = v;
   2730 	struct proc *p = l->l_proc;
   2731 	struct vattr vattr;
   2732 	struct vnode *vp;
   2733 	struct file *fp;
   2734 	int error;
   2735 
   2736 	/* getvnode() will use the descriptor for us */
   2737 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2738 		return (error);
   2739 	if ((fp->f_flag & FWRITE) == 0) {
   2740 		error = EINVAL;
   2741 		goto out;
   2742 	}
   2743 	vp = (struct vnode *)fp->f_data;
   2744 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2745 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2746 	if (vp->v_type == VDIR)
   2747 		error = EISDIR;
   2748 	else if ((error = vn_writechk(vp)) == 0) {
   2749 		VATTR_NULL(&vattr);
   2750 		vattr.va_size = SCARG(uap, length);
   2751 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   2752 	}
   2753 	VOP_UNLOCK(vp, 0);
   2754  out:
   2755 	FILE_UNUSE(fp, p);
   2756 	return (error);
   2757 }
   2758 
   2759 /*
   2760  * Sync an open file.
   2761  */
   2762 /* ARGSUSED */
   2763 int
   2764 sys_fsync(l, v, retval)
   2765 	struct lwp *l;
   2766 	void *v;
   2767 	register_t *retval;
   2768 {
   2769 	struct sys_fsync_args /* {
   2770 		syscallarg(int) fd;
   2771 	} */ *uap = v;
   2772 	struct proc *p = l->l_proc;
   2773 	struct vnode *vp;
   2774 	struct file *fp;
   2775 	int error;
   2776 
   2777 	/* getvnode() will use the descriptor for us */
   2778 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2779 		return (error);
   2780 	vp = (struct vnode *)fp->f_data;
   2781 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2782 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, p);
   2783 	if (error == 0 && bioops.io_fsync != NULL &&
   2784 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   2785 		(*bioops.io_fsync)(vp);
   2786 	VOP_UNLOCK(vp, 0);
   2787 	FILE_UNUSE(fp, p);
   2788 	return (error);
   2789 }
   2790 
   2791 /*
   2792  * Sync the data of an open file.
   2793  */
   2794 /* ARGSUSED */
   2795 int
   2796 sys_fdatasync(l, v, retval)
   2797 	struct lwp *l;
   2798 	void *v;
   2799 	register_t *retval;
   2800 {
   2801 	struct sys_fdatasync_args /* {
   2802 		syscallarg(int) fd;
   2803 	} */ *uap = v;
   2804 	struct proc *p = l->l_proc;
   2805 	struct vnode *vp;
   2806 	struct file *fp;
   2807 	int error;
   2808 
   2809 	/* getvnode() will use the descriptor for us */
   2810 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2811 		return (error);
   2812 	vp = (struct vnode *)fp->f_data;
   2813 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2814 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, p);
   2815 	VOP_UNLOCK(vp, 0);
   2816 	FILE_UNUSE(fp, p);
   2817 	return (error);
   2818 }
   2819 
   2820 /*
   2821  * Rename files, (standard) BSD semantics frontend.
   2822  */
   2823 /* ARGSUSED */
   2824 int
   2825 sys_rename(l, v, retval)
   2826 	struct lwp *l;
   2827 	void *v;
   2828 	register_t *retval;
   2829 {
   2830 	struct sys_rename_args /* {
   2831 		syscallarg(const char *) from;
   2832 		syscallarg(const char *) to;
   2833 	} */ *uap = v;
   2834 	struct proc *p = l->l_proc;
   2835 
   2836 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
   2837 }
   2838 
   2839 /*
   2840  * Rename files, POSIX semantics frontend.
   2841  */
   2842 /* ARGSUSED */
   2843 int
   2844 sys___posix_rename(l, v, retval)
   2845 	struct lwp *l;
   2846 	void *v;
   2847 	register_t *retval;
   2848 {
   2849 	struct sys___posix_rename_args /* {
   2850 		syscallarg(const char *) from;
   2851 		syscallarg(const char *) to;
   2852 	} */ *uap = v;
   2853 	struct proc *p = l->l_proc;
   2854 
   2855 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
   2856 }
   2857 
   2858 /*
   2859  * Rename files.  Source and destination must either both be directories,
   2860  * or both not be directories.  If target is a directory, it must be empty.
   2861  * If `from' and `to' refer to the same object, the value of the `retain'
   2862  * argument is used to determine whether `from' will be
   2863  *
   2864  * (retain == 0)	deleted unless `from' and `to' refer to the same
   2865  *			object in the file system's name space (BSD).
   2866  * (retain == 1)	always retained (POSIX).
   2867  */
   2868 static int
   2869 rename_files(from, to, p, retain)
   2870 	const char *from, *to;
   2871 	struct proc *p;
   2872 	int retain;
   2873 {
   2874 	struct vnode *tvp, *fvp, *tdvp;
   2875 	struct nameidata fromnd, tond;
   2876 	int error;
   2877 
   2878 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   2879 	    from, p);
   2880 	if ((error = namei(&fromnd)) != 0)
   2881 		return (error);
   2882 	fvp = fromnd.ni_vp;
   2883 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
   2884 	    UIO_USERSPACE, to, p);
   2885 	if ((error = namei(&tond)) != 0) {
   2886 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2887 		vrele(fromnd.ni_dvp);
   2888 		vrele(fvp);
   2889 		goto out1;
   2890 	}
   2891 	tdvp = tond.ni_dvp;
   2892 	tvp = tond.ni_vp;
   2893 
   2894 	if (tvp != NULL) {
   2895 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   2896 			error = ENOTDIR;
   2897 			goto out;
   2898 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   2899 			error = EISDIR;
   2900 			goto out;
   2901 		}
   2902 	}
   2903 
   2904 	if (fvp == tdvp)
   2905 		error = EINVAL;
   2906 
   2907 	/*
   2908 	 * Source and destination refer to the same object.
   2909 	 */
   2910 	if (fvp == tvp) {
   2911 		if (retain)
   2912 			error = -1;
   2913 		else if (fromnd.ni_dvp == tdvp &&
   2914 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   2915 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
   2916 		          tond.ni_cnd.cn_nameptr,
   2917 		          fromnd.ni_cnd.cn_namelen))
   2918 		error = -1;
   2919 	}
   2920 
   2921 out:
   2922 	if (!error) {
   2923 		VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
   2924 		if (fromnd.ni_dvp != tdvp)
   2925 			VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2926 		if (tvp) {
   2927 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   2928 		}
   2929 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   2930 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   2931 	} else {
   2932 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   2933 		if (tdvp == tvp)
   2934 			vrele(tdvp);
   2935 		else
   2936 			vput(tdvp);
   2937 		if (tvp)
   2938 			vput(tvp);
   2939 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2940 		vrele(fromnd.ni_dvp);
   2941 		vrele(fvp);
   2942 	}
   2943 	vrele(tond.ni_startdir);
   2944 	PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
   2945 out1:
   2946 	if (fromnd.ni_startdir)
   2947 		vrele(fromnd.ni_startdir);
   2948 	PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
   2949 	return (error == -1 ? 0 : error);
   2950 }
   2951 
   2952 /*
   2953  * Make a directory file.
   2954  */
   2955 /* ARGSUSED */
   2956 int
   2957 sys_mkdir(l, v, retval)
   2958 	struct lwp *l;
   2959 	void *v;
   2960 	register_t *retval;
   2961 {
   2962 	struct sys_mkdir_args /* {
   2963 		syscallarg(const char *) path;
   2964 		syscallarg(int) mode;
   2965 	} */ *uap = v;
   2966 	struct proc *p = l->l_proc;
   2967 	struct vnode *vp;
   2968 	struct vattr vattr;
   2969 	int error;
   2970 	struct nameidata nd;
   2971 
   2972 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   2973 	if ((error = namei(&nd)) != 0)
   2974 		return (error);
   2975 	vp = nd.ni_vp;
   2976 	if (vp != NULL) {
   2977 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2978 		if (nd.ni_dvp == vp)
   2979 			vrele(nd.ni_dvp);
   2980 		else
   2981 			vput(nd.ni_dvp);
   2982 		vrele(vp);
   2983 		return (EEXIST);
   2984 	}
   2985 	VATTR_NULL(&vattr);
   2986 	vattr.va_type = VDIR;
   2987 	vattr.va_mode =
   2988 	    (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   2989 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2990 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   2991 	if (!error)
   2992 		vput(nd.ni_vp);
   2993 	return (error);
   2994 }
   2995 
   2996 /*
   2997  * Remove a directory file.
   2998  */
   2999 /* ARGSUSED */
   3000 int
   3001 sys_rmdir(l, v, retval)
   3002 	struct lwp *l;
   3003 	void *v;
   3004 	register_t *retval;
   3005 {
   3006 	struct sys_rmdir_args /* {
   3007 		syscallarg(const char *) path;
   3008 	} */ *uap = v;
   3009 	struct proc *p = l->l_proc;
   3010 	struct vnode *vp;
   3011 	int error;
   3012 	struct nameidata nd;
   3013 
   3014 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   3015 	    SCARG(uap, path), p);
   3016 	if ((error = namei(&nd)) != 0)
   3017 		return (error);
   3018 	vp = nd.ni_vp;
   3019 	if (vp->v_type != VDIR) {
   3020 		error = ENOTDIR;
   3021 		goto out;
   3022 	}
   3023 	/*
   3024 	 * No rmdir "." please.
   3025 	 */
   3026 	if (nd.ni_dvp == vp) {
   3027 		error = EINVAL;
   3028 		goto out;
   3029 	}
   3030 	/*
   3031 	 * The root of a mounted filesystem cannot be deleted.
   3032 	 */
   3033 	if (vp->v_flag & VROOT)
   3034 		error = EBUSY;
   3035 out:
   3036 	if (!error) {
   3037 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   3038 		VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   3039 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   3040 	} else {
   3041 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3042 		if (nd.ni_dvp == vp)
   3043 			vrele(nd.ni_dvp);
   3044 		else
   3045 			vput(nd.ni_dvp);
   3046 		vput(vp);
   3047 	}
   3048 	return (error);
   3049 }
   3050 
   3051 /*
   3052  * Read a block of directory entries in a file system independent format.
   3053  */
   3054 int
   3055 sys_getdents(l, v, retval)
   3056 	struct lwp *l;
   3057 	void *v;
   3058 	register_t *retval;
   3059 {
   3060 	struct sys_getdents_args /* {
   3061 		syscallarg(int) fd;
   3062 		syscallarg(char *) buf;
   3063 		syscallarg(size_t) count;
   3064 	} */ *uap = v;
   3065 	struct proc *p = l->l_proc;
   3066 	struct file *fp;
   3067 	int error, done;
   3068 
   3069 	/* getvnode() will use the descriptor for us */
   3070 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3071 		return (error);
   3072 	if ((fp->f_flag & FREAD) == 0) {
   3073 		error = EBADF;
   3074 		goto out;
   3075 	}
   3076 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   3077 			SCARG(uap, count), &done, p, 0, 0);
   3078 	*retval = done;
   3079  out:
   3080 	FILE_UNUSE(fp, p);
   3081 	return (error);
   3082 }
   3083 
   3084 /*
   3085  * Set the mode mask for creation of filesystem nodes.
   3086  */
   3087 int
   3088 sys_umask(l, v, retval)
   3089 	struct lwp *l;
   3090 	void *v;
   3091 	register_t *retval;
   3092 {
   3093 	struct sys_umask_args /* {
   3094 		syscallarg(mode_t) newmask;
   3095 	} */ *uap = v;
   3096 	struct proc *p = l->l_proc;
   3097 	struct cwdinfo *cwdi;
   3098 
   3099 	cwdi = p->p_cwdi;
   3100 	*retval = cwdi->cwdi_cmask;
   3101 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
   3102 	return (0);
   3103 }
   3104 
   3105 /*
   3106  * Void all references to file by ripping underlying filesystem
   3107  * away from vnode.
   3108  */
   3109 /* ARGSUSED */
   3110 int
   3111 sys_revoke(l, v, retval)
   3112 	struct lwp *l;
   3113 	void *v;
   3114 	register_t *retval;
   3115 {
   3116 	struct sys_revoke_args /* {
   3117 		syscallarg(const char *) path;
   3118 	} */ *uap = v;
   3119 	struct proc *p = l->l_proc;
   3120 	struct vnode *vp;
   3121 	struct vattr vattr;
   3122 	int error;
   3123 	struct nameidata nd;
   3124 
   3125 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   3126 	if ((error = namei(&nd)) != 0)
   3127 		return (error);
   3128 	vp = nd.ni_vp;
   3129 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   3130 		goto out;
   3131 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   3132 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
   3133 		goto out;
   3134 	if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
   3135 		VOP_REVOKE(vp, REVOKEALL);
   3136 out:
   3137 	vrele(vp);
   3138 	return (error);
   3139 }
   3140 
   3141 /*
   3142  * Convert a user file descriptor to a kernel file entry.
   3143  */
   3144 int
   3145 getvnode(fdp, fd, fpp)
   3146 	struct filedesc *fdp;
   3147 	int fd;
   3148 	struct file **fpp;
   3149 {
   3150 	struct vnode *vp;
   3151 	struct file *fp;
   3152 
   3153 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   3154 		return (EBADF);
   3155 
   3156 	FILE_USE(fp);
   3157 
   3158 	if (fp->f_type != DTYPE_VNODE) {
   3159 		FILE_UNUSE(fp, NULL);
   3160 		return (EINVAL);
   3161 	}
   3162 
   3163 	vp = (struct vnode *)fp->f_data;
   3164 	if (vp->v_type == VBAD) {
   3165 		FILE_UNUSE(fp, NULL);
   3166 		return (EBADF);
   3167 	}
   3168 
   3169 	*fpp = fp;
   3170 	return (0);
   3171 }
   3172