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