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