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