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