Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.164.2.6
      1 /*	$NetBSD: vfs_syscalls.c,v 1.164.2.6 2001/10/22 20:41:52 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_IGNORE | MNT_NOATIME | MNT_NODEVMTIME |
    309 	    MNT_SYMPERM | 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 = crget();
   1928 	struct vnode *vp;
   1929 	int error, flags;
   1930 	struct nameidata nd;
   1931 
   1932 	(void)memcpy(cred, p->p_ucred, sizeof(*cred));
   1933 	cred->cr_ref = 1;
   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 	/* Override default credentials */
   1939 	nd.ni_cnd.cn_cred = cred;
   1940 	if ((error = namei(&nd)) != 0)
   1941 		goto out;
   1942 	vp = nd.ni_vp;
   1943 
   1944 	/* Flags == 0 means only check for existence. */
   1945 	if (SCARG(uap, flags)) {
   1946 		flags = 0;
   1947 		if (SCARG(uap, flags) & R_OK)
   1948 			flags |= VREAD;
   1949 		if (SCARG(uap, flags) & W_OK)
   1950 			flags |= VWRITE;
   1951 		if (SCARG(uap, flags) & X_OK)
   1952 			flags |= VEXEC;
   1953 
   1954 		error = VOP_ACCESS(vp, flags, cred, p);
   1955 		if (!error && (flags & VWRITE))
   1956 			error = vn_writechk(vp);
   1957 	}
   1958 	vput(vp);
   1959 out:
   1960 	crfree(cred);
   1961 	return (error);
   1962 }
   1963 
   1964 /*
   1965  * Get file status; this version follows links.
   1966  */
   1967 /* ARGSUSED */
   1968 int
   1969 sys___stat13(l, v, retval)
   1970 	struct lwp *l;
   1971 	void *v;
   1972 	register_t *retval;
   1973 {
   1974 	struct sys___stat13_args /* {
   1975 		syscallarg(const char *) path;
   1976 		syscallarg(struct stat *) ub;
   1977 	} */ *uap = v;
   1978 	struct proc *p = l->l_proc;
   1979 	struct stat sb;
   1980 	int error;
   1981 	struct nameidata nd;
   1982 
   1983 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1984 	    SCARG(uap, path), p);
   1985 	if ((error = namei(&nd)) != 0)
   1986 		return (error);
   1987 	error = vn_stat(nd.ni_vp, &sb, p);
   1988 	vput(nd.ni_vp);
   1989 	if (error)
   1990 		return (error);
   1991 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   1992 	return (error);
   1993 }
   1994 
   1995 /*
   1996  * Get file status; this version does not follow links.
   1997  */
   1998 /* ARGSUSED */
   1999 int
   2000 sys___lstat13(l, v, retval)
   2001 	struct lwp *l;
   2002 	void *v;
   2003 	register_t *retval;
   2004 {
   2005 	struct sys___lstat13_args /* {
   2006 		syscallarg(const char *) path;
   2007 		syscallarg(struct stat *) ub;
   2008 	} */ *uap = v;
   2009 	struct proc *p = l->l_proc;
   2010 	struct stat sb;
   2011 	int error;
   2012 	struct nameidata nd;
   2013 
   2014 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2015 	    SCARG(uap, path), p);
   2016 	if ((error = namei(&nd)) != 0)
   2017 		return (error);
   2018 	error = vn_stat(nd.ni_vp, &sb, p);
   2019 	vput(nd.ni_vp);
   2020 	if (error)
   2021 		return (error);
   2022 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2023 	return (error);
   2024 }
   2025 
   2026 /*
   2027  * Get configurable pathname variables.
   2028  */
   2029 /* ARGSUSED */
   2030 int
   2031 sys_pathconf(l, v, retval)
   2032 	struct lwp *l;
   2033 	void *v;
   2034 	register_t *retval;
   2035 {
   2036 	struct sys_pathconf_args /* {
   2037 		syscallarg(const char *) path;
   2038 		syscallarg(int) name;
   2039 	} */ *uap = v;
   2040 	struct proc *p = l->l_proc;
   2041 	int error;
   2042 	struct nameidata nd;
   2043 
   2044 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   2045 	    SCARG(uap, path), p);
   2046 	if ((error = namei(&nd)) != 0)
   2047 		return (error);
   2048 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   2049 	vput(nd.ni_vp);
   2050 	return (error);
   2051 }
   2052 
   2053 /*
   2054  * Return target name of a symbolic link.
   2055  */
   2056 /* ARGSUSED */
   2057 int
   2058 sys_readlink(l, v, retval)
   2059 	struct lwp *l;
   2060 	void *v;
   2061 	register_t *retval;
   2062 {
   2063 	struct sys_readlink_args /* {
   2064 		syscallarg(const char *) path;
   2065 		syscallarg(char *) buf;
   2066 		syscallarg(size_t) count;
   2067 	} */ *uap = v;
   2068 	struct proc *p = l->l_proc;
   2069 	struct vnode *vp;
   2070 	struct iovec aiov;
   2071 	struct uio auio;
   2072 	int error;
   2073 	struct nameidata nd;
   2074 
   2075 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2076 	    SCARG(uap, path), p);
   2077 	if ((error = namei(&nd)) != 0)
   2078 		return (error);
   2079 	vp = nd.ni_vp;
   2080 	if (vp->v_type != VLNK)
   2081 		error = EINVAL;
   2082 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   2083 	    (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
   2084 		aiov.iov_base = SCARG(uap, buf);
   2085 		aiov.iov_len = SCARG(uap, count);
   2086 		auio.uio_iov = &aiov;
   2087 		auio.uio_iovcnt = 1;
   2088 		auio.uio_offset = 0;
   2089 		auio.uio_rw = UIO_READ;
   2090 		auio.uio_segflg = UIO_USERSPACE;
   2091 		auio.uio_procp = p;
   2092 		auio.uio_resid = SCARG(uap, count);
   2093 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   2094 	}
   2095 	vput(vp);
   2096 	*retval = SCARG(uap, count) - auio.uio_resid;
   2097 	return (error);
   2098 }
   2099 
   2100 /*
   2101  * Change flags of a file given a path name.
   2102  */
   2103 /* ARGSUSED */
   2104 int
   2105 sys_chflags(l, v, retval)
   2106 	struct lwp *l;
   2107 	void *v;
   2108 	register_t *retval;
   2109 {
   2110 	struct sys_chflags_args /* {
   2111 		syscallarg(const char *) path;
   2112 		syscallarg(u_long) flags;
   2113 	} */ *uap = v;
   2114 	struct proc *p = l->l_proc;
   2115 	struct vnode *vp;
   2116 	int error;
   2117 	struct nameidata nd;
   2118 
   2119 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2120 	if ((error = namei(&nd)) != 0)
   2121 		return (error);
   2122 	vp = nd.ni_vp;
   2123 	error = change_flags(vp, SCARG(uap, flags), p);
   2124 	vput(vp);
   2125 	return (error);
   2126 }
   2127 
   2128 /*
   2129  * Change flags of a file given a file descriptor.
   2130  */
   2131 /* ARGSUSED */
   2132 int
   2133 sys_fchflags(l, v, retval)
   2134 	struct lwp *l;
   2135 	void *v;
   2136 	register_t *retval;
   2137 {
   2138 	struct sys_fchflags_args /* {
   2139 		syscallarg(int) fd;
   2140 		syscallarg(u_long) flags;
   2141 	} */ *uap = v;
   2142 	struct proc *p = l->l_proc;
   2143 	struct vnode *vp;
   2144 	struct file *fp;
   2145 	int error;
   2146 
   2147 	/* getvnode() will use the descriptor for us */
   2148 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2149 		return (error);
   2150 	vp = (struct vnode *)fp->f_data;
   2151 	error = change_flags(vp, SCARG(uap, flags), p);
   2152 	VOP_UNLOCK(vp, 0);
   2153 	FILE_UNUSE(fp, p);
   2154 	return (error);
   2155 }
   2156 
   2157 /*
   2158  * Change flags of a file given a path name; this version does
   2159  * not follow links.
   2160  */
   2161 int
   2162 sys_lchflags(l, v, retval)
   2163 	struct lwp *l;
   2164 	void *v;
   2165 	register_t *retval;
   2166 {
   2167 	struct sys_lchflags_args /* {
   2168 		syscallarg(const char *) path;
   2169 		syscallarg(u_long) flags;
   2170 	} */ *uap = v;
   2171 	struct proc *p = l->l_proc;
   2172 	struct vnode *vp;
   2173 	int error;
   2174 	struct nameidata nd;
   2175 
   2176 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2177 	if ((error = namei(&nd)) != 0)
   2178 		return (error);
   2179 	vp = nd.ni_vp;
   2180 	error = change_flags(vp, SCARG(uap, flags), p);
   2181 	vput(vp);
   2182 	return (error);
   2183 }
   2184 
   2185 /*
   2186  * Common routine to change flags of a file.
   2187  */
   2188 int
   2189 change_flags(vp, flags, p)
   2190 	struct vnode *vp;
   2191 	u_long flags;
   2192 	struct proc *p;
   2193 {
   2194 	struct vattr vattr;
   2195 	int error;
   2196 
   2197 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2198 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2199 	/*
   2200 	 * Non-superusers cannot change the flags on devices, even if they
   2201 	 * own them.
   2202 	 */
   2203 	if (suser(p->p_ucred, &p->p_acflag) != 0) {
   2204 		if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2205 			goto out;
   2206 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2207 			error = EINVAL;
   2208 			goto out;
   2209 		}
   2210 	}
   2211 	VATTR_NULL(&vattr);
   2212 	vattr.va_flags = flags;
   2213 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2214 out:
   2215 	return (error);
   2216 }
   2217 
   2218 /*
   2219  * Change mode of a file given path name; this version follows links.
   2220  */
   2221 /* ARGSUSED */
   2222 int
   2223 sys_chmod(l, v, retval)
   2224 	struct lwp *l;
   2225 	void *v;
   2226 	register_t *retval;
   2227 {
   2228 	struct sys_chmod_args /* {
   2229 		syscallarg(const char *) path;
   2230 		syscallarg(int) mode;
   2231 	} */ *uap = v;
   2232 	struct proc *p = l->l_proc;
   2233 	int error;
   2234 	struct nameidata nd;
   2235 
   2236 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2237 	if ((error = namei(&nd)) != 0)
   2238 		return (error);
   2239 
   2240 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2241 
   2242 	vrele(nd.ni_vp);
   2243 	return (error);
   2244 }
   2245 
   2246 /*
   2247  * Change mode of a file given a file descriptor.
   2248  */
   2249 /* ARGSUSED */
   2250 int
   2251 sys_fchmod(l, v, retval)
   2252 	struct lwp *l;
   2253 	void *v;
   2254 	register_t *retval;
   2255 {
   2256 	struct sys_fchmod_args /* {
   2257 		syscallarg(int) fd;
   2258 		syscallarg(int) mode;
   2259 	} */ *uap = v;
   2260 	struct proc *p = l->l_proc;
   2261 	struct file *fp;
   2262 	int error;
   2263 
   2264 	/* getvnode() will use the descriptor for us */
   2265 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2266 		return (error);
   2267 
   2268 	error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
   2269 	FILE_UNUSE(fp, p);
   2270 	return (error);
   2271 }
   2272 
   2273 /*
   2274  * Change mode of a file given path name; this version does not follow links.
   2275  */
   2276 /* ARGSUSED */
   2277 int
   2278 sys_lchmod(l, v, retval)
   2279 	struct lwp *l;
   2280 	void *v;
   2281 	register_t *retval;
   2282 {
   2283 	struct sys_lchmod_args /* {
   2284 		syscallarg(const char *) path;
   2285 		syscallarg(int) mode;
   2286 	} */ *uap = v;
   2287 	struct proc *p = l->l_proc;
   2288 	int error;
   2289 	struct nameidata nd;
   2290 
   2291 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2292 	if ((error = namei(&nd)) != 0)
   2293 		return (error);
   2294 
   2295 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2296 
   2297 	vrele(nd.ni_vp);
   2298 	return (error);
   2299 }
   2300 
   2301 /*
   2302  * Common routine to set mode given a vnode.
   2303  */
   2304 static int
   2305 change_mode(vp, mode, p)
   2306 	struct vnode *vp;
   2307 	int mode;
   2308 	struct proc *p;
   2309 {
   2310 	struct vattr vattr;
   2311 	int error;
   2312 
   2313 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2314 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2315 	VATTR_NULL(&vattr);
   2316 	vattr.va_mode = mode & ALLPERMS;
   2317 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2318 	VOP_UNLOCK(vp, 0);
   2319 	return (error);
   2320 }
   2321 
   2322 /*
   2323  * Set ownership given a path name; this version follows links.
   2324  */
   2325 /* ARGSUSED */
   2326 int
   2327 sys_chown(l, v, retval)
   2328 	struct lwp *l;
   2329 	void *v;
   2330 	register_t *retval;
   2331 {
   2332 	struct sys_chown_args /* {
   2333 		syscallarg(const char *) path;
   2334 		syscallarg(uid_t) uid;
   2335 		syscallarg(gid_t) gid;
   2336 	} */ *uap = v;
   2337 	struct proc *p = l->l_proc;
   2338 	int error;
   2339 	struct nameidata nd;
   2340 
   2341 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2342 	if ((error = namei(&nd)) != 0)
   2343 		return (error);
   2344 
   2345 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2346 
   2347 	vrele(nd.ni_vp);
   2348 	return (error);
   2349 }
   2350 
   2351 /*
   2352  * Set ownership given a path name; this version follows links.
   2353  * Provides POSIX semantics.
   2354  */
   2355 /* ARGSUSED */
   2356 int
   2357 sys___posix_chown(l, v, retval)
   2358 	struct lwp *l;
   2359 	void *v;
   2360 	register_t *retval;
   2361 {
   2362 	struct sys_chown_args /* {
   2363 		syscallarg(const char *) path;
   2364 		syscallarg(uid_t) uid;
   2365 		syscallarg(gid_t) gid;
   2366 	} */ *uap = v;
   2367 	struct proc *p = l->l_proc;
   2368 	int error;
   2369 	struct nameidata nd;
   2370 
   2371 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2372 	if ((error = namei(&nd)) != 0)
   2373 		return (error);
   2374 
   2375 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2376 
   2377 	vrele(nd.ni_vp);
   2378 	return (error);
   2379 }
   2380 
   2381 /*
   2382  * Set ownership given a file descriptor.
   2383  */
   2384 /* ARGSUSED */
   2385 int
   2386 sys_fchown(l, v, retval)
   2387 	struct lwp *l;
   2388 	void *v;
   2389 	register_t *retval;
   2390 {
   2391 	struct sys_fchown_args /* {
   2392 		syscallarg(int) fd;
   2393 		syscallarg(uid_t) uid;
   2394 		syscallarg(gid_t) gid;
   2395 	} */ *uap = v;
   2396 	struct proc *p = l->l_proc;
   2397 	int error;
   2398 	struct file *fp;
   2399 
   2400 	/* getvnode() will use the descriptor for us */
   2401 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2402 		return (error);
   2403 
   2404 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2405 	    SCARG(uap, gid), p, 0);
   2406 	FILE_UNUSE(fp, p);
   2407 	return (error);
   2408 }
   2409 
   2410 /*
   2411  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   2412  */
   2413 /* ARGSUSED */
   2414 int
   2415 sys___posix_fchown(l, v, retval)
   2416 	struct lwp *l;
   2417 	void *v;
   2418 	register_t *retval;
   2419 {
   2420 	struct sys_fchown_args /* {
   2421 		syscallarg(int) fd;
   2422 		syscallarg(uid_t) uid;
   2423 		syscallarg(gid_t) gid;
   2424 	} */ *uap = v;
   2425 	struct proc *p = l->l_proc;
   2426 	int error;
   2427 	struct file *fp;
   2428 
   2429 	/* getvnode() will use the descriptor for us */
   2430 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2431 		return (error);
   2432 
   2433 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2434 	    SCARG(uap, gid), p, 1);
   2435 	FILE_UNUSE(fp, p);
   2436 	return (error);
   2437 }
   2438 
   2439 /*
   2440  * Set ownership given a path name; this version does not follow links.
   2441  */
   2442 /* ARGSUSED */
   2443 int
   2444 sys_lchown(l, v, retval)
   2445 	struct lwp *l;
   2446 	void *v;
   2447 	register_t *retval;
   2448 {
   2449 	struct sys_lchown_args /* {
   2450 		syscallarg(const char *) path;
   2451 		syscallarg(uid_t) uid;
   2452 		syscallarg(gid_t) gid;
   2453 	} */ *uap = v;
   2454 	struct proc *p = l->l_proc;
   2455 	int error;
   2456 	struct nameidata nd;
   2457 
   2458 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2459 	if ((error = namei(&nd)) != 0)
   2460 		return (error);
   2461 
   2462 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2463 
   2464 	vrele(nd.ni_vp);
   2465 	return (error);
   2466 }
   2467 
   2468 /*
   2469  * Set ownership given a path name; this version does not follow links.
   2470  * Provides POSIX/XPG semantics.
   2471  */
   2472 /* ARGSUSED */
   2473 int
   2474 sys___posix_lchown(l, v, retval)
   2475 	struct lwp *l;
   2476 	void *v;
   2477 	register_t *retval;
   2478 {
   2479 	struct sys_lchown_args /* {
   2480 		syscallarg(const char *) path;
   2481 		syscallarg(uid_t) uid;
   2482 		syscallarg(gid_t) gid;
   2483 	} */ *uap = v;
   2484 	struct proc *p = l->l_proc;
   2485 	int error;
   2486 	struct nameidata nd;
   2487 
   2488 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2489 	if ((error = namei(&nd)) != 0)
   2490 		return (error);
   2491 
   2492 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2493 
   2494 	vrele(nd.ni_vp);
   2495 	return (error);
   2496 }
   2497 
   2498 /*
   2499  * Common routine to set ownership given a vnode.
   2500  */
   2501 static int
   2502 change_owner(vp, uid, gid, p, posix_semantics)
   2503 	struct vnode *vp;
   2504 	uid_t uid;
   2505 	gid_t gid;
   2506 	struct proc *p;
   2507 	int posix_semantics;
   2508 {
   2509 	struct vattr vattr;
   2510 	mode_t newmode;
   2511 	int error;
   2512 
   2513 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2514 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2515 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2516 		goto out;
   2517 
   2518 #define CHANGED(x) ((x) != -1)
   2519 	newmode = vattr.va_mode;
   2520 	if (posix_semantics) {
   2521 		/*
   2522 		 * POSIX/XPG semantics: if the caller is not the super-user,
   2523 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   2524 		 * the XPG consider the behaviour for calls by the super-user
   2525 		 * implementation-defined; we leave the set-user-id and set-
   2526 		 * group-id settings intact in that case.
   2527 		 */
   2528 		if (suser(p->p_ucred, NULL) != 0)
   2529 			newmode &= ~(S_ISUID | S_ISGID);
   2530 	} else {
   2531 		/*
   2532 		 * NetBSD semantics: when changing owner and/or group,
   2533 		 * clear the respective bit(s).
   2534 		 */
   2535 		if (CHANGED(uid))
   2536 			newmode &= ~S_ISUID;
   2537 		if (CHANGED(gid))
   2538 			newmode &= ~S_ISGID;
   2539 	}
   2540 	/* Update va_mode iff altered. */
   2541 	if (vattr.va_mode == newmode)
   2542 		newmode = VNOVAL;
   2543 
   2544 	VATTR_NULL(&vattr);
   2545 	vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
   2546 	vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
   2547 	vattr.va_mode = newmode;
   2548 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2549 #undef CHANGED
   2550 
   2551 out:
   2552 	VOP_UNLOCK(vp, 0);
   2553 	return (error);
   2554 }
   2555 
   2556 /*
   2557  * Set the access and modification times given a path name; this
   2558  * version follows links.
   2559  */
   2560 /* ARGSUSED */
   2561 int
   2562 sys_utimes(l, v, retval)
   2563 	struct lwp *l;
   2564 	void *v;
   2565 	register_t *retval;
   2566 {
   2567 	struct sys_utimes_args /* {
   2568 		syscallarg(const char *) path;
   2569 		syscallarg(const struct timeval *) tptr;
   2570 	} */ *uap = v;
   2571 	struct proc *p = l->l_proc;
   2572 	int error;
   2573 	struct nameidata nd;
   2574 
   2575 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2576 	if ((error = namei(&nd)) != 0)
   2577 		return (error);
   2578 
   2579 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2580 
   2581 	vrele(nd.ni_vp);
   2582 	return (error);
   2583 }
   2584 
   2585 /*
   2586  * Set the access and modification times given a file descriptor.
   2587  */
   2588 /* ARGSUSED */
   2589 int
   2590 sys_futimes(l, v, retval)
   2591 	struct lwp *l;
   2592 	void *v;
   2593 	register_t *retval;
   2594 {
   2595 	struct sys_futimes_args /* {
   2596 		syscallarg(int) fd;
   2597 		syscallarg(const struct timeval *) tptr;
   2598 	} */ *uap = v;
   2599 	struct proc *p = l->l_proc;
   2600 	int error;
   2601 	struct file *fp;
   2602 
   2603 	/* getvnode() will use the descriptor for us */
   2604 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2605 		return (error);
   2606 
   2607 	error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
   2608 	FILE_UNUSE(fp, p);
   2609 	return (error);
   2610 }
   2611 
   2612 /*
   2613  * Set the access and modification times given a path name; this
   2614  * version does not follow links.
   2615  */
   2616 /* ARGSUSED */
   2617 int
   2618 sys_lutimes(l, v, retval)
   2619 	struct lwp *l;
   2620 	void *v;
   2621 	register_t *retval;
   2622 {
   2623 	struct sys_lutimes_args /* {
   2624 		syscallarg(const char *) path;
   2625 		syscallarg(const struct timeval *) tptr;
   2626 	} */ *uap = v;
   2627 	struct proc *p = l->l_proc;
   2628 	int error;
   2629 	struct nameidata nd;
   2630 
   2631 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2632 	if ((error = namei(&nd)) != 0)
   2633 		return (error);
   2634 
   2635 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2636 
   2637 	vrele(nd.ni_vp);
   2638 	return (error);
   2639 }
   2640 
   2641 /*
   2642  * Common routine to set access and modification times given a vnode.
   2643  */
   2644 static int
   2645 change_utimes(vp, tptr, p)
   2646 	struct vnode *vp;
   2647 	const struct timeval *tptr;
   2648 	struct proc *p;
   2649 {
   2650 	struct timeval tv[2];
   2651 	struct vattr vattr;
   2652 	int error;
   2653 
   2654 	VATTR_NULL(&vattr);
   2655 	if (tptr == NULL) {
   2656 		microtime(&tv[0]);
   2657 		tv[1] = tv[0];
   2658 		vattr.va_vaflags |= VA_UTIMES_NULL;
   2659 	} else {
   2660 		error = copyin(tptr, tv, sizeof(tv));
   2661 		if (error)
   2662 			return (error);
   2663 	}
   2664 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2665 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2666 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   2667 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   2668 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   2669 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   2670 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2671 	VOP_UNLOCK(vp, 0);
   2672 	return (error);
   2673 }
   2674 
   2675 /*
   2676  * Truncate a file given its path name.
   2677  */
   2678 /* ARGSUSED */
   2679 int
   2680 sys_truncate(l, v, retval)
   2681 	struct lwp *l;
   2682 	void *v;
   2683 	register_t *retval;
   2684 {
   2685 	struct sys_truncate_args /* {
   2686 		syscallarg(const char *) path;
   2687 		syscallarg(int) pad;
   2688 		syscallarg(off_t) length;
   2689 	} */ *uap = v;
   2690 	struct proc *p = l->l_proc;
   2691 	struct vnode *vp;
   2692 	struct vattr vattr;
   2693 	int error;
   2694 	struct nameidata nd;
   2695 
   2696 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2697 	if ((error = namei(&nd)) != 0)
   2698 		return (error);
   2699 	vp = nd.ni_vp;
   2700 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2701 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2702 	if (vp->v_type == VDIR)
   2703 		error = EISDIR;
   2704 	else if ((error = vn_writechk(vp)) == 0 &&
   2705 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   2706 		VATTR_NULL(&vattr);
   2707 		vattr.va_size = SCARG(uap, length);
   2708 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2709 	}
   2710 	vput(vp);
   2711 	return (error);
   2712 }
   2713 
   2714 /*
   2715  * Truncate a file given a file descriptor.
   2716  */
   2717 /* ARGSUSED */
   2718 int
   2719 sys_ftruncate(l, v, retval)
   2720 	struct lwp *l;
   2721 	void *v;
   2722 	register_t *retval;
   2723 {
   2724 	struct sys_ftruncate_args /* {
   2725 		syscallarg(int) fd;
   2726 		syscallarg(int) pad;
   2727 		syscallarg(off_t) length;
   2728 	} */ *uap = v;
   2729 	struct proc *p = l->l_proc;
   2730 	struct vattr vattr;
   2731 	struct vnode *vp;
   2732 	struct file *fp;
   2733 	int error;
   2734 
   2735 	/* getvnode() will use the descriptor for us */
   2736 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2737 		return (error);
   2738 	if ((fp->f_flag & FWRITE) == 0) {
   2739 		error = EINVAL;
   2740 		goto out;
   2741 	}
   2742 	vp = (struct vnode *)fp->f_data;
   2743 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2744 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2745 	if (vp->v_type == VDIR)
   2746 		error = EISDIR;
   2747 	else if ((error = vn_writechk(vp)) == 0) {
   2748 		VATTR_NULL(&vattr);
   2749 		vattr.va_size = SCARG(uap, length);
   2750 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   2751 	}
   2752 	VOP_UNLOCK(vp, 0);
   2753  out:
   2754 	FILE_UNUSE(fp, p);
   2755 	return (error);
   2756 }
   2757 
   2758 /*
   2759  * Sync an open file.
   2760  */
   2761 /* ARGSUSED */
   2762 int
   2763 sys_fsync(l, v, retval)
   2764 	struct lwp *l;
   2765 	void *v;
   2766 	register_t *retval;
   2767 {
   2768 	struct sys_fsync_args /* {
   2769 		syscallarg(int) fd;
   2770 	} */ *uap = v;
   2771 	struct proc *p = l->l_proc;
   2772 	struct vnode *vp;
   2773 	struct file *fp;
   2774 	int error;
   2775 
   2776 	/* getvnode() will use the descriptor for us */
   2777 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2778 		return (error);
   2779 	vp = (struct vnode *)fp->f_data;
   2780 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2781 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, p);
   2782 	if (error == 0 && bioops.io_fsync != NULL &&
   2783 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   2784 		(*bioops.io_fsync)(vp);
   2785 	VOP_UNLOCK(vp, 0);
   2786 	FILE_UNUSE(fp, p);
   2787 	return (error);
   2788 }
   2789 
   2790 /*
   2791  * Sync the data of an open file.
   2792  */
   2793 /* ARGSUSED */
   2794 int
   2795 sys_fdatasync(l, v, retval)
   2796 	struct lwp *l;
   2797 	void *v;
   2798 	register_t *retval;
   2799 {
   2800 	struct sys_fdatasync_args /* {
   2801 		syscallarg(int) fd;
   2802 	} */ *uap = v;
   2803 	struct proc *p = l->l_proc;
   2804 	struct vnode *vp;
   2805 	struct file *fp;
   2806 	int error;
   2807 
   2808 	/* getvnode() will use the descriptor for us */
   2809 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2810 		return (error);
   2811 	vp = (struct vnode *)fp->f_data;
   2812 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2813 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, p);
   2814 	VOP_UNLOCK(vp, 0);
   2815 	FILE_UNUSE(fp, p);
   2816 	return (error);
   2817 }
   2818 
   2819 /*
   2820  * Rename files, (standard) BSD semantics frontend.
   2821  */
   2822 /* ARGSUSED */
   2823 int
   2824 sys_rename(l, v, retval)
   2825 	struct lwp *l;
   2826 	void *v;
   2827 	register_t *retval;
   2828 {
   2829 	struct sys_rename_args /* {
   2830 		syscallarg(const char *) from;
   2831 		syscallarg(const char *) to;
   2832 	} */ *uap = v;
   2833 	struct proc *p = l->l_proc;
   2834 
   2835 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
   2836 }
   2837 
   2838 /*
   2839  * Rename files, POSIX semantics frontend.
   2840  */
   2841 /* ARGSUSED */
   2842 int
   2843 sys___posix_rename(l, v, retval)
   2844 	struct lwp *l;
   2845 	void *v;
   2846 	register_t *retval;
   2847 {
   2848 	struct sys___posix_rename_args /* {
   2849 		syscallarg(const char *) from;
   2850 		syscallarg(const char *) to;
   2851 	} */ *uap = v;
   2852 	struct proc *p = l->l_proc;
   2853 
   2854 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
   2855 }
   2856 
   2857 /*
   2858  * Rename files.  Source and destination must either both be directories,
   2859  * or both not be directories.  If target is a directory, it must be empty.
   2860  * If `from' and `to' refer to the same object, the value of the `retain'
   2861  * argument is used to determine whether `from' will be
   2862  *
   2863  * (retain == 0)	deleted unless `from' and `to' refer to the same
   2864  *			object in the file system's name space (BSD).
   2865  * (retain == 1)	always retained (POSIX).
   2866  */
   2867 static int
   2868 rename_files(from, to, p, retain)
   2869 	const char *from, *to;
   2870 	struct proc *p;
   2871 	int retain;
   2872 {
   2873 	struct vnode *tvp, *fvp, *tdvp;
   2874 	struct nameidata fromnd, tond;
   2875 	int error;
   2876 
   2877 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   2878 	    from, p);
   2879 	if ((error = namei(&fromnd)) != 0)
   2880 		return (error);
   2881 	fvp = fromnd.ni_vp;
   2882 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
   2883 	    UIO_USERSPACE, to, p);
   2884 	if ((error = namei(&tond)) != 0) {
   2885 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2886 		vrele(fromnd.ni_dvp);
   2887 		vrele(fvp);
   2888 		goto out1;
   2889 	}
   2890 	tdvp = tond.ni_dvp;
   2891 	tvp = tond.ni_vp;
   2892 
   2893 	if (tvp != NULL) {
   2894 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   2895 			error = ENOTDIR;
   2896 			goto out;
   2897 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   2898 			error = EISDIR;
   2899 			goto out;
   2900 		}
   2901 	}
   2902 
   2903 	if (fvp == tdvp)
   2904 		error = EINVAL;
   2905 
   2906 	/*
   2907 	 * Source and destination refer to the same object.
   2908 	 */
   2909 	if (fvp == tvp) {
   2910 		if (retain)
   2911 			error = -1;
   2912 		else if (fromnd.ni_dvp == tdvp &&
   2913 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   2914 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
   2915 		          tond.ni_cnd.cn_nameptr,
   2916 		          fromnd.ni_cnd.cn_namelen))
   2917 		error = -1;
   2918 	}
   2919 
   2920 out:
   2921 	if (!error) {
   2922 		VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
   2923 		if (fromnd.ni_dvp != tdvp)
   2924 			VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2925 		if (tvp) {
   2926 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   2927 		}
   2928 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   2929 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   2930 	} else {
   2931 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   2932 		if (tdvp == tvp)
   2933 			vrele(tdvp);
   2934 		else
   2935 			vput(tdvp);
   2936 		if (tvp)
   2937 			vput(tvp);
   2938 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2939 		vrele(fromnd.ni_dvp);
   2940 		vrele(fvp);
   2941 	}
   2942 	vrele(tond.ni_startdir);
   2943 	PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
   2944 out1:
   2945 	if (fromnd.ni_startdir)
   2946 		vrele(fromnd.ni_startdir);
   2947 	PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
   2948 	return (error == -1 ? 0 : error);
   2949 }
   2950 
   2951 /*
   2952  * Make a directory file.
   2953  */
   2954 /* ARGSUSED */
   2955 int
   2956 sys_mkdir(l, v, retval)
   2957 	struct lwp *l;
   2958 	void *v;
   2959 	register_t *retval;
   2960 {
   2961 	struct sys_mkdir_args /* {
   2962 		syscallarg(const char *) path;
   2963 		syscallarg(int) mode;
   2964 	} */ *uap = v;
   2965 	struct proc *p = l->l_proc;
   2966 	struct vnode *vp;
   2967 	struct vattr vattr;
   2968 	int error;
   2969 	struct nameidata nd;
   2970 
   2971 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   2972 	if ((error = namei(&nd)) != 0)
   2973 		return (error);
   2974 	vp = nd.ni_vp;
   2975 	if (vp != NULL) {
   2976 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2977 		if (nd.ni_dvp == vp)
   2978 			vrele(nd.ni_dvp);
   2979 		else
   2980 			vput(nd.ni_dvp);
   2981 		vrele(vp);
   2982 		return (EEXIST);
   2983 	}
   2984 	VATTR_NULL(&vattr);
   2985 	vattr.va_type = VDIR;
   2986 	vattr.va_mode =
   2987 	    (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   2988 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2989 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   2990 	if (!error)
   2991 		vput(nd.ni_vp);
   2992 	return (error);
   2993 }
   2994 
   2995 /*
   2996  * Remove a directory file.
   2997  */
   2998 /* ARGSUSED */
   2999 int
   3000 sys_rmdir(l, v, retval)
   3001 	struct lwp *l;
   3002 	void *v;
   3003 	register_t *retval;
   3004 {
   3005 	struct sys_rmdir_args /* {
   3006 		syscallarg(const char *) path;
   3007 	} */ *uap = v;
   3008 	struct proc *p = l->l_proc;
   3009 	struct vnode *vp;
   3010 	int error;
   3011 	struct nameidata nd;
   3012 
   3013 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   3014 	    SCARG(uap, path), p);
   3015 	if ((error = namei(&nd)) != 0)
   3016 		return (error);
   3017 	vp = nd.ni_vp;
   3018 	if (vp->v_type != VDIR) {
   3019 		error = ENOTDIR;
   3020 		goto out;
   3021 	}
   3022 	/*
   3023 	 * No rmdir "." please.
   3024 	 */
   3025 	if (nd.ni_dvp == vp) {
   3026 		error = EINVAL;
   3027 		goto out;
   3028 	}
   3029 	/*
   3030 	 * The root of a mounted filesystem cannot be deleted.
   3031 	 */
   3032 	if (vp->v_flag & VROOT)
   3033 		error = EBUSY;
   3034 out:
   3035 	if (!error) {
   3036 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   3037 		VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   3038 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   3039 	} else {
   3040 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3041 		if (nd.ni_dvp == vp)
   3042 			vrele(nd.ni_dvp);
   3043 		else
   3044 			vput(nd.ni_dvp);
   3045 		vput(vp);
   3046 	}
   3047 	return (error);
   3048 }
   3049 
   3050 /*
   3051  * Read a block of directory entries in a file system independent format.
   3052  */
   3053 int
   3054 sys_getdents(l, v, retval)
   3055 	struct lwp *l;
   3056 	void *v;
   3057 	register_t *retval;
   3058 {
   3059 	struct sys_getdents_args /* {
   3060 		syscallarg(int) fd;
   3061 		syscallarg(char *) buf;
   3062 		syscallarg(size_t) count;
   3063 	} */ *uap = v;
   3064 	struct proc *p = l->l_proc;
   3065 	struct file *fp;
   3066 	int error, done;
   3067 
   3068 	/* getvnode() will use the descriptor for us */
   3069 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3070 		return (error);
   3071 	if ((fp->f_flag & FREAD) == 0) {
   3072 		error = EBADF;
   3073 		goto out;
   3074 	}
   3075 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   3076 			SCARG(uap, count), &done, p, 0, 0);
   3077 	*retval = done;
   3078  out:
   3079 	FILE_UNUSE(fp, p);
   3080 	return (error);
   3081 }
   3082 
   3083 /*
   3084  * Set the mode mask for creation of filesystem nodes.
   3085  */
   3086 int
   3087 sys_umask(l, v, retval)
   3088 	struct lwp *l;
   3089 	void *v;
   3090 	register_t *retval;
   3091 {
   3092 	struct sys_umask_args /* {
   3093 		syscallarg(mode_t) newmask;
   3094 	} */ *uap = v;
   3095 	struct proc *p = l->l_proc;
   3096 	struct cwdinfo *cwdi;
   3097 
   3098 	cwdi = p->p_cwdi;
   3099 	*retval = cwdi->cwdi_cmask;
   3100 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
   3101 	return (0);
   3102 }
   3103 
   3104 /*
   3105  * Void all references to file by ripping underlying filesystem
   3106  * away from vnode.
   3107  */
   3108 /* ARGSUSED */
   3109 int
   3110 sys_revoke(l, v, retval)
   3111 	struct lwp *l;
   3112 	void *v;
   3113 	register_t *retval;
   3114 {
   3115 	struct sys_revoke_args /* {
   3116 		syscallarg(const char *) path;
   3117 	} */ *uap = v;
   3118 	struct proc *p = l->l_proc;
   3119 	struct vnode *vp;
   3120 	struct vattr vattr;
   3121 	int error;
   3122 	struct nameidata nd;
   3123 
   3124 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   3125 	if ((error = namei(&nd)) != 0)
   3126 		return (error);
   3127 	vp = nd.ni_vp;
   3128 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   3129 		goto out;
   3130 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   3131 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
   3132 		goto out;
   3133 	if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
   3134 		VOP_REVOKE(vp, REVOKEALL);
   3135 out:
   3136 	vrele(vp);
   3137 	return (error);
   3138 }
   3139 
   3140 /*
   3141  * Convert a user file descriptor to a kernel file entry.
   3142  */
   3143 int
   3144 getvnode(fdp, fd, fpp)
   3145 	struct filedesc *fdp;
   3146 	int fd;
   3147 	struct file **fpp;
   3148 {
   3149 	struct vnode *vp;
   3150 	struct file *fp;
   3151 
   3152 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   3153 		return (EBADF);
   3154 
   3155 	FILE_USE(fp);
   3156 
   3157 	if (fp->f_type != DTYPE_VNODE) {
   3158 		FILE_UNUSE(fp, NULL);
   3159 		return (EINVAL);
   3160 	}
   3161 
   3162 	vp = (struct vnode *)fp->f_data;
   3163 	if (vp->v_type == VBAD) {
   3164 		FILE_UNUSE(fp, NULL);
   3165 		return (EBADF);
   3166 	}
   3167 
   3168 	*fpp = fp;
   3169 	return (0);
   3170 }
   3171