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