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