Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.377
      1 /*	$NetBSD: vfs_syscalls.c,v 1.377 2008/11/12 12:36:16 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.377 2008/11/12 12:36:16 ad Exp $");
     67 
     68 #include "opt_compat_netbsd.h"
     69 #include "opt_compat_43.h"
     70 #include "opt_fileassoc.h"
     71 #include "veriexec.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/namei.h>
     76 #include <sys/filedesc.h>
     77 #include <sys/kernel.h>
     78 #include <sys/file.h>
     79 #include <sys/stat.h>
     80 #include <sys/vnode.h>
     81 #include <sys/mount.h>
     82 #include <sys/proc.h>
     83 #include <sys/uio.h>
     84 #include <sys/malloc.h>
     85 #include <sys/kmem.h>
     86 #include <sys/dirent.h>
     87 #include <sys/sysctl.h>
     88 #include <sys/syscallargs.h>
     89 #include <sys/vfs_syscalls.h>
     90 #include <sys/ktrace.h>
     91 #ifdef FILEASSOC
     92 #include <sys/fileassoc.h>
     93 #endif /* FILEASSOC */
     94 #include <sys/verified_exec.h>
     95 #include <sys/kauth.h>
     96 #include <sys/atomic.h>
     97 #include <sys/module.h>
     98 
     99 #include <miscfs/genfs/genfs.h>
    100 #include <miscfs/syncfs/syncfs.h>
    101 #include <miscfs/specfs/specdev.h>
    102 
    103 #ifdef COMPAT_30
    104 #include "opt_nfsserver.h"
    105 #include <nfs/rpcv2.h>
    106 #endif
    107 #include <nfs/nfsproto.h>
    108 #ifdef COMPAT_30
    109 #include <nfs/nfs.h>
    110 #include <nfs/nfs_var.h>
    111 #endif
    112 
    113 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
    114 
    115 static int change_dir(struct nameidata *, struct lwp *);
    116 static int change_flags(struct vnode *, u_long, struct lwp *);
    117 static int change_mode(struct vnode *, int, struct lwp *l);
    118 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
    119 
    120 void checkdirs(struct vnode *);
    121 
    122 int dovfsusermount = 0;
    123 
    124 /*
    125  * Virtual File System System Calls
    126  */
    127 
    128 /*
    129  * Mount a file system.
    130  */
    131 
    132 #if defined(COMPAT_09) || defined(COMPAT_43)
    133 /*
    134  * This table is used to maintain compatibility with 4.3BSD
    135  * and NetBSD 0.9 mount syscalls.  Note, the order is important!
    136  *
    137  * Do not modify this table. It should only contain filesystems
    138  * supported by NetBSD 0.9 and 4.3BSD.
    139  */
    140 const char * const mountcompatnames[] = {
    141 	NULL,		/* 0 = MOUNT_NONE */
    142 	MOUNT_FFS,	/* 1 = MOUNT_UFS */
    143 	MOUNT_NFS,	/* 2 */
    144 	MOUNT_MFS,	/* 3 */
    145 	MOUNT_MSDOS,	/* 4 */
    146 	MOUNT_CD9660,	/* 5 = MOUNT_ISOFS */
    147 	MOUNT_FDESC,	/* 6 */
    148 	MOUNT_KERNFS,	/* 7 */
    149 	NULL,		/* 8 = MOUNT_DEVFS */
    150 	MOUNT_AFS,	/* 9 */
    151 };
    152 const int nmountcompatnames = sizeof(mountcompatnames) /
    153     sizeof(mountcompatnames[0]);
    154 #endif /* COMPAT_09 || COMPAT_43 */
    155 
    156 static int
    157 mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
    158     void *data, size_t *data_len)
    159 {
    160 	struct mount *mp;
    161 	int error = 0, saved_flags;
    162 
    163 	mp = vp->v_mount;
    164 	saved_flags = mp->mnt_flag;
    165 
    166 	/* We can operate only on VV_ROOT nodes. */
    167 	if ((vp->v_vflag & VV_ROOT) == 0) {
    168 		error = EINVAL;
    169 		goto out;
    170 	}
    171 
    172 	/*
    173 	 * We only allow the filesystem to be reloaded if it
    174 	 * is currently mounted read-only.  Additionally, we
    175 	 * prevent read-write to read-only downgrades.
    176 	 */
    177 	if ((flags & (MNT_RELOAD | MNT_RDONLY)) != 0 &&
    178 	    (mp->mnt_flag & MNT_RDONLY) == 0) {
    179 		error = EOPNOTSUPP;	/* Needs translation */
    180 		goto out;
    181 	}
    182 
    183 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    184 	    KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
    185 	if (error)
    186 		goto out;
    187 
    188 	if (vfs_busy(mp, NULL)) {
    189 		error = EPERM;
    190 		goto out;
    191 	}
    192 
    193 	mutex_enter(&mp->mnt_updating);
    194 
    195 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    196 	mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    197 
    198 	/*
    199 	 * Set the mount level flags.
    200 	 */
    201 	if (flags & MNT_RDONLY)
    202 		mp->mnt_flag |= MNT_RDONLY;
    203 	else if (mp->mnt_flag & MNT_RDONLY)
    204 		mp->mnt_iflag |= IMNT_WANTRDWR;
    205 	mp->mnt_flag &=
    206 	  ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    207 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    208 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    209 	    MNT_LOG);
    210 	mp->mnt_flag |= flags &
    211 	   (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    212 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    213 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    214 	    MNT_LOG | MNT_IGNORE);
    215 
    216 	error = VFS_MOUNT(mp, path, data, data_len);
    217 
    218 #if defined(COMPAT_30) && defined(NFSSERVER)
    219 	if (error && data != NULL) {
    220 		int error2;
    221 
    222 		/* Update failed; let's try and see if it was an
    223 		 * export request. */
    224 		error2 = nfs_update_exports_30(mp, path, data, l);
    225 
    226 		/* Only update error code if the export request was
    227 		 * understood but some problem occurred while
    228 		 * processing it. */
    229 		if (error2 != EJUSTRETURN)
    230 			error = error2;
    231 	}
    232 #endif
    233 	if (mp->mnt_iflag & IMNT_WANTRDWR)
    234 		mp->mnt_flag &= ~MNT_RDONLY;
    235 	if (error)
    236 		mp->mnt_flag = saved_flags;
    237 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    238 	mp->mnt_iflag &= ~IMNT_WANTRDWR;
    239 	if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
    240 		if (mp->mnt_syncer == NULL)
    241 			error = vfs_allocate_syncvnode(mp);
    242 	} else {
    243 		if (mp->mnt_syncer != NULL)
    244 			vfs_deallocate_syncvnode(mp);
    245 	}
    246 	mutex_exit(&mp->mnt_updating);
    247 	vfs_unbusy(mp, false, NULL);
    248 
    249  out:
    250 	return (error);
    251 }
    252 
    253 static int
    254 mount_get_vfsops(const char *fstype, struct vfsops **vfsops)
    255 {
    256 	char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
    257 	int error;
    258 
    259 	/* Copy file-system type from userspace.  */
    260 	error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
    261 	if (error) {
    262 #if defined(COMPAT_09) || defined(COMPAT_43)
    263 		/*
    264 		 * Historically, filesystem types were identified by numbers.
    265 		 * If we get an integer for the filesystem type instead of a
    266 		 * string, we check to see if it matches one of the historic
    267 		 * filesystem types.
    268 		 */
    269 		u_long fsindex = (u_long)fstype;
    270 		if (fsindex >= nmountcompatnames ||
    271 		    mountcompatnames[fsindex] == NULL)
    272 			return ENODEV;
    273 		strlcpy(fstypename, mountcompatnames[fsindex],
    274 		    sizeof(fstypename));
    275 #else
    276 		return error;
    277 #endif
    278 	}
    279 
    280 #ifdef	COMPAT_10
    281 	/* Accept `ufs' as an alias for `ffs'. */
    282 	if (strcmp(fstypename, "ufs") == 0)
    283 		fstypename[0] = 'f';
    284 #endif
    285 
    286 	if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
    287 		return 0;
    288 
    289 	/* If we can autoload a vfs module, try again */
    290 	mutex_enter(&module_lock);
    291 	(void)module_autoload(fstype, MODULE_CLASS_VFS);
    292 	mutex_exit(&module_lock);
    293 
    294 	if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
    295 		return 0;
    296 
    297 	return ENODEV;
    298 }
    299 
    300 static int
    301 mount_domount(struct lwp *l, struct vnode **vpp, struct vfsops *vfsops,
    302     const char *path, int flags, void *data, size_t *data_len, u_int recurse)
    303 {
    304 	struct mount *mp;
    305 	struct vnode *vp = *vpp;
    306 	struct vattr va;
    307 	int error;
    308 
    309 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    310 	    KAUTH_REQ_SYSTEM_MOUNT_NEW, vp, KAUTH_ARG(flags), data);
    311 	if (error)
    312 		return error;
    313 
    314 	/* Can't make a non-dir a mount-point (from here anyway). */
    315 	if (vp->v_type != VDIR)
    316 		return ENOTDIR;
    317 
    318 	/*
    319 	 * If the user is not root, ensure that they own the directory
    320 	 * onto which we are attempting to mount.
    321 	 */
    322 	if ((error = VOP_GETATTR(vp, &va, l->l_cred)) != 0 ||
    323 	    (va.va_uid != kauth_cred_geteuid(l->l_cred) &&
    324 	    (error = kauth_authorize_generic(l->l_cred,
    325 	    KAUTH_GENERIC_ISSUSER, NULL)) != 0)) {
    326 		return error;
    327 	}
    328 
    329 	if (flags & MNT_EXPORTED)
    330 		return EINVAL;
    331 
    332 	if ((error = vinvalbuf(vp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
    333 		return error;
    334 
    335 	/*
    336 	 * Check if a file-system is not already mounted on this vnode.
    337 	 */
    338 	if (vp->v_mountedhere != NULL)
    339 		return EBUSY;
    340 
    341 	mp = kmem_zalloc(sizeof(*mp), KM_SLEEP);
    342 	if (mp == NULL)
    343 		return ENOMEM;
    344 
    345 	mp->mnt_op = vfsops;
    346 	mp->mnt_refcnt = 1;
    347 
    348 	TAILQ_INIT(&mp->mnt_vnodelist);
    349 	rw_init(&mp->mnt_unmounting);
    350  	mutex_init(&mp->mnt_renamelock, MUTEX_DEFAULT, IPL_NONE);
    351 	mutex_init(&mp->mnt_updating, MUTEX_DEFAULT, IPL_NONE);
    352 	error = vfs_busy(mp, NULL);
    353 	KASSERT(error == 0);
    354 	mutex_enter(&mp->mnt_updating);
    355 
    356 	mp->mnt_vnodecovered = vp;
    357 	mp->mnt_stat.f_owner = kauth_cred_geteuid(l->l_cred);
    358 	mount_initspecific(mp);
    359 
    360 	/*
    361 	 * The underlying file system may refuse the mount for
    362 	 * various reasons.  Allow the user to force it to happen.
    363 	 *
    364 	 * Set the mount level flags.
    365 	 */
    366 	mp->mnt_flag = flags &
    367 	   (MNT_FORCE | MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    368 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    369 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    370 	    MNT_LOG | MNT_IGNORE | MNT_RDONLY);
    371 
    372 	error = VFS_MOUNT(mp, path, data, data_len);
    373 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    374 
    375 	/*
    376 	 * Put the new filesystem on the mount list after root.
    377 	 */
    378 	cache_purge(vp);
    379 	if (error != 0) {
    380 		vp->v_mountedhere = NULL;
    381 		mutex_exit(&mp->mnt_updating);
    382 		vfs_unbusy(mp, false, NULL);
    383 		vfs_destroy(mp);
    384 		return error;
    385 	}
    386 
    387 	mp->mnt_iflag &= ~IMNT_WANTRDWR;
    388 	mutex_enter(&mountlist_lock);
    389 	vp->v_mountedhere = mp;
    390 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    391 	mutex_exit(&mountlist_lock);
    392     	vn_restorerecurse(vp, recurse);
    393 	VOP_UNLOCK(vp, 0);
    394 	checkdirs(vp);
    395 	if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    396 		error = vfs_allocate_syncvnode(mp);
    397 	/* Hold an additional reference to the mount across VFS_START(). */
    398 	mutex_exit(&mp->mnt_updating);
    399 	vfs_unbusy(mp, true, NULL);
    400 	(void) VFS_STATVFS(mp, &mp->mnt_stat);
    401 	error = VFS_START(mp, 0);
    402 	if (error)
    403 		vrele(vp);
    404 	/* Drop reference held for VFS_START(). */
    405 	vfs_destroy(mp);
    406 	*vpp = NULL;
    407 	return error;
    408 }
    409 
    410 static int
    411 mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
    412     void *data, size_t *data_len)
    413 {
    414 	struct mount *mp;
    415 	int error;
    416 
    417 	/* If MNT_GETARGS is specified, it should be the only flag. */
    418 	if (flags & ~MNT_GETARGS)
    419 		return EINVAL;
    420 
    421 	mp = vp->v_mount;
    422 
    423 	/* XXX: probably some notion of "can see" here if we want isolation. */
    424 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    425 	    KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
    426 	if (error)
    427 		return error;
    428 
    429 	if ((vp->v_vflag & VV_ROOT) == 0)
    430 		return EINVAL;
    431 
    432 	if (vfs_busy(mp, NULL))
    433 		return EPERM;
    434 
    435 	mutex_enter(&mp->mnt_updating);
    436 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    437 	mp->mnt_flag |= MNT_GETARGS;
    438 	error = VFS_MOUNT(mp, path, data, data_len);
    439 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    440 	mutex_exit(&mp->mnt_updating);
    441 
    442 	vfs_unbusy(mp, false, NULL);
    443 	return (error);
    444 }
    445 
    446 #ifdef COMPAT_40
    447 /* ARGSUSED */
    448 int
    449 compat_40_sys_mount(struct lwp *l, const struct compat_40_sys_mount_args *uap, register_t *retval)
    450 {
    451 	/* {
    452 		syscallarg(const char *) type;
    453 		syscallarg(const char *) path;
    454 		syscallarg(int) flags;
    455 		syscallarg(void *) data;
    456 	} */
    457 	register_t dummy;
    458 
    459 	return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
    460 	    SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE, 0, &dummy);
    461 }
    462 #endif
    463 
    464 int
    465 sys___mount50(struct lwp *l, const struct sys___mount50_args *uap, register_t *retval)
    466 {
    467 	/* {
    468 		syscallarg(const char *) type;
    469 		syscallarg(const char *) path;
    470 		syscallarg(int) flags;
    471 		syscallarg(void *) data;
    472 		syscallarg(size_t) data_len;
    473 	} */
    474 
    475 	return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
    476 	    SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
    477 	    SCARG(uap, data_len), retval);
    478 }
    479 
    480 int
    481 do_sys_mount(struct lwp *l, struct vfsops *vfsops, const char *type,
    482     const char *path, int flags, void *data, enum uio_seg data_seg,
    483     size_t data_len, register_t *retval)
    484 {
    485 	struct vnode *vp;
    486 	struct nameidata nd;
    487 	void *data_buf = data;
    488 	u_int recurse;
    489 	int error;
    490 
    491 	/*
    492 	 * Get vnode to be covered
    493 	 */
    494 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path);
    495 	if ((error = namei(&nd)) != 0)
    496 		return (error);
    497 	vp = nd.ni_vp;
    498 
    499 	/*
    500 	 * A lookup in VFS_MOUNT might result in an attempt to
    501 	 * lock this vnode again, so make the lock recursive.
    502 	 */
    503 	if (vfsops == NULL) {
    504 		if (flags & (MNT_GETARGS | MNT_UPDATE)) {
    505 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    506 			recurse = vn_setrecurse(vp);
    507 			vfsops = vp->v_mount->mnt_op;
    508 		} else {
    509 			/* 'type' is userspace */
    510 			error = mount_get_vfsops(type, &vfsops);
    511 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    512 			recurse = vn_setrecurse(vp);
    513 			if (error != 0)
    514 				goto done;
    515 		}
    516 	} else {
    517 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    518 		recurse = vn_setrecurse(vp);
    519 	}
    520 
    521 	if (data != NULL && data_seg == UIO_USERSPACE) {
    522 		if (data_len == 0) {
    523 			/* No length supplied, use default for filesystem */
    524 			data_len = vfsops->vfs_min_mount_data;
    525 			if (data_len > VFS_MAX_MOUNT_DATA) {
    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 	vfs_destroy(mp);
    700 	return error;
    701 }
    702 
    703 /*
    704  * Do the actual file system unmount.  File system is assumed to have
    705  * been locked by the caller.
    706  *
    707  * => Caller hold reference to the mount, explicitly for dounmount().
    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 		return ENOENT;
    733 	}
    734 
    735 	used_syncer = (mp->mnt_syncer != NULL);
    736 
    737 	/*
    738 	 * XXX Syncer must be frozen when we get here.  This should really
    739 	 * be done on a per-mountpoint basis, but especially the softdep
    740 	 * code possibly called from the syncer doesn't exactly work on a
    741 	 * per-mountpoint basis, so the softdep code would become a maze
    742 	 * of vfs_busy() calls.
    743 	 *
    744 	 * The caller of dounmount() must acquire syncer_mutex because
    745 	 * the syncer itself acquires locks in syncer_mutex -> vfs_busy
    746 	 * order, and we must preserve that order to avoid deadlock.
    747 	 *
    748 	 * So, if the file system did not use the syncer, now is
    749 	 * the time to release the syncer_mutex.
    750 	 */
    751 	if (used_syncer == 0)
    752 		mutex_exit(&syncer_mutex);
    753 
    754 	mp->mnt_iflag |= IMNT_UNMOUNT;
    755 	async = mp->mnt_flag & MNT_ASYNC;
    756 	mp->mnt_flag &= ~MNT_ASYNC;
    757 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    758 	if (mp->mnt_syncer != NULL)
    759 		vfs_deallocate_syncvnode(mp);
    760 	error = 0;
    761 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    762 		error = VFS_SYNC(mp, MNT_WAIT, l->l_cred);
    763 	}
    764 	vfs_scrubvnlist(mp);
    765 	if (error == 0 || (flags & MNT_FORCE))
    766 		error = VFS_UNMOUNT(mp, flags);
    767 	if (error) {
    768 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    769 			(void) vfs_allocate_syncvnode(mp);
    770 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
    771 		mp->mnt_flag |= async;
    772 		rw_exit(&mp->mnt_unmounting);
    773 		if (used_syncer)
    774 			mutex_exit(&syncer_mutex);
    775 		return (error);
    776 	}
    777 	vfs_scrubvnlist(mp);
    778 	mutex_enter(&mountlist_lock);
    779 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP)
    780 		coveredvp->v_mountedhere = NULL;
    781 	CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
    782 	mp->mnt_iflag |= IMNT_GONE;
    783 	mutex_exit(&mountlist_lock);
    784 	if (TAILQ_FIRST(&mp->mnt_vnodelist) != NULL)
    785 		panic("unmount: dangling vnode");
    786 	if (used_syncer)
    787 		mutex_exit(&syncer_mutex);
    788 	vfs_hooks_unmount(mp);
    789 	rw_exit(&mp->mnt_unmounting);
    790 	vfs_destroy(mp);	/* reference from mount() */
    791 	if (coveredvp != NULLVP)
    792 		vrele(coveredvp);
    793 	return (0);
    794 }
    795 
    796 /*
    797  * Sync each mounted filesystem.
    798  */
    799 #ifdef DEBUG
    800 int syncprt = 0;
    801 struct ctldebug debug0 = { "syncprt", &syncprt };
    802 #endif
    803 
    804 /* ARGSUSED */
    805 int
    806 sys_sync(struct lwp *l, const void *v, register_t *retval)
    807 {
    808 	struct mount *mp, *nmp;
    809 	int asyncflag;
    810 
    811 	if (l == NULL)
    812 		l = &lwp0;
    813 
    814 	mutex_enter(&mountlist_lock);
    815 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    816 	     mp = nmp) {
    817 		if (vfs_busy(mp, &nmp)) {
    818 			continue;
    819 		}
    820 		mutex_enter(&mp->mnt_updating);
    821 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    822 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    823 			mp->mnt_flag &= ~MNT_ASYNC;
    824 			VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
    825 			if (asyncflag)
    826 				 mp->mnt_flag |= MNT_ASYNC;
    827 		}
    828 		mutex_exit(&mp->mnt_updating);
    829 		vfs_unbusy(mp, false, &nmp);
    830 	}
    831 	mutex_exit(&mountlist_lock);
    832 #ifdef DEBUG
    833 	if (syncprt)
    834 		vfs_bufstats();
    835 #endif /* DEBUG */
    836 	return (0);
    837 }
    838 
    839 /*
    840  * Change filesystem quotas.
    841  */
    842 /* ARGSUSED */
    843 int
    844 sys_quotactl(struct lwp *l, const struct sys_quotactl_args *uap, register_t *retval)
    845 {
    846 	/* {
    847 		syscallarg(const char *) path;
    848 		syscallarg(int) cmd;
    849 		syscallarg(int) uid;
    850 		syscallarg(void *) arg;
    851 	} */
    852 	struct mount *mp;
    853 	int error;
    854 	struct nameidata nd;
    855 
    856 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
    857 	    SCARG(uap, path));
    858 	if ((error = namei(&nd)) != 0)
    859 		return (error);
    860 	mp = nd.ni_vp->v_mount;
    861 	error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
    862 	    SCARG(uap, arg));
    863 	vrele(nd.ni_vp);
    864 	return (error);
    865 }
    866 
    867 int
    868 dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
    869     int root)
    870 {
    871 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    872 	int error = 0;
    873 
    874 	/*
    875 	 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    876 	 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
    877 	 * overrides MNT_NOWAIT.
    878 	 */
    879 	if (flags == MNT_NOWAIT	|| flags == MNT_LAZY ||
    880 	    (flags != MNT_WAIT && flags != 0)) {
    881 		memcpy(sp, &mp->mnt_stat, sizeof(*sp));
    882 		goto done;
    883 	}
    884 
    885 	/* Get the filesystem stats now */
    886 	memset(sp, 0, sizeof(*sp));
    887 	if ((error = VFS_STATVFS(mp, sp)) != 0) {
    888 		return error;
    889 	}
    890 
    891 	if (cwdi->cwdi_rdir == NULL)
    892 		(void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
    893 done:
    894 	if (cwdi->cwdi_rdir != NULL) {
    895 		size_t len;
    896 		char *bp;
    897 		char c;
    898 		char *path = PNBUF_GET();
    899 
    900 		bp = path + MAXPATHLEN;
    901 		*--bp = '\0';
    902 		rw_enter(&cwdi->cwdi_lock, RW_READER);
    903 		error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
    904 		    MAXPATHLEN / 2, 0, l);
    905 		rw_exit(&cwdi->cwdi_lock);
    906 		if (error) {
    907 			PNBUF_PUT(path);
    908 			return error;
    909 		}
    910 		len = strlen(bp);
    911 		/*
    912 		 * for mount points that are below our root, we can see
    913 		 * them, so we fix up the pathname and return them. The
    914 		 * rest we cannot see, so we don't allow viewing the
    915 		 * data.
    916 		 */
    917 		if (strncmp(bp, sp->f_mntonname, len) == 0 &&
    918 		    ((c = sp->f_mntonname[len]) == '/' || c == '\0')) {
    919 			(void)strlcpy(sp->f_mntonname, &sp->f_mntonname[len],
    920 			    sizeof(sp->f_mntonname));
    921 			if (sp->f_mntonname[0] == '\0')
    922 				(void)strlcpy(sp->f_mntonname, "/",
    923 				    sizeof(sp->f_mntonname));
    924 		} else {
    925 			if (root)
    926 				(void)strlcpy(sp->f_mntonname, "/",
    927 				    sizeof(sp->f_mntonname));
    928 			else
    929 				error = EPERM;
    930 		}
    931 		PNBUF_PUT(path);
    932 	}
    933 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    934 	return error;
    935 }
    936 
    937 /*
    938  * Get filesystem statistics by path.
    939  */
    940 int
    941 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
    942 {
    943 	struct mount *mp;
    944 	int error;
    945 	struct nameidata nd;
    946 
    947 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path);
    948 	if ((error = namei(&nd)) != 0)
    949 		return error;
    950 	mp = nd.ni_vp->v_mount;
    951 	error = dostatvfs(mp, sb, l, flags, 1);
    952 	vrele(nd.ni_vp);
    953 	return error;
    954 }
    955 
    956 /* ARGSUSED */
    957 int
    958 sys_statvfs1(struct lwp *l, const struct sys_statvfs1_args *uap, register_t *retval)
    959 {
    960 	/* {
    961 		syscallarg(const char *) path;
    962 		syscallarg(struct statvfs *) buf;
    963 		syscallarg(int) flags;
    964 	} */
    965 	struct statvfs *sb;
    966 	int error;
    967 
    968 	sb = STATVFSBUF_GET();
    969 	error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
    970 	if (error == 0)
    971 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
    972 	STATVFSBUF_PUT(sb);
    973 	return error;
    974 }
    975 
    976 /*
    977  * Get filesystem statistics by fd.
    978  */
    979 int
    980 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
    981 {
    982 	file_t *fp;
    983 	struct mount *mp;
    984 	int error;
    985 
    986 	/* fd_getvnode() will use the descriptor for us */
    987 	if ((error = fd_getvnode(fd, &fp)) != 0)
    988 		return (error);
    989 	mp = ((struct vnode *)fp->f_data)->v_mount;
    990 	error = dostatvfs(mp, sb, curlwp, flags, 1);
    991 	fd_putfile(fd);
    992 	return error;
    993 }
    994 
    995 /* ARGSUSED */
    996 int
    997 sys_fstatvfs1(struct lwp *l, const struct sys_fstatvfs1_args *uap, register_t *retval)
    998 {
    999 	/* {
   1000 		syscallarg(int) fd;
   1001 		syscallarg(struct statvfs *) buf;
   1002 		syscallarg(int) flags;
   1003 	} */
   1004 	struct statvfs *sb;
   1005 	int error;
   1006 
   1007 	sb = STATVFSBUF_GET();
   1008 	error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
   1009 	if (error == 0)
   1010 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
   1011 	STATVFSBUF_PUT(sb);
   1012 	return error;
   1013 }
   1014 
   1015 
   1016 /*
   1017  * Get statistics on all filesystems.
   1018  */
   1019 int
   1020 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
   1021     int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
   1022     register_t *retval)
   1023 {
   1024 	int root = 0;
   1025 	struct proc *p = l->l_proc;
   1026 	struct mount *mp, *nmp;
   1027 	struct statvfs *sb;
   1028 	size_t count, maxcount;
   1029 	int error = 0;
   1030 
   1031 	sb = STATVFSBUF_GET();
   1032 	maxcount = bufsize / entry_sz;
   1033 	mutex_enter(&mountlist_lock);
   1034 	count = 0;
   1035 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
   1036 	     mp = nmp) {
   1037 		if (vfs_busy(mp, &nmp)) {
   1038 			continue;
   1039 		}
   1040 		if (sfsp && count < maxcount) {
   1041 			error = dostatvfs(mp, sb, l, flags, 0);
   1042 			if (error) {
   1043 				vfs_unbusy(mp, false, &nmp);
   1044 				error = 0;
   1045 				continue;
   1046 			}
   1047 			error = copyfn(sb, sfsp, entry_sz);
   1048 			if (error) {
   1049 				vfs_unbusy(mp, false, NULL);
   1050 				goto out;
   1051 			}
   1052 			sfsp = (char *)sfsp + entry_sz;
   1053 			root |= strcmp(sb->f_mntonname, "/") == 0;
   1054 		}
   1055 		count++;
   1056 		vfs_unbusy(mp, false, &nmp);
   1057 	}
   1058 	mutex_exit(&mountlist_lock);
   1059 
   1060 	if (root == 0 && p->p_cwdi->cwdi_rdir) {
   1061 		/*
   1062 		 * fake a root entry
   1063 		 */
   1064 		error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
   1065 		    sb, l, flags, 1);
   1066 		if (error != 0)
   1067 			goto out;
   1068 		if (sfsp) {
   1069 			error = copyfn(sb, sfsp, entry_sz);
   1070 			if (error != 0)
   1071 				goto out;
   1072 		}
   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 	bool vanull, setbirthtime;
   3016 	struct timespec ts[2];
   3017 
   3018 	if (tptr == NULL) {
   3019 		vanull = true;
   3020 		nanotime(&ts[0]);
   3021 		ts[1] = ts[0];
   3022 	} else {
   3023 		struct timeval tv[2];
   3024 
   3025 		vanull = false;
   3026 		if (seg != UIO_SYSSPACE) {
   3027 			error = copyin(tptr, &tv, sizeof (tv));
   3028 			if (error != 0)
   3029 				return error;
   3030 			tptr = tv;
   3031 		}
   3032 		TIMEVAL_TO_TIMESPEC(&tptr[0], &ts[0]);
   3033 		TIMEVAL_TO_TIMESPEC(&tptr[1], &ts[1]);
   3034 	}
   3035 
   3036 	if (vp == NULL) {
   3037 		NDINIT(&nd, LOOKUP, flag | TRYEMULROOT, UIO_USERSPACE, path);
   3038 		if ((error = namei(&nd)) != 0)
   3039 			return error;
   3040 		vp = nd.ni_vp;
   3041 	} else
   3042 		nd.ni_vp = NULL;
   3043 
   3044 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3045 	setbirthtime = (VOP_GETATTR(vp, &vattr, l->l_cred) == 0 &&
   3046 	    timespeccmp(&ts[1], &vattr.va_birthtime, <));
   3047 	VATTR_NULL(&vattr);
   3048 	vattr.va_atime = ts[0];
   3049 	vattr.va_mtime = ts[1];
   3050 	if (setbirthtime)
   3051 		vattr.va_birthtime = ts[1];
   3052 	if (vanull)
   3053 		vattr.va_flags |= VA_UTIMES_NULL;
   3054 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   3055 	VOP_UNLOCK(vp, 0);
   3056 
   3057 	if (nd.ni_vp != NULL)
   3058 		vrele(nd.ni_vp);
   3059 
   3060 	return error;
   3061 }
   3062 
   3063 /*
   3064  * Truncate a file given its path name.
   3065  */
   3066 /* ARGSUSED */
   3067 int
   3068 sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval)
   3069 {
   3070 	/* {
   3071 		syscallarg(const char *) path;
   3072 		syscallarg(int) pad;
   3073 		syscallarg(off_t) length;
   3074 	} */
   3075 	struct vnode *vp;
   3076 	struct vattr vattr;
   3077 	int error;
   3078 	struct nameidata nd;
   3079 
   3080 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   3081 	    SCARG(uap, path));
   3082 	if ((error = namei(&nd)) != 0)
   3083 		return (error);
   3084 	vp = nd.ni_vp;
   3085 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3086 	if (vp->v_type == VDIR)
   3087 		error = EISDIR;
   3088 	else if ((error = vn_writechk(vp)) == 0 &&
   3089 	    (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
   3090 		VATTR_NULL(&vattr);
   3091 		vattr.va_size = SCARG(uap, length);
   3092 		error = VOP_SETATTR(vp, &vattr, l->l_cred);
   3093 	}
   3094 	vput(vp);
   3095 	return (error);
   3096 }
   3097 
   3098 /*
   3099  * Truncate a file given a file descriptor.
   3100  */
   3101 /* ARGSUSED */
   3102 int
   3103 sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
   3104 {
   3105 	/* {
   3106 		syscallarg(int) fd;
   3107 		syscallarg(int) pad;
   3108 		syscallarg(off_t) length;
   3109 	} */
   3110 	struct vattr vattr;
   3111 	struct vnode *vp;
   3112 	file_t *fp;
   3113 	int error;
   3114 
   3115 	/* fd_getvnode() will use the descriptor for us */
   3116 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3117 		return (error);
   3118 	if ((fp->f_flag & FWRITE) == 0) {
   3119 		error = EINVAL;
   3120 		goto out;
   3121 	}
   3122 	vp = fp->f_data;
   3123 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3124 	if (vp->v_type == VDIR)
   3125 		error = EISDIR;
   3126 	else if ((error = vn_writechk(vp)) == 0) {
   3127 		VATTR_NULL(&vattr);
   3128 		vattr.va_size = SCARG(uap, length);
   3129 		error = VOP_SETATTR(vp, &vattr, fp->f_cred);
   3130 	}
   3131 	VOP_UNLOCK(vp, 0);
   3132  out:
   3133 	fd_putfile(SCARG(uap, fd));
   3134 	return (error);
   3135 }
   3136 
   3137 /*
   3138  * Sync an open file.
   3139  */
   3140 /* ARGSUSED */
   3141 int
   3142 sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
   3143 {
   3144 	/* {
   3145 		syscallarg(int) fd;
   3146 	} */
   3147 	struct vnode *vp;
   3148 	file_t *fp;
   3149 	int error;
   3150 
   3151 	/* fd_getvnode() will use the descriptor for us */
   3152 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3153 		return (error);
   3154 	vp = fp->f_data;
   3155 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3156 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
   3157 	if (error == 0 && bioopsp != NULL &&
   3158 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   3159 		(*bioopsp->io_fsync)(vp, 0);
   3160 	VOP_UNLOCK(vp, 0);
   3161 	fd_putfile(SCARG(uap, fd));
   3162 	return (error);
   3163 }
   3164 
   3165 /*
   3166  * Sync a range of file data.  API modeled after that found in AIX.
   3167  *
   3168  * FDATASYNC indicates that we need only save enough metadata to be able
   3169  * to re-read the written data.  Note we duplicate AIX's requirement that
   3170  * the file be open for writing.
   3171  */
   3172 /* ARGSUSED */
   3173 int
   3174 sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
   3175 {
   3176 	/* {
   3177 		syscallarg(int) fd;
   3178 		syscallarg(int) flags;
   3179 		syscallarg(off_t) start;
   3180 		syscallarg(off_t) length;
   3181 	} */
   3182 	struct vnode *vp;
   3183 	file_t *fp;
   3184 	int flags, nflags;
   3185 	off_t s, e, len;
   3186 	int error;
   3187 
   3188 	/* fd_getvnode() will use the descriptor for us */
   3189 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3190 		return (error);
   3191 
   3192 	if ((fp->f_flag & FWRITE) == 0) {
   3193 		error = EBADF;
   3194 		goto out;
   3195 	}
   3196 
   3197 	flags = SCARG(uap, flags);
   3198 	if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
   3199 	    ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
   3200 		error = EINVAL;
   3201 		goto out;
   3202 	}
   3203 	/* Now set up the flags for value(s) to pass to VOP_FSYNC() */
   3204 	if (flags & FDATASYNC)
   3205 		nflags = FSYNC_DATAONLY | FSYNC_WAIT;
   3206 	else
   3207 		nflags = FSYNC_WAIT;
   3208 	if (flags & FDISKSYNC)
   3209 		nflags |= FSYNC_CACHE;
   3210 
   3211 	len = SCARG(uap, length);
   3212 	/* If length == 0, we do the whole file, and s = l = 0 will do that */
   3213 	if (len) {
   3214 		s = SCARG(uap, start);
   3215 		e = s + len;
   3216 		if (e < s) {
   3217 			error = EINVAL;
   3218 			goto out;
   3219 		}
   3220 	} else {
   3221 		e = 0;
   3222 		s = 0;
   3223 	}
   3224 
   3225 	vp = fp->f_data;
   3226 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3227 	error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
   3228 
   3229 	if (error == 0 && bioopsp != NULL &&
   3230 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   3231 		(*bioopsp->io_fsync)(vp, nflags);
   3232 
   3233 	VOP_UNLOCK(vp, 0);
   3234 out:
   3235 	fd_putfile(SCARG(uap, fd));
   3236 	return (error);
   3237 }
   3238 
   3239 /*
   3240  * Sync the data of an open file.
   3241  */
   3242 /* ARGSUSED */
   3243 int
   3244 sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
   3245 {
   3246 	/* {
   3247 		syscallarg(int) fd;
   3248 	} */
   3249 	struct vnode *vp;
   3250 	file_t *fp;
   3251 	int error;
   3252 
   3253 	/* fd_getvnode() will use the descriptor for us */
   3254 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3255 		return (error);
   3256 	if ((fp->f_flag & FWRITE) == 0) {
   3257 		fd_putfile(SCARG(uap, fd));
   3258 		return (EBADF);
   3259 	}
   3260 	vp = fp->f_data;
   3261 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3262 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
   3263 	VOP_UNLOCK(vp, 0);
   3264 	fd_putfile(SCARG(uap, fd));
   3265 	return (error);
   3266 }
   3267 
   3268 /*
   3269  * Rename files, (standard) BSD semantics frontend.
   3270  */
   3271 /* ARGSUSED */
   3272 int
   3273 sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
   3274 {
   3275 	/* {
   3276 		syscallarg(const char *) from;
   3277 		syscallarg(const char *) to;
   3278 	} */
   3279 
   3280 	return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 0));
   3281 }
   3282 
   3283 /*
   3284  * Rename files, POSIX semantics frontend.
   3285  */
   3286 /* ARGSUSED */
   3287 int
   3288 sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
   3289 {
   3290 	/* {
   3291 		syscallarg(const char *) from;
   3292 		syscallarg(const char *) to;
   3293 	} */
   3294 
   3295 	return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 1));
   3296 }
   3297 
   3298 /*
   3299  * Rename files.  Source and destination must either both be directories,
   3300  * or both not be directories.  If target is a directory, it must be empty.
   3301  * If `from' and `to' refer to the same object, the value of the `retain'
   3302  * argument is used to determine whether `from' will be
   3303  *
   3304  * (retain == 0)	deleted unless `from' and `to' refer to the same
   3305  *			object in the file system's name space (BSD).
   3306  * (retain == 1)	always retained (POSIX).
   3307  */
   3308 int
   3309 do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
   3310 {
   3311 	struct vnode *tvp, *fvp, *tdvp;
   3312 	struct nameidata fromnd, tond;
   3313 	struct mount *fs;
   3314 	struct lwp *l = curlwp;
   3315 	struct proc *p;
   3316 	uint32_t saveflag;
   3317 	int error;
   3318 
   3319 	NDINIT(&fromnd, DELETE, LOCKPARENT | SAVESTART | TRYEMULROOT,
   3320 	    seg, from);
   3321 	if ((error = namei(&fromnd)) != 0)
   3322 		return (error);
   3323 	if (fromnd.ni_dvp != fromnd.ni_vp)
   3324 		VOP_UNLOCK(fromnd.ni_dvp, 0);
   3325 	fvp = fromnd.ni_vp;
   3326 
   3327 	fs = fvp->v_mount;
   3328 	error = VFS_RENAMELOCK_ENTER(fs);
   3329 	if (error) {
   3330 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3331 		vrele(fromnd.ni_dvp);
   3332 		vrele(fvp);
   3333 		goto out1;
   3334 	}
   3335 
   3336 	/*
   3337 	 * close, partially, yet another race - ideally we should only
   3338 	 * go as far as getting fromnd.ni_dvp before getting the per-fs
   3339 	 * lock, and then continue to get fromnd.ni_vp, but we can't do
   3340 	 * that with namei as it stands.
   3341 	 *
   3342 	 * This still won't prevent rmdir from nuking fromnd.ni_vp
   3343 	 * under us. The real fix is to get the locks in the right
   3344 	 * order and do the lookups in the right places, but that's a
   3345 	 * major rototill.
   3346 	 *
   3347 	 * Preserve the SAVESTART in cn_flags, because who knows what
   3348 	 * might happen if we don't.
   3349 	 *
   3350 	 * Note: this logic (as well as this whole function) is cloned
   3351 	 * in nfs_serv.c. Proceed accordingly.
   3352 	 */
   3353 	vrele(fvp);
   3354 	if ((fromnd.ni_cnd.cn_namelen == 1 &&
   3355 	     fromnd.ni_cnd.cn_nameptr[0] == '.') ||
   3356 	    (fromnd.ni_cnd.cn_namelen == 2 &&
   3357 	     fromnd.ni_cnd.cn_nameptr[0] == '.' &&
   3358 	     fromnd.ni_cnd.cn_nameptr[1] == '.')) {
   3359 		error = EINVAL;
   3360 		VFS_RENAMELOCK_EXIT(fs);
   3361 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3362 		vrele(fromnd.ni_dvp);
   3363 		goto out1;
   3364 	}
   3365 	saveflag = fromnd.ni_cnd.cn_flags & SAVESTART;
   3366 	fromnd.ni_cnd.cn_flags &= ~SAVESTART;
   3367 	vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY);
   3368 	error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd);
   3369 	fromnd.ni_cnd.cn_flags |= saveflag;
   3370 	if (error) {
   3371 		VOP_UNLOCK(fromnd.ni_dvp, 0);
   3372 		VFS_RENAMELOCK_EXIT(fs);
   3373 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3374 		vrele(fromnd.ni_dvp);
   3375 		goto out1;
   3376 	}
   3377 	VOP_UNLOCK(fromnd.ni_vp, 0);
   3378 	if (fromnd.ni_dvp != fromnd.ni_vp)
   3379 		VOP_UNLOCK(fromnd.ni_dvp, 0);
   3380 	fvp = fromnd.ni_vp;
   3381 
   3382 	NDINIT(&tond, RENAME,
   3383 	    LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | TRYEMULROOT
   3384 	      | (fvp->v_type == VDIR ? CREATEDIR : 0),
   3385 	    seg, to);
   3386 	if ((error = namei(&tond)) != 0) {
   3387 		VFS_RENAMELOCK_EXIT(fs);
   3388 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3389 		vrele(fromnd.ni_dvp);
   3390 		vrele(fvp);
   3391 		goto out1;
   3392 	}
   3393 	tdvp = tond.ni_dvp;
   3394 	tvp = tond.ni_vp;
   3395 
   3396 	if (tvp != NULL) {
   3397 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   3398 			error = ENOTDIR;
   3399 			goto out;
   3400 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   3401 			error = EISDIR;
   3402 			goto out;
   3403 		}
   3404 	}
   3405 
   3406 	if (fvp == tdvp)
   3407 		error = EINVAL;
   3408 
   3409 	/*
   3410 	 * Source and destination refer to the same object.
   3411 	 */
   3412 	if (fvp == tvp) {
   3413 		if (retain)
   3414 			error = -1;
   3415 		else if (fromnd.ni_dvp == tdvp &&
   3416 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   3417 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
   3418 		          tond.ni_cnd.cn_nameptr,
   3419 		          fromnd.ni_cnd.cn_namelen))
   3420 		error = -1;
   3421 	}
   3422 
   3423 #if NVERIEXEC > 0
   3424 	if (!error) {
   3425 		char *f1, *f2;
   3426 
   3427 		f1 = malloc(fromnd.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
   3428 		strlcpy(f1, fromnd.ni_cnd.cn_nameptr, fromnd.ni_cnd.cn_namelen);
   3429 
   3430 		f2 = malloc(tond.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
   3431 		strlcpy(f2, tond.ni_cnd.cn_nameptr, tond.ni_cnd.cn_namelen);
   3432 
   3433 		error = veriexec_renamechk(l, fvp, f1, tvp, f2);
   3434 
   3435 		free(f1, M_TEMP);
   3436 		free(f2, M_TEMP);
   3437 	}
   3438 #endif /* NVERIEXEC > 0 */
   3439 
   3440 out:
   3441 	p = l->l_proc;
   3442 	if (!error) {
   3443 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   3444 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   3445 		VFS_RENAMELOCK_EXIT(fs);
   3446 	} else {
   3447 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   3448 		if (tdvp == tvp)
   3449 			vrele(tdvp);
   3450 		else
   3451 			vput(tdvp);
   3452 		if (tvp)
   3453 			vput(tvp);
   3454 		VFS_RENAMELOCK_EXIT(fs);
   3455 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3456 		vrele(fromnd.ni_dvp);
   3457 		vrele(fvp);
   3458 	}
   3459 	vrele(tond.ni_startdir);
   3460 	PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
   3461 out1:
   3462 	if (fromnd.ni_startdir)
   3463 		vrele(fromnd.ni_startdir);
   3464 	PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
   3465 	return (error == -1 ? 0 : error);
   3466 }
   3467 
   3468 /*
   3469  * Make a directory file.
   3470  */
   3471 /* ARGSUSED */
   3472 int
   3473 sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
   3474 {
   3475 	/* {
   3476 		syscallarg(const char *) path;
   3477 		syscallarg(int) mode;
   3478 	} */
   3479 	struct proc *p = l->l_proc;
   3480 	struct vnode *vp;
   3481 	struct vattr vattr;
   3482 	int error;
   3483 	struct nameidata nd;
   3484 
   3485 	NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, UIO_USERSPACE,
   3486 	    SCARG(uap, path));
   3487 	if ((error = namei(&nd)) != 0)
   3488 		return (error);
   3489 	vp = nd.ni_vp;
   3490 	if (vp != NULL) {
   3491 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3492 		if (nd.ni_dvp == vp)
   3493 			vrele(nd.ni_dvp);
   3494 		else
   3495 			vput(nd.ni_dvp);
   3496 		vrele(vp);
   3497 		return (EEXIST);
   3498 	}
   3499 	VATTR_NULL(&vattr);
   3500 	vattr.va_type = VDIR;
   3501 	/* We will read cwdi->cwdi_cmask unlocked. */
   3502 	vattr.va_mode =
   3503 	    (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   3504 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   3505 	if (!error)
   3506 		vput(nd.ni_vp);
   3507 	return (error);
   3508 }
   3509 
   3510 /*
   3511  * Remove a directory file.
   3512  */
   3513 /* ARGSUSED */
   3514 int
   3515 sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
   3516 {
   3517 	/* {
   3518 		syscallarg(const char *) path;
   3519 	} */
   3520 	struct vnode *vp;
   3521 	int error;
   3522 	struct nameidata nd;
   3523 
   3524 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
   3525 	    SCARG(uap, path));
   3526 	if ((error = namei(&nd)) != 0)
   3527 		return (error);
   3528 	vp = nd.ni_vp;
   3529 	if (vp->v_type != VDIR) {
   3530 		error = ENOTDIR;
   3531 		goto out;
   3532 	}
   3533 	/*
   3534 	 * No rmdir "." please.
   3535 	 */
   3536 	if (nd.ni_dvp == vp) {
   3537 		error = EINVAL;
   3538 		goto out;
   3539 	}
   3540 	/*
   3541 	 * The root of a mounted filesystem cannot be deleted.
   3542 	 */
   3543 	if ((vp->v_vflag & VV_ROOT) != 0 || vp->v_mountedhere != NULL) {
   3544 		error = EBUSY;
   3545 		goto out;
   3546 	}
   3547 	error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   3548 	return (error);
   3549 
   3550 out:
   3551 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3552 	if (nd.ni_dvp == vp)
   3553 		vrele(nd.ni_dvp);
   3554 	else
   3555 		vput(nd.ni_dvp);
   3556 	vput(vp);
   3557 	return (error);
   3558 }
   3559 
   3560 /*
   3561  * Read a block of directory entries in a file system independent format.
   3562  */
   3563 int
   3564 sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
   3565 {
   3566 	/* {
   3567 		syscallarg(int) fd;
   3568 		syscallarg(char *) buf;
   3569 		syscallarg(size_t) count;
   3570 	} */
   3571 	file_t *fp;
   3572 	int error, done;
   3573 
   3574 	/* fd_getvnode() will use the descriptor for us */
   3575 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3576 		return (error);
   3577 	if ((fp->f_flag & FREAD) == 0) {
   3578 		error = EBADF;
   3579 		goto out;
   3580 	}
   3581 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   3582 			SCARG(uap, count), &done, l, 0, 0);
   3583 	ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
   3584 	*retval = done;
   3585  out:
   3586 	fd_putfile(SCARG(uap, fd));
   3587 	return (error);
   3588 }
   3589 
   3590 /*
   3591  * Set the mode mask for creation of filesystem nodes.
   3592  */
   3593 int
   3594 sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
   3595 {
   3596 	/* {
   3597 		syscallarg(mode_t) newmask;
   3598 	} */
   3599 	struct proc *p = l->l_proc;
   3600 	struct cwdinfo *cwdi;
   3601 
   3602 	/*
   3603 	 * cwdi->cwdi_cmask will be read unlocked elsewhere.  What's
   3604 	 * important is that we serialize changes to the mask.  The
   3605 	 * rw_exit() will issue a write memory barrier on our behalf,
   3606 	 * and force the changes out to other CPUs (as it must use an
   3607 	 * atomic operation, draining the local CPU's store buffers).
   3608 	 */
   3609 	cwdi = p->p_cwdi;
   3610 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   3611 	*retval = cwdi->cwdi_cmask;
   3612 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
   3613 	rw_exit(&cwdi->cwdi_lock);
   3614 
   3615 	return (0);
   3616 }
   3617 
   3618 int
   3619 dorevoke(struct vnode *vp, kauth_cred_t cred)
   3620 {
   3621 	struct vattr vattr;
   3622 	int error;
   3623 
   3624 	if ((error = VOP_GETATTR(vp, &vattr, cred)) != 0)
   3625 		return error;
   3626 	if (kauth_cred_geteuid(cred) != vattr.va_uid &&
   3627 	    (error = kauth_authorize_generic(cred,
   3628 	    KAUTH_GENERIC_ISSUSER, NULL)) == 0)
   3629 		VOP_REVOKE(vp, REVOKEALL);
   3630 	return (error);
   3631 }
   3632 
   3633 /*
   3634  * Void all references to file by ripping underlying filesystem
   3635  * away from vnode.
   3636  */
   3637 /* ARGSUSED */
   3638 int
   3639 sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
   3640 {
   3641 	/* {
   3642 		syscallarg(const char *) path;
   3643 	} */
   3644 	struct vnode *vp;
   3645 	int error;
   3646 	struct nameidata nd;
   3647 
   3648 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
   3649 	    SCARG(uap, path));
   3650 	if ((error = namei(&nd)) != 0)
   3651 		return (error);
   3652 	vp = nd.ni_vp;
   3653 	error = dorevoke(vp, l->l_cred);
   3654 	vrele(vp);
   3655 	return (error);
   3656 }
   3657