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