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