Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.549.4.1
      1 /*	$NetBSD: vfs_syscalls.c,v 1.549.4.1 2021/08/01 22:42:39 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009, 2019, 2020 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 /*
     69  * Virtual File System System Calls
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.549.4.1 2021/08/01 22:42:39 thorpej Exp $");
     74 
     75 #ifdef _KERNEL_OPT
     76 #include "opt_fileassoc.h"
     77 #include "veriexec.h"
     78 #endif
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/namei.h>
     83 #include <sys/filedesc.h>
     84 #include <sys/kernel.h>
     85 #include <sys/file.h>
     86 #include <sys/fcntl.h>
     87 #include <sys/stat.h>
     88 #include <sys/vnode.h>
     89 #include <sys/mount.h>
     90 #include <sys/fstrans.h>
     91 #include <sys/proc.h>
     92 #include <sys/uio.h>
     93 #include <sys/kmem.h>
     94 #include <sys/dirent.h>
     95 #include <sys/sysctl.h>
     96 #include <sys/syscallargs.h>
     97 #include <sys/vfs_syscalls.h>
     98 #include <sys/quota.h>
     99 #include <sys/quotactl.h>
    100 #include <sys/ktrace.h>
    101 #ifdef FILEASSOC
    102 #include <sys/fileassoc.h>
    103 #endif /* FILEASSOC */
    104 #include <sys/extattr.h>
    105 #include <sys/verified_exec.h>
    106 #include <sys/kauth.h>
    107 #include <sys/atomic.h>
    108 #include <sys/module.h>
    109 #include <sys/buf.h>
    110 #include <sys/event.h>
    111 #include <sys/compat_stub.h>
    112 
    113 #include <miscfs/genfs/genfs.h>
    114 #include <miscfs/specfs/specdev.h>
    115 
    116 #include <nfs/rpcv2.h>
    117 #include <nfs/nfsproto.h>
    118 #include <nfs/nfs.h>
    119 #include <nfs/nfs_var.h>
    120 
    121 /* XXX this shouldn't be here */
    122 #ifndef OFF_T_MAX
    123 #define OFF_T_MAX __type_max(off_t)
    124 #endif
    125 
    126 static int change_flags(struct vnode *, u_long, struct lwp *);
    127 static int change_mode(struct vnode *, int, struct lwp *);
    128 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
    129 static int do_sys_openat(lwp_t *, int, const char *, int, int, int *);
    130 static int do_sys_mkdirat(struct lwp *l, int, const char *, mode_t,
    131     enum uio_seg);
    132 static int do_sys_mkfifoat(struct lwp *, int, const char *, mode_t);
    133 static int do_sys_symlinkat(struct lwp *, const char *, int, const char *,
    134     enum uio_seg);
    135 static int do_sys_renameat(struct lwp *l, int, const char *, int, const char *,
    136     enum uio_seg, int);
    137 static int do_sys_readlinkat(struct lwp *, int, const char *, char *,
    138     size_t, register_t *);
    139 static int do_sys_unlinkat(struct lwp *, int, const char *, int, enum uio_seg);
    140 
    141 static int fd_nameiat(struct lwp *, int, struct nameidata *);
    142 static int fd_nameiat_simple_user(struct lwp *, int, const char *,
    143     namei_simple_flags_t, struct vnode **);
    144 
    145 /*
    146  * This table is used to maintain compatibility with 4.3BSD
    147  * and NetBSD 0.9 mount syscalls - and possibly other systems.
    148  * Note, the order is important!
    149  *
    150  * Do not modify this table. It should only contain filesystems
    151  * supported by NetBSD 0.9 and 4.3BSD.
    152  */
    153 const char * const mountcompatnames[] = {
    154 	NULL,		/* 0 = MOUNT_NONE */
    155 	MOUNT_FFS,	/* 1 = MOUNT_UFS */
    156 	MOUNT_NFS,	/* 2 */
    157 	MOUNT_MFS,	/* 3 */
    158 	MOUNT_MSDOS,	/* 4 */
    159 	MOUNT_CD9660,	/* 5 = MOUNT_ISOFS */
    160 	MOUNT_FDESC,	/* 6 */
    161 	MOUNT_KERNFS,	/* 7 */
    162 	NULL,		/* 8 = MOUNT_DEVFS */
    163 	MOUNT_AFS,	/* 9 */
    164 };
    165 
    166 const u_int nmountcompatnames = __arraycount(mountcompatnames);
    167 
    168 static int
    169 fd_nameiat(struct lwp *l, int fdat, struct nameidata *ndp)
    170 {
    171 	file_t *dfp;
    172 	int error;
    173 
    174 	if (fdat != AT_FDCWD) {
    175 		if ((error = fd_getvnode(fdat, &dfp)) != 0)
    176 			goto out;
    177 
    178 		NDAT(ndp, dfp->f_vnode);
    179 	}
    180 
    181 	error = namei(ndp);
    182 
    183 	if (fdat != AT_FDCWD)
    184 		fd_putfile(fdat);
    185 out:
    186 	return error;
    187 }
    188 
    189 static int
    190 fd_nameiat_simple_user(struct lwp *l, int fdat, const char *path,
    191     namei_simple_flags_t sflags, struct vnode **vp_ret)
    192 {
    193 	file_t *dfp;
    194 	struct vnode *dvp;
    195 	int error;
    196 
    197 	if (fdat != AT_FDCWD) {
    198 		if ((error = fd_getvnode(fdat, &dfp)) != 0)
    199 			goto out;
    200 
    201 		dvp = dfp->f_vnode;
    202 	} else {
    203 		dvp = NULL;
    204 	}
    205 
    206 	error = nameiat_simple_user(dvp, path, sflags, vp_ret);
    207 
    208 	if (fdat != AT_FDCWD)
    209 		fd_putfile(fdat);
    210 out:
    211 	return error;
    212 }
    213 
    214 static int
    215 open_setfp(struct lwp *l, file_t *fp, struct vnode *vp, int indx, int flags)
    216 {
    217 	int error;
    218 
    219 	fp->f_flag = flags & FMASK;
    220 	fp->f_type = DTYPE_VNODE;
    221 	fp->f_ops = &vnops;
    222 	fp->f_vnode = vp;
    223 
    224 	if (flags & (O_EXLOCK | O_SHLOCK)) {
    225 		struct flock lf;
    226 		int type;
    227 
    228 		lf.l_whence = SEEK_SET;
    229 		lf.l_start = 0;
    230 		lf.l_len = 0;
    231 		if (flags & O_EXLOCK)
    232 			lf.l_type = F_WRLCK;
    233 		else
    234 			lf.l_type = F_RDLCK;
    235 		type = F_FLOCK;
    236 		if ((flags & FNONBLOCK) == 0)
    237 			type |= F_WAIT;
    238 		VOP_UNLOCK(vp);
    239 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
    240 		if (error) {
    241 			(void) vn_close(vp, fp->f_flag, fp->f_cred);
    242 			fd_abort(l->l_proc, fp, indx);
    243 			return error;
    244 		}
    245 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    246 		atomic_or_uint(&fp->f_flag, FHASLOCK);
    247 	}
    248 	if (flags & O_CLOEXEC)
    249 		fd_set_exclose(l, indx, true);
    250 	return 0;
    251 }
    252 
    253 static int
    254 mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
    255     void *data, size_t *data_len)
    256 {
    257 	struct mount *mp;
    258 	int error = 0, saved_flags;
    259 
    260 	mp = vp->v_mount;
    261 	saved_flags = mp->mnt_flag;
    262 
    263 	/* We can operate only on VV_ROOT nodes. */
    264 	if ((vp->v_vflag & VV_ROOT) == 0) {
    265 		error = EINVAL;
    266 		goto out;
    267 	}
    268 
    269 	/*
    270 	 * We only allow the filesystem to be reloaded if it
    271 	 * is currently mounted read-only.  Additionally, we
    272 	 * prevent read-write to read-only downgrades.
    273 	 */
    274 	if ((flags & (MNT_RELOAD | MNT_RDONLY)) != 0 &&
    275 	    (mp->mnt_flag & MNT_RDONLY) == 0 &&
    276 	    (mp->mnt_iflag & IMNT_CAN_RWTORO) == 0) {
    277 		error = EOPNOTSUPP;	/* Needs translation */
    278 		goto out;
    279 	}
    280 
    281 	/*
    282 	 * Enabling MNT_UNION requires a covered mountpoint and
    283 	 * must not happen on the root mount.
    284 	 */
    285 	if ((flags & MNT_UNION) != 0 && mp->mnt_vnodecovered == NULLVP) {
    286 		error = EOPNOTSUPP;
    287 		goto out;
    288 	}
    289 
    290 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    291 	    KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
    292 	if (error)
    293 		goto out;
    294 
    295 	error = vfs_suspend(mp, 0);
    296 	if (error)
    297 		goto out;
    298 
    299 	mutex_enter(mp->mnt_updating);
    300 
    301 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    302 	mp->mnt_flag |= flags & MNT_OP_FLAGS;
    303 
    304 	/*
    305 	 * Set the mount level flags.
    306 	 */
    307 	if ((flags & MNT_RDONLY) != (mp->mnt_flag & MNT_RDONLY)) {
    308 		if ((flags & MNT_RDONLY))
    309 			mp->mnt_iflag |= IMNT_WANTRDONLY;
    310 		else
    311 			mp->mnt_iflag |= IMNT_WANTRDWR;
    312 	}
    313 	mp->mnt_flag &= ~MNT_BASIC_FLAGS;
    314 	mp->mnt_flag |= flags & MNT_BASIC_FLAGS;
    315 	if ((mp->mnt_iflag & IMNT_WANTRDONLY))
    316 		mp->mnt_flag &= ~MNT_RDONLY;
    317 
    318 	error = VFS_MOUNT(mp, path, data, data_len);
    319 
    320 	if (error && data != NULL) {
    321 		int error2;
    322 
    323 		/*
    324 		 * Update failed; let's try and see if it was an
    325 		 * export request.  For compat with 3.0 and earlier.
    326 		 */
    327 		error2 = vfs_hooks_reexport(mp, path, data);
    328 
    329 		/*
    330 		 * Only update error code if the export request was
    331 		 * understood but some problem occurred while
    332 		 * processing it.
    333 		 */
    334 		if (error2 != EJUSTRETURN)
    335 			error = error2;
    336 	}
    337 
    338 	if (error == 0 && (mp->mnt_iflag & IMNT_WANTRDONLY))
    339 		mp->mnt_flag |= MNT_RDONLY;
    340 	if (error)
    341 		mp->mnt_flag = saved_flags;
    342 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    343 	mp->mnt_iflag &= ~(IMNT_WANTRDONLY | IMNT_WANTRDWR);
    344 	if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
    345 		if ((mp->mnt_iflag & IMNT_ONWORKLIST) == 0)
    346 			vfs_syncer_add_to_worklist(mp);
    347 	} else {
    348 		if ((mp->mnt_iflag & IMNT_ONWORKLIST) != 0)
    349 			vfs_syncer_remove_from_worklist(mp);
    350 	}
    351 	mutex_exit(mp->mnt_updating);
    352 	vfs_resume(mp);
    353 
    354 	if ((error == 0) && !(saved_flags & MNT_EXTATTR) &&
    355 	    (flags & MNT_EXTATTR)) {
    356 		if (VFS_EXTATTRCTL(mp, EXTATTR_CMD_START,
    357 				   NULL, 0, NULL) != 0) {
    358 			printf("%s: failed to start extattr, error = %d",
    359 			       mp->mnt_stat.f_mntonname, error);
    360 			mp->mnt_flag &= ~MNT_EXTATTR;
    361 		}
    362 	}
    363 
    364 	if ((error == 0) && (saved_flags & MNT_EXTATTR) &&
    365 	    !(flags & MNT_EXTATTR)) {
    366 		if (VFS_EXTATTRCTL(mp, EXTATTR_CMD_STOP,
    367 				   NULL, 0, NULL) != 0) {
    368 			printf("%s: failed to stop extattr, error = %d",
    369 			       mp->mnt_stat.f_mntonname, error);
    370 			mp->mnt_flag |= MNT_RDONLY;
    371 		}
    372 	}
    373  out:
    374 	return (error);
    375 }
    376 
    377 static int
    378 mount_get_vfsops(const char *fstype, enum uio_seg type_seg,
    379     struct vfsops **vfsops)
    380 {
    381 	char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
    382 	int error;
    383 
    384 	if (type_seg == UIO_USERSPACE) {
    385 		/* Copy file-system type from userspace.  */
    386 		error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
    387 	} else {
    388 		error = copystr(fstype, fstypename, sizeof(fstypename), NULL);
    389 		KASSERT(error == 0);
    390 	}
    391 
    392 	if (error) {
    393 		/*
    394 		 * Historically, filesystem types were identified by numbers.
    395 		 * If we get an integer for the filesystem type instead of a
    396 		 * string, we check to see if it matches one of the historic
    397 		 * filesystem types.
    398 		 */
    399 		u_long fsindex = (u_long)fstype;
    400 		if (fsindex >= nmountcompatnames ||
    401 		    mountcompatnames[fsindex] == NULL)
    402 			return ENODEV;
    403 		strlcpy(fstypename, mountcompatnames[fsindex],
    404 		    sizeof(fstypename));
    405 	}
    406 
    407 	/* Accept `ufs' as an alias for `ffs', for compatibility. */
    408 	if (strcmp(fstypename, "ufs") == 0)
    409 		fstypename[0] = 'f';
    410 
    411 	if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
    412 		return 0;
    413 
    414 	/* If we can autoload a vfs module, try again */
    415 	(void)module_autoload(fstypename, MODULE_CLASS_VFS);
    416 
    417 	if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
    418 		return 0;
    419 
    420 	return ENODEV;
    421 }
    422 
    423 static int
    424 mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
    425     void *data, size_t *data_len)
    426 {
    427 	struct mount *mp;
    428 	int error;
    429 
    430 	/* If MNT_GETARGS is specified, it should be the only flag. */
    431 	if (flags & ~MNT_GETARGS)
    432 		return EINVAL;
    433 
    434 	mp = vp->v_mount;
    435 
    436 	/* XXX: probably some notion of "can see" here if we want isolation. */
    437 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    438 	    KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
    439 	if (error)
    440 		return error;
    441 
    442 	if ((vp->v_vflag & VV_ROOT) == 0)
    443 		return EINVAL;
    444 
    445 	if (vfs_busy(mp))
    446 		return EPERM;
    447 
    448 	mutex_enter(mp->mnt_updating);
    449 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    450 	mp->mnt_flag |= MNT_GETARGS;
    451 	error = VFS_MOUNT(mp, path, data, data_len);
    452 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    453 	mutex_exit(mp->mnt_updating);
    454 
    455 	vfs_unbusy(mp);
    456 	return (error);
    457 }
    458 
    459 int
    460 sys___mount50(struct lwp *l, const struct sys___mount50_args *uap, register_t *retval)
    461 {
    462 	/* {
    463 		syscallarg(const char *) type;
    464 		syscallarg(const char *) path;
    465 		syscallarg(int) flags;
    466 		syscallarg(void *) data;
    467 		syscallarg(size_t) data_len;
    468 	} */
    469 
    470 	return do_sys_mount(l, SCARG(uap, type), UIO_USERSPACE, SCARG(uap, path),
    471 	    SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
    472 	    SCARG(uap, data_len), retval);
    473 }
    474 
    475 int
    476 do_sys_mount(struct lwp *l, const char *type, enum uio_seg type_seg,
    477     const char *path, int flags, void *data, enum uio_seg data_seg,
    478     size_t data_len, register_t *retval)
    479 {
    480 	struct vfsops *vfsops = NULL;	/* XXX gcc4.8 */
    481 	struct vnode *vp;
    482 	void *data_buf = data;
    483 	bool vfsopsrele = false;
    484 	size_t alloc_sz = 0;
    485 	int error;
    486 
    487 	/*
    488 	 * Get vnode to be covered
    489 	 */
    490 	error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
    491 	if (error != 0) {
    492 		vp = NULL;
    493 		goto done;
    494 	}
    495 
    496 	if (flags & (MNT_GETARGS | MNT_UPDATE)) {
    497 		vfsops = vp->v_mount->mnt_op;
    498 	} else {
    499 		/* 'type' is userspace */
    500 		error = mount_get_vfsops(type, type_seg, &vfsops);
    501 		if (error != 0)
    502 			goto done;
    503 		vfsopsrele = true;
    504 	}
    505 
    506 	/*
    507 	 * We allow data to be NULL, even for userspace. Some fs's don't need
    508 	 * it. The others will handle NULL.
    509 	 */
    510 	if (data != NULL && data_seg == UIO_USERSPACE) {
    511 		if (data_len == 0) {
    512 			/* No length supplied, use default for filesystem */
    513 			data_len = vfsops->vfs_min_mount_data;
    514 
    515 			/*
    516 			 * Hopefully a longer buffer won't make copyin() fail.
    517 			 * For compatibility with 3.0 and earlier.
    518 			 */
    519 			if (flags & MNT_UPDATE
    520 			    && data_len < sizeof (struct mnt_export_args30))
    521 				data_len = sizeof (struct mnt_export_args30);
    522 		}
    523 		if ((data_len == 0) || (data_len > VFS_MAX_MOUNT_DATA)) {
    524 			error = EINVAL;
    525 			goto done;
    526 		}
    527 		alloc_sz = data_len;
    528 		data_buf = kmem_alloc(alloc_sz, KM_SLEEP);
    529 
    530 		/* NFS needs the buffer even for mnt_getargs .... */
    531 		error = copyin(data, data_buf, data_len);
    532 		if (error != 0)
    533 			goto done;
    534 	}
    535 
    536 	if (flags & MNT_GETARGS) {
    537 		if (data_len == 0) {
    538 			error = EINVAL;
    539 			goto done;
    540 		}
    541 		error = mount_getargs(l, vp, path, flags, data_buf, &data_len);
    542 		if (error != 0)
    543 			goto done;
    544 		if (data_seg == UIO_USERSPACE)
    545 			error = copyout(data_buf, data, data_len);
    546 		*retval = data_len;
    547 	} else if (flags & MNT_UPDATE) {
    548 		error = mount_update(l, vp, path, flags, data_buf, &data_len);
    549 	} else {
    550 		/* Locking is handled internally in mount_domount(). */
    551 		KASSERT(vfsopsrele == true);
    552 		error = mount_domount(l, &vp, vfsops, path, flags, data_buf,
    553 		    &data_len);
    554 		vfsopsrele = false;
    555 	}
    556 	if (!error)
    557 		KNOTE(&fs_klist, VQ_MOUNT);
    558 
    559     done:
    560 	if (vfsopsrele)
    561 		vfs_delref(vfsops);
    562     	if (vp != NULL) {
    563 	    	vrele(vp);
    564 	}
    565 	if (data_buf != data)
    566 		kmem_free(data_buf, alloc_sz);
    567 	return (error);
    568 }
    569 
    570 /*
    571  * Unmount a file system.
    572  *
    573  * Note: unmount takes a path to the vnode mounted on as argument,
    574  * not special file (as before).
    575  */
    576 /* ARGSUSED */
    577 int
    578 sys_unmount(struct lwp *l, const struct sys_unmount_args *uap, register_t *retval)
    579 {
    580 	/* {
    581 		syscallarg(const char *) path;
    582 		syscallarg(int) flags;
    583 	} */
    584 	struct vnode *vp;
    585 	struct mount *mp;
    586 	int error;
    587 	struct pathbuf *pb;
    588 	struct nameidata nd;
    589 
    590 	error = pathbuf_copyin(SCARG(uap, path), &pb);
    591 	if (error) {
    592 		return error;
    593 	}
    594 
    595 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, pb);
    596 	if ((error = namei(&nd)) != 0) {
    597 		pathbuf_destroy(pb);
    598 		return error;
    599 	}
    600 	vp = nd.ni_vp;
    601 	pathbuf_destroy(pb);
    602 
    603 	mp = vp->v_mount;
    604 	vfs_ref(mp);
    605 	VOP_UNLOCK(vp);
    606 
    607 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    608 	    KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL);
    609 	if (error) {
    610 		vrele(vp);
    611 		vfs_rele(mp);
    612 		return (error);
    613 	}
    614 
    615 	/*
    616 	 * Don't allow unmounting the root file system.
    617 	 */
    618 	if (mp->mnt_flag & MNT_ROOTFS) {
    619 		vrele(vp);
    620 		vfs_rele(mp);
    621 		return (EINVAL);
    622 	}
    623 
    624 	/*
    625 	 * Must be the root of the filesystem
    626 	 */
    627 	if ((vp->v_vflag & VV_ROOT) == 0) {
    628 		vrele(vp);
    629 		vfs_rele(mp);
    630 		return (EINVAL);
    631 	}
    632 
    633 	vrele(vp);
    634 	error = dounmount(mp, SCARG(uap, flags), l);
    635 	vfs_rele(mp);
    636 	if (!error)
    637 		KNOTE(&fs_klist, VQ_UNMOUNT);
    638 	return error;
    639 }
    640 
    641 /*
    642  * Sync each mounted filesystem.
    643  */
    644 #ifdef DEBUG
    645 int syncprt = 0;
    646 struct ctldebug debug0 = { "syncprt", &syncprt };
    647 #endif
    648 
    649 void
    650 do_sys_sync(struct lwp *l)
    651 {
    652 	mount_iterator_t *iter;
    653 	struct mount *mp;
    654 	int asyncflag;
    655 
    656 	mountlist_iterator_init(&iter);
    657 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
    658 		mutex_enter(mp->mnt_updating);
    659 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    660 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    661 			mp->mnt_flag &= ~MNT_ASYNC;
    662 			VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
    663 			if (asyncflag)
    664 				 mp->mnt_flag |= MNT_ASYNC;
    665 		}
    666 		mutex_exit(mp->mnt_updating);
    667 	}
    668 	mountlist_iterator_destroy(iter);
    669 #ifdef DEBUG
    670 	if (syncprt)
    671 		vfs_bufstats();
    672 #endif /* DEBUG */
    673 }
    674 
    675 static bool
    676 sync_vnode_filter(void *cookie, vnode_t *vp)
    677 {
    678 
    679 	if (vp->v_numoutput > 0) {
    680 		++*(int *)cookie;
    681 	}
    682 	return false;
    683 }
    684 
    685 int
    686 vfs_syncwait(void)
    687 {
    688 	int nbusy, nbusy_prev, iter;
    689 	struct vnode_iterator *vniter;
    690 	mount_iterator_t *mpiter;
    691 	struct mount *mp;
    692 
    693 	for (nbusy_prev = 0, iter = 0; iter < 20;) {
    694 		nbusy = 0;
    695 		mountlist_iterator_init(&mpiter);
    696 		while ((mp = mountlist_iterator_next(mpiter)) != NULL) {
    697 			vnode_t *vp __diagused;
    698 			vfs_vnode_iterator_init(mp, &vniter);
    699 			vp = vfs_vnode_iterator_next(vniter,
    700 			    sync_vnode_filter, &nbusy);
    701 			KASSERT(vp == NULL);
    702 			vfs_vnode_iterator_destroy(vniter);
    703 		}
    704 		mountlist_iterator_destroy(mpiter);
    705 
    706 		if (nbusy == 0)
    707 			break;
    708 		if (nbusy_prev == 0)
    709 			nbusy_prev = nbusy;
    710 		printf("%d ", nbusy);
    711 		kpause("syncwait", false, MAX(1, hz / 25 * iter), NULL);
    712 		if (nbusy >= nbusy_prev) /* we didn't flush anything */
    713 			iter++;
    714 		else
    715 			nbusy_prev = nbusy;
    716 	}
    717 
    718 	if (nbusy) {
    719 #if defined(DEBUG) || defined(DEBUG_HALT_BUSY)
    720 		printf("giving up\nPrinting vnodes for busy buffers\n");
    721 		mountlist_iterator_init(&mpiter);
    722 		while ((mp = mountlist_iterator_next(mpiter)) != NULL) {
    723 			vnode_t *vp;
    724 			vfs_vnode_iterator_init(mp, &vniter);
    725 			vp = vfs_vnode_iterator_next(vniter,
    726 			    NULL, NULL);
    727 			mutex_enter(vp->v_interlock);
    728 			if (vp->v_numoutput > 0)
    729 				vprint(NULL, vp);
    730 			mutex_exit(vp->v_interlock);
    731 			vrele(vp);
    732 			vfs_vnode_iterator_destroy(vniter);
    733 		}
    734 		mountlist_iterator_destroy(mpiter);
    735 #endif
    736 	}
    737 
    738 	return nbusy;
    739 }
    740 
    741 /* ARGSUSED */
    742 int
    743 sys_sync(struct lwp *l, const void *v, register_t *retval)
    744 {
    745 	do_sys_sync(l);
    746 	return (0);
    747 }
    748 
    749 
    750 /*
    751  * Access or change filesystem quotas.
    752  *
    753  * (this is really 14 different calls bundled into one)
    754  */
    755 
    756 static int
    757 do_sys_quotactl_stat(struct mount *mp, struct quotastat *info_u)
    758 {
    759 	struct quotastat info_k;
    760 	int error;
    761 
    762 	/* ensure any padding bytes are cleared */
    763 	memset(&info_k, 0, sizeof(info_k));
    764 
    765 	error = vfs_quotactl_stat(mp, &info_k);
    766 	if (error) {
    767 		return error;
    768 	}
    769 
    770 	return copyout(&info_k, info_u, sizeof(info_k));
    771 }
    772 
    773 static int
    774 do_sys_quotactl_idtypestat(struct mount *mp, int idtype,
    775     struct quotaidtypestat *info_u)
    776 {
    777 	struct quotaidtypestat info_k;
    778 	int error;
    779 
    780 	/* ensure any padding bytes are cleared */
    781 	memset(&info_k, 0, sizeof(info_k));
    782 
    783 	error = vfs_quotactl_idtypestat(mp, idtype, &info_k);
    784 	if (error) {
    785 		return error;
    786 	}
    787 
    788 	return copyout(&info_k, info_u, sizeof(info_k));
    789 }
    790 
    791 static int
    792 do_sys_quotactl_objtypestat(struct mount *mp, int objtype,
    793     struct quotaobjtypestat *info_u)
    794 {
    795 	struct quotaobjtypestat info_k;
    796 	int error;
    797 
    798 	/* ensure any padding bytes are cleared */
    799 	memset(&info_k, 0, sizeof(info_k));
    800 
    801 	error = vfs_quotactl_objtypestat(mp, objtype, &info_k);
    802 	if (error) {
    803 		return error;
    804 	}
    805 
    806 	return copyout(&info_k, info_u, sizeof(info_k));
    807 }
    808 
    809 static int
    810 do_sys_quotactl_get(struct mount *mp, const struct quotakey *key_u,
    811     struct quotaval *val_u)
    812 {
    813 	struct quotakey key_k;
    814 	struct quotaval val_k;
    815 	int error;
    816 
    817 	/* ensure any padding bytes are cleared */
    818 	memset(&val_k, 0, sizeof(val_k));
    819 
    820 	error = copyin(key_u, &key_k, sizeof(key_k));
    821 	if (error) {
    822 		return error;
    823 	}
    824 
    825 	error = vfs_quotactl_get(mp, &key_k, &val_k);
    826 	if (error) {
    827 		return error;
    828 	}
    829 
    830 	return copyout(&val_k, val_u, sizeof(val_k));
    831 }
    832 
    833 static int
    834 do_sys_quotactl_put(struct mount *mp, const struct quotakey *key_u,
    835     const struct quotaval *val_u)
    836 {
    837 	struct quotakey key_k;
    838 	struct quotaval val_k;
    839 	int error;
    840 
    841 	error = copyin(key_u, &key_k, sizeof(key_k));
    842 	if (error) {
    843 		return error;
    844 	}
    845 
    846 	error = copyin(val_u, &val_k, sizeof(val_k));
    847 	if (error) {
    848 		return error;
    849 	}
    850 
    851 	return vfs_quotactl_put(mp, &key_k, &val_k);
    852 }
    853 
    854 static int
    855 do_sys_quotactl_del(struct mount *mp, const struct quotakey *key_u)
    856 {
    857 	struct quotakey key_k;
    858 	int error;
    859 
    860 	error = copyin(key_u, &key_k, sizeof(key_k));
    861 	if (error) {
    862 		return error;
    863 	}
    864 
    865 	return vfs_quotactl_del(mp, &key_k);
    866 }
    867 
    868 static int
    869 do_sys_quotactl_cursoropen(struct mount *mp, struct quotakcursor *cursor_u)
    870 {
    871 	struct quotakcursor cursor_k;
    872 	int error;
    873 
    874 	/* ensure any padding bytes are cleared */
    875 	memset(&cursor_k, 0, sizeof(cursor_k));
    876 
    877 	error = vfs_quotactl_cursoropen(mp, &cursor_k);
    878 	if (error) {
    879 		return error;
    880 	}
    881 
    882 	return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
    883 }
    884 
    885 static int
    886 do_sys_quotactl_cursorclose(struct mount *mp, struct quotakcursor *cursor_u)
    887 {
    888 	struct quotakcursor cursor_k;
    889 	int error;
    890 
    891 	error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
    892 	if (error) {
    893 		return error;
    894 	}
    895 
    896 	return vfs_quotactl_cursorclose(mp, &cursor_k);
    897 }
    898 
    899 static int
    900 do_sys_quotactl_cursorskipidtype(struct mount *mp,
    901     struct quotakcursor *cursor_u, int idtype)
    902 {
    903 	struct quotakcursor cursor_k;
    904 	int error;
    905 
    906 	error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
    907 	if (error) {
    908 		return error;
    909 	}
    910 
    911 	error = vfs_quotactl_cursorskipidtype(mp, &cursor_k, idtype);
    912 	if (error) {
    913 		return error;
    914 	}
    915 
    916 	return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
    917 }
    918 
    919 static int
    920 do_sys_quotactl_cursorget(struct mount *mp, struct quotakcursor *cursor_u,
    921     struct quotakey *keys_u, struct quotaval *vals_u, unsigned maxnum,
    922     unsigned *ret_u)
    923 {
    924 #define CGET_STACK_MAX 8
    925 	struct quotakcursor cursor_k;
    926 	struct quotakey stackkeys[CGET_STACK_MAX];
    927 	struct quotaval stackvals[CGET_STACK_MAX];
    928 	struct quotakey *keys_k;
    929 	struct quotaval *vals_k;
    930 	unsigned ret_k;
    931 	int error;
    932 
    933 	if (maxnum > 128) {
    934 		maxnum = 128;
    935 	}
    936 
    937 	error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
    938 	if (error) {
    939 		return error;
    940 	}
    941 
    942 	if (maxnum <= CGET_STACK_MAX) {
    943 		keys_k = stackkeys;
    944 		vals_k = stackvals;
    945 		/* ensure any padding bytes are cleared */
    946 		memset(keys_k, 0, maxnum * sizeof(keys_k[0]));
    947 		memset(vals_k, 0, maxnum * sizeof(vals_k[0]));
    948 	} else {
    949 		keys_k = kmem_zalloc(maxnum * sizeof(keys_k[0]), KM_SLEEP);
    950 		vals_k = kmem_zalloc(maxnum * sizeof(vals_k[0]), KM_SLEEP);
    951 	}
    952 
    953 	error = vfs_quotactl_cursorget(mp, &cursor_k, keys_k, vals_k, maxnum,
    954 				       &ret_k);
    955 	if (error) {
    956 		goto fail;
    957 	}
    958 
    959 	error = copyout(keys_k, keys_u, ret_k * sizeof(keys_k[0]));
    960 	if (error) {
    961 		goto fail;
    962 	}
    963 
    964 	error = copyout(vals_k, vals_u, ret_k * sizeof(vals_k[0]));
    965 	if (error) {
    966 		goto fail;
    967 	}
    968 
    969 	error = copyout(&ret_k, ret_u, sizeof(ret_k));
    970 	if (error) {
    971 		goto fail;
    972 	}
    973 
    974 	/* do last to maximize the chance of being able to recover a failure */
    975 	error = copyout(&cursor_k, cursor_u, sizeof(cursor_k));
    976 
    977 fail:
    978 	if (keys_k != stackkeys) {
    979 		kmem_free(keys_k, maxnum * sizeof(keys_k[0]));
    980 	}
    981 	if (vals_k != stackvals) {
    982 		kmem_free(vals_k, maxnum * sizeof(vals_k[0]));
    983 	}
    984 	return error;
    985 }
    986 
    987 static int
    988 do_sys_quotactl_cursoratend(struct mount *mp, struct quotakcursor *cursor_u,
    989     int *ret_u)
    990 {
    991 	struct quotakcursor cursor_k;
    992 	int ret_k;
    993 	int error;
    994 
    995 	error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
    996 	if (error) {
    997 		return error;
    998 	}
    999 
   1000 	error = vfs_quotactl_cursoratend(mp, &cursor_k, &ret_k);
   1001 	if (error) {
   1002 		return error;
   1003 	}
   1004 
   1005 	error = copyout(&ret_k, ret_u, sizeof(ret_k));
   1006 	if (error) {
   1007 		return error;
   1008 	}
   1009 
   1010 	return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
   1011 }
   1012 
   1013 static int
   1014 do_sys_quotactl_cursorrewind(struct mount *mp, struct quotakcursor *cursor_u)
   1015 {
   1016 	struct quotakcursor cursor_k;
   1017 	int error;
   1018 
   1019 	error = copyin(cursor_u, &cursor_k, sizeof(cursor_k));
   1020 	if (error) {
   1021 		return error;
   1022 	}
   1023 
   1024 	error = vfs_quotactl_cursorrewind(mp, &cursor_k);
   1025 	if (error) {
   1026 		return error;
   1027 	}
   1028 
   1029 	return copyout(&cursor_k, cursor_u, sizeof(cursor_k));
   1030 }
   1031 
   1032 static int
   1033 do_sys_quotactl_quotaon(struct mount *mp, int idtype, const char *path_u)
   1034 {
   1035 	char *path_k;
   1036 	int error;
   1037 
   1038 	/* XXX this should probably be a struct pathbuf */
   1039 	path_k = PNBUF_GET();
   1040 	error = copyin(path_u, path_k, PATH_MAX);
   1041 	if (error) {
   1042 		PNBUF_PUT(path_k);
   1043 		return error;
   1044 	}
   1045 
   1046 	error = vfs_quotactl_quotaon(mp, idtype, path_k);
   1047 
   1048 	PNBUF_PUT(path_k);
   1049 	return error;
   1050 }
   1051 
   1052 static int
   1053 do_sys_quotactl_quotaoff(struct mount *mp, int idtype)
   1054 {
   1055 	return vfs_quotactl_quotaoff(mp, idtype);
   1056 }
   1057 
   1058 int
   1059 do_sys_quotactl(const char *path_u, const struct quotactl_args *args)
   1060 {
   1061 	struct mount *mp;
   1062 	struct vnode *vp;
   1063 	int error;
   1064 
   1065 	error = namei_simple_user(path_u, NSM_FOLLOW_TRYEMULROOT, &vp);
   1066 	if (error != 0)
   1067 		return (error);
   1068 	mp = vp->v_mount;
   1069 
   1070 	switch (args->qc_op) {
   1071 	    case QUOTACTL_STAT:
   1072 		error = do_sys_quotactl_stat(mp, args->u.stat.qc_info);
   1073 		break;
   1074 	    case QUOTACTL_IDTYPESTAT:
   1075 		error = do_sys_quotactl_idtypestat(mp,
   1076 				args->u.idtypestat.qc_idtype,
   1077 				args->u.idtypestat.qc_info);
   1078 		break;
   1079 	    case QUOTACTL_OBJTYPESTAT:
   1080 		error = do_sys_quotactl_objtypestat(mp,
   1081 				args->u.objtypestat.qc_objtype,
   1082 				args->u.objtypestat.qc_info);
   1083 		break;
   1084 	    case QUOTACTL_GET:
   1085 		error = do_sys_quotactl_get(mp,
   1086 				args->u.get.qc_key,
   1087 				args->u.get.qc_val);
   1088 		break;
   1089 	    case QUOTACTL_PUT:
   1090 		error = do_sys_quotactl_put(mp,
   1091 				args->u.put.qc_key,
   1092 				args->u.put.qc_val);
   1093 		break;
   1094 	    case QUOTACTL_DEL:
   1095 		error = do_sys_quotactl_del(mp, args->u.del.qc_key);
   1096 		break;
   1097 	    case QUOTACTL_CURSOROPEN:
   1098 		error = do_sys_quotactl_cursoropen(mp,
   1099 				args->u.cursoropen.qc_cursor);
   1100 		break;
   1101 	    case QUOTACTL_CURSORCLOSE:
   1102 		error = do_sys_quotactl_cursorclose(mp,
   1103 				args->u.cursorclose.qc_cursor);
   1104 		break;
   1105 	    case QUOTACTL_CURSORSKIPIDTYPE:
   1106 		error = do_sys_quotactl_cursorskipidtype(mp,
   1107 				args->u.cursorskipidtype.qc_cursor,
   1108 				args->u.cursorskipidtype.qc_idtype);
   1109 		break;
   1110 	    case QUOTACTL_CURSORGET:
   1111 		error = do_sys_quotactl_cursorget(mp,
   1112 				args->u.cursorget.qc_cursor,
   1113 				args->u.cursorget.qc_keys,
   1114 				args->u.cursorget.qc_vals,
   1115 				args->u.cursorget.qc_maxnum,
   1116 				args->u.cursorget.qc_ret);
   1117 		break;
   1118 	    case QUOTACTL_CURSORATEND:
   1119 		error = do_sys_quotactl_cursoratend(mp,
   1120 				args->u.cursoratend.qc_cursor,
   1121 				args->u.cursoratend.qc_ret);
   1122 		break;
   1123 	    case QUOTACTL_CURSORREWIND:
   1124 		error = do_sys_quotactl_cursorrewind(mp,
   1125 				args->u.cursorrewind.qc_cursor);
   1126 		break;
   1127 	    case QUOTACTL_QUOTAON:
   1128 		error = do_sys_quotactl_quotaon(mp,
   1129 				args->u.quotaon.qc_idtype,
   1130 				args->u.quotaon.qc_quotafile);
   1131 		break;
   1132 	    case QUOTACTL_QUOTAOFF:
   1133 		error = do_sys_quotactl_quotaoff(mp,
   1134 				args->u.quotaoff.qc_idtype);
   1135 		break;
   1136 	    default:
   1137 		error = EINVAL;
   1138 		break;
   1139 	}
   1140 
   1141 	vrele(vp);
   1142 	return error;
   1143 }
   1144 
   1145 /* ARGSUSED */
   1146 int
   1147 sys___quotactl(struct lwp *l, const struct sys___quotactl_args *uap,
   1148     register_t *retval)
   1149 {
   1150 	/* {
   1151 		syscallarg(const char *) path;
   1152 		syscallarg(struct quotactl_args *) args;
   1153 	} */
   1154 	struct quotactl_args args;
   1155 	int error;
   1156 
   1157 	error = copyin(SCARG(uap, args), &args, sizeof(args));
   1158 	if (error) {
   1159 		return error;
   1160 	}
   1161 
   1162 	return do_sys_quotactl(SCARG(uap, path), &args);
   1163 }
   1164 
   1165 int
   1166 dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
   1167     int root)
   1168 {
   1169 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
   1170 	bool chrooted;
   1171 	int error = 0;
   1172 
   1173 	KASSERT(l == curlwp);
   1174 
   1175 	/*
   1176 	 * This is safe unlocked.  cwdi_rdir never goes non-NULL -> NULL,
   1177 	 * since it would imply chroots can be escaped.  Just make sure this
   1178 	 * routine is self-consistent.
   1179 	 */
   1180 	chrooted = (atomic_load_relaxed(&cwdi->cwdi_rdir) != NULL);
   1181 
   1182 	/*
   1183 	 * If MNT_NOWAIT or MNT_LAZY is specified, do not
   1184 	 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
   1185 	 * overrides MNT_NOWAIT.
   1186 	 */
   1187 	if (flags == MNT_NOWAIT	|| flags == MNT_LAZY ||
   1188 	    (flags != MNT_WAIT && flags != 0)) {
   1189 		memcpy(sp, &mp->mnt_stat, sizeof(*sp));
   1190 	} else {
   1191 		/* Get the filesystem stats now */
   1192 		memset(sp, 0, sizeof(*sp));
   1193 		if ((error = VFS_STATVFS(mp, sp)) != 0)
   1194 			return error;
   1195 		if (!chrooted)
   1196 			(void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
   1197 	}
   1198 
   1199 	if (chrooted) {
   1200 		size_t len;
   1201 		char *bp;
   1202 		char c;
   1203 		char *path = PNBUF_GET();
   1204 
   1205 		bp = path + MAXPATHLEN;
   1206 		*--bp = '\0';
   1207 		rw_enter(&cwdi->cwdi_lock, RW_READER);
   1208 		error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
   1209 		    MAXPATHLEN / 2, 0, l);
   1210 		rw_exit(&cwdi->cwdi_lock);
   1211 		if (error) {
   1212 			PNBUF_PUT(path);
   1213 			return error;
   1214 		}
   1215 		len = strlen(bp);
   1216 		if (len != 1) {
   1217 			/*
   1218 			 * for mount points that are below our root, we can see
   1219 			 * them, so we fix up the pathname and return them. The
   1220 			 * rest we cannot see, so we don't allow viewing the
   1221 			 * data.
   1222 			 */
   1223 			if (strncmp(bp, sp->f_mntonname, len) == 0 &&
   1224 			    ((c = sp->f_mntonname[len]) == '/' || c == '\0')) {
   1225 				(void)strlcpy(sp->f_mntonname,
   1226 				    c == '\0' ? "/" : &sp->f_mntonname[len],
   1227 				    sizeof(sp->f_mntonname));
   1228 			} else {
   1229 				if (root)
   1230 					(void)strlcpy(sp->f_mntonname, "/",
   1231 					    sizeof(sp->f_mntonname));
   1232 				else
   1233 					error = EPERM;
   1234 			}
   1235 		}
   1236 		PNBUF_PUT(path);
   1237 	}
   1238 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
   1239 	return error;
   1240 }
   1241 
   1242 /*
   1243  * Get filesystem statistics by path.
   1244  */
   1245 int
   1246 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
   1247 {
   1248 	struct mount *mp;
   1249 	int error;
   1250 	struct vnode *vp;
   1251 
   1252 	error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
   1253 	if (error != 0)
   1254 		return error;
   1255 	mp = vp->v_mount;
   1256 	error = dostatvfs(mp, sb, l, flags, 1);
   1257 	vrele(vp);
   1258 	return error;
   1259 }
   1260 
   1261 /* ARGSUSED */
   1262 int
   1263 sys___statvfs190(struct lwp *l, const struct sys___statvfs190_args *uap, register_t *retval)
   1264 {
   1265 	/* {
   1266 		syscallarg(const char *) path;
   1267 		syscallarg(struct statvfs *) buf;
   1268 		syscallarg(int) flags;
   1269 	} */
   1270 	struct statvfs *sb;
   1271 	int error;
   1272 
   1273 	sb = STATVFSBUF_GET();
   1274 	error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
   1275 	if (error == 0)
   1276 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
   1277 	STATVFSBUF_PUT(sb);
   1278 	return error;
   1279 }
   1280 
   1281 /*
   1282  * Get filesystem statistics by fd.
   1283  */
   1284 int
   1285 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
   1286 {
   1287 	file_t *fp;
   1288 	struct mount *mp;
   1289 	int error;
   1290 
   1291 	/* fd_getvnode() will use the descriptor for us */
   1292 	if ((error = fd_getvnode(fd, &fp)) != 0)
   1293 		return (error);
   1294 	mp = fp->f_vnode->v_mount;
   1295 	error = dostatvfs(mp, sb, curlwp, flags, 1);
   1296 	fd_putfile(fd);
   1297 	return error;
   1298 }
   1299 
   1300 /* ARGSUSED */
   1301 int
   1302 sys___fstatvfs190(struct lwp *l, const struct sys___fstatvfs190_args *uap, register_t *retval)
   1303 {
   1304 	/* {
   1305 		syscallarg(int) fd;
   1306 		syscallarg(struct statvfs *) buf;
   1307 		syscallarg(int) flags;
   1308 	} */
   1309 	struct statvfs *sb;
   1310 	int error;
   1311 
   1312 	sb = STATVFSBUF_GET();
   1313 	error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
   1314 	if (error == 0)
   1315 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
   1316 	STATVFSBUF_PUT(sb);
   1317 	return error;
   1318 }
   1319 
   1320 
   1321 /*
   1322  * Get statistics on all filesystems.
   1323  */
   1324 int
   1325 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
   1326     int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
   1327     register_t *retval)
   1328 {
   1329 	int root = 0;
   1330 	mount_iterator_t *iter;
   1331 	struct proc *p = l->l_proc;
   1332 	struct mount *mp;
   1333 	struct statvfs *sb;
   1334 	size_t count, maxcount;
   1335 	int error = 0;
   1336 
   1337 	sb = STATVFSBUF_GET();
   1338 	maxcount = bufsize / entry_sz;
   1339 	count = 0;
   1340 	mountlist_iterator_init(&iter);
   1341 	while ((mp = mountlist_iterator_next(iter)) != NULL) {
   1342 		if (sfsp && count < maxcount) {
   1343 			error = dostatvfs(mp, sb, l, flags, 0);
   1344 			if (error) {
   1345 				error = 0;
   1346 				continue;
   1347 			}
   1348 			error = copyfn(sb, sfsp, entry_sz);
   1349 			if (error)
   1350 				goto out;
   1351 			sfsp = (char *)sfsp + entry_sz;
   1352 			root |= strcmp(sb->f_mntonname, "/") == 0;
   1353 		}
   1354 		count++;
   1355 	}
   1356 
   1357 	if (root == 0 && p->p_cwdi->cwdi_rdir) {
   1358 		/*
   1359 		 * fake a root entry
   1360 		 */
   1361 		error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
   1362 		    sb, l, flags, 1);
   1363 		if (error != 0)
   1364 			goto out;
   1365 		if (sfsp) {
   1366 			error = copyfn(sb, sfsp, entry_sz);
   1367 			if (error != 0)
   1368 				goto out;
   1369 		}
   1370 		count++;
   1371 	}
   1372 	if (sfsp && count > maxcount)
   1373 		*retval = maxcount;
   1374 	else
   1375 		*retval = count;
   1376 out:
   1377 	mountlist_iterator_destroy(iter);
   1378 	STATVFSBUF_PUT(sb);
   1379 	return error;
   1380 }
   1381 
   1382 int
   1383 sys___getvfsstat90(struct lwp *l, const struct sys___getvfsstat90_args *uap,
   1384     register_t *retval)
   1385 {
   1386 	/* {
   1387 		syscallarg(struct statvfs *) buf;
   1388 		syscallarg(size_t) bufsize;
   1389 		syscallarg(int) flags;
   1390 	} */
   1391 
   1392 	return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
   1393 	    SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
   1394 }
   1395 
   1396 /*
   1397  * Change current working directory to a given file descriptor.
   1398  */
   1399 /* ARGSUSED */
   1400 int
   1401 sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval)
   1402 {
   1403 	/* {
   1404 		syscallarg(int) fd;
   1405 	} */
   1406 	struct proc *p = l->l_proc;
   1407 	struct cwdinfo *cwdi;
   1408 	struct vnode *vp, *tdp;
   1409 	struct mount *mp;
   1410 	file_t *fp;
   1411 	int error, fd;
   1412 
   1413 	/* fd_getvnode() will use the descriptor for us */
   1414 	fd = SCARG(uap, fd);
   1415 	if ((error = fd_getvnode(fd, &fp)) != 0)
   1416 		return (error);
   1417 	vp = fp->f_vnode;
   1418 
   1419 	vref(vp);
   1420 	vn_lock(vp, LK_SHARED | LK_RETRY);
   1421 	if (vp->v_type != VDIR)
   1422 		error = ENOTDIR;
   1423 	else
   1424 		error = VOP_ACCESS(vp, VEXEC, l->l_cred);
   1425 	if (error) {
   1426 		vput(vp);
   1427 		goto out;
   1428 	}
   1429 	while ((mp = vp->v_mountedhere) != NULL) {
   1430 		error = vfs_busy(mp);
   1431 		vput(vp);
   1432 		if (error != 0)
   1433 			goto out;
   1434 		error = VFS_ROOT(mp, LK_SHARED, &tdp);
   1435 		vfs_unbusy(mp);
   1436 		if (error)
   1437 			goto out;
   1438 		vp = tdp;
   1439 	}
   1440 	VOP_UNLOCK(vp);
   1441 
   1442 	/*
   1443 	 * Disallow changing to a directory not under the process's
   1444 	 * current root directory (if there is one).
   1445 	 */
   1446 	cwdi = p->p_cwdi;
   1447 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1448 	if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
   1449 		vrele(vp);
   1450 		error = EPERM;	/* operation not permitted */
   1451 	} else {
   1452 		vrele(cwdi->cwdi_cdir);
   1453 		cwdi->cwdi_cdir = vp;
   1454 	}
   1455 	rw_exit(&cwdi->cwdi_lock);
   1456 
   1457  out:
   1458 	fd_putfile(fd);
   1459 	return (error);
   1460 }
   1461 
   1462 /*
   1463  * Change this process's notion of the root directory to a given file
   1464  * descriptor.
   1465  */
   1466 int
   1467 sys_fchroot(struct lwp *l, const struct sys_fchroot_args *uap, register_t *retval)
   1468 {
   1469 	struct vnode	*vp;
   1470 	file_t	*fp;
   1471 	int		 error, fd = SCARG(uap, fd);
   1472 
   1473 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
   1474  	    KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
   1475 		return error;
   1476 	/* fd_getvnode() will use the descriptor for us */
   1477 	if ((error = fd_getvnode(fd, &fp)) != 0)
   1478 		return error;
   1479 	vp = fp->f_vnode;
   1480 	vn_lock(vp, LK_SHARED | LK_RETRY);
   1481 	if (vp->v_type != VDIR)
   1482 		error = ENOTDIR;
   1483 	else
   1484 		error = VOP_ACCESS(vp, VEXEC, l->l_cred);
   1485 	VOP_UNLOCK(vp);
   1486 	if (error)
   1487 		goto out;
   1488 	vref(vp);
   1489 	change_root(vp);
   1490 
   1491  out:
   1492 	fd_putfile(fd);
   1493 	return (error);
   1494 }
   1495 
   1496 /*
   1497  * Change current working directory (``.'').
   1498  */
   1499 /* ARGSUSED */
   1500 int
   1501 sys_chdir(struct lwp *l, const struct sys_chdir_args *uap, register_t *retval)
   1502 {
   1503 	/* {
   1504 		syscallarg(const char *) path;
   1505 	} */
   1506 	struct proc *p = l->l_proc;
   1507 	struct cwdinfo *cwdi;
   1508 	int error;
   1509 	struct vnode *vp;
   1510 
   1511 	if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
   1512 				  &vp, l)) != 0)
   1513 		return (error);
   1514 	cwdi = p->p_cwdi;
   1515 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1516 	vrele(cwdi->cwdi_cdir);
   1517 	cwdi->cwdi_cdir = vp;
   1518 	rw_exit(&cwdi->cwdi_lock);
   1519 	return (0);
   1520 }
   1521 
   1522 /*
   1523  * Change notion of root (``/'') directory.
   1524  */
   1525 /* ARGSUSED */
   1526 int
   1527 sys_chroot(struct lwp *l, const struct sys_chroot_args *uap, register_t *retval)
   1528 {
   1529 	/* {
   1530 		syscallarg(const char *) path;
   1531 	} */
   1532 	int error;
   1533 	struct vnode *vp;
   1534 
   1535 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
   1536 	    KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
   1537 		return (error);
   1538 
   1539 	error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE, &vp, l);
   1540 	if (error == 0)
   1541 		change_root(vp);
   1542 	return error;
   1543 }
   1544 
   1545 /*
   1546  * Common routine for chroot and fchroot.
   1547  * NB: callers need to properly authorize the change root operation.
   1548  */
   1549 void
   1550 change_root(struct vnode *vp)
   1551 {
   1552 	kauth_cred_t ncred;
   1553 	struct lwp *l = curlwp;
   1554 	struct proc *p = l->l_proc;
   1555 	struct cwdinfo *cwdi = p->p_cwdi;
   1556 
   1557 	ncred = kauth_cred_alloc();
   1558 
   1559 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1560 	if (cwdi->cwdi_rdir != NULL)
   1561 		vrele(cwdi->cwdi_rdir);
   1562 	cwdi->cwdi_rdir = vp;
   1563 
   1564 	/*
   1565 	 * Prevent escaping from chroot by putting the root under
   1566 	 * the working directory.  Silently chdir to / if we aren't
   1567 	 * already there.
   1568 	 */
   1569 	if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
   1570 		/*
   1571 		 * XXX would be more failsafe to change directory to a
   1572 		 * deadfs node here instead
   1573 		 */
   1574 		vrele(cwdi->cwdi_cdir);
   1575 		vref(vp);
   1576 		cwdi->cwdi_cdir = vp;
   1577 	}
   1578 	rw_exit(&cwdi->cwdi_lock);
   1579 
   1580 	/* Get a write lock on the process credential. */
   1581 	proc_crmod_enter();
   1582 
   1583 	kauth_cred_clone(p->p_cred, ncred);
   1584 	kauth_proc_chroot(ncred, p->p_cwdi);
   1585 
   1586 	/* Broadcast our credentials to the process and other LWPs. */
   1587  	proc_crmod_leave(ncred, p->p_cred, true);
   1588 }
   1589 
   1590 /*
   1591  * Common routine for chroot and chdir.
   1592  * XXX "where" should be enum uio_seg
   1593  */
   1594 int
   1595 chdir_lookup(const char *path, int where, struct vnode **vpp, struct lwp *l)
   1596 {
   1597 	struct pathbuf *pb;
   1598 	struct nameidata nd;
   1599 	int error;
   1600 
   1601 	error = pathbuf_maybe_copyin(path, where, &pb);
   1602 	if (error) {
   1603 		return error;
   1604 	}
   1605 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | LOCKSHARED | TRYEMULROOT, pb);
   1606 	if ((error = namei(&nd)) != 0) {
   1607 		pathbuf_destroy(pb);
   1608 		return error;
   1609 	}
   1610 	*vpp = nd.ni_vp;
   1611 	pathbuf_destroy(pb);
   1612 
   1613 	if ((*vpp)->v_type != VDIR)
   1614 		error = ENOTDIR;
   1615 	else
   1616 		error = VOP_ACCESS(*vpp, VEXEC, l->l_cred);
   1617 
   1618 	if (error)
   1619 		vput(*vpp);
   1620 	else
   1621 		VOP_UNLOCK(*vpp);
   1622 	return (error);
   1623 }
   1624 
   1625 /*
   1626  * Internals of sys_open - path has already been converted into a pathbuf
   1627  * (so we can easily reuse this function from other parts of the kernel,
   1628  * like posix_spawn post-processing).
   1629  */
   1630 int
   1631 do_open(lwp_t *l, struct vnode *dvp, struct pathbuf *pb, int open_flags,
   1632 	int open_mode, int *fd)
   1633 {
   1634 	struct proc *p = l->l_proc;
   1635 	struct cwdinfo *cwdi = p->p_cwdi;
   1636 	file_t *fp;
   1637 	struct vnode *vp;
   1638 	int dupfd;
   1639 	bool dupfd_move;
   1640 	int flags, cmode;
   1641 	int indx, error;
   1642 
   1643 	if (open_flags & O_SEARCH) {
   1644 		open_flags &= ~(int)O_SEARCH;
   1645 	}
   1646 
   1647 	/*
   1648 	 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
   1649 	 * may be specified.
   1650 	 */
   1651 	if ((open_flags & O_EXEC) && (open_flags & O_ACCMODE))
   1652 		return EINVAL;
   1653 
   1654 	flags = FFLAGS(open_flags);
   1655 	if ((flags & (FREAD | FWRITE)) == 0)
   1656 		return EINVAL;
   1657 
   1658 	if ((error = fd_allocfile(&fp, &indx)) != 0) {
   1659 		return error;
   1660 	}
   1661 
   1662 	/* We're going to read cwdi->cwdi_cmask unlocked here. */
   1663 	cmode = ((open_mode &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
   1664 
   1665 	error = vn_open(dvp, pb, TRYEMULROOT, flags, cmode,
   1666 	    &vp, &dupfd_move, &dupfd);
   1667 	if (error != 0) {
   1668 		fd_abort(p, fp, indx);
   1669 		if (error == ERESTART)
   1670 			error = EINTR;
   1671 		return error;
   1672 	}
   1673 
   1674 	if (vp == NULL) {
   1675 		fd_abort(p, fp, indx);
   1676 		error = fd_dupopen(dupfd, dupfd_move, flags, &indx);
   1677 		if (error)
   1678 			return error;
   1679 		*fd = indx;
   1680 	} else {
   1681 		error = open_setfp(l, fp, vp, indx, flags);
   1682 		if (error)
   1683 			return error;
   1684 		VOP_UNLOCK(vp);
   1685 		*fd = indx;
   1686 		fd_affix(p, fp, indx);
   1687 	}
   1688 
   1689 	return 0;
   1690 }
   1691 
   1692 int
   1693 fd_open(const char *path, int open_flags, int open_mode, int *fd)
   1694 {
   1695 	struct pathbuf *pb;
   1696 	int error, oflags;
   1697 
   1698 	oflags = FFLAGS(open_flags);
   1699 	if ((oflags & (FREAD | FWRITE)) == 0)
   1700 		return EINVAL;
   1701 
   1702 	pb = pathbuf_create(path);
   1703 	if (pb == NULL)
   1704 		return ENOMEM;
   1705 
   1706 	error = do_open(curlwp, NULL, pb, open_flags, open_mode, fd);
   1707 	pathbuf_destroy(pb);
   1708 
   1709 	return error;
   1710 }
   1711 
   1712 static int
   1713 do_sys_openat(lwp_t *l, int fdat, const char *path, int flags,
   1714     int mode, int *fd)
   1715 {
   1716 	file_t *dfp = NULL;
   1717 	struct vnode *dvp = NULL;
   1718 	struct pathbuf *pb;
   1719 	const char *pathstring = NULL;
   1720 	int error;
   1721 
   1722 	if (path == NULL) {
   1723 		MODULE_HOOK_CALL(vfs_openat_10_hook, (&pb), enosys(), error);
   1724 		if (error == ENOSYS)
   1725 			goto no_compat;
   1726 		if (error)
   1727 			return error;
   1728 	} else {
   1729 no_compat:
   1730 		error = pathbuf_copyin(path, &pb);
   1731 		if (error)
   1732 			return error;
   1733 	}
   1734 
   1735 	pathstring = pathbuf_stringcopy_get(pb);
   1736 
   1737 	/*
   1738 	 * fdat is ignored if:
   1739 	 * 1) if fdat is AT_FDCWD, which means use current directory as base.
   1740 	 * 2) if path is absolute, then fdat is useless.
   1741 	 */
   1742 	if (fdat != AT_FDCWD && pathstring[0] != '/') {
   1743 		/* fd_getvnode() will use the descriptor for us */
   1744 		if ((error = fd_getvnode(fdat, &dfp)) != 0)
   1745 			goto out;
   1746 
   1747 		dvp = dfp->f_vnode;
   1748 	}
   1749 
   1750 	error = do_open(l, dvp, pb, flags, mode, fd);
   1751 
   1752 	if (dfp != NULL)
   1753 		fd_putfile(fdat);
   1754 out:
   1755 	pathbuf_stringcopy_put(pb, pathstring);
   1756 	pathbuf_destroy(pb);
   1757 	return error;
   1758 }
   1759 
   1760 int
   1761 sys_open(struct lwp *l, const struct sys_open_args *uap, register_t *retval)
   1762 {
   1763 	/* {
   1764 		syscallarg(const char *) path;
   1765 		syscallarg(int) flags;
   1766 		syscallarg(int) mode;
   1767 	} */
   1768 	int error;
   1769 	int fd;
   1770 
   1771 	error = do_sys_openat(l, AT_FDCWD, SCARG(uap, path),
   1772 			      SCARG(uap, flags), SCARG(uap, mode), &fd);
   1773 
   1774 	if (error == 0)
   1775 		*retval = fd;
   1776 
   1777 	return error;
   1778 }
   1779 
   1780 int
   1781 sys_openat(struct lwp *l, const struct sys_openat_args *uap, register_t *retval)
   1782 {
   1783 	/* {
   1784 		syscallarg(int) fd;
   1785 		syscallarg(const char *) path;
   1786 		syscallarg(int) oflags;
   1787 		syscallarg(int) mode;
   1788 	} */
   1789 	int error;
   1790 	int fd;
   1791 
   1792 	error = do_sys_openat(l, SCARG(uap, fd), SCARG(uap, path),
   1793 			      SCARG(uap, oflags), SCARG(uap, mode), &fd);
   1794 
   1795 	if (error == 0)
   1796 		*retval = fd;
   1797 
   1798 	return error;
   1799 }
   1800 
   1801 static void
   1802 vfs__fhfree(fhandle_t *fhp)
   1803 {
   1804 	size_t fhsize;
   1805 
   1806 	fhsize = FHANDLE_SIZE(fhp);
   1807 	kmem_free(fhp, fhsize);
   1808 }
   1809 
   1810 /*
   1811  * vfs_composefh: compose a filehandle.
   1812  */
   1813 
   1814 int
   1815 vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
   1816 {
   1817 	struct mount *mp;
   1818 	struct fid *fidp;
   1819 	int error;
   1820 	size_t needfhsize;
   1821 	size_t fidsize;
   1822 
   1823 	mp = vp->v_mount;
   1824 	fidp = NULL;
   1825 	if (*fh_size < FHANDLE_SIZE_MIN) {
   1826 		fidsize = 0;
   1827 	} else {
   1828 		fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
   1829 		if (fhp != NULL) {
   1830 			memset(fhp, 0, *fh_size);
   1831 			fhp->fh_fsid = mp->mnt_stat.f_fsidx;
   1832 			fidp = &fhp->fh_fid;
   1833 		}
   1834 	}
   1835 	error = VFS_VPTOFH(vp, fidp, &fidsize);
   1836 	needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
   1837 	if (error == 0 && *fh_size < needfhsize) {
   1838 		error = E2BIG;
   1839 	}
   1840 	*fh_size = needfhsize;
   1841 	return error;
   1842 }
   1843 
   1844 int
   1845 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
   1846 {
   1847 	struct mount *mp;
   1848 	fhandle_t *fhp;
   1849 	size_t fhsize;
   1850 	size_t fidsize;
   1851 	int error;
   1852 
   1853 	mp = vp->v_mount;
   1854 	fidsize = 0;
   1855 	error = VFS_VPTOFH(vp, NULL, &fidsize);
   1856 	KASSERT(error != 0);
   1857 	if (error != E2BIG) {
   1858 		goto out;
   1859 	}
   1860 	fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
   1861 	fhp = kmem_zalloc(fhsize, KM_SLEEP);
   1862 	fhp->fh_fsid = mp->mnt_stat.f_fsidx;
   1863 	error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
   1864 	if (error == 0) {
   1865 		KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
   1866 		    FHANDLE_FILEID(fhp)->fid_len == fidsize));
   1867 		*fhpp = fhp;
   1868 	} else {
   1869 		kmem_free(fhp, fhsize);
   1870 	}
   1871 out:
   1872 	return error;
   1873 }
   1874 
   1875 void
   1876 vfs_composefh_free(fhandle_t *fhp)
   1877 {
   1878 
   1879 	vfs__fhfree(fhp);
   1880 }
   1881 
   1882 /*
   1883  * vfs_fhtovp: lookup a vnode by a filehandle.
   1884  */
   1885 
   1886 int
   1887 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
   1888 {
   1889 	struct mount *mp;
   1890 	int error;
   1891 
   1892 	*vpp = NULL;
   1893 	mp = vfs_getvfs(FHANDLE_FSID(fhp));
   1894 	if (mp == NULL) {
   1895 		error = ESTALE;
   1896 		goto out;
   1897 	}
   1898 	if (mp->mnt_op->vfs_fhtovp == NULL) {
   1899 		error = EOPNOTSUPP;
   1900 		goto out;
   1901 	}
   1902 	error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), LK_EXCLUSIVE, vpp);
   1903 out:
   1904 	return error;
   1905 }
   1906 
   1907 /*
   1908  * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
   1909  * the needed size.
   1910  */
   1911 
   1912 int
   1913 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
   1914 {
   1915 	fhandle_t *fhp;
   1916 	int error;
   1917 
   1918 	if (fhsize > FHANDLE_SIZE_MAX) {
   1919 		return EINVAL;
   1920 	}
   1921 	if (fhsize < FHANDLE_SIZE_MIN) {
   1922 		return EINVAL;
   1923 	}
   1924 again:
   1925 	fhp = kmem_alloc(fhsize, KM_SLEEP);
   1926 	error = copyin(ufhp, fhp, fhsize);
   1927 	if (error == 0) {
   1928 		/* XXX this check shouldn't be here */
   1929 		if (FHANDLE_SIZE(fhp) == fhsize) {
   1930 			*fhpp = fhp;
   1931 			return 0;
   1932 		} else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
   1933 			/*
   1934 			 * a kludge for nfsv2 padded handles.
   1935 			 */
   1936 			size_t sz;
   1937 
   1938 			sz = FHANDLE_SIZE(fhp);
   1939 			kmem_free(fhp, fhsize);
   1940 			fhsize = sz;
   1941 			goto again;
   1942 		} else {
   1943 			/*
   1944 			 * userland told us wrong size.
   1945 			 */
   1946 		    	error = EINVAL;
   1947 		}
   1948 	}
   1949 	kmem_free(fhp, fhsize);
   1950 	return error;
   1951 }
   1952 
   1953 void
   1954 vfs_copyinfh_free(fhandle_t *fhp)
   1955 {
   1956 
   1957 	vfs__fhfree(fhp);
   1958 }
   1959 
   1960 /*
   1961  * Get file handle system call
   1962  */
   1963 int
   1964 sys___getfh30(struct lwp *l, const struct sys___getfh30_args *uap, register_t *retval)
   1965 {
   1966 	/* {
   1967 		syscallarg(char *) fname;
   1968 		syscallarg(fhandle_t *) fhp;
   1969 		syscallarg(size_t *) fh_size;
   1970 	} */
   1971 	struct vnode *vp;
   1972 	fhandle_t *fh;
   1973 	int error;
   1974 	struct pathbuf *pb;
   1975 	struct nameidata nd;
   1976 	size_t sz;
   1977 	size_t usz;
   1978 
   1979 	/*
   1980 	 * Must be super user
   1981 	 */
   1982 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   1983 	    0, NULL, NULL, NULL);
   1984 	if (error)
   1985 		return (error);
   1986 
   1987 	error = pathbuf_copyin(SCARG(uap, fname), &pb);
   1988 	if (error) {
   1989 		return error;
   1990 	}
   1991 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
   1992 	error = namei(&nd);
   1993 	if (error) {
   1994 		pathbuf_destroy(pb);
   1995 		return error;
   1996 	}
   1997 	vp = nd.ni_vp;
   1998 	pathbuf_destroy(pb);
   1999 
   2000 	error = vfs_composefh_alloc(vp, &fh);
   2001 	vput(vp);
   2002 	if (error != 0) {
   2003 		return error;
   2004 	}
   2005 	error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
   2006 	if (error != 0) {
   2007 		goto out;
   2008 	}
   2009 	sz = FHANDLE_SIZE(fh);
   2010 	error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
   2011 	if (error != 0) {
   2012 		goto out;
   2013 	}
   2014 	if (usz >= sz) {
   2015 		error = copyout(fh, SCARG(uap, fhp), sz);
   2016 	} else {
   2017 		error = E2BIG;
   2018 	}
   2019 out:
   2020 	vfs_composefh_free(fh);
   2021 	return (error);
   2022 }
   2023 
   2024 /*
   2025  * Open a file given a file handle.
   2026  *
   2027  * Check permissions, allocate an open file structure,
   2028  * and call the device open routine if any.
   2029  */
   2030 
   2031 int
   2032 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
   2033     register_t *retval)
   2034 {
   2035 	file_t *fp;
   2036 	struct vnode *vp = NULL;
   2037 	kauth_cred_t cred = l->l_cred;
   2038 	file_t *nfp;
   2039 	int indx, error;
   2040 	struct vattr va;
   2041 	fhandle_t *fh;
   2042 	int flags;
   2043 	proc_t *p;
   2044 
   2045 	p = curproc;
   2046 
   2047 	/*
   2048 	 * Must be super user
   2049 	 */
   2050 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   2051 	    0, NULL, NULL, NULL)))
   2052 		return (error);
   2053 
   2054 	if (oflags & O_SEARCH) {
   2055 		oflags &= ~(int)O_SEARCH;
   2056 	}
   2057 
   2058 	flags = FFLAGS(oflags);
   2059 	if ((flags & (FREAD | FWRITE)) == 0)
   2060 		return (EINVAL);
   2061 	if ((flags & O_CREAT))
   2062 		return (EINVAL);
   2063 	if ((error = fd_allocfile(&nfp, &indx)) != 0)
   2064 		return (error);
   2065 	fp = nfp;
   2066 	error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
   2067 	if (error != 0) {
   2068 		goto bad;
   2069 	}
   2070 	error = vfs_fhtovp(fh, &vp);
   2071 	vfs_copyinfh_free(fh);
   2072 	if (error != 0) {
   2073 		goto bad;
   2074 	}
   2075 
   2076 	/* Now do an effective vn_open */
   2077 
   2078 	if (vp->v_type == VSOCK) {
   2079 		error = EOPNOTSUPP;
   2080 		goto bad;
   2081 	}
   2082 	error = vn_openchk(vp, cred, flags);
   2083 	if (error != 0)
   2084 		goto bad;
   2085 	if (flags & O_TRUNC) {
   2086 		VOP_UNLOCK(vp);			/* XXX */
   2087 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
   2088 		vattr_null(&va);
   2089 		va.va_size = 0;
   2090 		error = VOP_SETATTR(vp, &va, cred);
   2091 		if (error)
   2092 			goto bad;
   2093 	}
   2094 	if ((error = VOP_OPEN(vp, flags, cred)) != 0)
   2095 		goto bad;
   2096 	if (flags & FWRITE) {
   2097 		mutex_enter(vp->v_interlock);
   2098 		vp->v_writecount++;
   2099 		mutex_exit(vp->v_interlock);
   2100 	}
   2101 
   2102 	/* done with modified vn_open, now finish what sys_open does. */
   2103 	if ((error = open_setfp(l, fp, vp, indx, flags)))
   2104 		return error;
   2105 
   2106 	VOP_UNLOCK(vp);
   2107 	*retval = indx;
   2108 	fd_affix(p, fp, indx);
   2109 	return (0);
   2110 
   2111 bad:
   2112 	fd_abort(p, fp, indx);
   2113 	if (vp != NULL)
   2114 		vput(vp);
   2115 	if (error == EDUPFD || error == EMOVEFD) {
   2116 		/* XXX should probably close curlwp->l_dupfd */
   2117 		error = EOPNOTSUPP;
   2118 	}
   2119 	return (error);
   2120 }
   2121 
   2122 int
   2123 sys___fhopen40(struct lwp *l, const struct sys___fhopen40_args *uap, register_t *retval)
   2124 {
   2125 	/* {
   2126 		syscallarg(const void *) fhp;
   2127 		syscallarg(size_t) fh_size;
   2128 		syscallarg(int) flags;
   2129 	} */
   2130 
   2131 	return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
   2132 	    SCARG(uap, flags), retval);
   2133 }
   2134 
   2135 int
   2136 do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
   2137 {
   2138 	int error;
   2139 	fhandle_t *fh;
   2140 	struct vnode *vp;
   2141 
   2142 	/*
   2143 	 * Must be super user
   2144 	 */
   2145 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   2146 	    0, NULL, NULL, NULL)))
   2147 		return (error);
   2148 
   2149 	error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
   2150 	if (error != 0)
   2151 		return error;
   2152 
   2153 	error = vfs_fhtovp(fh, &vp);
   2154 	vfs_copyinfh_free(fh);
   2155 	if (error != 0)
   2156 		return error;
   2157 
   2158 	error = vn_stat(vp, sb);
   2159 	vput(vp);
   2160 	return error;
   2161 }
   2162 
   2163 
   2164 /* ARGSUSED */
   2165 int
   2166 sys___fhstat50(struct lwp *l, const struct sys___fhstat50_args *uap, register_t *retval)
   2167 {
   2168 	/* {
   2169 		syscallarg(const void *) fhp;
   2170 		syscallarg(size_t) fh_size;
   2171 		syscallarg(struct stat *) sb;
   2172 	} */
   2173 	struct stat sb;
   2174 	int error;
   2175 
   2176 	error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
   2177 	if (error)
   2178 		return error;
   2179 	return copyout(&sb, SCARG(uap, sb), sizeof(sb));
   2180 }
   2181 
   2182 int
   2183 do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
   2184     int flags)
   2185 {
   2186 	fhandle_t *fh;
   2187 	struct mount *mp;
   2188 	struct vnode *vp;
   2189 	int error;
   2190 
   2191 	/*
   2192 	 * Must be super user
   2193 	 */
   2194 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   2195 	    0, NULL, NULL, NULL)))
   2196 		return error;
   2197 
   2198 	error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
   2199 	if (error != 0)
   2200 		return error;
   2201 
   2202 	error = vfs_fhtovp(fh, &vp);
   2203 	vfs_copyinfh_free(fh);
   2204 	if (error != 0)
   2205 		return error;
   2206 
   2207 	mp = vp->v_mount;
   2208 	error = dostatvfs(mp, sb, l, flags, 1);
   2209 	vput(vp);
   2210 	return error;
   2211 }
   2212 
   2213 /* ARGSUSED */
   2214 int
   2215 sys___fhstatvfs190(struct lwp *l, const struct sys___fhstatvfs190_args *uap, register_t *retval)
   2216 {
   2217 	/* {
   2218 		syscallarg(const void *) fhp;
   2219 		syscallarg(size_t) fh_size;
   2220 		syscallarg(struct statvfs *) buf;
   2221 		syscallarg(int)	flags;
   2222 	} */
   2223 	struct statvfs *sb = STATVFSBUF_GET();
   2224 	int error;
   2225 
   2226 	error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
   2227 	    SCARG(uap, flags));
   2228 	if (error == 0)
   2229 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
   2230 	STATVFSBUF_PUT(sb);
   2231 	return error;
   2232 }
   2233 
   2234 int
   2235 do_posix_mknodat(struct lwp *l, int fdat, const char *pathname, mode_t mode,
   2236     dev_t dev)
   2237 {
   2238 
   2239 	/*
   2240 	 * The POSIX mknod(2) call is an alias for mkfifo(2) for S_IFIFO
   2241 	 * in mode and dev=0.
   2242 	 *
   2243 	 * In all the other cases it's implementation defined behavior.
   2244 	 */
   2245 
   2246 	if ((mode & S_IFIFO) && dev == 0)
   2247 		return do_sys_mkfifoat(l, fdat, pathname, mode);
   2248 	else
   2249 		return do_sys_mknodat(l, fdat, pathname, mode, dev,
   2250 		    UIO_USERSPACE);
   2251 }
   2252 
   2253 /*
   2254  * Create a special file.
   2255  */
   2256 /* ARGSUSED */
   2257 int
   2258 sys___mknod50(struct lwp *l, const struct sys___mknod50_args *uap,
   2259     register_t *retval)
   2260 {
   2261 	/* {
   2262 		syscallarg(const char *) path;
   2263 		syscallarg(mode_t) mode;
   2264 		syscallarg(dev_t) dev;
   2265 	} */
   2266 	return do_posix_mknodat(l, AT_FDCWD, SCARG(uap, path),
   2267 	    SCARG(uap, mode), SCARG(uap, dev));
   2268 }
   2269 
   2270 int
   2271 sys_mknodat(struct lwp *l, const struct sys_mknodat_args *uap,
   2272     register_t *retval)
   2273 {
   2274 	/* {
   2275 		syscallarg(int) fd;
   2276 		syscallarg(const char *) path;
   2277 		syscallarg(mode_t) mode;
   2278 		syscallarg(int) pad;
   2279 		syscallarg(dev_t) dev;
   2280 	} */
   2281 
   2282 	return do_posix_mknodat(l, SCARG(uap, fd), SCARG(uap, path),
   2283 	    SCARG(uap, mode), SCARG(uap, dev));
   2284 }
   2285 
   2286 int
   2287 do_sys_mknod(struct lwp *l, const char *pathname, mode_t mode, dev_t dev,
   2288     enum uio_seg seg)
   2289 {
   2290 	return do_sys_mknodat(l, AT_FDCWD, pathname, mode, dev, seg);
   2291 }
   2292 
   2293 int
   2294 do_sys_mknodat(struct lwp *l, int fdat, const char *pathname, mode_t mode,
   2295     dev_t dev, enum uio_seg seg)
   2296 {
   2297 	struct proc *p = l->l_proc;
   2298 	struct vnode *vp;
   2299 	struct vattr vattr;
   2300 	int error, optype;
   2301 	struct pathbuf *pb;
   2302 	struct nameidata nd;
   2303 	const char *pathstring;
   2304 
   2305 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
   2306 	    0, NULL, NULL, NULL)) != 0)
   2307 		return (error);
   2308 
   2309 	optype = VOP_MKNOD_DESCOFFSET;
   2310 
   2311 	error = pathbuf_maybe_copyin(pathname, seg, &pb);
   2312 	if (error) {
   2313 		return error;
   2314 	}
   2315 	pathstring = pathbuf_stringcopy_get(pb);
   2316 	if (pathstring == NULL) {
   2317 		pathbuf_destroy(pb);
   2318 		return ENOMEM;
   2319 	}
   2320 
   2321 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
   2322 
   2323 	if ((error = fd_nameiat(l, fdat, &nd)) != 0)
   2324 		goto out;
   2325 	vp = nd.ni_vp;
   2326 
   2327 	if (vp != NULL)
   2328 		error = EEXIST;
   2329 	else {
   2330 		vattr_null(&vattr);
   2331 		/* We will read cwdi->cwdi_cmask unlocked. */
   2332 		vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   2333 		vattr.va_rdev = dev;
   2334 
   2335 		switch (mode & S_IFMT) {
   2336 		case S_IFMT:	/* used by badsect to flag bad sectors */
   2337 			vattr.va_type = VBAD;
   2338 			break;
   2339 		case S_IFCHR:
   2340 			vattr.va_type = VCHR;
   2341 			break;
   2342 		case S_IFBLK:
   2343 			vattr.va_type = VBLK;
   2344 			break;
   2345 		case S_IFWHT:
   2346 			optype = VOP_WHITEOUT_DESCOFFSET;
   2347 			break;
   2348 		case S_IFREG:
   2349 #if NVERIEXEC > 0
   2350 			error = veriexec_openchk(l, nd.ni_vp, pathstring,
   2351 			    O_CREAT);
   2352 #endif /* NVERIEXEC > 0 */
   2353 			vattr.va_type = VREG;
   2354 			vattr.va_rdev = VNOVAL;
   2355 			optype = VOP_CREATE_DESCOFFSET;
   2356 			break;
   2357 		default:
   2358 			error = EINVAL;
   2359 			break;
   2360 		}
   2361 
   2362 		if (error == 0 && optype == VOP_MKNOD_DESCOFFSET &&
   2363 		    vattr.va_rdev == VNOVAL)
   2364 			error = EINVAL;
   2365 	}
   2366 
   2367 	if (!error) {
   2368 		switch (optype) {
   2369 		case VOP_WHITEOUT_DESCOFFSET:
   2370 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
   2371 			if (error)
   2372 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2373 			vput(nd.ni_dvp);
   2374 			break;
   2375 
   2376 		case VOP_MKNOD_DESCOFFSET:
   2377 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
   2378 						&nd.ni_cnd, &vattr);
   2379 			if (error == 0)
   2380 				vrele(nd.ni_vp);
   2381 			vput(nd.ni_dvp);
   2382 			break;
   2383 
   2384 		case VOP_CREATE_DESCOFFSET:
   2385 			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
   2386 						&nd.ni_cnd, &vattr);
   2387 			if (error == 0)
   2388 				vrele(nd.ni_vp);
   2389 			vput(nd.ni_dvp);
   2390 			break;
   2391 		}
   2392 	} else {
   2393 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2394 		if (nd.ni_dvp == vp)
   2395 			vrele(nd.ni_dvp);
   2396 		else
   2397 			vput(nd.ni_dvp);
   2398 		if (vp)
   2399 			vrele(vp);
   2400 	}
   2401 out:
   2402 	pathbuf_stringcopy_put(pb, pathstring);
   2403 	pathbuf_destroy(pb);
   2404 	return (error);
   2405 }
   2406 
   2407 /*
   2408  * Create a named pipe.
   2409  */
   2410 /* ARGSUSED */
   2411 int
   2412 sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval)
   2413 {
   2414 	/* {
   2415 		syscallarg(const char *) path;
   2416 		syscallarg(int) mode;
   2417 	} */
   2418 	return do_sys_mkfifoat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap, mode));
   2419 }
   2420 
   2421 int
   2422 sys_mkfifoat(struct lwp *l, const struct sys_mkfifoat_args *uap,
   2423     register_t *retval)
   2424 {
   2425 	/* {
   2426 		syscallarg(int) fd;
   2427 		syscallarg(const char *) path;
   2428 		syscallarg(int) mode;
   2429 	} */
   2430 
   2431 	return do_sys_mkfifoat(l, SCARG(uap, fd), SCARG(uap, path),
   2432 	    SCARG(uap, mode));
   2433 }
   2434 
   2435 static int
   2436 do_sys_mkfifoat(struct lwp *l, int fdat, const char *path, mode_t mode)
   2437 {
   2438 	struct proc *p = l->l_proc;
   2439 	struct vattr vattr;
   2440 	int error;
   2441 	struct pathbuf *pb;
   2442 	struct nameidata nd;
   2443 
   2444 	error = pathbuf_copyin(path, &pb);
   2445 	if (error) {
   2446 		return error;
   2447 	}
   2448 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
   2449 
   2450 	if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
   2451 		pathbuf_destroy(pb);
   2452 		return error;
   2453 	}
   2454 	if (nd.ni_vp != NULL) {
   2455 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2456 		if (nd.ni_dvp == nd.ni_vp)
   2457 			vrele(nd.ni_dvp);
   2458 		else
   2459 			vput(nd.ni_dvp);
   2460 		vrele(nd.ni_vp);
   2461 		pathbuf_destroy(pb);
   2462 		return (EEXIST);
   2463 	}
   2464 	vattr_null(&vattr);
   2465 	vattr.va_type = VFIFO;
   2466 	/* We will read cwdi->cwdi_cmask unlocked. */
   2467 	vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   2468 	error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   2469 	if (error == 0)
   2470 		vrele(nd.ni_vp);
   2471 	vput(nd.ni_dvp);
   2472 	pathbuf_destroy(pb);
   2473 	return (error);
   2474 }
   2475 
   2476 /*
   2477  * Make a hard file link.
   2478  */
   2479 /* ARGSUSED */
   2480 int
   2481 do_sys_linkat(struct lwp *l, int fdpath, const char *path, int fdlink,
   2482     const char *link, int follow, register_t *retval)
   2483 {
   2484 	struct vnode *vp;
   2485 	struct pathbuf *linkpb;
   2486 	struct nameidata nd;
   2487 	namei_simple_flags_t ns_flags;
   2488 	int error;
   2489 
   2490 	if (follow & AT_SYMLINK_FOLLOW)
   2491 		ns_flags = NSM_FOLLOW_TRYEMULROOT;
   2492 	else
   2493 		ns_flags = NSM_NOFOLLOW_TRYEMULROOT;
   2494 
   2495 	error = fd_nameiat_simple_user(l, fdpath, path, ns_flags, &vp);
   2496 	if (error != 0)
   2497 		return (error);
   2498 	error = pathbuf_copyin(link, &linkpb);
   2499 	if (error) {
   2500 		goto out1;
   2501 	}
   2502 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
   2503 	if ((error = fd_nameiat(l, fdlink, &nd)) != 0)
   2504 		goto out2;
   2505 	if (nd.ni_vp) {
   2506 		error = EEXIST;
   2507 		goto abortop;
   2508 	}
   2509 	/* Prevent hard links on directories. */
   2510 	if (vp->v_type == VDIR) {
   2511 		error = EPERM;
   2512 		goto abortop;
   2513 	}
   2514 	/* Prevent cross-mount operation. */
   2515 	if (nd.ni_dvp->v_mount != vp->v_mount) {
   2516 		error = EXDEV;
   2517 		goto abortop;
   2518 	}
   2519 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   2520 	VOP_UNLOCK(nd.ni_dvp);
   2521 	vrele(nd.ni_dvp);
   2522 out2:
   2523 	pathbuf_destroy(linkpb);
   2524 out1:
   2525 	vrele(vp);
   2526 	return (error);
   2527 abortop:
   2528 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2529 	if (nd.ni_dvp == nd.ni_vp)
   2530 		vrele(nd.ni_dvp);
   2531 	else
   2532 		vput(nd.ni_dvp);
   2533 	if (nd.ni_vp != NULL)
   2534 		vrele(nd.ni_vp);
   2535 	goto out2;
   2536 }
   2537 
   2538 int
   2539 sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval)
   2540 {
   2541 	/* {
   2542 		syscallarg(const char *) path;
   2543 		syscallarg(const char *) link;
   2544 	} */
   2545 	const char *path = SCARG(uap, path);
   2546 	const char *link = SCARG(uap, link);
   2547 
   2548 	return do_sys_linkat(l, AT_FDCWD, path, AT_FDCWD, link,
   2549 	    AT_SYMLINK_FOLLOW, retval);
   2550 }
   2551 
   2552 int
   2553 sys_linkat(struct lwp *l, const struct sys_linkat_args *uap,
   2554     register_t *retval)
   2555 {
   2556 	/* {
   2557 		syscallarg(int) fd1;
   2558 		syscallarg(const char *) name1;
   2559 		syscallarg(int) fd2;
   2560 		syscallarg(const char *) name2;
   2561 		syscallarg(int) flags;
   2562 	} */
   2563 	int fd1 = SCARG(uap, fd1);
   2564 	const char *name1 = SCARG(uap, name1);
   2565 	int fd2 = SCARG(uap, fd2);
   2566 	const char *name2 = SCARG(uap, name2);
   2567 	int follow;
   2568 
   2569 	follow = SCARG(uap, flags) & AT_SYMLINK_FOLLOW;
   2570 
   2571 	return do_sys_linkat(l, fd1, name1, fd2, name2, follow, retval);
   2572 }
   2573 
   2574 
   2575 int
   2576 do_sys_symlink(const char *patharg, const char *link, enum uio_seg seg)
   2577 {
   2578 	return do_sys_symlinkat(NULL, patharg, AT_FDCWD, link, seg);
   2579 }
   2580 
   2581 static int
   2582 do_sys_symlinkat(struct lwp *l, const char *patharg, int fdat,
   2583     const char *link, enum uio_seg seg)
   2584 {
   2585 	struct proc *p = curproc;
   2586 	struct vattr vattr;
   2587 	char *path;
   2588 	int error;
   2589 	size_t len;
   2590 	struct pathbuf *linkpb;
   2591 	struct nameidata nd;
   2592 
   2593 	KASSERT(l != NULL || fdat == AT_FDCWD);
   2594 
   2595 	path = PNBUF_GET();
   2596 	if (seg == UIO_USERSPACE) {
   2597 		if ((error = copyinstr(patharg, path, MAXPATHLEN, &len)) != 0)
   2598 			goto out1;
   2599 		if ((error = pathbuf_copyin(link, &linkpb)) != 0)
   2600 			goto out1;
   2601 	} else {
   2602 		len = strlen(patharg) + 1;
   2603 		KASSERT(len <= MAXPATHLEN);
   2604 		memcpy(path, patharg, len);
   2605 		linkpb = pathbuf_create(link);
   2606 		if (linkpb == NULL) {
   2607 			error = ENOMEM;
   2608 			goto out1;
   2609 		}
   2610 	}
   2611 	ktrkuser("symlink-target", path, len - 1);
   2612 
   2613 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
   2614 	if ((error = fd_nameiat(l, fdat, &nd)) != 0)
   2615 		goto out2;
   2616 	if (nd.ni_vp) {
   2617 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2618 		if (nd.ni_dvp == nd.ni_vp)
   2619 			vrele(nd.ni_dvp);
   2620 		else
   2621 			vput(nd.ni_dvp);
   2622 		vrele(nd.ni_vp);
   2623 		error = EEXIST;
   2624 		goto out2;
   2625 	}
   2626 	vattr_null(&vattr);
   2627 	vattr.va_type = VLNK;
   2628 	/* We will read cwdi->cwdi_cmask unlocked. */
   2629 	vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
   2630 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   2631 	if (error == 0)
   2632 		vrele(nd.ni_vp);
   2633 	vput(nd.ni_dvp);
   2634 out2:
   2635 	pathbuf_destroy(linkpb);
   2636 out1:
   2637 	PNBUF_PUT(path);
   2638 	return (error);
   2639 }
   2640 
   2641 /*
   2642  * Make a symbolic link.
   2643  */
   2644 /* ARGSUSED */
   2645 int
   2646 sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval)
   2647 {
   2648 	/* {
   2649 		syscallarg(const char *) path;
   2650 		syscallarg(const char *) link;
   2651 	} */
   2652 
   2653 	return do_sys_symlinkat(l, SCARG(uap, path), AT_FDCWD, SCARG(uap, link),
   2654 	    UIO_USERSPACE);
   2655 }
   2656 
   2657 int
   2658 sys_symlinkat(struct lwp *l, const struct sys_symlinkat_args *uap,
   2659     register_t *retval)
   2660 {
   2661 	/* {
   2662 		syscallarg(const char *) path1;
   2663 		syscallarg(int) fd;
   2664 		syscallarg(const char *) path2;
   2665 	} */
   2666 
   2667 	return do_sys_symlinkat(l, SCARG(uap, path1), SCARG(uap, fd),
   2668 	    SCARG(uap, path2), UIO_USERSPACE);
   2669 }
   2670 
   2671 /*
   2672  * Delete a whiteout from the filesystem.
   2673  */
   2674 /* ARGSUSED */
   2675 int
   2676 sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval)
   2677 {
   2678 	/* {
   2679 		syscallarg(const char *) path;
   2680 	} */
   2681 	int error;
   2682 	struct pathbuf *pb;
   2683 	struct nameidata nd;
   2684 
   2685 	error = pathbuf_copyin(SCARG(uap, path), &pb);
   2686 	if (error) {
   2687 		return error;
   2688 	}
   2689 
   2690 	NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, pb);
   2691 	error = namei(&nd);
   2692 	if (error) {
   2693 		pathbuf_destroy(pb);
   2694 		return (error);
   2695 	}
   2696 
   2697 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   2698 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2699 		if (nd.ni_dvp == nd.ni_vp)
   2700 			vrele(nd.ni_dvp);
   2701 		else
   2702 			vput(nd.ni_dvp);
   2703 		if (nd.ni_vp)
   2704 			vrele(nd.ni_vp);
   2705 		pathbuf_destroy(pb);
   2706 		return (EEXIST);
   2707 	}
   2708 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   2709 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2710 	vput(nd.ni_dvp);
   2711 	pathbuf_destroy(pb);
   2712 	return (error);
   2713 }
   2714 
   2715 /*
   2716  * Delete a name from the filesystem.
   2717  */
   2718 /* ARGSUSED */
   2719 int
   2720 sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval)
   2721 {
   2722 	/* {
   2723 		syscallarg(const char *) path;
   2724 	} */
   2725 
   2726 	return do_sys_unlinkat(l, AT_FDCWD, SCARG(uap, path), 0, UIO_USERSPACE);
   2727 }
   2728 
   2729 int
   2730 sys_unlinkat(struct lwp *l, const struct sys_unlinkat_args *uap,
   2731     register_t *retval)
   2732 {
   2733 	/* {
   2734 		syscallarg(int) fd;
   2735 		syscallarg(const char *) path;
   2736 		syscallarg(int) flag;
   2737 	} */
   2738 
   2739 	return do_sys_unlinkat(l, SCARG(uap, fd), SCARG(uap, path),
   2740 	    SCARG(uap, flag), UIO_USERSPACE);
   2741 }
   2742 
   2743 int
   2744 do_sys_unlink(const char *arg, enum uio_seg seg)
   2745 {
   2746 	return do_sys_unlinkat(NULL, AT_FDCWD, arg, 0, seg);
   2747 }
   2748 
   2749 static int
   2750 do_sys_unlinkat(struct lwp *l, int fdat, const char *arg, int flags,
   2751     enum uio_seg seg)
   2752 {
   2753 	struct vnode *vp;
   2754 	int error;
   2755 	struct pathbuf *pb;
   2756 	struct nameidata nd;
   2757 	const char *pathstring;
   2758 
   2759 	KASSERT(l != NULL || fdat == AT_FDCWD);
   2760 
   2761 	error = pathbuf_maybe_copyin(arg, seg, &pb);
   2762 	if (error) {
   2763 		return error;
   2764 	}
   2765 	pathstring = pathbuf_stringcopy_get(pb);
   2766 	if (pathstring == NULL) {
   2767 		pathbuf_destroy(pb);
   2768 		return ENOMEM;
   2769 	}
   2770 
   2771 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
   2772 	if ((error = fd_nameiat(l, fdat, &nd)) != 0)
   2773 		goto out;
   2774 	vp = nd.ni_vp;
   2775 
   2776 	/*
   2777 	 * The root of a mounted filesystem cannot be deleted.
   2778 	 */
   2779 	if ((vp->v_vflag & VV_ROOT) != 0) {
   2780 		error = EBUSY;
   2781 		goto abort;
   2782 	}
   2783 
   2784 	if ((vp->v_type == VDIR) && (vp->v_mountedhere != NULL)) {
   2785 		error = EBUSY;
   2786 		goto abort;
   2787 	}
   2788 
   2789 	/*
   2790 	 * No rmdir "." please.
   2791 	 */
   2792 	if (nd.ni_dvp == vp) {
   2793 		error = EINVAL;
   2794 		goto abort;
   2795 	}
   2796 
   2797 	/*
   2798 	 * AT_REMOVEDIR is required to remove a directory
   2799 	 */
   2800 	if (vp->v_type == VDIR) {
   2801 		if (!(flags & AT_REMOVEDIR)) {
   2802 			error = EPERM;
   2803 			goto abort;
   2804 		} else {
   2805 			error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   2806 			vput(nd.ni_dvp);
   2807 			goto out;
   2808 		}
   2809 	}
   2810 
   2811 	/*
   2812 	 * Starting here we only deal with non directories.
   2813 	 */
   2814 	if (flags & AT_REMOVEDIR) {
   2815 		error = ENOTDIR;
   2816 		goto abort;
   2817 	}
   2818 
   2819 #if NVERIEXEC > 0
   2820 	/* Handle remove requests for veriexec entries. */
   2821 	if ((error = veriexec_removechk(curlwp, nd.ni_vp, pathstring)) != 0) {
   2822 		goto abort;
   2823 	}
   2824 #endif /* NVERIEXEC > 0 */
   2825 
   2826 #ifdef FILEASSOC
   2827 	(void)fileassoc_file_delete(vp);
   2828 #endif /* FILEASSOC */
   2829 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   2830 	vput(nd.ni_dvp);
   2831 	goto out;
   2832 
   2833 abort:
   2834 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2835 	if (nd.ni_dvp == vp)
   2836 		vrele(nd.ni_dvp);
   2837 	else
   2838 		vput(nd.ni_dvp);
   2839 	vput(vp);
   2840 
   2841 out:
   2842 	pathbuf_stringcopy_put(pb, pathstring);
   2843 	pathbuf_destroy(pb);
   2844 	return (error);
   2845 }
   2846 
   2847 /*
   2848  * Reposition read/write file offset.
   2849  */
   2850 int
   2851 sys_lseek(struct lwp *l, const struct sys_lseek_args *uap, register_t *retval)
   2852 {
   2853 	/* {
   2854 		syscallarg(int) fd;
   2855 		syscallarg(int) pad;
   2856 		syscallarg(off_t) offset;
   2857 		syscallarg(int) whence;
   2858 	} */
   2859 	kauth_cred_t cred = l->l_cred;
   2860 	file_t *fp;
   2861 	struct vnode *vp;
   2862 	struct vattr vattr;
   2863 	off_t newoff;
   2864 	int error, fd;
   2865 
   2866 	fd = SCARG(uap, fd);
   2867 
   2868 	if ((fp = fd_getfile(fd)) == NULL)
   2869 		return (EBADF);
   2870 
   2871 	vp = fp->f_vnode;
   2872 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   2873 		error = ESPIPE;
   2874 		goto out;
   2875 	}
   2876 
   2877 	vn_lock(vp, LK_SHARED | LK_RETRY);
   2878 
   2879 	switch (SCARG(uap, whence)) {
   2880 	case SEEK_CUR:
   2881 		newoff = fp->f_offset + SCARG(uap, offset);
   2882 		break;
   2883 	case SEEK_END:
   2884 		error = VOP_GETATTR(vp, &vattr, cred);
   2885 		if (error) {
   2886 			VOP_UNLOCK(vp);
   2887 			goto out;
   2888 		}
   2889 		newoff = SCARG(uap, offset) + vattr.va_size;
   2890 		break;
   2891 	case SEEK_SET:
   2892 		newoff = SCARG(uap, offset);
   2893 		break;
   2894 	default:
   2895 		error = EINVAL;
   2896 		VOP_UNLOCK(vp);
   2897 		goto out;
   2898 	}
   2899 	VOP_UNLOCK(vp);
   2900 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
   2901 		*(off_t *)retval = fp->f_offset = newoff;
   2902 	}
   2903  out:
   2904  	fd_putfile(fd);
   2905 	return (error);
   2906 }
   2907 
   2908 /*
   2909  * Positional read system call.
   2910  */
   2911 int
   2912 sys_pread(struct lwp *l, const struct sys_pread_args *uap, register_t *retval)
   2913 {
   2914 	/* {
   2915 		syscallarg(int) fd;
   2916 		syscallarg(void *) buf;
   2917 		syscallarg(size_t) nbyte;
   2918 		syscallarg(off_t) offset;
   2919 	} */
   2920 	file_t *fp;
   2921 	struct vnode *vp;
   2922 	off_t offset;
   2923 	int error, fd = SCARG(uap, fd);
   2924 
   2925 	if ((fp = fd_getfile(fd)) == NULL)
   2926 		return (EBADF);
   2927 
   2928 	if ((fp->f_flag & FREAD) == 0) {
   2929 		fd_putfile(fd);
   2930 		return (EBADF);
   2931 	}
   2932 
   2933 	vp = fp->f_vnode;
   2934 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   2935 		error = ESPIPE;
   2936 		goto out;
   2937 	}
   2938 
   2939 	offset = SCARG(uap, offset);
   2940 
   2941 	/*
   2942 	 * XXX This works because no file systems actually
   2943 	 * XXX take any action on the seek operation.
   2944 	 */
   2945 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   2946 		goto out;
   2947 
   2948 	/* dofileread() will unuse the descriptor for us */
   2949 	return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   2950 	    &offset, 0, retval));
   2951 
   2952  out:
   2953 	fd_putfile(fd);
   2954 	return (error);
   2955 }
   2956 
   2957 /*
   2958  * Positional scatter read system call.
   2959  */
   2960 int
   2961 sys_preadv(struct lwp *l, const struct sys_preadv_args *uap, register_t *retval)
   2962 {
   2963 	/* {
   2964 		syscallarg(int) fd;
   2965 		syscallarg(const struct iovec *) iovp;
   2966 		syscallarg(int) iovcnt;
   2967 		syscallarg(off_t) offset;
   2968 	} */
   2969 	off_t offset = SCARG(uap, offset);
   2970 
   2971 	return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
   2972 	    SCARG(uap, iovcnt), &offset, 0, retval);
   2973 }
   2974 
   2975 /*
   2976  * Positional write system call.
   2977  */
   2978 int
   2979 sys_pwrite(struct lwp *l, const struct sys_pwrite_args *uap, register_t *retval)
   2980 {
   2981 	/* {
   2982 		syscallarg(int) fd;
   2983 		syscallarg(const void *) buf;
   2984 		syscallarg(size_t) nbyte;
   2985 		syscallarg(off_t) offset;
   2986 	} */
   2987 	file_t *fp;
   2988 	struct vnode *vp;
   2989 	off_t offset;
   2990 	int error, fd = SCARG(uap, fd);
   2991 
   2992 	if ((fp = fd_getfile(fd)) == NULL)
   2993 		return (EBADF);
   2994 
   2995 	if ((fp->f_flag & FWRITE) == 0) {
   2996 		fd_putfile(fd);
   2997 		return (EBADF);
   2998 	}
   2999 
   3000 	vp = fp->f_vnode;
   3001 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   3002 		error = ESPIPE;
   3003 		goto out;
   3004 	}
   3005 
   3006 	offset = SCARG(uap, offset);
   3007 
   3008 	/*
   3009 	 * XXX This works because no file systems actually
   3010 	 * XXX take any action on the seek operation.
   3011 	 */
   3012 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   3013 		goto out;
   3014 
   3015 	/* dofilewrite() will unuse the descriptor for us */
   3016 	return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   3017 	    &offset, 0, retval));
   3018 
   3019  out:
   3020 	fd_putfile(fd);
   3021 	return (error);
   3022 }
   3023 
   3024 /*
   3025  * Positional gather write system call.
   3026  */
   3027 int
   3028 sys_pwritev(struct lwp *l, const struct sys_pwritev_args *uap, register_t *retval)
   3029 {
   3030 	/* {
   3031 		syscallarg(int) fd;
   3032 		syscallarg(const struct iovec *) iovp;
   3033 		syscallarg(int) iovcnt;
   3034 		syscallarg(off_t) offset;
   3035 	} */
   3036 	off_t offset = SCARG(uap, offset);
   3037 
   3038 	return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
   3039 	    SCARG(uap, iovcnt), &offset, 0, retval);
   3040 }
   3041 
   3042 /*
   3043  * Check access permissions.
   3044  */
   3045 int
   3046 sys_access(struct lwp *l, const struct sys_access_args *uap, register_t *retval)
   3047 {
   3048 	/* {
   3049 		syscallarg(const char *) path;
   3050 		syscallarg(int) flags;
   3051 	} */
   3052 
   3053 	return do_sys_accessat(l, AT_FDCWD, SCARG(uap, path),
   3054 	     SCARG(uap, flags), 0);
   3055 }
   3056 
   3057 int
   3058 do_sys_accessat(struct lwp *l, int fdat, const char *path,
   3059     int mode, int flags)
   3060 {
   3061 	kauth_cred_t cred;
   3062 	struct vnode *vp;
   3063 	int error, nd_flag, vmode;
   3064 	struct pathbuf *pb;
   3065 	struct nameidata nd;
   3066 
   3067 	CTASSERT(F_OK == 0);
   3068 	if ((mode & ~(R_OK | W_OK | X_OK)) != 0) {
   3069 		/* nonsense mode */
   3070 		return EINVAL;
   3071 	}
   3072 
   3073 	nd_flag = FOLLOW | LOCKLEAF | LOCKSHARED | TRYEMULROOT;
   3074 	if (flags & AT_SYMLINK_NOFOLLOW)
   3075 		nd_flag &= ~FOLLOW;
   3076 
   3077 	error = pathbuf_copyin(path, &pb);
   3078 	if (error)
   3079 		return error;
   3080 
   3081 	NDINIT(&nd, LOOKUP, nd_flag, pb);
   3082 
   3083 	/* Override default credentials */
   3084 	cred = kauth_cred_dup(l->l_cred);
   3085 	if (!(flags & AT_EACCESS)) {
   3086 		kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
   3087 		kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
   3088 	}
   3089 	nd.ni_cnd.cn_cred = cred;
   3090 
   3091 	if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
   3092 		pathbuf_destroy(pb);
   3093 		goto out;
   3094 	}
   3095 	vp = nd.ni_vp;
   3096 	pathbuf_destroy(pb);
   3097 
   3098 	/* Flags == 0 means only check for existence. */
   3099 	if (mode) {
   3100 		vmode = 0;
   3101 		if (mode & R_OK)
   3102 			vmode |= VREAD;
   3103 		if (mode & W_OK)
   3104 			vmode |= VWRITE;
   3105 		if (mode & X_OK)
   3106 			vmode |= VEXEC;
   3107 
   3108 		error = VOP_ACCESS(vp, vmode, cred);
   3109 		if (!error && (vmode & VWRITE))
   3110 			error = vn_writechk(vp);
   3111 	}
   3112 	vput(vp);
   3113 out:
   3114 	kauth_cred_free(cred);
   3115 	return (error);
   3116 }
   3117 
   3118 int
   3119 sys_faccessat(struct lwp *l, const struct sys_faccessat_args *uap,
   3120     register_t *retval)
   3121 {
   3122 	/* {
   3123 		syscallarg(int) fd;
   3124 		syscallarg(const char *) path;
   3125 		syscallarg(int) amode;
   3126 		syscallarg(int) flag;
   3127 	} */
   3128 
   3129 	return do_sys_accessat(l, SCARG(uap, fd), SCARG(uap, path),
   3130 	     SCARG(uap, amode), SCARG(uap, flag));
   3131 }
   3132 
   3133 /*
   3134  * Common code for all sys_stat functions, including compat versions.
   3135  */
   3136 int
   3137 do_sys_stat(const char *userpath, unsigned int nd_flag,
   3138     struct stat *sb)
   3139 {
   3140 	return do_sys_statat(NULL, AT_FDCWD, userpath, nd_flag, sb);
   3141 }
   3142 
   3143 int
   3144 do_sys_statat(struct lwp *l, int fdat, const char *userpath,
   3145     unsigned int nd_flag, struct stat *sb)
   3146 {
   3147 	int error;
   3148 	struct pathbuf *pb;
   3149 	struct nameidata nd;
   3150 
   3151 	KASSERT(l != NULL || fdat == AT_FDCWD);
   3152 
   3153 	error = pathbuf_copyin(userpath, &pb);
   3154 	if (error) {
   3155 		return error;
   3156 	}
   3157 
   3158 	NDINIT(&nd, LOOKUP, nd_flag | LOCKLEAF | TRYEMULROOT, pb);
   3159 
   3160 	error = fd_nameiat(l, fdat, &nd);
   3161 	if (error != 0) {
   3162 		pathbuf_destroy(pb);
   3163 		return error;
   3164 	}
   3165 	error = vn_stat(nd.ni_vp, sb);
   3166 	vput(nd.ni_vp);
   3167 	pathbuf_destroy(pb);
   3168 	return error;
   3169 }
   3170 
   3171 /*
   3172  * Get file status; this version follows links.
   3173  */
   3174 /* ARGSUSED */
   3175 int
   3176 sys___stat50(struct lwp *l, const struct sys___stat50_args *uap, register_t *retval)
   3177 {
   3178 	/* {
   3179 		syscallarg(const char *) path;
   3180 		syscallarg(struct stat *) ub;
   3181 	} */
   3182 	struct stat sb;
   3183 	int error;
   3184 
   3185 	error = do_sys_statat(l, AT_FDCWD, SCARG(uap, path), FOLLOW, &sb);
   3186 	if (error)
   3187 		return error;
   3188 	return copyout(&sb, SCARG(uap, ub), sizeof(sb));
   3189 }
   3190 
   3191 /*
   3192  * Get file status; this version does not follow links.
   3193  */
   3194 /* ARGSUSED */
   3195 int
   3196 sys___lstat50(struct lwp *l, const struct sys___lstat50_args *uap, register_t *retval)
   3197 {
   3198 	/* {
   3199 		syscallarg(const char *) path;
   3200 		syscallarg(struct stat *) ub;
   3201 	} */
   3202 	struct stat sb;
   3203 	int error;
   3204 
   3205 	error = do_sys_statat(l, AT_FDCWD, SCARG(uap, path), NOFOLLOW, &sb);
   3206 	if (error)
   3207 		return error;
   3208 	return copyout(&sb, SCARG(uap, ub), sizeof(sb));
   3209 }
   3210 
   3211 int
   3212 sys_fstatat(struct lwp *l, const struct sys_fstatat_args *uap,
   3213     register_t *retval)
   3214 {
   3215 	/* {
   3216 		syscallarg(int) fd;
   3217 		syscallarg(const char *) path;
   3218 		syscallarg(struct stat *) buf;
   3219 		syscallarg(int) flag;
   3220 	} */
   3221 	unsigned int nd_flag;
   3222 	struct stat sb;
   3223 	int error;
   3224 
   3225 	if (SCARG(uap, flag) & AT_SYMLINK_NOFOLLOW)
   3226 		nd_flag = NOFOLLOW;
   3227 	else
   3228 		nd_flag = FOLLOW;
   3229 
   3230 	error = do_sys_statat(l, SCARG(uap, fd), SCARG(uap, path), nd_flag,
   3231 	    &sb);
   3232 	if (error)
   3233 		return error;
   3234 	return copyout(&sb, SCARG(uap, buf), sizeof(sb));
   3235 }
   3236 
   3237 static int
   3238 kern_pathconf(register_t *retval, const char *path, int name, int flag)
   3239 {
   3240 	int error;
   3241 	struct pathbuf *pb;
   3242 	struct nameidata nd;
   3243 
   3244 	error = pathbuf_copyin(path, &pb);
   3245 	if (error) {
   3246 		return error;
   3247 	}
   3248 	NDINIT(&nd, LOOKUP, flag | LOCKLEAF | TRYEMULROOT, pb);
   3249 	if ((error = namei(&nd)) != 0) {
   3250 		pathbuf_destroy(pb);
   3251 		return error;
   3252 	}
   3253 	error = VOP_PATHCONF(nd.ni_vp, name, retval);
   3254 	vput(nd.ni_vp);
   3255 	pathbuf_destroy(pb);
   3256 	return error;
   3257 }
   3258 
   3259 /*
   3260  * Get configurable pathname variables.
   3261  */
   3262 /* ARGSUSED */
   3263 int
   3264 sys_pathconf(struct lwp *l, const struct sys_pathconf_args *uap,
   3265     register_t *retval)
   3266 {
   3267 	/* {
   3268 		syscallarg(const char *) path;
   3269 		syscallarg(int) name;
   3270 	} */
   3271 	return kern_pathconf(retval, SCARG(uap, path), SCARG(uap, name),
   3272 	    FOLLOW);
   3273 }
   3274 
   3275 /* ARGSUSED */
   3276 int
   3277 sys_lpathconf(struct lwp *l, const struct sys_lpathconf_args *uap,
   3278     register_t *retval)
   3279 {
   3280 	/* {
   3281 		syscallarg(const char *) path;
   3282 		syscallarg(int) name;
   3283 	} */
   3284 	return kern_pathconf(retval, SCARG(uap, path), SCARG(uap, name),
   3285 	    NOFOLLOW);
   3286 }
   3287 
   3288 /*
   3289  * Return target name of a symbolic link.
   3290  */
   3291 /* ARGSUSED */
   3292 int
   3293 sys_readlink(struct lwp *l, const struct sys_readlink_args *uap,
   3294     register_t *retval)
   3295 {
   3296 	/* {
   3297 		syscallarg(const char *) path;
   3298 		syscallarg(char *) buf;
   3299 		syscallarg(size_t) count;
   3300 	} */
   3301 	return do_sys_readlinkat(l, AT_FDCWD, SCARG(uap, path),
   3302 	    SCARG(uap, buf), SCARG(uap, count), retval);
   3303 }
   3304 
   3305 static int
   3306 do_sys_readlinkat(struct lwp *l, int fdat, const char *path, char *buf,
   3307     size_t count, register_t *retval)
   3308 {
   3309 	struct vnode *vp;
   3310 	struct iovec aiov;
   3311 	struct uio auio;
   3312 	int error;
   3313 	struct pathbuf *pb;
   3314 	struct nameidata nd;
   3315 
   3316 	error = pathbuf_copyin(path, &pb);
   3317 	if (error) {
   3318 		return error;
   3319 	}
   3320 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | LOCKSHARED | TRYEMULROOT, pb);
   3321 	if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
   3322 		pathbuf_destroy(pb);
   3323 		return error;
   3324 	}
   3325 	vp = nd.ni_vp;
   3326 	pathbuf_destroy(pb);
   3327 	if (vp->v_type != VLNK)
   3328 		error = EINVAL;
   3329 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   3330 	    (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) {
   3331 		aiov.iov_base = buf;
   3332 		aiov.iov_len = count;
   3333 		auio.uio_iov = &aiov;
   3334 		auio.uio_iovcnt = 1;
   3335 		auio.uio_offset = 0;
   3336 		auio.uio_rw = UIO_READ;
   3337 		KASSERT(l == curlwp);
   3338 		auio.uio_vmspace = l->l_proc->p_vmspace;
   3339 		auio.uio_resid = count;
   3340 		if ((error = VOP_READLINK(vp, &auio, l->l_cred)) == 0)
   3341 			*retval = count - auio.uio_resid;
   3342 	}
   3343 	vput(vp);
   3344 	return (error);
   3345 }
   3346 
   3347 int
   3348 sys_readlinkat(struct lwp *l, const struct sys_readlinkat_args *uap,
   3349     register_t *retval)
   3350 {
   3351 	/* {
   3352 		syscallarg(int) fd;
   3353 		syscallarg(const char *) path;
   3354 		syscallarg(char *) buf;
   3355 		syscallarg(size_t) bufsize;
   3356 	} */
   3357 
   3358 	return do_sys_readlinkat(l, SCARG(uap, fd), SCARG(uap, path),
   3359 	    SCARG(uap, buf), SCARG(uap, bufsize), retval);
   3360 }
   3361 
   3362 /*
   3363  * Change flags of a file given a path name.
   3364  */
   3365 /* ARGSUSED */
   3366 int
   3367 sys_chflags(struct lwp *l, const struct sys_chflags_args *uap, register_t *retval)
   3368 {
   3369 	/* {
   3370 		syscallarg(const char *) path;
   3371 		syscallarg(u_long) flags;
   3372 	} */
   3373 	struct vnode *vp;
   3374 	int error;
   3375 
   3376 	error = namei_simple_user(SCARG(uap, path),
   3377 				NSM_FOLLOW_TRYEMULROOT, &vp);
   3378 	if (error != 0)
   3379 		return (error);
   3380 	error = change_flags(vp, SCARG(uap, flags), l);
   3381 	vput(vp);
   3382 	return (error);
   3383 }
   3384 
   3385 /*
   3386  * Change flags of a file given a file descriptor.
   3387  */
   3388 /* ARGSUSED */
   3389 int
   3390 sys_fchflags(struct lwp *l, const struct sys_fchflags_args *uap, register_t *retval)
   3391 {
   3392 	/* {
   3393 		syscallarg(int) fd;
   3394 		syscallarg(u_long) flags;
   3395 	} */
   3396 	struct vnode *vp;
   3397 	file_t *fp;
   3398 	int error;
   3399 
   3400 	/* fd_getvnode() will use the descriptor for us */
   3401 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3402 		return (error);
   3403 	vp = fp->f_vnode;
   3404 	error = change_flags(vp, SCARG(uap, flags), l);
   3405 	VOP_UNLOCK(vp);
   3406 	fd_putfile(SCARG(uap, fd));
   3407 	return (error);
   3408 }
   3409 
   3410 /*
   3411  * Change flags of a file given a path name; this version does
   3412  * not follow links.
   3413  */
   3414 int
   3415 sys_lchflags(struct lwp *l, const struct sys_lchflags_args *uap, register_t *retval)
   3416 {
   3417 	/* {
   3418 		syscallarg(const char *) path;
   3419 		syscallarg(u_long) flags;
   3420 	} */
   3421 	struct vnode *vp;
   3422 	int error;
   3423 
   3424 	error = namei_simple_user(SCARG(uap, path),
   3425 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
   3426 	if (error != 0)
   3427 		return (error);
   3428 	error = change_flags(vp, SCARG(uap, flags), l);
   3429 	vput(vp);
   3430 	return (error);
   3431 }
   3432 
   3433 /*
   3434  * Common routine to change flags of a file.
   3435  */
   3436 int
   3437 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
   3438 {
   3439 	struct vattr vattr;
   3440 	int error;
   3441 
   3442 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3443 
   3444 	vattr_null(&vattr);
   3445 	vattr.va_flags = flags;
   3446 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   3447 
   3448 	return (error);
   3449 }
   3450 
   3451 /*
   3452  * Change mode of a file given path name; this version follows links.
   3453  */
   3454 /* ARGSUSED */
   3455 int
   3456 sys_chmod(struct lwp *l, const struct sys_chmod_args *uap, register_t *retval)
   3457 {
   3458 	/* {
   3459 		syscallarg(const char *) path;
   3460 		syscallarg(int) mode;
   3461 	} */
   3462 	return do_sys_chmodat(l, AT_FDCWD, SCARG(uap, path),
   3463 			      SCARG(uap, mode), 0);
   3464 }
   3465 
   3466 int
   3467 do_sys_chmodat(struct lwp *l, int fdat, const char *path, int mode, int flags)
   3468 {
   3469 	int error;
   3470 	struct vnode *vp;
   3471 	namei_simple_flags_t ns_flag;
   3472 
   3473 	if (flags & AT_SYMLINK_NOFOLLOW)
   3474 		ns_flag = NSM_NOFOLLOW_TRYEMULROOT;
   3475 	else
   3476 		ns_flag = NSM_FOLLOW_TRYEMULROOT;
   3477 
   3478 	error = fd_nameiat_simple_user(l, fdat, path, ns_flag, &vp);
   3479 	if (error != 0)
   3480 		return error;
   3481 
   3482 	error = change_mode(vp, mode, l);
   3483 
   3484 	vrele(vp);
   3485 
   3486 	return (error);
   3487 }
   3488 
   3489 /*
   3490  * Change mode of a file given a file descriptor.
   3491  */
   3492 /* ARGSUSED */
   3493 int
   3494 sys_fchmod(struct lwp *l, const struct sys_fchmod_args *uap, register_t *retval)
   3495 {
   3496 	/* {
   3497 		syscallarg(int) fd;
   3498 		syscallarg(int) mode;
   3499 	} */
   3500 	file_t *fp;
   3501 	int error;
   3502 
   3503 	/* fd_getvnode() will use the descriptor for us */
   3504 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3505 		return (error);
   3506 	error = change_mode(fp->f_vnode, SCARG(uap, mode), l);
   3507 	fd_putfile(SCARG(uap, fd));
   3508 	return (error);
   3509 }
   3510 
   3511 int
   3512 sys_fchmodat(struct lwp *l, const struct sys_fchmodat_args *uap,
   3513     register_t *retval)
   3514 {
   3515 	/* {
   3516 		syscallarg(int) fd;
   3517 		syscallarg(const char *) path;
   3518 		syscallarg(int) mode;
   3519 		syscallarg(int) flag;
   3520 	} */
   3521 
   3522 	return do_sys_chmodat(l, SCARG(uap, fd), SCARG(uap, path),
   3523 			      SCARG(uap, mode), SCARG(uap, flag));
   3524 }
   3525 
   3526 /*
   3527  * Change mode of a file given path name; this version does not follow links.
   3528  */
   3529 /* ARGSUSED */
   3530 int
   3531 sys_lchmod(struct lwp *l, const struct sys_lchmod_args *uap, register_t *retval)
   3532 {
   3533 	/* {
   3534 		syscallarg(const char *) path;
   3535 		syscallarg(int) mode;
   3536 	} */
   3537 	int error;
   3538 	struct vnode *vp;
   3539 
   3540 	error = namei_simple_user(SCARG(uap, path),
   3541 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
   3542 	if (error != 0)
   3543 		return (error);
   3544 
   3545 	error = change_mode(vp, SCARG(uap, mode), l);
   3546 
   3547 	vrele(vp);
   3548 	return (error);
   3549 }
   3550 
   3551 /*
   3552  * Common routine to set mode given a vnode.
   3553  */
   3554 static int
   3555 change_mode(struct vnode *vp, int mode, struct lwp *l)
   3556 {
   3557 	struct vattr vattr;
   3558 	int error;
   3559 
   3560 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3561 	vattr_null(&vattr);
   3562 	vattr.va_mode = mode & ALLPERMS;
   3563 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   3564 	VOP_UNLOCK(vp);
   3565 	return (error);
   3566 }
   3567 
   3568 /*
   3569  * Set ownership given a path name; this version follows links.
   3570  */
   3571 /* ARGSUSED */
   3572 int
   3573 sys_chown(struct lwp *l, const struct sys_chown_args *uap, register_t *retval)
   3574 {
   3575 	/* {
   3576 		syscallarg(const char *) path;
   3577 		syscallarg(uid_t) uid;
   3578 		syscallarg(gid_t) gid;
   3579 	} */
   3580 	return do_sys_chownat(l, AT_FDCWD, SCARG(uap, path), SCARG(uap,uid),
   3581 			      SCARG(uap, gid), 0);
   3582 }
   3583 
   3584 int
   3585 do_sys_chownat(struct lwp *l, int fdat, const char *path, uid_t uid,
   3586    gid_t gid, int flags)
   3587 {
   3588 	int error;
   3589 	struct vnode *vp;
   3590 	namei_simple_flags_t ns_flag;
   3591 
   3592 	if (flags & AT_SYMLINK_NOFOLLOW)
   3593 		ns_flag = NSM_NOFOLLOW_TRYEMULROOT;
   3594 	else
   3595 		ns_flag = NSM_FOLLOW_TRYEMULROOT;
   3596 
   3597 	error = fd_nameiat_simple_user(l, fdat, path, ns_flag, &vp);
   3598 	if (error != 0)
   3599 		return error;
   3600 
   3601 	error = change_owner(vp, uid, gid, l, 0);
   3602 
   3603 	vrele(vp);
   3604 
   3605 	return (error);
   3606 }
   3607 
   3608 /*
   3609  * Set ownership given a path name; this version follows links.
   3610  * Provides POSIX semantics.
   3611  */
   3612 /* ARGSUSED */
   3613 int
   3614 sys___posix_chown(struct lwp *l, const struct sys___posix_chown_args *uap, register_t *retval)
   3615 {
   3616 	/* {
   3617 		syscallarg(const char *) path;
   3618 		syscallarg(uid_t) uid;
   3619 		syscallarg(gid_t) gid;
   3620 	} */
   3621 	int error;
   3622 	struct vnode *vp;
   3623 
   3624 	error = namei_simple_user(SCARG(uap, path),
   3625 				NSM_FOLLOW_TRYEMULROOT, &vp);
   3626 	if (error != 0)
   3627 		return (error);
   3628 
   3629 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
   3630 
   3631 	vrele(vp);
   3632 	return (error);
   3633 }
   3634 
   3635 /*
   3636  * Set ownership given a file descriptor.
   3637  */
   3638 /* ARGSUSED */
   3639 int
   3640 sys_fchown(struct lwp *l, const struct sys_fchown_args *uap, register_t *retval)
   3641 {
   3642 	/* {
   3643 		syscallarg(int) fd;
   3644 		syscallarg(uid_t) uid;
   3645 		syscallarg(gid_t) gid;
   3646 	} */
   3647 	int error;
   3648 	file_t *fp;
   3649 
   3650 	/* fd_getvnode() will use the descriptor for us */
   3651 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3652 		return (error);
   3653 	error = change_owner(fp->f_vnode, SCARG(uap, uid), SCARG(uap, gid),
   3654 	    l, 0);
   3655 	fd_putfile(SCARG(uap, fd));
   3656 	return (error);
   3657 }
   3658 
   3659 int
   3660 sys_fchownat(struct lwp *l, const struct sys_fchownat_args *uap,
   3661     register_t *retval)
   3662 {
   3663 	/* {
   3664 		syscallarg(int) fd;
   3665 		syscallarg(const char *) path;
   3666 		syscallarg(uid_t) owner;
   3667 		syscallarg(gid_t) group;
   3668 		syscallarg(int) flag;
   3669 	} */
   3670 
   3671 	return do_sys_chownat(l, SCARG(uap, fd), SCARG(uap, path),
   3672 			      SCARG(uap, owner), SCARG(uap, group),
   3673 			      SCARG(uap, flag));
   3674 }
   3675 
   3676 /*
   3677  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   3678  */
   3679 /* ARGSUSED */
   3680 int
   3681 sys___posix_fchown(struct lwp *l, const struct sys___posix_fchown_args *uap, register_t *retval)
   3682 {
   3683 	/* {
   3684 		syscallarg(int) fd;
   3685 		syscallarg(uid_t) uid;
   3686 		syscallarg(gid_t) gid;
   3687 	} */
   3688 	int error;
   3689 	file_t *fp;
   3690 
   3691 	/* fd_getvnode() will use the descriptor for us */
   3692 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3693 		return (error);
   3694 	error = change_owner(fp->f_vnode, SCARG(uap, uid), SCARG(uap, gid),
   3695 	    l, 1);
   3696 	fd_putfile(SCARG(uap, fd));
   3697 	return (error);
   3698 }
   3699 
   3700 /*
   3701  * Set ownership given a path name; this version does not follow links.
   3702  */
   3703 /* ARGSUSED */
   3704 int
   3705 sys_lchown(struct lwp *l, const struct sys_lchown_args *uap, register_t *retval)
   3706 {
   3707 	/* {
   3708 		syscallarg(const char *) path;
   3709 		syscallarg(uid_t) uid;
   3710 		syscallarg(gid_t) gid;
   3711 	} */
   3712 	int error;
   3713 	struct vnode *vp;
   3714 
   3715 	error = namei_simple_user(SCARG(uap, path),
   3716 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
   3717 	if (error != 0)
   3718 		return (error);
   3719 
   3720 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
   3721 
   3722 	vrele(vp);
   3723 	return (error);
   3724 }
   3725 
   3726 /*
   3727  * Set ownership given a path name; this version does not follow links.
   3728  * Provides POSIX/XPG semantics.
   3729  */
   3730 /* ARGSUSED */
   3731 int
   3732 sys___posix_lchown(struct lwp *l, const struct sys___posix_lchown_args *uap, register_t *retval)
   3733 {
   3734 	/* {
   3735 		syscallarg(const char *) path;
   3736 		syscallarg(uid_t) uid;
   3737 		syscallarg(gid_t) gid;
   3738 	} */
   3739 	int error;
   3740 	struct vnode *vp;
   3741 
   3742 	error = namei_simple_user(SCARG(uap, path),
   3743 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
   3744 	if (error != 0)
   3745 		return (error);
   3746 
   3747 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
   3748 
   3749 	vrele(vp);
   3750 	return (error);
   3751 }
   3752 
   3753 /*
   3754  * Common routine to set ownership given a vnode.
   3755  */
   3756 static int
   3757 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
   3758     int posix_semantics)
   3759 {
   3760 	struct vattr vattr;
   3761 	mode_t newmode;
   3762 	int error;
   3763 
   3764 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3765 	if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
   3766 		goto out;
   3767 
   3768 #define CHANGED(x) ((int)(x) != -1)
   3769 	newmode = vattr.va_mode;
   3770 	if (posix_semantics) {
   3771 		/*
   3772 		 * POSIX/XPG semantics: if the caller is not the super-user,
   3773 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   3774 		 * the XPG consider the behaviour for calls by the super-user
   3775 		 * implementation-defined; we leave the set-user-id and set-
   3776 		 * group-id settings intact in that case.
   3777 		 */
   3778 		if (vattr.va_mode & S_ISUID) {
   3779 			if (kauth_authorize_vnode(l->l_cred,
   3780 			    KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0)
   3781 				newmode &= ~S_ISUID;
   3782 		}
   3783 		if (vattr.va_mode & S_ISGID) {
   3784 			if (kauth_authorize_vnode(l->l_cred,
   3785 			    KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0)
   3786 				newmode &= ~S_ISGID;
   3787 		}
   3788 	} else {
   3789 		/*
   3790 		 * NetBSD semantics: when changing owner and/or group,
   3791 		 * clear the respective bit(s).
   3792 		 */
   3793 		if (CHANGED(uid))
   3794 			newmode &= ~S_ISUID;
   3795 		if (CHANGED(gid))
   3796 			newmode &= ~S_ISGID;
   3797 	}
   3798 	/* Update va_mode iff altered. */
   3799 	if (vattr.va_mode == newmode)
   3800 		newmode = VNOVAL;
   3801 
   3802 	vattr_null(&vattr);
   3803 	vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
   3804 	vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
   3805 	vattr.va_mode = newmode;
   3806 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   3807 #undef CHANGED
   3808 
   3809 out:
   3810 	VOP_UNLOCK(vp);
   3811 	return (error);
   3812 }
   3813 
   3814 /*
   3815  * Set the access and modification times given a path name; this
   3816  * version follows links.
   3817  */
   3818 /* ARGSUSED */
   3819 int
   3820 sys___utimes50(struct lwp *l, const struct sys___utimes50_args *uap,
   3821     register_t *retval)
   3822 {
   3823 	/* {
   3824 		syscallarg(const char *) path;
   3825 		syscallarg(const struct timeval *) tptr;
   3826 	} */
   3827 
   3828 	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
   3829 	    SCARG(uap, tptr), UIO_USERSPACE);
   3830 }
   3831 
   3832 /*
   3833  * Set the access and modification times given a file descriptor.
   3834  */
   3835 /* ARGSUSED */
   3836 int
   3837 sys___futimes50(struct lwp *l, const struct sys___futimes50_args *uap,
   3838     register_t *retval)
   3839 {
   3840 	/* {
   3841 		syscallarg(int) fd;
   3842 		syscallarg(const struct timeval *) tptr;
   3843 	} */
   3844 	int error;
   3845 	file_t *fp;
   3846 
   3847 	/* fd_getvnode() will use the descriptor for us */
   3848 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3849 		return (error);
   3850 	error = do_sys_utimes(l, fp->f_vnode, NULL, 0, SCARG(uap, tptr),
   3851 	    UIO_USERSPACE);
   3852 	fd_putfile(SCARG(uap, fd));
   3853 	return (error);
   3854 }
   3855 
   3856 int
   3857 sys_futimens(struct lwp *l, const struct sys_futimens_args *uap,
   3858     register_t *retval)
   3859 {
   3860 	/* {
   3861 		syscallarg(int) fd;
   3862 		syscallarg(const struct timespec *) tptr;
   3863 	} */
   3864 	int error;
   3865 	file_t *fp;
   3866 
   3867 	/* fd_getvnode() will use the descriptor for us */
   3868 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3869 		return (error);
   3870 	error = do_sys_utimensat(l, AT_FDCWD, fp->f_vnode, NULL, 0,
   3871 	    SCARG(uap, tptr), UIO_USERSPACE);
   3872 	fd_putfile(SCARG(uap, fd));
   3873 	return (error);
   3874 }
   3875 
   3876 /*
   3877  * Set the access and modification times given a path name; this
   3878  * version does not follow links.
   3879  */
   3880 int
   3881 sys___lutimes50(struct lwp *l, const struct sys___lutimes50_args *uap,
   3882     register_t *retval)
   3883 {
   3884 	/* {
   3885 		syscallarg(const char *) path;
   3886 		syscallarg(const struct timeval *) tptr;
   3887 	} */
   3888 
   3889 	return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
   3890 	    SCARG(uap, tptr), UIO_USERSPACE);
   3891 }
   3892 
   3893 int
   3894 sys_utimensat(struct lwp *l, const struct sys_utimensat_args *uap,
   3895     register_t *retval)
   3896 {
   3897 	/* {
   3898 		syscallarg(int) fd;
   3899 		syscallarg(const char *) path;
   3900 		syscallarg(const struct timespec *) tptr;
   3901 		syscallarg(int) flag;
   3902 	} */
   3903 	int follow;
   3904 	const struct timespec *tptr;
   3905 	int error;
   3906 
   3907 	tptr = SCARG(uap, tptr);
   3908 	follow = (SCARG(uap, flag) & AT_SYMLINK_NOFOLLOW) ? NOFOLLOW : FOLLOW;
   3909 
   3910 	error = do_sys_utimensat(l, SCARG(uap, fd), NULL,
   3911 	    SCARG(uap, path), follow, tptr, UIO_USERSPACE);
   3912 
   3913 	return error;
   3914 }
   3915 
   3916 /*
   3917  * Common routine to set access and modification times given a vnode.
   3918  */
   3919 int
   3920 do_sys_utimens(struct lwp *l, struct vnode *vp, const char *path, int flag,
   3921     const struct timespec *tptr, enum uio_seg seg)
   3922 {
   3923 	return do_sys_utimensat(l, AT_FDCWD, vp, path, flag, tptr, seg);
   3924 }
   3925 
   3926 int
   3927 do_sys_utimensat(struct lwp *l, int fdat, struct vnode *vp,
   3928     const char *path, int flag, const struct timespec *tptr, enum uio_seg seg)
   3929 {
   3930 	struct vattr vattr;
   3931 	int error, dorele = 0;
   3932 	namei_simple_flags_t sflags;
   3933 	bool vanull, setbirthtime;
   3934 	struct timespec ts[2];
   3935 
   3936 	KASSERT(l != NULL || fdat == AT_FDCWD);
   3937 
   3938 	/*
   3939 	 * I have checked all callers and they pass either FOLLOW,
   3940 	 * NOFOLLOW, or 0 (when they don't pass a path), and NOFOLLOW
   3941 	 * is 0. More to the point, they don't pass anything else.
   3942 	 * Let's keep it that way at least until the namei interfaces
   3943 	 * are fully sanitized.
   3944 	 */
   3945 	KASSERT(flag == NOFOLLOW || flag == FOLLOW);
   3946 	sflags = (flag == FOLLOW) ?
   3947 		NSM_FOLLOW_TRYEMULROOT : NSM_NOFOLLOW_TRYEMULROOT;
   3948 
   3949 	if (tptr == NULL) {
   3950 		vanull = true;
   3951 		nanotime(&ts[0]);
   3952 		ts[1] = ts[0];
   3953 	} else {
   3954 		vanull = false;
   3955 		if (seg != UIO_SYSSPACE) {
   3956 			error = copyin(tptr, ts, sizeof (ts));
   3957 			if (error != 0)
   3958 				return error;
   3959 		} else {
   3960 			ts[0] = tptr[0];
   3961 			ts[1] = tptr[1];
   3962 		}
   3963 	}
   3964 
   3965 	if (ts[0].tv_nsec == UTIME_NOW) {
   3966 		nanotime(&ts[0]);
   3967 		if (ts[1].tv_nsec == UTIME_NOW) {
   3968 			vanull = true;
   3969 			ts[1] = ts[0];
   3970 		}
   3971 	} else if (ts[1].tv_nsec == UTIME_NOW)
   3972 		nanotime(&ts[1]);
   3973 
   3974 	if (vp == NULL) {
   3975 		/* note: SEG describes TPTR, not PATH; PATH is always user */
   3976 		error = fd_nameiat_simple_user(l, fdat, path, sflags, &vp);
   3977 		if (error != 0)
   3978 			return error;
   3979 		dorele = 1;
   3980 	}
   3981 
   3982 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3983 	setbirthtime = (VOP_GETATTR(vp, &vattr, l->l_cred) == 0 &&
   3984 	    timespeccmp(&ts[1], &vattr.va_birthtime, <));
   3985 	vattr_null(&vattr);
   3986 
   3987 	if (ts[0].tv_nsec != UTIME_OMIT)
   3988 		vattr.va_atime = ts[0];
   3989 
   3990 	if (ts[1].tv_nsec != UTIME_OMIT) {
   3991 		vattr.va_mtime = ts[1];
   3992 		if (setbirthtime)
   3993 			vattr.va_birthtime = ts[1];
   3994 	}
   3995 
   3996 	if (vanull)
   3997 		vattr.va_vaflags |= VA_UTIMES_NULL;
   3998 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   3999 	VOP_UNLOCK(vp);
   4000 
   4001 	if (dorele != 0)
   4002 		vrele(vp);
   4003 
   4004 	return error;
   4005 }
   4006 
   4007 int
   4008 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
   4009     const struct timeval *tptr, enum uio_seg seg)
   4010 {
   4011 	struct timespec ts[2];
   4012 	struct timespec *tsptr = NULL;
   4013 	int error;
   4014 
   4015 	if (tptr != NULL) {
   4016 		struct timeval tv[2];
   4017 
   4018 		if (seg != UIO_SYSSPACE) {
   4019 			error = copyin(tptr, tv, sizeof(tv));
   4020 			if (error != 0)
   4021 				return error;
   4022 			tptr = tv;
   4023 		}
   4024 
   4025 		if ((tptr[0].tv_usec == UTIME_NOW) ||
   4026 		    (tptr[0].tv_usec == UTIME_OMIT))
   4027 			ts[0].tv_nsec = tptr[0].tv_usec;
   4028 		else {
   4029 			if (tptr[0].tv_usec < 0 || tptr[0].tv_usec >= 1000000)
   4030 				return EINVAL;
   4031 
   4032 			TIMEVAL_TO_TIMESPEC(&tptr[0], &ts[0]);
   4033 		}
   4034 
   4035 		if ((tptr[1].tv_usec == UTIME_NOW) ||
   4036 		    (tptr[1].tv_usec == UTIME_OMIT))
   4037 			ts[1].tv_nsec = tptr[1].tv_usec;
   4038 		else {
   4039 			if (tptr[1].tv_usec < 0 || tptr[1].tv_usec >= 1000000)
   4040 				return EINVAL;
   4041 
   4042 			TIMEVAL_TO_TIMESPEC(&tptr[1], &ts[1]);
   4043 		}
   4044 
   4045 		tsptr = &ts[0];
   4046 	}
   4047 
   4048 	return do_sys_utimens(l, vp, path, flag, tsptr, UIO_SYSSPACE);
   4049 }
   4050 
   4051 /*
   4052  * Truncate a file given its path name.
   4053  */
   4054 /* ARGSUSED */
   4055 int
   4056 sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval)
   4057 {
   4058 	/* {
   4059 		syscallarg(const char *) path;
   4060 		syscallarg(int) pad;
   4061 		syscallarg(off_t) length;
   4062 	} */
   4063 	struct vnode *vp;
   4064 	struct vattr vattr;
   4065 	int error;
   4066 
   4067 	if (SCARG(uap, length) < 0)
   4068 		return EINVAL;
   4069 
   4070 	error = namei_simple_user(SCARG(uap, path),
   4071 				NSM_FOLLOW_TRYEMULROOT, &vp);
   4072 	if (error != 0)
   4073 		return (error);
   4074 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   4075 	if (vp->v_type == VDIR)
   4076 		error = EISDIR;
   4077 	else if ((error = vn_writechk(vp)) == 0 &&
   4078 	    (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
   4079 		vattr_null(&vattr);
   4080 		vattr.va_size = SCARG(uap, length);
   4081 		error = VOP_SETATTR(vp, &vattr, l->l_cred);
   4082 	}
   4083 	vput(vp);
   4084 	return (error);
   4085 }
   4086 
   4087 /*
   4088  * Truncate a file given a file descriptor.
   4089  */
   4090 /* ARGSUSED */
   4091 int
   4092 sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
   4093 {
   4094 	/* {
   4095 		syscallarg(int) fd;
   4096 		syscallarg(int) pad;
   4097 		syscallarg(off_t) length;
   4098 	} */
   4099 	struct vattr vattr;
   4100 	struct vnode *vp;
   4101 	file_t *fp;
   4102 	int error;
   4103 
   4104 	if (SCARG(uap, length) < 0)
   4105 		return EINVAL;
   4106 
   4107 	/* fd_getvnode() will use the descriptor for us */
   4108 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   4109 		return (error);
   4110 	if ((fp->f_flag & FWRITE) == 0) {
   4111 		error = EINVAL;
   4112 		goto out;
   4113 	}
   4114 	vp = fp->f_vnode;
   4115 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   4116 	if (vp->v_type == VDIR)
   4117 		error = EISDIR;
   4118 	else if ((error = vn_writechk(vp)) == 0) {
   4119 		vattr_null(&vattr);
   4120 		vattr.va_size = SCARG(uap, length);
   4121 		error = VOP_SETATTR(vp, &vattr, fp->f_cred);
   4122 	}
   4123 	VOP_UNLOCK(vp);
   4124  out:
   4125 	fd_putfile(SCARG(uap, fd));
   4126 	return (error);
   4127 }
   4128 
   4129 /*
   4130  * Sync an open file.
   4131  */
   4132 /* ARGSUSED */
   4133 int
   4134 sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
   4135 {
   4136 	/* {
   4137 		syscallarg(int) fd;
   4138 	} */
   4139 	struct vnode *vp;
   4140 	file_t *fp;
   4141 	int error;
   4142 
   4143 	/* fd_getvnode() will use the descriptor for us */
   4144 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   4145 		return (error);
   4146 	vp = fp->f_vnode;
   4147 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   4148 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
   4149 	VOP_UNLOCK(vp);
   4150 	fd_putfile(SCARG(uap, fd));
   4151 	return (error);
   4152 }
   4153 
   4154 /*
   4155  * Sync a range of file data.  API modeled after that found in AIX.
   4156  *
   4157  * FDATASYNC indicates that we need only save enough metadata to be able
   4158  * to re-read the written data.
   4159  */
   4160 /* ARGSUSED */
   4161 int
   4162 sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
   4163 {
   4164 	/* {
   4165 		syscallarg(int) fd;
   4166 		syscallarg(int) flags;
   4167 		syscallarg(off_t) start;
   4168 		syscallarg(off_t) length;
   4169 	} */
   4170 	struct vnode *vp;
   4171 	file_t *fp;
   4172 	int flags, nflags;
   4173 	off_t s, e, len;
   4174 	int error;
   4175 
   4176 	/* fd_getvnode() will use the descriptor for us */
   4177 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   4178 		return (error);
   4179 
   4180 	if ((fp->f_flag & FWRITE) == 0) {
   4181 		error = EBADF;
   4182 		goto out;
   4183 	}
   4184 
   4185 	flags = SCARG(uap, flags);
   4186 	if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
   4187 	    ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
   4188 		error = EINVAL;
   4189 		goto out;
   4190 	}
   4191 	/* Now set up the flags for value(s) to pass to VOP_FSYNC() */
   4192 	if (flags & FDATASYNC)
   4193 		nflags = FSYNC_DATAONLY | FSYNC_WAIT;
   4194 	else
   4195 		nflags = FSYNC_WAIT;
   4196 	if (flags & FDISKSYNC)
   4197 		nflags |= FSYNC_CACHE;
   4198 
   4199 	len = SCARG(uap, length);
   4200 	/* If length == 0, we do the whole file, and s = e = 0 will do that */
   4201 	if (len) {
   4202 		s = SCARG(uap, start);
   4203 		if (s < 0 || len < 0 || len > OFF_T_MAX - s) {
   4204 			error = EINVAL;
   4205 			goto out;
   4206 		}
   4207 		e = s + len;
   4208 		KASSERT(s <= e);
   4209 	} else {
   4210 		e = 0;
   4211 		s = 0;
   4212 	}
   4213 
   4214 	vp = fp->f_vnode;
   4215 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   4216 	error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
   4217 	VOP_UNLOCK(vp);
   4218 out:
   4219 	fd_putfile(SCARG(uap, fd));
   4220 	return (error);
   4221 }
   4222 
   4223 /*
   4224  * Sync the data of an open file.
   4225  */
   4226 /* ARGSUSED */
   4227 int
   4228 sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
   4229 {
   4230 	/* {
   4231 		syscallarg(int) fd;
   4232 	} */
   4233 	struct vnode *vp;
   4234 	file_t *fp;
   4235 	int error;
   4236 
   4237 	/* fd_getvnode() will use the descriptor for us */
   4238 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   4239 		return (error);
   4240 	vp = fp->f_vnode;
   4241 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   4242 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
   4243 	VOP_UNLOCK(vp);
   4244 	fd_putfile(SCARG(uap, fd));
   4245 	return (error);
   4246 }
   4247 
   4248 /*
   4249  * Rename files, (standard) BSD semantics frontend.
   4250  */
   4251 /* ARGSUSED */
   4252 int
   4253 sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
   4254 {
   4255 	/* {
   4256 		syscallarg(const char *) from;
   4257 		syscallarg(const char *) to;
   4258 	} */
   4259 
   4260 	return (do_sys_renameat(l, AT_FDCWD, SCARG(uap, from), AT_FDCWD,
   4261 	    SCARG(uap, to), UIO_USERSPACE, 0));
   4262 }
   4263 
   4264 int
   4265 sys_renameat(struct lwp *l, const struct sys_renameat_args *uap,
   4266     register_t *retval)
   4267 {
   4268 	/* {
   4269 		syscallarg(int) fromfd;
   4270 		syscallarg(const char *) from;
   4271 		syscallarg(int) tofd;
   4272 		syscallarg(const char *) to;
   4273 	} */
   4274 
   4275 	return (do_sys_renameat(l, SCARG(uap, fromfd), SCARG(uap, from),
   4276 	    SCARG(uap, tofd), SCARG(uap, to), UIO_USERSPACE, 0));
   4277 }
   4278 
   4279 /*
   4280  * Rename files, POSIX semantics frontend.
   4281  */
   4282 /* ARGSUSED */
   4283 int
   4284 sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
   4285 {
   4286 	/* {
   4287 		syscallarg(const char *) from;
   4288 		syscallarg(const char *) to;
   4289 	} */
   4290 
   4291 	return (do_sys_renameat(l, AT_FDCWD, SCARG(uap, from), AT_FDCWD,
   4292 	    SCARG(uap, to), UIO_USERSPACE, 1));
   4293 }
   4294 
   4295 /*
   4296  * Rename files.  Source and destination must either both be directories,
   4297  * or both not be directories.  If target is a directory, it must be empty.
   4298  * If `from' and `to' refer to the same object, the value of the `retain'
   4299  * argument is used to determine whether `from' will be
   4300  *
   4301  * (retain == 0)	deleted unless `from' and `to' refer to the same
   4302  *			object in the file system's name space (BSD).
   4303  * (retain == 1)	always retained (POSIX).
   4304  *
   4305  * XXX Synchronize with nfsrv_rename in nfs_serv.c.
   4306  */
   4307 int
   4308 do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
   4309 {
   4310 	return do_sys_renameat(NULL, AT_FDCWD, from, AT_FDCWD, to, seg, retain);
   4311 }
   4312 
   4313 static int
   4314 do_sys_renameat(struct lwp *l, int fromfd, const char *from, int tofd,
   4315     const char *to, enum uio_seg seg, int retain)
   4316 {
   4317 	struct pathbuf *fpb, *tpb;
   4318 	struct nameidata fnd, tnd;
   4319 	struct vnode *fdvp, *fvp;
   4320 	struct vnode *tdvp, *tvp;
   4321 	struct mount *mp, *tmp;
   4322 	int error;
   4323 
   4324 	KASSERT(l != NULL || (fromfd == AT_FDCWD && tofd == AT_FDCWD));
   4325 
   4326 	error = pathbuf_maybe_copyin(from, seg, &fpb);
   4327 	if (error)
   4328 		goto out0;
   4329 	KASSERT(fpb != NULL);
   4330 
   4331 	error = pathbuf_maybe_copyin(to, seg, &tpb);
   4332 	if (error)
   4333 		goto out1;
   4334 	KASSERT(tpb != NULL);
   4335 
   4336 	/*
   4337 	 * Lookup from.
   4338 	 *
   4339 	 * XXX LOCKPARENT is wrong because we don't actually want it
   4340 	 * locked yet, but (a) namei is insane, and (b) VOP_RENAME is
   4341 	 * insane, so for the time being we need to leave it like this.
   4342 	 */
   4343 	NDINIT(&fnd, DELETE, (LOCKPARENT | TRYEMULROOT), fpb);
   4344 	if ((error = fd_nameiat(l, fromfd, &fnd)) != 0)
   4345 		goto out2;
   4346 
   4347 	/*
   4348 	 * Pull out the important results of the lookup, fdvp and fvp.
   4349 	 * Of course, fvp is bogus because we're about to unlock fdvp.
   4350 	 */
   4351 	fdvp = fnd.ni_dvp;
   4352 	fvp = fnd.ni_vp;
   4353 	mp = fdvp->v_mount;
   4354 	KASSERT(fdvp != NULL);
   4355 	KASSERT(fvp != NULL);
   4356 	KASSERT((fdvp == fvp) || (VOP_ISLOCKED(fdvp) == LK_EXCLUSIVE));
   4357 	/*
   4358 	 * Bracket the operation with fstrans_start()/fstrans_done().
   4359 	 *
   4360 	 * Inside the bracket this file system cannot be unmounted so
   4361 	 * a vnode on this file system cannot change its v_mount.
   4362 	 * A vnode on another file system may still change to dead mount.
   4363 	 */
   4364 	fstrans_start(mp);
   4365 
   4366 	/*
   4367 	 * Make sure neither fdvp nor fvp is locked.
   4368 	 */
   4369 	if (fdvp != fvp)
   4370 		VOP_UNLOCK(fdvp);
   4371 	/* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
   4372 	/* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
   4373 
   4374 	/*
   4375 	 * Reject renaming `.' and `..'.  Can't do this until after
   4376 	 * namei because we need namei's parsing to find the final
   4377 	 * component name.  (namei should just leave us with the final
   4378 	 * component name and not look it up itself, but anyway...)
   4379 	 *
   4380 	 * This was here before because we used to relookup from
   4381 	 * instead of to and relookup requires the caller to check
   4382 	 * this, but now file systems may depend on this check, so we
   4383 	 * must retain it until the file systems are all rototilled.
   4384 	 */
   4385 	if (((fnd.ni_cnd.cn_namelen == 1) &&
   4386 		(fnd.ni_cnd.cn_nameptr[0] == '.')) ||
   4387 	    ((fnd.ni_cnd.cn_namelen == 2) &&
   4388 		(fnd.ni_cnd.cn_nameptr[0] == '.') &&
   4389 		(fnd.ni_cnd.cn_nameptr[1] == '.'))) {
   4390 		error = EINVAL;	/* XXX EISDIR?  */
   4391 		goto abort0;
   4392 	}
   4393 
   4394 	/*
   4395 	 * Lookup to.
   4396 	 *
   4397 	 * XXX LOCKPARENT is wrong, but...insanity, &c.  Also, using
   4398 	 * fvp here to decide whether to add CREATEDIR is a load of
   4399 	 * bollocks because fvp might be the wrong node by now, since
   4400 	 * fdvp is unlocked.
   4401 	 *
   4402 	 * XXX Why not pass CREATEDIR always?
   4403 	 */
   4404 	NDINIT(&tnd, RENAME,
   4405 	    (LOCKPARENT | NOCACHE | TRYEMULROOT |
   4406 		((fvp->v_type == VDIR)? CREATEDIR : 0)),
   4407 	    tpb);
   4408 	if ((error = fd_nameiat(l, tofd, &tnd)) != 0)
   4409 		goto abort0;
   4410 
   4411 	/*
   4412 	 * Pull out the important results of the lookup, tdvp and tvp.
   4413 	 * Of course, tvp is bogus because we're about to unlock tdvp.
   4414 	 */
   4415 	tdvp = tnd.ni_dvp;
   4416 	tvp = tnd.ni_vp;
   4417 	KASSERT(tdvp != NULL);
   4418 	KASSERT((tdvp == tvp) || (VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE));
   4419 
   4420 	if (fvp->v_type == VDIR)
   4421 		tnd.ni_cnd.cn_flags |= WILLBEDIR;
   4422 	/*
   4423 	 * Make sure neither tdvp nor tvp is locked.
   4424 	 */
   4425 	if (tdvp != tvp)
   4426 		VOP_UNLOCK(tdvp);
   4427 	/* XXX KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
   4428 	/* XXX KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
   4429 
   4430 	/*
   4431 	 * Reject renaming onto `.' or `..'.  relookup is unhappy with
   4432 	 * these, which is why we must do this here.  Once upon a time
   4433 	 * we relooked up from instead of to, and consequently didn't
   4434 	 * need this check, but now that we relookup to instead of
   4435 	 * from, we need this; and we shall need it forever forward
   4436 	 * until the VOP_RENAME protocol changes, because file systems
   4437 	 * will no doubt begin to depend on this check.
   4438 	 */
   4439 	if ((tnd.ni_cnd.cn_namelen == 1) && (tnd.ni_cnd.cn_nameptr[0] == '.')) {
   4440 		error = EISDIR;
   4441 		goto abort1;
   4442 	}
   4443 	if ((tnd.ni_cnd.cn_namelen == 2) &&
   4444 	    (tnd.ni_cnd.cn_nameptr[0] == '.') &&
   4445 	    (tnd.ni_cnd.cn_nameptr[1] == '.')) {
   4446 		error = EINVAL;
   4447 		goto abort1;
   4448 	}
   4449 
   4450 	/*
   4451 	 * Make sure the mount points match.  Although we don't hold
   4452 	 * any vnode locks, the v_mount on fdvp file system are stable.
   4453 	 *
   4454 	 * Unmounting another file system at an inopportune moment may
   4455 	 * cause tdvp to disappear and change its v_mount to dead.
   4456 	 *
   4457 	 * So in either case different v_mount means cross-device rename.
   4458 	 */
   4459 	KASSERT(mp != NULL);
   4460 	tmp = tdvp->v_mount;
   4461 
   4462 	if (mp != tmp) {
   4463 		error = EXDEV;
   4464 		goto abort1;
   4465 	}
   4466 
   4467 	/*
   4468 	 * Take the vfs rename lock to avoid cross-directory screw cases.
   4469 	 * Nothing is locked currently, so taking this lock is safe.
   4470 	 */
   4471 	error = VFS_RENAMELOCK_ENTER(mp);
   4472 	if (error)
   4473 		goto abort1;
   4474 
   4475 	/*
   4476 	 * Now fdvp, fvp, tdvp, and (if nonnull) tvp are referenced,
   4477 	 * and nothing is locked except for the vfs rename lock.
   4478 	 *
   4479 	 * The next step is a little rain dance to conform to the
   4480 	 * insane lock protocol, even though it does nothing to ward
   4481 	 * off race conditions.
   4482 	 *
   4483 	 * We need tdvp and tvp to be locked.  However, because we have
   4484 	 * unlocked tdvp in order to hold no locks while we take the
   4485 	 * vfs rename lock, tvp may be wrong here, and we can't safely
   4486 	 * lock it even if the sensible file systems will just unlock
   4487 	 * it straight away.  Consequently, we must lock tdvp and then
   4488 	 * relookup tvp to get it locked.
   4489 	 *
   4490 	 * Finally, because the VOP_RENAME protocol is brain-damaged
   4491 	 * and various file systems insanely depend on the semantics of
   4492 	 * this brain damage, the lookup of to must be the last lookup
   4493 	 * before VOP_RENAME.
   4494 	 */
   4495 	vn_lock(tdvp, LK_EXCLUSIVE | LK_RETRY);
   4496 	error = relookup(tdvp, &tnd.ni_vp, &tnd.ni_cnd, 0);
   4497 	if (error)
   4498 		goto abort2;
   4499 
   4500 	/*
   4501 	 * Drop the old tvp and pick up the new one -- which might be
   4502 	 * the same, but that doesn't matter to us.  After this, tdvp
   4503 	 * and tvp should both be locked.
   4504 	 */
   4505 	if (tvp != NULL)
   4506 		vrele(tvp);
   4507 	tvp = tnd.ni_vp;
   4508 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   4509 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
   4510 
   4511 	/*
   4512 	 * The old do_sys_rename had various consistency checks here
   4513 	 * involving fvp and tvp.  fvp is bogus already here, and tvp
   4514 	 * will become bogus soon in any sensible file system, so the
   4515 	 * only purpose in putting these checks here is to give lip
   4516 	 * service to these screw cases and to acknowledge that they
   4517 	 * exist, not actually to handle them, but here you go
   4518 	 * anyway...
   4519 	 */
   4520 
   4521 	/*
   4522 	 * Acknowledge that directories and non-directories aren't
   4523 	 * suposed to mix.
   4524 	 */
   4525 	if (tvp != NULL) {
   4526 		if ((fvp->v_type == VDIR) && (tvp->v_type != VDIR)) {
   4527 			error = ENOTDIR;
   4528 			goto abort3;
   4529 		} else if ((fvp->v_type != VDIR) && (tvp->v_type == VDIR)) {
   4530 			error = EISDIR;
   4531 			goto abort3;
   4532 		}
   4533 	}
   4534 
   4535 	/*
   4536 	 * Acknowledge some random screw case, among the dozens that
   4537 	 * might arise.
   4538 	 */
   4539 	if (fvp == tdvp) {
   4540 		error = EINVAL;
   4541 		goto abort3;
   4542 	}
   4543 
   4544 	/*
   4545 	 * Acknowledge that POSIX has a wacky screw case.
   4546 	 *
   4547 	 * XXX Eventually the retain flag needs to be passed on to
   4548 	 * VOP_RENAME.
   4549 	 */
   4550 	if (fvp == tvp) {
   4551 		if (retain) {
   4552 			error = 0;
   4553 			goto abort3;
   4554 		} else if ((fdvp == tdvp) &&
   4555 		    (fnd.ni_cnd.cn_namelen == tnd.ni_cnd.cn_namelen) &&
   4556 		    (0 == memcmp(fnd.ni_cnd.cn_nameptr, tnd.ni_cnd.cn_nameptr,
   4557 			fnd.ni_cnd.cn_namelen))) {
   4558 			error = 0;
   4559 			goto abort3;
   4560 		}
   4561 	}
   4562 
   4563 	/*
   4564 	 * Make sure veriexec can screw us up.  (But a race can screw
   4565 	 * up veriexec, of course -- remember, fvp and (soon) tvp are
   4566 	 * bogus.)
   4567 	 */
   4568 #if NVERIEXEC > 0
   4569 	{
   4570 		char *f1, *f2;
   4571 		size_t f1_len;
   4572 		size_t f2_len;
   4573 
   4574 		f1_len = fnd.ni_cnd.cn_namelen + 1;
   4575 		f1 = kmem_alloc(f1_len, KM_SLEEP);
   4576 		strlcpy(f1, fnd.ni_cnd.cn_nameptr, f1_len);
   4577 
   4578 		f2_len = tnd.ni_cnd.cn_namelen + 1;
   4579 		f2 = kmem_alloc(f2_len, KM_SLEEP);
   4580 		strlcpy(f2, tnd.ni_cnd.cn_nameptr, f2_len);
   4581 
   4582 		error = veriexec_renamechk(curlwp, fvp, f1, tvp, f2);
   4583 
   4584 		kmem_free(f1, f1_len);
   4585 		kmem_free(f2, f2_len);
   4586 
   4587 		if (error)
   4588 			goto abort3;
   4589 	}
   4590 #endif /* NVERIEXEC > 0 */
   4591 
   4592 	/*
   4593 	 * All ready.  Incant the rename vop.
   4594 	 */
   4595 	/* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
   4596 	/* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
   4597 	KASSERT(VOP_ISLOCKED(tdvp) == LK_EXCLUSIVE);
   4598 	KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
   4599 	error = VOP_RENAME(fdvp, fvp, &fnd.ni_cnd, tdvp, tvp, &tnd.ni_cnd);
   4600 
   4601 	/*
   4602 	 * VOP_RENAME releases fdvp, fvp, tdvp, and tvp, and unlocks
   4603 	 * tdvp and tvp.  But we can't assert any of that.
   4604 	 */
   4605 	/* XXX KASSERT(VOP_ISLOCKED(fdvp) != LK_EXCLUSIVE); */
   4606 	/* XXX KASSERT(VOP_ISLOCKED(fvp) != LK_EXCLUSIVE); */
   4607 	/* XXX KASSERT(VOP_ISLOCKED(tdvp) != LK_EXCLUSIVE); */
   4608 	/* XXX KASSERT((tvp == NULL) || (VOP_ISLOCKED(tvp) != LK_EXCLUSIVE)); */
   4609 
   4610 	/*
   4611 	 * So all we have left to do is to drop the rename lock and
   4612 	 * destroy the pathbufs.
   4613 	 */
   4614 	VFS_RENAMELOCK_EXIT(mp);
   4615 	fstrans_done(mp);
   4616 	goto out2;
   4617 
   4618 abort3:	if ((tvp != NULL) && (tvp != tdvp))
   4619 		VOP_UNLOCK(tvp);
   4620 abort2:	VOP_UNLOCK(tdvp);
   4621 	VFS_RENAMELOCK_EXIT(mp);
   4622 abort1:	VOP_ABORTOP(tdvp, &tnd.ni_cnd);
   4623 	vrele(tdvp);
   4624 	if (tvp != NULL)
   4625 		vrele(tvp);
   4626 abort0:	VOP_ABORTOP(fdvp, &fnd.ni_cnd);
   4627 	vrele(fdvp);
   4628 	vrele(fvp);
   4629 	fstrans_done(mp);
   4630 out2:	pathbuf_destroy(tpb);
   4631 out1:	pathbuf_destroy(fpb);
   4632 out0:	return error;
   4633 }
   4634 
   4635 /*
   4636  * Make a directory file.
   4637  */
   4638 /* ARGSUSED */
   4639 int
   4640 sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
   4641 {
   4642 	/* {
   4643 		syscallarg(const char *) path;
   4644 		syscallarg(int) mode;
   4645 	} */
   4646 
   4647 	return do_sys_mkdirat(l, AT_FDCWD, SCARG(uap, path),
   4648 	    SCARG(uap, mode), UIO_USERSPACE);
   4649 }
   4650 
   4651 int
   4652 sys_mkdirat(struct lwp *l, const struct sys_mkdirat_args *uap,
   4653     register_t *retval)
   4654 {
   4655 	/* {
   4656 		syscallarg(int) fd;
   4657 		syscallarg(const char *) path;
   4658 		syscallarg(int) mode;
   4659 	} */
   4660 
   4661 	return do_sys_mkdirat(l, SCARG(uap, fd), SCARG(uap, path),
   4662 	    SCARG(uap, mode), UIO_USERSPACE);
   4663 }
   4664 
   4665 
   4666 int
   4667 do_sys_mkdir(const char *path, mode_t mode, enum uio_seg seg)
   4668 {
   4669 	return do_sys_mkdirat(NULL, AT_FDCWD, path, mode, seg);
   4670 }
   4671 
   4672 static int
   4673 do_sys_mkdirat(struct lwp *l, int fdat, const char *path, mode_t mode,
   4674     enum uio_seg seg)
   4675 {
   4676 	struct proc *p = curlwp->l_proc;
   4677 	struct vnode *vp;
   4678 	struct vattr vattr;
   4679 	int error;
   4680 	struct pathbuf *pb;
   4681 	struct nameidata nd;
   4682 
   4683 	KASSERT(l != NULL || fdat == AT_FDCWD);
   4684 
   4685 	/* XXX bollocks, should pass in a pathbuf */
   4686 	error = pathbuf_maybe_copyin(path, seg, &pb);
   4687 	if (error) {
   4688 		return error;
   4689 	}
   4690 
   4691 	NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, pb);
   4692 
   4693 	if ((error = fd_nameiat(l, fdat, &nd)) != 0) {
   4694 		pathbuf_destroy(pb);
   4695 		return (error);
   4696 	}
   4697 	vp = nd.ni_vp;
   4698 	if (vp != NULL) {
   4699 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   4700 		if (nd.ni_dvp == vp)
   4701 			vrele(nd.ni_dvp);
   4702 		else
   4703 			vput(nd.ni_dvp);
   4704 		vrele(vp);
   4705 		pathbuf_destroy(pb);
   4706 		return (EEXIST);
   4707 	}
   4708 	vattr_null(&vattr);
   4709 	vattr.va_type = VDIR;
   4710 	/* We will read cwdi->cwdi_cmask unlocked. */
   4711 	vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   4712 	nd.ni_cnd.cn_flags |= WILLBEDIR;
   4713 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   4714 	if (!error)
   4715 		vrele(nd.ni_vp);
   4716 	vput(nd.ni_dvp);
   4717 	pathbuf_destroy(pb);
   4718 	return (error);
   4719 }
   4720 
   4721 /*
   4722  * Remove a directory file.
   4723  */
   4724 /* ARGSUSED */
   4725 int
   4726 sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
   4727 {
   4728 	return do_sys_unlinkat(l, AT_FDCWD, SCARG(uap, path),
   4729 	    AT_REMOVEDIR, UIO_USERSPACE);
   4730 }
   4731 
   4732 /*
   4733  * Read a block of directory entries in a file system independent format.
   4734  */
   4735 int
   4736 sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
   4737 {
   4738 	/* {
   4739 		syscallarg(int) fd;
   4740 		syscallarg(char *) buf;
   4741 		syscallarg(size_t) count;
   4742 	} */
   4743 	file_t *fp;
   4744 	int error, done;
   4745 
   4746 	/* fd_getvnode() will use the descriptor for us */
   4747 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   4748 		return (error);
   4749 	if ((fp->f_flag & FREAD) == 0) {
   4750 		error = EBADF;
   4751 		goto out;
   4752 	}
   4753 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   4754 			SCARG(uap, count), &done, l, 0, 0);
   4755 	ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
   4756 	*retval = done;
   4757  out:
   4758 	fd_putfile(SCARG(uap, fd));
   4759 	return (error);
   4760 }
   4761 
   4762 /*
   4763  * Set the mode mask for creation of filesystem nodes.
   4764  */
   4765 int
   4766 sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
   4767 {
   4768 	/* {
   4769 		syscallarg(mode_t) newmask;
   4770 	} */
   4771 
   4772 	/*
   4773 	 * cwdi->cwdi_cmask will be read unlocked elsewhere, and no kind of
   4774 	 * serialization with those reads is required.  It's important to
   4775 	 * return a coherent answer for the caller of umask() though, and
   4776 	 * the atomic operation accomplishes that.
   4777 	 */
   4778 	*retval = atomic_swap_uint(&curproc->p_cwdi->cwdi_cmask,
   4779 	    SCARG(uap, newmask) & ALLPERMS);
   4780 
   4781 	return (0);
   4782 }
   4783 
   4784 int
   4785 dorevoke(struct vnode *vp, kauth_cred_t cred)
   4786 {
   4787 	struct vattr vattr;
   4788 	int error, fs_decision;
   4789 
   4790 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   4791 	error = VOP_GETATTR(vp, &vattr, cred);
   4792 	VOP_UNLOCK(vp);
   4793 	if (error != 0)
   4794 		return error;
   4795 	fs_decision = (kauth_cred_geteuid(cred) == vattr.va_uid) ? 0 : EPERM;
   4796 	error = kauth_authorize_vnode(cred, KAUTH_VNODE_REVOKE, vp, NULL,
   4797 	    fs_decision);
   4798 	if (!error)
   4799 		VOP_REVOKE(vp, REVOKEALL);
   4800 	return (error);
   4801 }
   4802 
   4803 /*
   4804  * Void all references to file by ripping underlying filesystem
   4805  * away from vnode.
   4806  */
   4807 /* ARGSUSED */
   4808 int
   4809 sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
   4810 {
   4811 	/* {
   4812 		syscallarg(const char *) path;
   4813 	} */
   4814 	struct vnode *vp;
   4815 	int error;
   4816 
   4817 	error = namei_simple_user(SCARG(uap, path),
   4818 				NSM_FOLLOW_TRYEMULROOT, &vp);
   4819 	if (error != 0)
   4820 		return (error);
   4821 	error = dorevoke(vp, l->l_cred);
   4822 	vrele(vp);
   4823 	return (error);
   4824 }
   4825 
   4826 /*
   4827  * Allocate backing store for a file, filling a hole without having to
   4828  * explicitly write anything out.
   4829  */
   4830 /* ARGSUSED */
   4831 int
   4832 sys_posix_fallocate(struct lwp *l, const struct sys_posix_fallocate_args *uap,
   4833 		register_t *retval)
   4834 {
   4835 	/* {
   4836 		syscallarg(int) fd;
   4837 		syscallarg(off_t) pos;
   4838 		syscallarg(off_t) len;
   4839 	} */
   4840 	int fd;
   4841 	off_t pos, len;
   4842 	struct file *fp;
   4843 	struct vnode *vp;
   4844 	int error;
   4845 
   4846 	fd = SCARG(uap, fd);
   4847 	pos = SCARG(uap, pos);
   4848 	len = SCARG(uap, len);
   4849 
   4850 	if (pos < 0 || len < 0 || len > OFF_T_MAX - pos) {
   4851 		*retval = EINVAL;
   4852 		return 0;
   4853 	}
   4854 
   4855 	error = fd_getvnode(fd, &fp);
   4856 	if (error) {
   4857 		*retval = error;
   4858 		return 0;
   4859 	}
   4860 	if ((fp->f_flag & FWRITE) == 0) {
   4861 		error = EBADF;
   4862 		goto fail;
   4863 	}
   4864 	vp = fp->f_vnode;
   4865 
   4866 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   4867 	if (vp->v_type == VDIR) {
   4868 		error = EISDIR;
   4869 	} else {
   4870 		error = VOP_FALLOCATE(vp, pos, len);
   4871 	}
   4872 	VOP_UNLOCK(vp);
   4873 
   4874 fail:
   4875 	fd_putfile(fd);
   4876 	*retval = error;
   4877 	return 0;
   4878 }
   4879 
   4880 /*
   4881  * Deallocate backing store for a file, creating a hole. Also used for
   4882  * invoking TRIM on disks.
   4883  */
   4884 /* ARGSUSED */
   4885 int
   4886 sys_fdiscard(struct lwp *l, const struct sys_fdiscard_args *uap,
   4887 		register_t *retval)
   4888 {
   4889 	/* {
   4890 		syscallarg(int) fd;
   4891 		syscallarg(off_t) pos;
   4892 		syscallarg(off_t) len;
   4893 	} */
   4894 	int fd;
   4895 	off_t pos, len;
   4896 	struct file *fp;
   4897 	struct vnode *vp;
   4898 	int error;
   4899 
   4900 	fd = SCARG(uap, fd);
   4901 	pos = SCARG(uap, pos);
   4902 	len = SCARG(uap, len);
   4903 
   4904 	if (pos < 0 || len < 0 || len > OFF_T_MAX - pos) {
   4905 		return EINVAL;
   4906 	}
   4907 
   4908 	error = fd_getvnode(fd, &fp);
   4909 	if (error) {
   4910 		return error;
   4911 	}
   4912 	if ((fp->f_flag & FWRITE) == 0) {
   4913 		error = EBADF;
   4914 		goto fail;
   4915 	}
   4916 	vp = fp->f_vnode;
   4917 
   4918 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   4919 	if (vp->v_type == VDIR) {
   4920 		error = EISDIR;
   4921 	} else {
   4922 		error = VOP_FDISCARD(vp, pos, len);
   4923 	}
   4924 	VOP_UNLOCK(vp);
   4925 
   4926 fail:
   4927 	fd_putfile(fd);
   4928 	return error;
   4929 }
   4930