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