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