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