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