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