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