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