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