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