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