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