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