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