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