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