Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.422
      1 /*	$NetBSD: vfs_syscalls.c,v 1.422 2011/04/10 15:45:33 christos 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.422 2011/04/10 15:45:33 christos 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/stat.h>
     87 #include <sys/vnode.h>
     88 #include <sys/mount.h>
     89 #include <sys/proc.h>
     90 #include <sys/uio.h>
     91 #include <sys/kmem.h>
     92 #include <sys/dirent.h>
     93 #include <sys/sysctl.h>
     94 #include <sys/syscallargs.h>
     95 #include <sys/vfs_syscalls.h>
     96 #include <sys/ktrace.h>
     97 #ifdef FILEASSOC
     98 #include <sys/fileassoc.h>
     99 #endif /* FILEASSOC */
    100 #include <sys/verified_exec.h>
    101 #include <sys/kauth.h>
    102 #include <sys/atomic.h>
    103 #include <sys/module.h>
    104 #include <sys/buf.h>
    105 
    106 #include <miscfs/genfs/genfs.h>
    107 #include <miscfs/syncfs/syncfs.h>
    108 #include <miscfs/specfs/specdev.h>
    109 
    110 #include <nfs/rpcv2.h>
    111 #include <nfs/nfsproto.h>
    112 #include <nfs/nfs.h>
    113 #include <nfs/nfs_var.h>
    114 
    115 static int change_flags(struct vnode *, u_long, struct lwp *);
    116 static int change_mode(struct vnode *, int, struct lwp *l);
    117 static int change_owner(struct vnode *, uid_t, gid_t, struct lwp *, int);
    118 
    119 /*
    120  * This table is used to maintain compatibility with 4.3BSD
    121  * and NetBSD 0.9 mount syscalls - and possibly other systems.
    122  * Note, the order is important!
    123  *
    124  * Do not modify this table. It should only contain filesystems
    125  * supported by NetBSD 0.9 and 4.3BSD.
    126  */
    127 const char * const mountcompatnames[] = {
    128 	NULL,		/* 0 = MOUNT_NONE */
    129 	MOUNT_FFS,	/* 1 = MOUNT_UFS */
    130 	MOUNT_NFS,	/* 2 */
    131 	MOUNT_MFS,	/* 3 */
    132 	MOUNT_MSDOS,	/* 4 */
    133 	MOUNT_CD9660,	/* 5 = MOUNT_ISOFS */
    134 	MOUNT_FDESC,	/* 6 */
    135 	MOUNT_KERNFS,	/* 7 */
    136 	NULL,		/* 8 = MOUNT_DEVFS */
    137 	MOUNT_AFS,	/* 9 */
    138 };
    139 
    140 const int nmountcompatnames = __arraycount(mountcompatnames);
    141 
    142 static int
    143 open_setfp(struct lwp *l, file_t *fp, struct vnode *vp, int indx, int flags)
    144 {
    145 	int error;
    146 
    147 	fp->f_flag = flags & FMASK;
    148 	fp->f_type = DTYPE_VNODE;
    149 	fp->f_ops = &vnops;
    150 	fp->f_data = vp;
    151 
    152 	if (flags & (O_EXLOCK | O_SHLOCK)) {
    153 		struct flock lf;
    154 		int type;
    155 
    156 		lf.l_whence = SEEK_SET;
    157 		lf.l_start = 0;
    158 		lf.l_len = 0;
    159 		if (flags & O_EXLOCK)
    160 			lf.l_type = F_WRLCK;
    161 		else
    162 			lf.l_type = F_RDLCK;
    163 		type = F_FLOCK;
    164 		if ((flags & FNONBLOCK) == 0)
    165 			type |= F_WAIT;
    166 		VOP_UNLOCK(vp);
    167 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
    168 		if (error) {
    169 			(void) vn_close(vp, fp->f_flag, fp->f_cred);
    170 			fd_abort(l->l_proc, fp, indx);
    171 			return error;
    172 		}
    173 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    174 		atomic_or_uint(&fp->f_flag, FHASLOCK);
    175 	}
    176 	if (flags & O_CLOEXEC)
    177 		fd_set_exclose(l, indx, true);
    178 	return 0;
    179 }
    180 
    181 static int
    182 mount_update(struct lwp *l, struct vnode *vp, const char *path, int flags,
    183     void *data, size_t *data_len)
    184 {
    185 	struct mount *mp;
    186 	int error = 0, saved_flags;
    187 
    188 	mp = vp->v_mount;
    189 	saved_flags = mp->mnt_flag;
    190 
    191 	/* We can operate only on VV_ROOT nodes. */
    192 	if ((vp->v_vflag & VV_ROOT) == 0) {
    193 		error = EINVAL;
    194 		goto out;
    195 	}
    196 
    197 	/*
    198 	 * We only allow the filesystem to be reloaded if it
    199 	 * is currently mounted read-only.  Additionally, we
    200 	 * prevent read-write to read-only downgrades.
    201 	 */
    202 	if ((flags & (MNT_RELOAD | MNT_RDONLY)) != 0 &&
    203 	    (mp->mnt_flag & MNT_RDONLY) == 0 &&
    204 	    (mp->mnt_iflag & IMNT_CAN_RWTORO) == 0) {
    205 		error = EOPNOTSUPP;	/* Needs translation */
    206 		goto out;
    207 	}
    208 
    209 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    210 	    KAUTH_REQ_SYSTEM_MOUNT_UPDATE, mp, KAUTH_ARG(flags), data);
    211 	if (error)
    212 		goto out;
    213 
    214 	if (vfs_busy(mp, NULL)) {
    215 		error = EPERM;
    216 		goto out;
    217 	}
    218 
    219 	mutex_enter(&mp->mnt_updating);
    220 
    221 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    222 	mp->mnt_flag |= flags & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    223 
    224 	/*
    225 	 * Set the mount level flags.
    226 	 */
    227 	if (flags & MNT_RDONLY)
    228 		mp->mnt_flag |= MNT_RDONLY;
    229 	else if (mp->mnt_flag & MNT_RDONLY)
    230 		mp->mnt_iflag |= IMNT_WANTRDWR;
    231 	mp->mnt_flag &=
    232 	  ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    233 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    234 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    235 	    MNT_LOG);
    236 	mp->mnt_flag |= flags &
    237 	   (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    238 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    239 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    240 	    MNT_LOG | MNT_IGNORE);
    241 
    242 	error = VFS_MOUNT(mp, path, data, data_len);
    243 
    244 	if (error && data != NULL) {
    245 		int error2;
    246 
    247 		/*
    248 		 * Update failed; let's try and see if it was an
    249 		 * export request.  For compat with 3.0 and earlier.
    250 		 */
    251 		error2 = vfs_hooks_reexport(mp, path, data);
    252 
    253 		/*
    254 		 * Only update error code if the export request was
    255 		 * understood but some problem occurred while
    256 		 * processing it.
    257 		 */
    258 		if (error2 != EJUSTRETURN)
    259 			error = error2;
    260 	}
    261 
    262 	if (mp->mnt_iflag & IMNT_WANTRDWR)
    263 		mp->mnt_flag &= ~MNT_RDONLY;
    264 	if (error)
    265 		mp->mnt_flag = saved_flags;
    266 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    267 	mp->mnt_iflag &= ~IMNT_WANTRDWR;
    268 	if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
    269 		if (mp->mnt_syncer == NULL)
    270 			error = vfs_allocate_syncvnode(mp);
    271 	} else {
    272 		if (mp->mnt_syncer != NULL)
    273 			vfs_deallocate_syncvnode(mp);
    274 	}
    275 	mutex_exit(&mp->mnt_updating);
    276 	vfs_unbusy(mp, false, NULL);
    277 
    278  out:
    279 	return (error);
    280 }
    281 
    282 static int
    283 mount_get_vfsops(const char *fstype, struct vfsops **vfsops)
    284 {
    285 	char fstypename[sizeof(((struct statvfs *)NULL)->f_fstypename)];
    286 	int error;
    287 
    288 	/* Copy file-system type from userspace.  */
    289 	error = copyinstr(fstype, fstypename, sizeof(fstypename), NULL);
    290 	if (error) {
    291 		/*
    292 		 * Historically, filesystem types were identified by numbers.
    293 		 * If we get an integer for the filesystem type instead of a
    294 		 * string, we check to see if it matches one of the historic
    295 		 * filesystem types.
    296 		 */
    297 		u_long fsindex = (u_long)fstype;
    298 		if (fsindex >= nmountcompatnames ||
    299 		    mountcompatnames[fsindex] == NULL)
    300 			return ENODEV;
    301 		strlcpy(fstypename, mountcompatnames[fsindex],
    302 		    sizeof(fstypename));
    303 	}
    304 
    305 	/* Accept `ufs' as an alias for `ffs', for compatibility. */
    306 	if (strcmp(fstypename, "ufs") == 0)
    307 		fstypename[0] = 'f';
    308 
    309 	if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
    310 		return 0;
    311 
    312 	/* If we can autoload a vfs module, try again */
    313 	(void)module_autoload(fstypename, MODULE_CLASS_VFS);
    314 
    315 	if ((*vfsops = vfs_getopsbyname(fstypename)) != NULL)
    316 		return 0;
    317 
    318 	return ENODEV;
    319 }
    320 
    321 static int
    322 mount_getargs(struct lwp *l, struct vnode *vp, const char *path, int flags,
    323     void *data, size_t *data_len)
    324 {
    325 	struct mount *mp;
    326 	int error;
    327 
    328 	/* If MNT_GETARGS is specified, it should be the only flag. */
    329 	if (flags & ~MNT_GETARGS)
    330 		return EINVAL;
    331 
    332 	mp = vp->v_mount;
    333 
    334 	/* XXX: probably some notion of "can see" here if we want isolation. */
    335 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    336 	    KAUTH_REQ_SYSTEM_MOUNT_GET, mp, data, NULL);
    337 	if (error)
    338 		return error;
    339 
    340 	if ((vp->v_vflag & VV_ROOT) == 0)
    341 		return EINVAL;
    342 
    343 	if (vfs_busy(mp, NULL))
    344 		return EPERM;
    345 
    346 	mutex_enter(&mp->mnt_updating);
    347 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    348 	mp->mnt_flag |= MNT_GETARGS;
    349 	error = VFS_MOUNT(mp, path, data, data_len);
    350 	mp->mnt_flag &= ~MNT_OP_FLAGS;
    351 	mutex_exit(&mp->mnt_updating);
    352 
    353 	vfs_unbusy(mp, false, NULL);
    354 	return (error);
    355 }
    356 
    357 int
    358 sys___mount50(struct lwp *l, const struct sys___mount50_args *uap, register_t *retval)
    359 {
    360 	/* {
    361 		syscallarg(const char *) type;
    362 		syscallarg(const char *) path;
    363 		syscallarg(int) flags;
    364 		syscallarg(void *) data;
    365 		syscallarg(size_t) data_len;
    366 	} */
    367 
    368 	return do_sys_mount(l, NULL, SCARG(uap, type), SCARG(uap, path),
    369 	    SCARG(uap, flags), SCARG(uap, data), UIO_USERSPACE,
    370 	    SCARG(uap, data_len), retval);
    371 }
    372 
    373 int
    374 do_sys_mount(struct lwp *l, struct vfsops *vfsops, const char *type,
    375     const char *path, int flags, void *data, enum uio_seg data_seg,
    376     size_t data_len, register_t *retval)
    377 {
    378 	struct vnode *vp;
    379 	void *data_buf = data;
    380 	bool vfsopsrele = false;
    381 	int error;
    382 
    383 	/* XXX: The calling convention of this routine is totally bizarre */
    384 	if (vfsops)
    385 		vfsopsrele = true;
    386 
    387 	/*
    388 	 * Get vnode to be covered
    389 	 */
    390 	error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
    391 	if (error != 0) {
    392 		vp = NULL;
    393 		goto done;
    394 	}
    395 
    396 	if (vfsops == NULL) {
    397 		if (flags & (MNT_GETARGS | MNT_UPDATE)) {
    398 			vfsops = vp->v_mount->mnt_op;
    399 		} else {
    400 			/* 'type' is userspace */
    401 			error = mount_get_vfsops(type, &vfsops);
    402 			if (error != 0)
    403 				goto done;
    404 			vfsopsrele = true;
    405 		}
    406 	}
    407 
    408 	if (data != NULL && data_seg == UIO_USERSPACE) {
    409 		if (data_len == 0) {
    410 			/* No length supplied, use default for filesystem */
    411 			data_len = vfsops->vfs_min_mount_data;
    412 			if (data_len > VFS_MAX_MOUNT_DATA) {
    413 				error = EINVAL;
    414 				goto done;
    415 			}
    416 			/*
    417 			 * Hopefully a longer buffer won't make copyin() fail.
    418 			 * For compatibility with 3.0 and earlier.
    419 			 */
    420 			if (flags & MNT_UPDATE
    421 			    && data_len < sizeof (struct mnt_export_args30))
    422 				data_len = sizeof (struct mnt_export_args30);
    423 		}
    424 		data_buf = kmem_alloc(data_len, KM_SLEEP);
    425 
    426 		/* NFS needs the buffer even for mnt_getargs .... */
    427 		error = copyin(data, data_buf, data_len);
    428 		if (error != 0)
    429 			goto done;
    430 	}
    431 
    432 	if (flags & MNT_GETARGS) {
    433 		if (data_len == 0) {
    434 			error = EINVAL;
    435 			goto done;
    436 		}
    437 		error = mount_getargs(l, vp, path, flags, data_buf, &data_len);
    438 		if (error != 0)
    439 			goto done;
    440 		if (data_seg == UIO_USERSPACE)
    441 			error = copyout(data_buf, data, data_len);
    442 		*retval = data_len;
    443 	} else if (flags & MNT_UPDATE) {
    444 		error = mount_update(l, vp, path, flags, data_buf, &data_len);
    445 	} else {
    446 		/* Locking is handled internally in mount_domount(). */
    447 		KASSERT(vfsopsrele == true);
    448 		error = mount_domount(l, &vp, vfsops, path, flags, data_buf,
    449 		    &data_len);
    450 		vfsopsrele = false;
    451 	}
    452 
    453     done:
    454 	if (vfsopsrele)
    455 		vfs_delref(vfsops);
    456     	if (vp != NULL) {
    457 	    	vrele(vp);
    458 	}
    459 	if (data_buf != data)
    460 		kmem_free(data_buf, data_len);
    461 	return (error);
    462 }
    463 
    464 /*
    465  * Unmount a file system.
    466  *
    467  * Note: unmount takes a path to the vnode mounted on as argument,
    468  * not special file (as before).
    469  */
    470 /* ARGSUSED */
    471 int
    472 sys_unmount(struct lwp *l, const struct sys_unmount_args *uap, register_t *retval)
    473 {
    474 	/* {
    475 		syscallarg(const char *) path;
    476 		syscallarg(int) flags;
    477 	} */
    478 	struct vnode *vp;
    479 	struct mount *mp;
    480 	int error;
    481 	struct pathbuf *pb;
    482 	struct nameidata nd;
    483 
    484 	error = pathbuf_copyin(SCARG(uap, path), &pb);
    485 	if (error) {
    486 		return error;
    487 	}
    488 
    489 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
    490 	if ((error = namei(&nd)) != 0) {
    491 		pathbuf_destroy(pb);
    492 		return error;
    493 	}
    494 	vp = nd.ni_vp;
    495 	pathbuf_destroy(pb);
    496 
    497 	mp = vp->v_mount;
    498 	atomic_inc_uint(&mp->mnt_refcnt);
    499 	VOP_UNLOCK(vp);
    500 
    501 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    502 	    KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT, mp, NULL, NULL);
    503 	if (error) {
    504 		vrele(vp);
    505 		vfs_destroy(mp);
    506 		return (error);
    507 	}
    508 
    509 	/*
    510 	 * Don't allow unmounting the root file system.
    511 	 */
    512 	if (mp->mnt_flag & MNT_ROOTFS) {
    513 		vrele(vp);
    514 		vfs_destroy(mp);
    515 		return (EINVAL);
    516 	}
    517 
    518 	/*
    519 	 * Must be the root of the filesystem
    520 	 */
    521 	if ((vp->v_vflag & VV_ROOT) == 0) {
    522 		vrele(vp);
    523 		vfs_destroy(mp);
    524 		return (EINVAL);
    525 	}
    526 
    527 	vrele(vp);
    528 	error = dounmount(mp, SCARG(uap, flags), l);
    529 	vfs_destroy(mp);
    530 	return error;
    531 }
    532 
    533 /*
    534  * Sync each mounted filesystem.
    535  */
    536 #ifdef DEBUG
    537 int syncprt = 0;
    538 struct ctldebug debug0 = { "syncprt", &syncprt };
    539 #endif
    540 
    541 /* ARGSUSED */
    542 int
    543 sys_sync(struct lwp *l, const void *v, register_t *retval)
    544 {
    545 	struct mount *mp, *nmp;
    546 	int asyncflag;
    547 
    548 	if (l == NULL)
    549 		l = &lwp0;
    550 
    551 	mutex_enter(&mountlist_lock);
    552 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    553 	     mp = nmp) {
    554 		if (vfs_busy(mp, &nmp)) {
    555 			continue;
    556 		}
    557 		mutex_enter(&mp->mnt_updating);
    558 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    559 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    560 			mp->mnt_flag &= ~MNT_ASYNC;
    561 			VFS_SYNC(mp, MNT_NOWAIT, l->l_cred);
    562 			if (asyncflag)
    563 				 mp->mnt_flag |= MNT_ASYNC;
    564 		}
    565 		mutex_exit(&mp->mnt_updating);
    566 		vfs_unbusy(mp, false, &nmp);
    567 	}
    568 	mutex_exit(&mountlist_lock);
    569 #ifdef DEBUG
    570 	if (syncprt)
    571 		vfs_bufstats();
    572 #endif /* DEBUG */
    573 	return (0);
    574 }
    575 
    576 /*
    577  * Change filesystem quotas.
    578  */
    579 /* ARGSUSED */
    580 int
    581 sys___quotactl50(struct lwp *l, const struct sys___quotactl50_args *uap,
    582     register_t *retval)
    583 {
    584 	/* {
    585 		syscallarg(const char *) path;
    586 		syscallarg(struct plistref *) pref;
    587 	} */
    588 	struct mount *mp;
    589 	int error;
    590 	struct vnode *vp;
    591 	prop_dictionary_t dict;
    592 	struct plistref pref;
    593 
    594 	error = namei_simple_user(SCARG(uap, path),
    595 				NSM_FOLLOW_TRYEMULROOT, &vp);
    596 	if (error != 0)
    597 		return (error);
    598 	mp = vp->v_mount;
    599 	error = copyin(SCARG(uap, pref), &pref, sizeof(pref));
    600 	if (error)
    601 		return error;
    602 	error = prop_dictionary_copyin(&pref, &dict);
    603 	if (error)
    604 		return error;
    605 	error = VFS_QUOTACTL(mp, dict);
    606 	vrele(vp);
    607 	if (!error)
    608 		error = prop_dictionary_copyout(&pref, dict);
    609 	if (!error)
    610 		error = copyout(&pref, SCARG(uap, pref), sizeof(pref));
    611 	prop_object_release(dict);
    612 	return (error);
    613 }
    614 
    615 int
    616 dostatvfs(struct mount *mp, struct statvfs *sp, struct lwp *l, int flags,
    617     int root)
    618 {
    619 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    620 	int error = 0;
    621 
    622 	/*
    623 	 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    624 	 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
    625 	 * overrides MNT_NOWAIT.
    626 	 */
    627 	if (flags == MNT_NOWAIT	|| flags == MNT_LAZY ||
    628 	    (flags != MNT_WAIT && flags != 0)) {
    629 		memcpy(sp, &mp->mnt_stat, sizeof(*sp));
    630 		goto done;
    631 	}
    632 
    633 	/* Get the filesystem stats now */
    634 	memset(sp, 0, sizeof(*sp));
    635 	if ((error = VFS_STATVFS(mp, sp)) != 0) {
    636 		return error;
    637 	}
    638 
    639 	if (cwdi->cwdi_rdir == NULL)
    640 		(void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
    641 done:
    642 	if (cwdi->cwdi_rdir != NULL) {
    643 		size_t len;
    644 		char *bp;
    645 		char c;
    646 		char *path = PNBUF_GET();
    647 
    648 		bp = path + MAXPATHLEN;
    649 		*--bp = '\0';
    650 		rw_enter(&cwdi->cwdi_lock, RW_READER);
    651 		error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
    652 		    MAXPATHLEN / 2, 0, l);
    653 		rw_exit(&cwdi->cwdi_lock);
    654 		if (error) {
    655 			PNBUF_PUT(path);
    656 			return error;
    657 		}
    658 		len = strlen(bp);
    659 		if (len != 1) {
    660 			/*
    661 			 * for mount points that are below our root, we can see
    662 			 * them, so we fix up the pathname and return them. The
    663 			 * rest we cannot see, so we don't allow viewing the
    664 			 * data.
    665 			 */
    666 			if (strncmp(bp, sp->f_mntonname, len) == 0 &&
    667 			    ((c = sp->f_mntonname[len]) == '/' || c == '\0')) {
    668 				(void)strlcpy(sp->f_mntonname,
    669 				    c == '\0' ? "/" : &sp->f_mntonname[len],
    670 				    sizeof(sp->f_mntonname));
    671 			} else {
    672 				if (root)
    673 					(void)strlcpy(sp->f_mntonname, "/",
    674 					    sizeof(sp->f_mntonname));
    675 				else
    676 					error = EPERM;
    677 			}
    678 		}
    679 		PNBUF_PUT(path);
    680 	}
    681 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    682 	return error;
    683 }
    684 
    685 /*
    686  * Get filesystem statistics by path.
    687  */
    688 int
    689 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
    690 {
    691 	struct mount *mp;
    692 	int error;
    693 	struct vnode *vp;
    694 
    695 	error = namei_simple_user(path, NSM_FOLLOW_TRYEMULROOT, &vp);
    696 	if (error != 0)
    697 		return error;
    698 	mp = vp->v_mount;
    699 	error = dostatvfs(mp, sb, l, flags, 1);
    700 	vrele(vp);
    701 	return error;
    702 }
    703 
    704 /* ARGSUSED */
    705 int
    706 sys_statvfs1(struct lwp *l, const struct sys_statvfs1_args *uap, register_t *retval)
    707 {
    708 	/* {
    709 		syscallarg(const char *) path;
    710 		syscallarg(struct statvfs *) buf;
    711 		syscallarg(int) flags;
    712 	} */
    713 	struct statvfs *sb;
    714 	int error;
    715 
    716 	sb = STATVFSBUF_GET();
    717 	error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
    718 	if (error == 0)
    719 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
    720 	STATVFSBUF_PUT(sb);
    721 	return error;
    722 }
    723 
    724 /*
    725  * Get filesystem statistics by fd.
    726  */
    727 int
    728 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
    729 {
    730 	file_t *fp;
    731 	struct mount *mp;
    732 	int error;
    733 
    734 	/* fd_getvnode() will use the descriptor for us */
    735 	if ((error = fd_getvnode(fd, &fp)) != 0)
    736 		return (error);
    737 	mp = ((struct vnode *)fp->f_data)->v_mount;
    738 	error = dostatvfs(mp, sb, curlwp, flags, 1);
    739 	fd_putfile(fd);
    740 	return error;
    741 }
    742 
    743 /* ARGSUSED */
    744 int
    745 sys_fstatvfs1(struct lwp *l, const struct sys_fstatvfs1_args *uap, register_t *retval)
    746 {
    747 	/* {
    748 		syscallarg(int) fd;
    749 		syscallarg(struct statvfs *) buf;
    750 		syscallarg(int) flags;
    751 	} */
    752 	struct statvfs *sb;
    753 	int error;
    754 
    755 	sb = STATVFSBUF_GET();
    756 	error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
    757 	if (error == 0)
    758 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
    759 	STATVFSBUF_PUT(sb);
    760 	return error;
    761 }
    762 
    763 
    764 /*
    765  * Get statistics on all filesystems.
    766  */
    767 int
    768 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
    769     int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
    770     register_t *retval)
    771 {
    772 	int root = 0;
    773 	struct proc *p = l->l_proc;
    774 	struct mount *mp, *nmp;
    775 	struct statvfs *sb;
    776 	size_t count, maxcount;
    777 	int error = 0;
    778 
    779 	sb = STATVFSBUF_GET();
    780 	maxcount = bufsize / entry_sz;
    781 	mutex_enter(&mountlist_lock);
    782 	count = 0;
    783 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    784 	     mp = nmp) {
    785 		if (vfs_busy(mp, &nmp)) {
    786 			continue;
    787 		}
    788 		if (sfsp && count < maxcount) {
    789 			error = dostatvfs(mp, sb, l, flags, 0);
    790 			if (error) {
    791 				vfs_unbusy(mp, false, &nmp);
    792 				error = 0;
    793 				continue;
    794 			}
    795 			error = copyfn(sb, sfsp, entry_sz);
    796 			if (error) {
    797 				vfs_unbusy(mp, false, NULL);
    798 				goto out;
    799 			}
    800 			sfsp = (char *)sfsp + entry_sz;
    801 			root |= strcmp(sb->f_mntonname, "/") == 0;
    802 		}
    803 		count++;
    804 		vfs_unbusy(mp, false, &nmp);
    805 	}
    806 	mutex_exit(&mountlist_lock);
    807 
    808 	if (root == 0 && p->p_cwdi->cwdi_rdir) {
    809 		/*
    810 		 * fake a root entry
    811 		 */
    812 		error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount,
    813 		    sb, l, flags, 1);
    814 		if (error != 0)
    815 			goto out;
    816 		if (sfsp) {
    817 			error = copyfn(sb, sfsp, entry_sz);
    818 			if (error != 0)
    819 				goto out;
    820 		}
    821 		count++;
    822 	}
    823 	if (sfsp && count > maxcount)
    824 		*retval = maxcount;
    825 	else
    826 		*retval = count;
    827 out:
    828 	STATVFSBUF_PUT(sb);
    829 	return error;
    830 }
    831 
    832 int
    833 sys_getvfsstat(struct lwp *l, const struct sys_getvfsstat_args *uap, register_t *retval)
    834 {
    835 	/* {
    836 		syscallarg(struct statvfs *) buf;
    837 		syscallarg(size_t) bufsize;
    838 		syscallarg(int) flags;
    839 	} */
    840 
    841 	return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
    842 	    SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
    843 }
    844 
    845 /*
    846  * Change current working directory to a given file descriptor.
    847  */
    848 /* ARGSUSED */
    849 int
    850 sys_fchdir(struct lwp *l, const struct sys_fchdir_args *uap, register_t *retval)
    851 {
    852 	/* {
    853 		syscallarg(int) fd;
    854 	} */
    855 	struct proc *p = l->l_proc;
    856 	struct cwdinfo *cwdi;
    857 	struct vnode *vp, *tdp;
    858 	struct mount *mp;
    859 	file_t *fp;
    860 	int error, fd;
    861 
    862 	/* fd_getvnode() will use the descriptor for us */
    863 	fd = SCARG(uap, fd);
    864 	if ((error = fd_getvnode(fd, &fp)) != 0)
    865 		return (error);
    866 	vp = fp->f_data;
    867 
    868 	vref(vp);
    869 	vn_lock(vp,  LK_EXCLUSIVE | LK_RETRY);
    870 	if (vp->v_type != VDIR)
    871 		error = ENOTDIR;
    872 	else
    873 		error = VOP_ACCESS(vp, VEXEC, l->l_cred);
    874 	if (error) {
    875 		vput(vp);
    876 		goto out;
    877 	}
    878 	while ((mp = vp->v_mountedhere) != NULL) {
    879 		error = vfs_busy(mp, NULL);
    880 		vput(vp);
    881 		if (error != 0)
    882 			goto out;
    883 		error = VFS_ROOT(mp, &tdp);
    884 		vfs_unbusy(mp, false, NULL);
    885 		if (error)
    886 			goto out;
    887 		vp = tdp;
    888 	}
    889 	VOP_UNLOCK(vp);
    890 
    891 	/*
    892 	 * Disallow changing to a directory not under the process's
    893 	 * current root directory (if there is one).
    894 	 */
    895 	cwdi = p->p_cwdi;
    896 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    897 	if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
    898 		vrele(vp);
    899 		error = EPERM;	/* operation not permitted */
    900 	} else {
    901 		vrele(cwdi->cwdi_cdir);
    902 		cwdi->cwdi_cdir = vp;
    903 	}
    904 	rw_exit(&cwdi->cwdi_lock);
    905 
    906  out:
    907 	fd_putfile(fd);
    908 	return (error);
    909 }
    910 
    911 /*
    912  * Change this process's notion of the root directory to a given file
    913  * descriptor.
    914  */
    915 int
    916 sys_fchroot(struct lwp *l, const struct sys_fchroot_args *uap, register_t *retval)
    917 {
    918 	struct proc *p = l->l_proc;
    919 	struct vnode	*vp;
    920 	file_t	*fp;
    921 	int		 error, fd = SCARG(uap, fd);
    922 
    923 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
    924  	    KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
    925 		return error;
    926 	/* fd_getvnode() will use the descriptor for us */
    927 	if ((error = fd_getvnode(fd, &fp)) != 0)
    928 		return error;
    929 	vp = fp->f_data;
    930 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    931 	if (vp->v_type != VDIR)
    932 		error = ENOTDIR;
    933 	else
    934 		error = VOP_ACCESS(vp, VEXEC, l->l_cred);
    935 	VOP_UNLOCK(vp);
    936 	if (error)
    937 		goto out;
    938 	vref(vp);
    939 
    940 	change_root(p->p_cwdi, vp, l);
    941 
    942  out:
    943 	fd_putfile(fd);
    944 	return (error);
    945 }
    946 
    947 /*
    948  * Change current working directory (``.'').
    949  */
    950 /* ARGSUSED */
    951 int
    952 sys_chdir(struct lwp *l, const struct sys_chdir_args *uap, register_t *retval)
    953 {
    954 	/* {
    955 		syscallarg(const char *) path;
    956 	} */
    957 	struct proc *p = l->l_proc;
    958 	struct cwdinfo *cwdi;
    959 	int error;
    960 	struct vnode *vp;
    961 
    962 	if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
    963 				  &vp, l)) != 0)
    964 		return (error);
    965 	cwdi = p->p_cwdi;
    966 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
    967 	vrele(cwdi->cwdi_cdir);
    968 	cwdi->cwdi_cdir = vp;
    969 	rw_exit(&cwdi->cwdi_lock);
    970 	return (0);
    971 }
    972 
    973 /*
    974  * Change notion of root (``/'') directory.
    975  */
    976 /* ARGSUSED */
    977 int
    978 sys_chroot(struct lwp *l, const struct sys_chroot_args *uap, register_t *retval)
    979 {
    980 	/* {
    981 		syscallarg(const char *) path;
    982 	} */
    983 	struct proc *p = l->l_proc;
    984 	int error;
    985 	struct vnode *vp;
    986 
    987 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
    988 	    KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
    989 		return (error);
    990 	if ((error = chdir_lookup(SCARG(uap, path), UIO_USERSPACE,
    991 				  &vp, l)) != 0)
    992 		return (error);
    993 
    994 	change_root(p->p_cwdi, vp, l);
    995 
    996 	return (0);
    997 }
    998 
    999 /*
   1000  * Common routine for chroot and fchroot.
   1001  * NB: callers need to properly authorize the change root operation.
   1002  */
   1003 void
   1004 change_root(struct cwdinfo *cwdi, struct vnode *vp, struct lwp *l)
   1005 {
   1006 
   1007 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1008 	if (cwdi->cwdi_rdir != NULL)
   1009 		vrele(cwdi->cwdi_rdir);
   1010 	cwdi->cwdi_rdir = vp;
   1011 
   1012 	/*
   1013 	 * Prevent escaping from chroot by putting the root under
   1014 	 * the working directory.  Silently chdir to / if we aren't
   1015 	 * already there.
   1016 	 */
   1017 	if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
   1018 		/*
   1019 		 * XXX would be more failsafe to change directory to a
   1020 		 * deadfs node here instead
   1021 		 */
   1022 		vrele(cwdi->cwdi_cdir);
   1023 		vref(vp);
   1024 		cwdi->cwdi_cdir = vp;
   1025 	}
   1026 	rw_exit(&cwdi->cwdi_lock);
   1027 }
   1028 
   1029 /*
   1030  * Common routine for chroot and chdir.
   1031  * XXX "where" should be enum uio_seg
   1032  */
   1033 int
   1034 chdir_lookup(const char *path, int where, struct vnode **vpp, struct lwp *l)
   1035 {
   1036 	struct pathbuf *pb;
   1037 	struct nameidata nd;
   1038 	int error;
   1039 
   1040 	error = pathbuf_maybe_copyin(path, where, &pb);
   1041 	if (error) {
   1042 		return error;
   1043 	}
   1044 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
   1045 	if ((error = namei(&nd)) != 0) {
   1046 		pathbuf_destroy(pb);
   1047 		return error;
   1048 	}
   1049 	*vpp = nd.ni_vp;
   1050 	pathbuf_destroy(pb);
   1051 
   1052 	if ((*vpp)->v_type != VDIR)
   1053 		error = ENOTDIR;
   1054 	else
   1055 		error = VOP_ACCESS(*vpp, VEXEC, l->l_cred);
   1056 
   1057 	if (error)
   1058 		vput(*vpp);
   1059 	else
   1060 		VOP_UNLOCK(*vpp);
   1061 	return (error);
   1062 }
   1063 
   1064 /*
   1065  * Check permissions, allocate an open file structure,
   1066  * and call the device open routine if any.
   1067  */
   1068 int
   1069 sys_open(struct lwp *l, const struct sys_open_args *uap, register_t *retval)
   1070 {
   1071 	/* {
   1072 		syscallarg(const char *) path;
   1073 		syscallarg(int) flags;
   1074 		syscallarg(int) mode;
   1075 	} */
   1076 	struct proc *p = l->l_proc;
   1077 	struct cwdinfo *cwdi = p->p_cwdi;
   1078 	file_t *fp;
   1079 	struct vnode *vp;
   1080 	int flags, cmode;
   1081 	int indx, error;
   1082 	struct pathbuf *pb;
   1083 	struct nameidata nd;
   1084 
   1085 	flags = FFLAGS(SCARG(uap, flags));
   1086 	if ((flags & (FREAD | FWRITE)) == 0)
   1087 		return (EINVAL);
   1088 
   1089 	error = pathbuf_copyin(SCARG(uap, path), &pb);
   1090 	if (error) {
   1091 		return error;
   1092 	}
   1093 
   1094 	if ((error = fd_allocfile(&fp, &indx)) != 0) {
   1095 		pathbuf_destroy(pb);
   1096 		return error;
   1097 	}
   1098 	/* We're going to read cwdi->cwdi_cmask unlocked here. */
   1099 	cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
   1100 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, pb);
   1101 	l->l_dupfd = -indx - 1;			/* XXX check for fdopen */
   1102 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
   1103 		fd_abort(p, fp, indx);
   1104 		if ((error == EDUPFD || error == EMOVEFD) &&
   1105 		    l->l_dupfd >= 0 &&			/* XXX from fdopen */
   1106 		    (error =
   1107 			fd_dupopen(l->l_dupfd, &indx, flags, error)) == 0) {
   1108 			*retval = indx;
   1109 			pathbuf_destroy(pb);
   1110 			return (0);
   1111 		}
   1112 		if (error == ERESTART)
   1113 			error = EINTR;
   1114 		pathbuf_destroy(pb);
   1115 		return (error);
   1116 	}
   1117 
   1118 	l->l_dupfd = 0;
   1119 	vp = nd.ni_vp;
   1120 	pathbuf_destroy(pb);
   1121 
   1122 	if ((error = open_setfp(l, fp, vp, indx, flags)))
   1123 		return error;
   1124 
   1125 	VOP_UNLOCK(vp);
   1126 	*retval = indx;
   1127 	fd_affix(p, fp, indx);
   1128 	return (0);
   1129 }
   1130 
   1131 static void
   1132 vfs__fhfree(fhandle_t *fhp)
   1133 {
   1134 	size_t fhsize;
   1135 
   1136 	if (fhp == NULL) {
   1137 		return;
   1138 	}
   1139 	fhsize = FHANDLE_SIZE(fhp);
   1140 	kmem_free(fhp, fhsize);
   1141 }
   1142 
   1143 /*
   1144  * vfs_composefh: compose a filehandle.
   1145  */
   1146 
   1147 int
   1148 vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
   1149 {
   1150 	struct mount *mp;
   1151 	struct fid *fidp;
   1152 	int error;
   1153 	size_t needfhsize;
   1154 	size_t fidsize;
   1155 
   1156 	mp = vp->v_mount;
   1157 	fidp = NULL;
   1158 	if (*fh_size < FHANDLE_SIZE_MIN) {
   1159 		fidsize = 0;
   1160 	} else {
   1161 		fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
   1162 		if (fhp != NULL) {
   1163 			memset(fhp, 0, *fh_size);
   1164 			fhp->fh_fsid = mp->mnt_stat.f_fsidx;
   1165 			fidp = &fhp->fh_fid;
   1166 		}
   1167 	}
   1168 	error = VFS_VPTOFH(vp, fidp, &fidsize);
   1169 	needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
   1170 	if (error == 0 && *fh_size < needfhsize) {
   1171 		error = E2BIG;
   1172 	}
   1173 	*fh_size = needfhsize;
   1174 	return error;
   1175 }
   1176 
   1177 int
   1178 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
   1179 {
   1180 	struct mount *mp;
   1181 	fhandle_t *fhp;
   1182 	size_t fhsize;
   1183 	size_t fidsize;
   1184 	int error;
   1185 
   1186 	*fhpp = NULL;
   1187 	mp = vp->v_mount;
   1188 	fidsize = 0;
   1189 	error = VFS_VPTOFH(vp, NULL, &fidsize);
   1190 	KASSERT(error != 0);
   1191 	if (error != E2BIG) {
   1192 		goto out;
   1193 	}
   1194 	fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
   1195 	fhp = kmem_zalloc(fhsize, KM_SLEEP);
   1196 	if (fhp == NULL) {
   1197 		error = ENOMEM;
   1198 		goto out;
   1199 	}
   1200 	fhp->fh_fsid = mp->mnt_stat.f_fsidx;
   1201 	error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
   1202 	if (error == 0) {
   1203 		KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
   1204 		    FHANDLE_FILEID(fhp)->fid_len == fidsize));
   1205 		*fhpp = fhp;
   1206 	} else {
   1207 		kmem_free(fhp, fhsize);
   1208 	}
   1209 out:
   1210 	return error;
   1211 }
   1212 
   1213 void
   1214 vfs_composefh_free(fhandle_t *fhp)
   1215 {
   1216 
   1217 	vfs__fhfree(fhp);
   1218 }
   1219 
   1220 /*
   1221  * vfs_fhtovp: lookup a vnode by a filehandle.
   1222  */
   1223 
   1224 int
   1225 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
   1226 {
   1227 	struct mount *mp;
   1228 	int error;
   1229 
   1230 	*vpp = NULL;
   1231 	mp = vfs_getvfs(FHANDLE_FSID(fhp));
   1232 	if (mp == NULL) {
   1233 		error = ESTALE;
   1234 		goto out;
   1235 	}
   1236 	if (mp->mnt_op->vfs_fhtovp == NULL) {
   1237 		error = EOPNOTSUPP;
   1238 		goto out;
   1239 	}
   1240 	error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
   1241 out:
   1242 	return error;
   1243 }
   1244 
   1245 /*
   1246  * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
   1247  * the needed size.
   1248  */
   1249 
   1250 int
   1251 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
   1252 {
   1253 	fhandle_t *fhp;
   1254 	int error;
   1255 
   1256 	*fhpp = NULL;
   1257 	if (fhsize > FHANDLE_SIZE_MAX) {
   1258 		return EINVAL;
   1259 	}
   1260 	if (fhsize < FHANDLE_SIZE_MIN) {
   1261 		return EINVAL;
   1262 	}
   1263 again:
   1264 	fhp = kmem_alloc(fhsize, KM_SLEEP);
   1265 	if (fhp == NULL) {
   1266 		return ENOMEM;
   1267 	}
   1268 	error = copyin(ufhp, fhp, fhsize);
   1269 	if (error == 0) {
   1270 		/* XXX this check shouldn't be here */
   1271 		if (FHANDLE_SIZE(fhp) == fhsize) {
   1272 			*fhpp = fhp;
   1273 			return 0;
   1274 		} else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
   1275 			/*
   1276 			 * a kludge for nfsv2 padded handles.
   1277 			 */
   1278 			size_t sz;
   1279 
   1280 			sz = FHANDLE_SIZE(fhp);
   1281 			kmem_free(fhp, fhsize);
   1282 			fhsize = sz;
   1283 			goto again;
   1284 		} else {
   1285 			/*
   1286 			 * userland told us wrong size.
   1287 			 */
   1288 		    	error = EINVAL;
   1289 		}
   1290 	}
   1291 	kmem_free(fhp, fhsize);
   1292 	return error;
   1293 }
   1294 
   1295 void
   1296 vfs_copyinfh_free(fhandle_t *fhp)
   1297 {
   1298 
   1299 	vfs__fhfree(fhp);
   1300 }
   1301 
   1302 /*
   1303  * Get file handle system call
   1304  */
   1305 int
   1306 sys___getfh30(struct lwp *l, const struct sys___getfh30_args *uap, register_t *retval)
   1307 {
   1308 	/* {
   1309 		syscallarg(char *) fname;
   1310 		syscallarg(fhandle_t *) fhp;
   1311 		syscallarg(size_t *) fh_size;
   1312 	} */
   1313 	struct vnode *vp;
   1314 	fhandle_t *fh;
   1315 	int error;
   1316 	struct pathbuf *pb;
   1317 	struct nameidata nd;
   1318 	size_t sz;
   1319 	size_t usz;
   1320 
   1321 	/*
   1322 	 * Must be super user
   1323 	 */
   1324 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   1325 	    0, NULL, NULL, NULL);
   1326 	if (error)
   1327 		return (error);
   1328 
   1329 	error = pathbuf_copyin(SCARG(uap, fname), &pb);
   1330 	if (error) {
   1331 		return error;
   1332 	}
   1333 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
   1334 	error = namei(&nd);
   1335 	if (error) {
   1336 		pathbuf_destroy(pb);
   1337 		return error;
   1338 	}
   1339 	vp = nd.ni_vp;
   1340 	pathbuf_destroy(pb);
   1341 
   1342 	error = vfs_composefh_alloc(vp, &fh);
   1343 	vput(vp);
   1344 	if (error != 0) {
   1345 		goto out;
   1346 	}
   1347 	error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
   1348 	if (error != 0) {
   1349 		goto out;
   1350 	}
   1351 	sz = FHANDLE_SIZE(fh);
   1352 	error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
   1353 	if (error != 0) {
   1354 		goto out;
   1355 	}
   1356 	if (usz >= sz) {
   1357 		error = copyout(fh, SCARG(uap, fhp), sz);
   1358 	} else {
   1359 		error = E2BIG;
   1360 	}
   1361 out:
   1362 	vfs_composefh_free(fh);
   1363 	return (error);
   1364 }
   1365 
   1366 /*
   1367  * Open a file given a file handle.
   1368  *
   1369  * Check permissions, allocate an open file structure,
   1370  * and call the device open routine if any.
   1371  */
   1372 
   1373 int
   1374 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
   1375     register_t *retval)
   1376 {
   1377 	file_t *fp;
   1378 	struct vnode *vp = NULL;
   1379 	kauth_cred_t cred = l->l_cred;
   1380 	file_t *nfp;
   1381 	int indx, error = 0;
   1382 	struct vattr va;
   1383 	fhandle_t *fh;
   1384 	int flags;
   1385 	proc_t *p;
   1386 
   1387 	p = curproc;
   1388 
   1389 	/*
   1390 	 * Must be super user
   1391 	 */
   1392 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   1393 	    0, NULL, NULL, NULL)))
   1394 		return (error);
   1395 
   1396 	flags = FFLAGS(oflags);
   1397 	if ((flags & (FREAD | FWRITE)) == 0)
   1398 		return (EINVAL);
   1399 	if ((flags & O_CREAT))
   1400 		return (EINVAL);
   1401 	if ((error = fd_allocfile(&nfp, &indx)) != 0)
   1402 		return (error);
   1403 	fp = nfp;
   1404 	error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
   1405 	if (error != 0) {
   1406 		goto bad;
   1407 	}
   1408 	error = vfs_fhtovp(fh, &vp);
   1409 	if (error != 0) {
   1410 		goto bad;
   1411 	}
   1412 
   1413 	/* Now do an effective vn_open */
   1414 
   1415 	if (vp->v_type == VSOCK) {
   1416 		error = EOPNOTSUPP;
   1417 		goto bad;
   1418 	}
   1419 	error = vn_openchk(vp, cred, flags);
   1420 	if (error != 0)
   1421 		goto bad;
   1422 	if (flags & O_TRUNC) {
   1423 		VOP_UNLOCK(vp);			/* XXX */
   1424 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
   1425 		vattr_null(&va);
   1426 		va.va_size = 0;
   1427 		error = VOP_SETATTR(vp, &va, cred);
   1428 		if (error)
   1429 			goto bad;
   1430 	}
   1431 	if ((error = VOP_OPEN(vp, flags, cred)) != 0)
   1432 		goto bad;
   1433 	if (flags & FWRITE) {
   1434 		mutex_enter(&vp->v_interlock);
   1435 		vp->v_writecount++;
   1436 		mutex_exit(&vp->v_interlock);
   1437 	}
   1438 
   1439 	/* done with modified vn_open, now finish what sys_open does. */
   1440 	if ((error = open_setfp(l, fp, vp, indx, flags)))
   1441 		return error;
   1442 
   1443 	VOP_UNLOCK(vp);
   1444 	*retval = indx;
   1445 	fd_affix(p, fp, indx);
   1446 	vfs_copyinfh_free(fh);
   1447 	return (0);
   1448 
   1449 bad:
   1450 	fd_abort(p, fp, indx);
   1451 	if (vp != NULL)
   1452 		vput(vp);
   1453 	vfs_copyinfh_free(fh);
   1454 	return (error);
   1455 }
   1456 
   1457 int
   1458 sys___fhopen40(struct lwp *l, const struct sys___fhopen40_args *uap, register_t *retval)
   1459 {
   1460 	/* {
   1461 		syscallarg(const void *) fhp;
   1462 		syscallarg(size_t) fh_size;
   1463 		syscallarg(int) flags;
   1464 	} */
   1465 
   1466 	return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
   1467 	    SCARG(uap, flags), retval);
   1468 }
   1469 
   1470 int
   1471 do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
   1472 {
   1473 	int error;
   1474 	fhandle_t *fh;
   1475 	struct vnode *vp;
   1476 
   1477 	/*
   1478 	 * Must be super user
   1479 	 */
   1480 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   1481 	    0, NULL, NULL, NULL)))
   1482 		return (error);
   1483 
   1484 	error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
   1485 	if (error != 0)
   1486 		return error;
   1487 
   1488 	error = vfs_fhtovp(fh, &vp);
   1489 	vfs_copyinfh_free(fh);
   1490 	if (error != 0)
   1491 		return error;
   1492 
   1493 	error = vn_stat(vp, sb);
   1494 	vput(vp);
   1495 	return error;
   1496 }
   1497 
   1498 
   1499 /* ARGSUSED */
   1500 int
   1501 sys___fhstat50(struct lwp *l, const struct sys___fhstat50_args *uap, register_t *retval)
   1502 {
   1503 	/* {
   1504 		syscallarg(const void *) fhp;
   1505 		syscallarg(size_t) fh_size;
   1506 		syscallarg(struct stat *) sb;
   1507 	} */
   1508 	struct stat sb;
   1509 	int error;
   1510 
   1511 	error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
   1512 	if (error)
   1513 		return error;
   1514 	return copyout(&sb, SCARG(uap, sb), sizeof(sb));
   1515 }
   1516 
   1517 int
   1518 do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
   1519     int flags)
   1520 {
   1521 	fhandle_t *fh;
   1522 	struct mount *mp;
   1523 	struct vnode *vp;
   1524 	int error;
   1525 
   1526 	/*
   1527 	 * Must be super user
   1528 	 */
   1529 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
   1530 	    0, NULL, NULL, NULL)))
   1531 		return error;
   1532 
   1533 	error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
   1534 	if (error != 0)
   1535 		return error;
   1536 
   1537 	error = vfs_fhtovp(fh, &vp);
   1538 	vfs_copyinfh_free(fh);
   1539 	if (error != 0)
   1540 		return error;
   1541 
   1542 	mp = vp->v_mount;
   1543 	error = dostatvfs(mp, sb, l, flags, 1);
   1544 	vput(vp);
   1545 	return error;
   1546 }
   1547 
   1548 /* ARGSUSED */
   1549 int
   1550 sys___fhstatvfs140(struct lwp *l, const struct sys___fhstatvfs140_args *uap, register_t *retval)
   1551 {
   1552 	/* {
   1553 		syscallarg(const void *) fhp;
   1554 		syscallarg(size_t) fh_size;
   1555 		syscallarg(struct statvfs *) buf;
   1556 		syscallarg(int)	flags;
   1557 	} */
   1558 	struct statvfs *sb = STATVFSBUF_GET();
   1559 	int error;
   1560 
   1561 	error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
   1562 	    SCARG(uap, flags));
   1563 	if (error == 0)
   1564 		error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
   1565 	STATVFSBUF_PUT(sb);
   1566 	return error;
   1567 }
   1568 
   1569 /*
   1570  * Create a special file.
   1571  */
   1572 /* ARGSUSED */
   1573 int
   1574 sys___mknod50(struct lwp *l, const struct sys___mknod50_args *uap,
   1575     register_t *retval)
   1576 {
   1577 	/* {
   1578 		syscallarg(const char *) path;
   1579 		syscallarg(mode_t) mode;
   1580 		syscallarg(dev_t) dev;
   1581 	} */
   1582 	return do_sys_mknod(l, SCARG(uap, path), SCARG(uap, mode),
   1583 	    SCARG(uap, dev), retval, UIO_USERSPACE);
   1584 }
   1585 
   1586 int
   1587 do_sys_mknod(struct lwp *l, const char *pathname, mode_t mode, dev_t dev,
   1588     register_t *retval, enum uio_seg seg)
   1589 {
   1590 	struct proc *p = l->l_proc;
   1591 	struct vnode *vp;
   1592 	struct vattr vattr;
   1593 	int error, optype;
   1594 	struct pathbuf *pb;
   1595 	struct nameidata nd;
   1596 	const char *pathstring;
   1597 
   1598 	if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
   1599 	    0, NULL, NULL, NULL)) != 0)
   1600 		return (error);
   1601 
   1602 	optype = VOP_MKNOD_DESCOFFSET;
   1603 
   1604 	error = pathbuf_maybe_copyin(pathname, seg, &pb);
   1605 	if (error) {
   1606 		return error;
   1607 	}
   1608 	pathstring = pathbuf_stringcopy_get(pb);
   1609 	if (pathstring == NULL) {
   1610 		pathbuf_destroy(pb);
   1611 		return ENOMEM;
   1612 	}
   1613 
   1614 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
   1615 	if ((error = namei(&nd)) != 0)
   1616 		goto out;
   1617 	vp = nd.ni_vp;
   1618 
   1619 	if (vp != NULL)
   1620 		error = EEXIST;
   1621 	else {
   1622 		vattr_null(&vattr);
   1623 		/* We will read cwdi->cwdi_cmask unlocked. */
   1624 		vattr.va_mode = (mode & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1625 		vattr.va_rdev = dev;
   1626 
   1627 		switch (mode & S_IFMT) {
   1628 		case S_IFMT:	/* used by badsect to flag bad sectors */
   1629 			vattr.va_type = VBAD;
   1630 			break;
   1631 		case S_IFCHR:
   1632 			vattr.va_type = VCHR;
   1633 			break;
   1634 		case S_IFBLK:
   1635 			vattr.va_type = VBLK;
   1636 			break;
   1637 		case S_IFWHT:
   1638 			optype = VOP_WHITEOUT_DESCOFFSET;
   1639 			break;
   1640 		case S_IFREG:
   1641 #if NVERIEXEC > 0
   1642 			error = veriexec_openchk(l, nd.ni_vp, pathstring,
   1643 			    O_CREAT);
   1644 #endif /* NVERIEXEC > 0 */
   1645 			vattr.va_type = VREG;
   1646 			vattr.va_rdev = VNOVAL;
   1647 			optype = VOP_CREATE_DESCOFFSET;
   1648 			break;
   1649 		default:
   1650 			error = EINVAL;
   1651 			break;
   1652 		}
   1653 	}
   1654 	if (!error) {
   1655 		switch (optype) {
   1656 		case VOP_WHITEOUT_DESCOFFSET:
   1657 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
   1658 			if (error)
   1659 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1660 			vput(nd.ni_dvp);
   1661 			break;
   1662 
   1663 		case VOP_MKNOD_DESCOFFSET:
   1664 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
   1665 						&nd.ni_cnd, &vattr);
   1666 			if (error == 0)
   1667 				vput(nd.ni_vp);
   1668 			break;
   1669 
   1670 		case VOP_CREATE_DESCOFFSET:
   1671 			error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
   1672 						&nd.ni_cnd, &vattr);
   1673 			if (error == 0)
   1674 				vput(nd.ni_vp);
   1675 			break;
   1676 		}
   1677 	} else {
   1678 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1679 		if (nd.ni_dvp == vp)
   1680 			vrele(nd.ni_dvp);
   1681 		else
   1682 			vput(nd.ni_dvp);
   1683 		if (vp)
   1684 			vrele(vp);
   1685 	}
   1686 out:
   1687 	pathbuf_stringcopy_put(pb, pathstring);
   1688 	pathbuf_destroy(pb);
   1689 	return (error);
   1690 }
   1691 
   1692 /*
   1693  * Create a named pipe.
   1694  */
   1695 /* ARGSUSED */
   1696 int
   1697 sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval)
   1698 {
   1699 	/* {
   1700 		syscallarg(const char *) path;
   1701 		syscallarg(int) mode;
   1702 	} */
   1703 	struct proc *p = l->l_proc;
   1704 	struct vattr vattr;
   1705 	int error;
   1706 	struct pathbuf *pb;
   1707 	struct nameidata nd;
   1708 
   1709 	error = pathbuf_copyin(SCARG(uap, path), &pb);
   1710 	if (error) {
   1711 		return error;
   1712 	}
   1713 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, pb);
   1714 	if ((error = namei(&nd)) != 0) {
   1715 		pathbuf_destroy(pb);
   1716 		return error;
   1717 	}
   1718 	if (nd.ni_vp != NULL) {
   1719 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1720 		if (nd.ni_dvp == nd.ni_vp)
   1721 			vrele(nd.ni_dvp);
   1722 		else
   1723 			vput(nd.ni_dvp);
   1724 		vrele(nd.ni_vp);
   1725 		pathbuf_destroy(pb);
   1726 		return (EEXIST);
   1727 	}
   1728 	vattr_null(&vattr);
   1729 	vattr.va_type = VFIFO;
   1730 	/* We will read cwdi->cwdi_cmask unlocked. */
   1731 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1732 	error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1733 	if (error == 0)
   1734 		vput(nd.ni_vp);
   1735 	pathbuf_destroy(pb);
   1736 	return (error);
   1737 }
   1738 
   1739 /*
   1740  * Make a hard file link.
   1741  */
   1742 /* ARGSUSED */
   1743 int
   1744 sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval)
   1745 {
   1746 	/* {
   1747 		syscallarg(const char *) path;
   1748 		syscallarg(const char *) link;
   1749 	} */
   1750 	struct vnode *vp;
   1751 	struct pathbuf *linkpb;
   1752 	struct nameidata nd;
   1753 	int error;
   1754 
   1755 	error = namei_simple_user(SCARG(uap, path),
   1756 				NSM_FOLLOW_TRYEMULROOT, &vp);
   1757 	if (error != 0)
   1758 		return (error);
   1759 	error = pathbuf_copyin(SCARG(uap, link), &linkpb);
   1760 	if (error) {
   1761 		goto out1;
   1762 	}
   1763 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
   1764 	if ((error = namei(&nd)) != 0)
   1765 		goto out2;
   1766 	if (nd.ni_vp) {
   1767 		error = EEXIST;
   1768 		goto abortop;
   1769 	}
   1770 	/*
   1771 	 * Prevent cross-mount operation.
   1772 	 */
   1773 	if (nd.ni_dvp->v_mount != vp->v_mount) {
   1774 		error = EXDEV;
   1775 		goto abortop;
   1776 	}
   1777 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1778 out2:
   1779 	pathbuf_destroy(linkpb);
   1780 out1:
   1781 	vrele(vp);
   1782 	return (error);
   1783 abortop:
   1784 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1785 	if (nd.ni_dvp == nd.ni_vp)
   1786 		vrele(nd.ni_dvp);
   1787 	else
   1788 		vput(nd.ni_dvp);
   1789 	if (nd.ni_vp != NULL)
   1790 		vrele(nd.ni_vp);
   1791 	goto out2;
   1792 }
   1793 
   1794 int
   1795 do_sys_symlink(const char *patharg, const char *link, enum uio_seg seg)
   1796 {
   1797 	struct proc *p = curproc;
   1798 	struct vattr vattr;
   1799 	char *path;
   1800 	int error;
   1801 	struct pathbuf *linkpb;
   1802 	struct nameidata nd;
   1803 
   1804 	path = PNBUF_GET();
   1805 	if (seg == UIO_USERSPACE) {
   1806 		if ((error = copyinstr(patharg, path, MAXPATHLEN, NULL)) != 0)
   1807 			goto out1;
   1808 		if ((error = pathbuf_copyin(link, &linkpb)) != 0)
   1809 			goto out1;
   1810 	} else {
   1811 		KASSERT(strlen(patharg) < MAXPATHLEN);
   1812 		strcpy(path, patharg);
   1813 		linkpb = pathbuf_create(link);
   1814 		if (linkpb == NULL) {
   1815 			error = ENOMEM;
   1816 			goto out1;
   1817 		}
   1818 	}
   1819 	NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, linkpb);
   1820 	if ((error = namei(&nd)) != 0)
   1821 		goto out2;
   1822 	if (nd.ni_vp) {
   1823 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1824 		if (nd.ni_dvp == nd.ni_vp)
   1825 			vrele(nd.ni_dvp);
   1826 		else
   1827 			vput(nd.ni_dvp);
   1828 		vrele(nd.ni_vp);
   1829 		error = EEXIST;
   1830 		goto out2;
   1831 	}
   1832 	vattr_null(&vattr);
   1833 	vattr.va_type = VLNK;
   1834 	/* We will read cwdi->cwdi_cmask unlocked. */
   1835 	vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
   1836 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   1837 	if (error == 0)
   1838 		vput(nd.ni_vp);
   1839 out2:
   1840 	pathbuf_destroy(linkpb);
   1841 out1:
   1842 	PNBUF_PUT(path);
   1843 	return (error);
   1844 }
   1845 
   1846 /*
   1847  * Make a symbolic link.
   1848  */
   1849 /* ARGSUSED */
   1850 int
   1851 sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval)
   1852 {
   1853 	/* {
   1854 		syscallarg(const char *) path;
   1855 		syscallarg(const char *) link;
   1856 	} */
   1857 
   1858 	return do_sys_symlink(SCARG(uap, path), SCARG(uap, link),
   1859 	    UIO_USERSPACE);
   1860 }
   1861 
   1862 /*
   1863  * Delete a whiteout from the filesystem.
   1864  */
   1865 /* ARGSUSED */
   1866 int
   1867 sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval)
   1868 {
   1869 	/* {
   1870 		syscallarg(const char *) path;
   1871 	} */
   1872 	int error;
   1873 	struct pathbuf *pb;
   1874 	struct nameidata nd;
   1875 
   1876 	error = pathbuf_copyin(SCARG(uap, path), &pb);
   1877 	if (error) {
   1878 		return error;
   1879 	}
   1880 
   1881 	NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, pb);
   1882 	error = namei(&nd);
   1883 	if (error) {
   1884 		pathbuf_destroy(pb);
   1885 		return (error);
   1886 	}
   1887 
   1888 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   1889 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1890 		if (nd.ni_dvp == nd.ni_vp)
   1891 			vrele(nd.ni_dvp);
   1892 		else
   1893 			vput(nd.ni_dvp);
   1894 		if (nd.ni_vp)
   1895 			vrele(nd.ni_vp);
   1896 		pathbuf_destroy(pb);
   1897 		return (EEXIST);
   1898 	}
   1899 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   1900 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1901 	vput(nd.ni_dvp);
   1902 	pathbuf_destroy(pb);
   1903 	return (error);
   1904 }
   1905 
   1906 /*
   1907  * Delete a name from the filesystem.
   1908  */
   1909 /* ARGSUSED */
   1910 int
   1911 sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval)
   1912 {
   1913 	/* {
   1914 		syscallarg(const char *) path;
   1915 	} */
   1916 
   1917 	return do_sys_unlink(SCARG(uap, path), UIO_USERSPACE);
   1918 }
   1919 
   1920 int
   1921 do_sys_unlink(const char *arg, enum uio_seg seg)
   1922 {
   1923 	struct vnode *vp;
   1924 	int error;
   1925 	struct pathbuf *pb;
   1926 	struct nameidata nd;
   1927 	const char *pathstring;
   1928 
   1929 	error = pathbuf_maybe_copyin(arg, seg, &pb);
   1930 	if (error) {
   1931 		return error;
   1932 	}
   1933 	pathstring = pathbuf_stringcopy_get(pb);
   1934 	if (pathstring == NULL) {
   1935 		pathbuf_destroy(pb);
   1936 		return ENOMEM;
   1937 	}
   1938 
   1939 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
   1940 	if ((error = namei(&nd)) != 0)
   1941 		goto out;
   1942 	vp = nd.ni_vp;
   1943 
   1944 	/*
   1945 	 * The root of a mounted filesystem cannot be deleted.
   1946 	 */
   1947 	if (vp->v_vflag & VV_ROOT) {
   1948 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1949 		if (nd.ni_dvp == vp)
   1950 			vrele(nd.ni_dvp);
   1951 		else
   1952 			vput(nd.ni_dvp);
   1953 		vput(vp);
   1954 		error = EBUSY;
   1955 		goto out;
   1956 	}
   1957 
   1958 #if NVERIEXEC > 0
   1959 	/* Handle remove requests for veriexec entries. */
   1960 	if ((error = veriexec_removechk(curlwp, nd.ni_vp, pathstring)) != 0) {
   1961 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1962 		if (nd.ni_dvp == vp)
   1963 			vrele(nd.ni_dvp);
   1964 		else
   1965 			vput(nd.ni_dvp);
   1966 		vput(vp);
   1967 		goto out;
   1968 	}
   1969 #endif /* NVERIEXEC > 0 */
   1970 
   1971 #ifdef FILEASSOC
   1972 	(void)fileassoc_file_delete(vp);
   1973 #endif /* FILEASSOC */
   1974 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1975 out:
   1976 	pathbuf_stringcopy_put(pb, pathstring);
   1977 	pathbuf_destroy(pb);
   1978 	return (error);
   1979 }
   1980 
   1981 /*
   1982  * Reposition read/write file offset.
   1983  */
   1984 int
   1985 sys_lseek(struct lwp *l, const struct sys_lseek_args *uap, register_t *retval)
   1986 {
   1987 	/* {
   1988 		syscallarg(int) fd;
   1989 		syscallarg(int) pad;
   1990 		syscallarg(off_t) offset;
   1991 		syscallarg(int) whence;
   1992 	} */
   1993 	kauth_cred_t cred = l->l_cred;
   1994 	file_t *fp;
   1995 	struct vnode *vp;
   1996 	struct vattr vattr;
   1997 	off_t newoff;
   1998 	int error, fd;
   1999 
   2000 	fd = SCARG(uap, fd);
   2001 
   2002 	if ((fp = fd_getfile(fd)) == NULL)
   2003 		return (EBADF);
   2004 
   2005 	vp = fp->f_data;
   2006 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   2007 		error = ESPIPE;
   2008 		goto out;
   2009 	}
   2010 
   2011 	switch (SCARG(uap, whence)) {
   2012 	case SEEK_CUR:
   2013 		newoff = fp->f_offset + SCARG(uap, offset);
   2014 		break;
   2015 	case SEEK_END:
   2016 		error = VOP_GETATTR(vp, &vattr, cred);
   2017 		if (error) {
   2018 			goto out;
   2019 		}
   2020 		newoff = SCARG(uap, offset) + vattr.va_size;
   2021 		break;
   2022 	case SEEK_SET:
   2023 		newoff = SCARG(uap, offset);
   2024 		break;
   2025 	default:
   2026 		error = EINVAL;
   2027 		goto out;
   2028 	}
   2029 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
   2030 		*(off_t *)retval = fp->f_offset = newoff;
   2031 	}
   2032  out:
   2033  	fd_putfile(fd);
   2034 	return (error);
   2035 }
   2036 
   2037 /*
   2038  * Positional read system call.
   2039  */
   2040 int
   2041 sys_pread(struct lwp *l, const struct sys_pread_args *uap, register_t *retval)
   2042 {
   2043 	/* {
   2044 		syscallarg(int) fd;
   2045 		syscallarg(void *) buf;
   2046 		syscallarg(size_t) nbyte;
   2047 		syscallarg(off_t) offset;
   2048 	} */
   2049 	file_t *fp;
   2050 	struct vnode *vp;
   2051 	off_t offset;
   2052 	int error, fd = SCARG(uap, fd);
   2053 
   2054 	if ((fp = fd_getfile(fd)) == NULL)
   2055 		return (EBADF);
   2056 
   2057 	if ((fp->f_flag & FREAD) == 0) {
   2058 		fd_putfile(fd);
   2059 		return (EBADF);
   2060 	}
   2061 
   2062 	vp = fp->f_data;
   2063 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   2064 		error = ESPIPE;
   2065 		goto out;
   2066 	}
   2067 
   2068 	offset = SCARG(uap, offset);
   2069 
   2070 	/*
   2071 	 * XXX This works because no file systems actually
   2072 	 * XXX take any action on the seek operation.
   2073 	 */
   2074 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   2075 		goto out;
   2076 
   2077 	/* dofileread() will unuse the descriptor for us */
   2078 	return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   2079 	    &offset, 0, retval));
   2080 
   2081  out:
   2082 	fd_putfile(fd);
   2083 	return (error);
   2084 }
   2085 
   2086 /*
   2087  * Positional scatter read system call.
   2088  */
   2089 int
   2090 sys_preadv(struct lwp *l, const struct sys_preadv_args *uap, register_t *retval)
   2091 {
   2092 	/* {
   2093 		syscallarg(int) fd;
   2094 		syscallarg(const struct iovec *) iovp;
   2095 		syscallarg(int) iovcnt;
   2096 		syscallarg(off_t) offset;
   2097 	} */
   2098 	off_t offset = SCARG(uap, offset);
   2099 
   2100 	return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
   2101 	    SCARG(uap, iovcnt), &offset, 0, retval);
   2102 }
   2103 
   2104 /*
   2105  * Positional write system call.
   2106  */
   2107 int
   2108 sys_pwrite(struct lwp *l, const struct sys_pwrite_args *uap, register_t *retval)
   2109 {
   2110 	/* {
   2111 		syscallarg(int) fd;
   2112 		syscallarg(const void *) buf;
   2113 		syscallarg(size_t) nbyte;
   2114 		syscallarg(off_t) offset;
   2115 	} */
   2116 	file_t *fp;
   2117 	struct vnode *vp;
   2118 	off_t offset;
   2119 	int error, fd = SCARG(uap, fd);
   2120 
   2121 	if ((fp = fd_getfile(fd)) == NULL)
   2122 		return (EBADF);
   2123 
   2124 	if ((fp->f_flag & FWRITE) == 0) {
   2125 		fd_putfile(fd);
   2126 		return (EBADF);
   2127 	}
   2128 
   2129 	vp = fp->f_data;
   2130 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   2131 		error = ESPIPE;
   2132 		goto out;
   2133 	}
   2134 
   2135 	offset = SCARG(uap, offset);
   2136 
   2137 	/*
   2138 	 * XXX This works because no file systems actually
   2139 	 * XXX take any action on the seek operation.
   2140 	 */
   2141 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   2142 		goto out;
   2143 
   2144 	/* dofilewrite() will unuse the descriptor for us */
   2145 	return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   2146 	    &offset, 0, retval));
   2147 
   2148  out:
   2149 	fd_putfile(fd);
   2150 	return (error);
   2151 }
   2152 
   2153 /*
   2154  * Positional gather write system call.
   2155  */
   2156 int
   2157 sys_pwritev(struct lwp *l, const struct sys_pwritev_args *uap, register_t *retval)
   2158 {
   2159 	/* {
   2160 		syscallarg(int) fd;
   2161 		syscallarg(const struct iovec *) iovp;
   2162 		syscallarg(int) iovcnt;
   2163 		syscallarg(off_t) offset;
   2164 	} */
   2165 	off_t offset = SCARG(uap, offset);
   2166 
   2167 	return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
   2168 	    SCARG(uap, iovcnt), &offset, 0, retval);
   2169 }
   2170 
   2171 /*
   2172  * Check access permissions.
   2173  */
   2174 int
   2175 sys_access(struct lwp *l, const struct sys_access_args *uap, register_t *retval)
   2176 {
   2177 	/* {
   2178 		syscallarg(const char *) path;
   2179 		syscallarg(int) flags;
   2180 	} */
   2181 	kauth_cred_t cred;
   2182 	struct vnode *vp;
   2183 	int error, flags;
   2184 	struct pathbuf *pb;
   2185 	struct nameidata nd;
   2186 
   2187 	CTASSERT(F_OK == 0);
   2188 	if ((SCARG(uap, flags) & ~(R_OK | W_OK | X_OK)) != 0) {
   2189 		/* nonsense flags */
   2190 		return EINVAL;
   2191 	}
   2192 
   2193 	error = pathbuf_copyin(SCARG(uap, path), &pb);
   2194 	if (error) {
   2195 		return error;
   2196 	}
   2197 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
   2198 
   2199 	/* Override default credentials */
   2200 	cred = kauth_cred_dup(l->l_cred);
   2201 	kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
   2202 	kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
   2203 	nd.ni_cnd.cn_cred = cred;
   2204 
   2205 	if ((error = namei(&nd)) != 0) {
   2206 		pathbuf_destroy(pb);
   2207 		goto out;
   2208 	}
   2209 	vp = nd.ni_vp;
   2210 	pathbuf_destroy(pb);
   2211 
   2212 	/* Flags == 0 means only check for existence. */
   2213 	if (SCARG(uap, flags)) {
   2214 		flags = 0;
   2215 		if (SCARG(uap, flags) & R_OK)
   2216 			flags |= VREAD;
   2217 		if (SCARG(uap, flags) & W_OK)
   2218 			flags |= VWRITE;
   2219 		if (SCARG(uap, flags) & X_OK)
   2220 			flags |= VEXEC;
   2221 
   2222 		error = VOP_ACCESS(vp, flags, cred);
   2223 		if (!error && (flags & VWRITE))
   2224 			error = vn_writechk(vp);
   2225 	}
   2226 	vput(vp);
   2227 out:
   2228 	kauth_cred_free(cred);
   2229 	return (error);
   2230 }
   2231 
   2232 /*
   2233  * Common code for all sys_stat functions, including compat versions.
   2234  */
   2235 int
   2236 do_sys_stat(const char *userpath, unsigned int nd_flags, struct stat *sb)
   2237 {
   2238 	int error;
   2239 	struct pathbuf *pb;
   2240 	struct nameidata nd;
   2241 
   2242 	error = pathbuf_copyin(userpath, &pb);
   2243 	if (error) {
   2244 		return error;
   2245 	}
   2246 	NDINIT(&nd, LOOKUP, nd_flags | LOCKLEAF | TRYEMULROOT, pb);
   2247 	error = namei(&nd);
   2248 	if (error != 0) {
   2249 		pathbuf_destroy(pb);
   2250 		return error;
   2251 	}
   2252 	error = vn_stat(nd.ni_vp, sb);
   2253 	vput(nd.ni_vp);
   2254 	pathbuf_destroy(pb);
   2255 	return error;
   2256 }
   2257 
   2258 /*
   2259  * Get file status; this version follows links.
   2260  */
   2261 /* ARGSUSED */
   2262 int
   2263 sys___stat50(struct lwp *l, const struct sys___stat50_args *uap, register_t *retval)
   2264 {
   2265 	/* {
   2266 		syscallarg(const char *) path;
   2267 		syscallarg(struct stat *) ub;
   2268 	} */
   2269 	struct stat sb;
   2270 	int error;
   2271 
   2272 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
   2273 	if (error)
   2274 		return error;
   2275 	return copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2276 }
   2277 
   2278 /*
   2279  * Get file status; this version does not follow links.
   2280  */
   2281 /* ARGSUSED */
   2282 int
   2283 sys___lstat50(struct lwp *l, const struct sys___lstat50_args *uap, register_t *retval)
   2284 {
   2285 	/* {
   2286 		syscallarg(const char *) path;
   2287 		syscallarg(struct stat *) ub;
   2288 	} */
   2289 	struct stat sb;
   2290 	int error;
   2291 
   2292 	error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
   2293 	if (error)
   2294 		return error;
   2295 	return copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2296 }
   2297 
   2298 /*
   2299  * Get configurable pathname variables.
   2300  */
   2301 /* ARGSUSED */
   2302 int
   2303 sys_pathconf(struct lwp *l, const struct sys_pathconf_args *uap, register_t *retval)
   2304 {
   2305 	/* {
   2306 		syscallarg(const char *) path;
   2307 		syscallarg(int) name;
   2308 	} */
   2309 	int error;
   2310 	struct pathbuf *pb;
   2311 	struct nameidata nd;
   2312 
   2313 	error = pathbuf_copyin(SCARG(uap, path), &pb);
   2314 	if (error) {
   2315 		return error;
   2316 	}
   2317 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb);
   2318 	if ((error = namei(&nd)) != 0) {
   2319 		pathbuf_destroy(pb);
   2320 		return (error);
   2321 	}
   2322 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   2323 	vput(nd.ni_vp);
   2324 	pathbuf_destroy(pb);
   2325 	return (error);
   2326 }
   2327 
   2328 /*
   2329  * Return target name of a symbolic link.
   2330  */
   2331 /* ARGSUSED */
   2332 int
   2333 sys_readlink(struct lwp *l, const struct sys_readlink_args *uap, register_t *retval)
   2334 {
   2335 	/* {
   2336 		syscallarg(const char *) path;
   2337 		syscallarg(char *) buf;
   2338 		syscallarg(size_t) count;
   2339 	} */
   2340 	struct vnode *vp;
   2341 	struct iovec aiov;
   2342 	struct uio auio;
   2343 	int error;
   2344 	struct pathbuf *pb;
   2345 	struct nameidata nd;
   2346 
   2347 	error = pathbuf_copyin(SCARG(uap, path), &pb);
   2348 	if (error) {
   2349 		return error;
   2350 	}
   2351 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, pb);
   2352 	if ((error = namei(&nd)) != 0) {
   2353 		pathbuf_destroy(pb);
   2354 		return error;
   2355 	}
   2356 	vp = nd.ni_vp;
   2357 	pathbuf_destroy(pb);
   2358 	if (vp->v_type != VLNK)
   2359 		error = EINVAL;
   2360 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   2361 	    (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) {
   2362 		aiov.iov_base = SCARG(uap, buf);
   2363 		aiov.iov_len = SCARG(uap, count);
   2364 		auio.uio_iov = &aiov;
   2365 		auio.uio_iovcnt = 1;
   2366 		auio.uio_offset = 0;
   2367 		auio.uio_rw = UIO_READ;
   2368 		KASSERT(l == curlwp);
   2369 		auio.uio_vmspace = l->l_proc->p_vmspace;
   2370 		auio.uio_resid = SCARG(uap, count);
   2371 		error = VOP_READLINK(vp, &auio, l->l_cred);
   2372 	}
   2373 	vput(vp);
   2374 	*retval = SCARG(uap, count) - auio.uio_resid;
   2375 	return (error);
   2376 }
   2377 
   2378 /*
   2379  * Change flags of a file given a path name.
   2380  */
   2381 /* ARGSUSED */
   2382 int
   2383 sys_chflags(struct lwp *l, const struct sys_chflags_args *uap, register_t *retval)
   2384 {
   2385 	/* {
   2386 		syscallarg(const char *) path;
   2387 		syscallarg(u_long) flags;
   2388 	} */
   2389 	struct vnode *vp;
   2390 	int error;
   2391 
   2392 	error = namei_simple_user(SCARG(uap, path),
   2393 				NSM_FOLLOW_TRYEMULROOT, &vp);
   2394 	if (error != 0)
   2395 		return (error);
   2396 	error = change_flags(vp, SCARG(uap, flags), l);
   2397 	vput(vp);
   2398 	return (error);
   2399 }
   2400 
   2401 /*
   2402  * Change flags of a file given a file descriptor.
   2403  */
   2404 /* ARGSUSED */
   2405 int
   2406 sys_fchflags(struct lwp *l, const struct sys_fchflags_args *uap, register_t *retval)
   2407 {
   2408 	/* {
   2409 		syscallarg(int) fd;
   2410 		syscallarg(u_long) flags;
   2411 	} */
   2412 	struct vnode *vp;
   2413 	file_t *fp;
   2414 	int error;
   2415 
   2416 	/* fd_getvnode() will use the descriptor for us */
   2417 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2418 		return (error);
   2419 	vp = fp->f_data;
   2420 	error = change_flags(vp, SCARG(uap, flags), l);
   2421 	VOP_UNLOCK(vp);
   2422 	fd_putfile(SCARG(uap, fd));
   2423 	return (error);
   2424 }
   2425 
   2426 /*
   2427  * Change flags of a file given a path name; this version does
   2428  * not follow links.
   2429  */
   2430 int
   2431 sys_lchflags(struct lwp *l, const struct sys_lchflags_args *uap, register_t *retval)
   2432 {
   2433 	/* {
   2434 		syscallarg(const char *) path;
   2435 		syscallarg(u_long) flags;
   2436 	} */
   2437 	struct vnode *vp;
   2438 	int error;
   2439 
   2440 	error = namei_simple_user(SCARG(uap, path),
   2441 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
   2442 	if (error != 0)
   2443 		return (error);
   2444 	error = change_flags(vp, SCARG(uap, flags), l);
   2445 	vput(vp);
   2446 	return (error);
   2447 }
   2448 
   2449 /*
   2450  * Common routine to change flags of a file.
   2451  */
   2452 int
   2453 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
   2454 {
   2455 	struct vattr vattr;
   2456 	int error;
   2457 
   2458 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2459 	/*
   2460 	 * Non-superusers cannot change the flags on devices, even if they
   2461 	 * own them.
   2462 	 */
   2463 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
   2464 		if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
   2465 			goto out;
   2466 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2467 			error = EINVAL;
   2468 			goto out;
   2469 		}
   2470 	}
   2471 	vattr_null(&vattr);
   2472 	vattr.va_flags = flags;
   2473 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   2474 out:
   2475 	return (error);
   2476 }
   2477 
   2478 /*
   2479  * Change mode of a file given path name; this version follows links.
   2480  */
   2481 /* ARGSUSED */
   2482 int
   2483 sys_chmod(struct lwp *l, const struct sys_chmod_args *uap, register_t *retval)
   2484 {
   2485 	/* {
   2486 		syscallarg(const char *) path;
   2487 		syscallarg(int) mode;
   2488 	} */
   2489 	int error;
   2490 	struct vnode *vp;
   2491 
   2492 	error = namei_simple_user(SCARG(uap, path),
   2493 				NSM_FOLLOW_TRYEMULROOT, &vp);
   2494 	if (error != 0)
   2495 		return (error);
   2496 
   2497 	error = change_mode(vp, SCARG(uap, mode), l);
   2498 
   2499 	vrele(vp);
   2500 	return (error);
   2501 }
   2502 
   2503 /*
   2504  * Change mode of a file given a file descriptor.
   2505  */
   2506 /* ARGSUSED */
   2507 int
   2508 sys_fchmod(struct lwp *l, const struct sys_fchmod_args *uap, register_t *retval)
   2509 {
   2510 	/* {
   2511 		syscallarg(int) fd;
   2512 		syscallarg(int) mode;
   2513 	} */
   2514 	file_t *fp;
   2515 	int error;
   2516 
   2517 	/* fd_getvnode() will use the descriptor for us */
   2518 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2519 		return (error);
   2520 	error = change_mode(fp->f_data, SCARG(uap, mode), l);
   2521 	fd_putfile(SCARG(uap, fd));
   2522 	return (error);
   2523 }
   2524 
   2525 /*
   2526  * Change mode of a file given path name; this version does not follow links.
   2527  */
   2528 /* ARGSUSED */
   2529 int
   2530 sys_lchmod(struct lwp *l, const struct sys_lchmod_args *uap, register_t *retval)
   2531 {
   2532 	/* {
   2533 		syscallarg(const char *) path;
   2534 		syscallarg(int) mode;
   2535 	} */
   2536 	int error;
   2537 	struct vnode *vp;
   2538 
   2539 	error = namei_simple_user(SCARG(uap, path),
   2540 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
   2541 	if (error != 0)
   2542 		return (error);
   2543 
   2544 	error = change_mode(vp, SCARG(uap, mode), l);
   2545 
   2546 	vrele(vp);
   2547 	return (error);
   2548 }
   2549 
   2550 /*
   2551  * Common routine to set mode given a vnode.
   2552  */
   2553 static int
   2554 change_mode(struct vnode *vp, int mode, struct lwp *l)
   2555 {
   2556 	struct vattr vattr;
   2557 	int error;
   2558 
   2559 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2560 	vattr_null(&vattr);
   2561 	vattr.va_mode = mode & ALLPERMS;
   2562 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   2563 	VOP_UNLOCK(vp);
   2564 	return (error);
   2565 }
   2566 
   2567 /*
   2568  * Set ownership given a path name; this version follows links.
   2569  */
   2570 /* ARGSUSED */
   2571 int
   2572 sys_chown(struct lwp *l, const struct sys_chown_args *uap, register_t *retval)
   2573 {
   2574 	/* {
   2575 		syscallarg(const char *) path;
   2576 		syscallarg(uid_t) uid;
   2577 		syscallarg(gid_t) gid;
   2578 	} */
   2579 	int error;
   2580 	struct vnode *vp;
   2581 
   2582 	error = namei_simple_user(SCARG(uap, path),
   2583 				NSM_FOLLOW_TRYEMULROOT, &vp);
   2584 	if (error != 0)
   2585 		return (error);
   2586 
   2587 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
   2588 
   2589 	vrele(vp);
   2590 	return (error);
   2591 }
   2592 
   2593 /*
   2594  * Set ownership given a path name; this version follows links.
   2595  * Provides POSIX semantics.
   2596  */
   2597 /* ARGSUSED */
   2598 int
   2599 sys___posix_chown(struct lwp *l, const struct sys___posix_chown_args *uap, register_t *retval)
   2600 {
   2601 	/* {
   2602 		syscallarg(const char *) path;
   2603 		syscallarg(uid_t) uid;
   2604 		syscallarg(gid_t) gid;
   2605 	} */
   2606 	int error;
   2607 	struct vnode *vp;
   2608 
   2609 	error = namei_simple_user(SCARG(uap, path),
   2610 				NSM_FOLLOW_TRYEMULROOT, &vp);
   2611 	if (error != 0)
   2612 		return (error);
   2613 
   2614 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
   2615 
   2616 	vrele(vp);
   2617 	return (error);
   2618 }
   2619 
   2620 /*
   2621  * Set ownership given a file descriptor.
   2622  */
   2623 /* ARGSUSED */
   2624 int
   2625 sys_fchown(struct lwp *l, const struct sys_fchown_args *uap, register_t *retval)
   2626 {
   2627 	/* {
   2628 		syscallarg(int) fd;
   2629 		syscallarg(uid_t) uid;
   2630 		syscallarg(gid_t) gid;
   2631 	} */
   2632 	int error;
   2633 	file_t *fp;
   2634 
   2635 	/* fd_getvnode() will use the descriptor for us */
   2636 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2637 		return (error);
   2638 	error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
   2639 	    l, 0);
   2640 	fd_putfile(SCARG(uap, fd));
   2641 	return (error);
   2642 }
   2643 
   2644 /*
   2645  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   2646  */
   2647 /* ARGSUSED */
   2648 int
   2649 sys___posix_fchown(struct lwp *l, const struct sys___posix_fchown_args *uap, register_t *retval)
   2650 {
   2651 	/* {
   2652 		syscallarg(int) fd;
   2653 		syscallarg(uid_t) uid;
   2654 		syscallarg(gid_t) gid;
   2655 	} */
   2656 	int error;
   2657 	file_t *fp;
   2658 
   2659 	/* fd_getvnode() will use the descriptor for us */
   2660 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2661 		return (error);
   2662 	error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
   2663 	    l, 1);
   2664 	fd_putfile(SCARG(uap, fd));
   2665 	return (error);
   2666 }
   2667 
   2668 /*
   2669  * Set ownership given a path name; this version does not follow links.
   2670  */
   2671 /* ARGSUSED */
   2672 int
   2673 sys_lchown(struct lwp *l, const struct sys_lchown_args *uap, register_t *retval)
   2674 {
   2675 	/* {
   2676 		syscallarg(const char *) path;
   2677 		syscallarg(uid_t) uid;
   2678 		syscallarg(gid_t) gid;
   2679 	} */
   2680 	int error;
   2681 	struct vnode *vp;
   2682 
   2683 	error = namei_simple_user(SCARG(uap, path),
   2684 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
   2685 	if (error != 0)
   2686 		return (error);
   2687 
   2688 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
   2689 
   2690 	vrele(vp);
   2691 	return (error);
   2692 }
   2693 
   2694 /*
   2695  * Set ownership given a path name; this version does not follow links.
   2696  * Provides POSIX/XPG semantics.
   2697  */
   2698 /* ARGSUSED */
   2699 int
   2700 sys___posix_lchown(struct lwp *l, const struct sys___posix_lchown_args *uap, register_t *retval)
   2701 {
   2702 	/* {
   2703 		syscallarg(const char *) path;
   2704 		syscallarg(uid_t) uid;
   2705 		syscallarg(gid_t) gid;
   2706 	} */
   2707 	int error;
   2708 	struct vnode *vp;
   2709 
   2710 	error = namei_simple_user(SCARG(uap, path),
   2711 				NSM_NOFOLLOW_TRYEMULROOT, &vp);
   2712 	if (error != 0)
   2713 		return (error);
   2714 
   2715 	error = change_owner(vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
   2716 
   2717 	vrele(vp);
   2718 	return (error);
   2719 }
   2720 
   2721 /*
   2722  * Common routine to set ownership given a vnode.
   2723  */
   2724 static int
   2725 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
   2726     int posix_semantics)
   2727 {
   2728 	struct vattr vattr;
   2729 	mode_t newmode;
   2730 	int error;
   2731 
   2732 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2733 	if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
   2734 		goto out;
   2735 
   2736 #define CHANGED(x) ((int)(x) != -1)
   2737 	newmode = vattr.va_mode;
   2738 	if (posix_semantics) {
   2739 		/*
   2740 		 * POSIX/XPG semantics: if the caller is not the super-user,
   2741 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   2742 		 * the XPG consider the behaviour for calls by the super-user
   2743 		 * implementation-defined; we leave the set-user-id and set-
   2744 		 * group-id settings intact in that case.
   2745 		 */
   2746 		if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
   2747 				      NULL) != 0)
   2748 			newmode &= ~(S_ISUID | S_ISGID);
   2749 	} else {
   2750 		/*
   2751 		 * NetBSD semantics: when changing owner and/or group,
   2752 		 * clear the respective bit(s).
   2753 		 */
   2754 		if (CHANGED(uid))
   2755 			newmode &= ~S_ISUID;
   2756 		if (CHANGED(gid))
   2757 			newmode &= ~S_ISGID;
   2758 	}
   2759 	/* Update va_mode iff altered. */
   2760 	if (vattr.va_mode == newmode)
   2761 		newmode = VNOVAL;
   2762 
   2763 	vattr_null(&vattr);
   2764 	vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
   2765 	vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
   2766 	vattr.va_mode = newmode;
   2767 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   2768 #undef CHANGED
   2769 
   2770 out:
   2771 	VOP_UNLOCK(vp);
   2772 	return (error);
   2773 }
   2774 
   2775 /*
   2776  * Set the access and modification times given a path name; this
   2777  * version follows links.
   2778  */
   2779 /* ARGSUSED */
   2780 int
   2781 sys___utimes50(struct lwp *l, const struct sys___utimes50_args *uap,
   2782     register_t *retval)
   2783 {
   2784 	/* {
   2785 		syscallarg(const char *) path;
   2786 		syscallarg(const struct timeval *) tptr;
   2787 	} */
   2788 
   2789 	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
   2790 	    SCARG(uap, tptr), UIO_USERSPACE);
   2791 }
   2792 
   2793 /*
   2794  * Set the access and modification times given a file descriptor.
   2795  */
   2796 /* ARGSUSED */
   2797 int
   2798 sys___futimes50(struct lwp *l, const struct sys___futimes50_args *uap,
   2799     register_t *retval)
   2800 {
   2801 	/* {
   2802 		syscallarg(int) fd;
   2803 		syscallarg(const struct timeval *) tptr;
   2804 	} */
   2805 	int error;
   2806 	file_t *fp;
   2807 
   2808 	/* fd_getvnode() will use the descriptor for us */
   2809 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2810 		return (error);
   2811 	error = do_sys_utimes(l, fp->f_data, NULL, 0, SCARG(uap, tptr),
   2812 	    UIO_USERSPACE);
   2813 	fd_putfile(SCARG(uap, fd));
   2814 	return (error);
   2815 }
   2816 
   2817 /*
   2818  * Set the access and modification times given a path name; this
   2819  * version does not follow links.
   2820  */
   2821 int
   2822 sys___lutimes50(struct lwp *l, const struct sys___lutimes50_args *uap,
   2823     register_t *retval)
   2824 {
   2825 	/* {
   2826 		syscallarg(const char *) path;
   2827 		syscallarg(const struct timeval *) tptr;
   2828 	} */
   2829 
   2830 	return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
   2831 	    SCARG(uap, tptr), UIO_USERSPACE);
   2832 }
   2833 
   2834 /*
   2835  * Common routine to set access and modification times given a vnode.
   2836  */
   2837 int
   2838 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
   2839     const struct timeval *tptr, enum uio_seg seg)
   2840 {
   2841 	struct vattr vattr;
   2842 	int error, dorele = 0;
   2843 	namei_simple_flags_t sflags;
   2844 
   2845 	bool vanull, setbirthtime;
   2846 	struct timespec ts[2];
   2847 
   2848 	/*
   2849 	 * I have checked all callers and they pass either FOLLOW,
   2850 	 * NOFOLLOW, or 0 (when they don't pass a path), and NOFOLLOW
   2851 	 * is 0. More to the point, they don't pass anything else.
   2852 	 * Let's keep it that way at least until the namei interfaces
   2853 	 * are fully sanitized.
   2854 	 */
   2855 	KASSERT(flag == NOFOLLOW || flag == FOLLOW);
   2856 	sflags = (flag == FOLLOW) ?
   2857 		NSM_FOLLOW_TRYEMULROOT : NSM_NOFOLLOW_TRYEMULROOT;
   2858 
   2859 	if (tptr == NULL) {
   2860 		vanull = true;
   2861 		nanotime(&ts[0]);
   2862 		ts[1] = ts[0];
   2863 	} else {
   2864 		struct timeval tv[2];
   2865 
   2866 		vanull = false;
   2867 		if (seg != UIO_SYSSPACE) {
   2868 			error = copyin(tptr, tv, sizeof (tv));
   2869 			if (error != 0)
   2870 				return error;
   2871 			tptr = tv;
   2872 		}
   2873 		TIMEVAL_TO_TIMESPEC(&tptr[0], &ts[0]);
   2874 		TIMEVAL_TO_TIMESPEC(&tptr[1], &ts[1]);
   2875 	}
   2876 
   2877 	if (vp == NULL) {
   2878 		/* note: SEG describes TPTR, not PATH; PATH is always user */
   2879 		error = namei_simple_user(path, sflags, &vp);
   2880 		if (error != 0)
   2881 			return error;
   2882 		dorele = 1;
   2883 	}
   2884 
   2885 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2886 	setbirthtime = (VOP_GETATTR(vp, &vattr, l->l_cred) == 0 &&
   2887 	    timespeccmp(&ts[1], &vattr.va_birthtime, <));
   2888 	vattr_null(&vattr);
   2889 	vattr.va_atime = ts[0];
   2890 	vattr.va_mtime = ts[1];
   2891 	if (setbirthtime)
   2892 		vattr.va_birthtime = ts[1];
   2893 	if (vanull)
   2894 		vattr.va_vaflags |= VA_UTIMES_NULL;
   2895 	error = VOP_SETATTR(vp, &vattr, l->l_cred);
   2896 	VOP_UNLOCK(vp);
   2897 
   2898 	if (dorele != 0)
   2899 		vrele(vp);
   2900 
   2901 	return error;
   2902 }
   2903 
   2904 /*
   2905  * Truncate a file given its path name.
   2906  */
   2907 /* ARGSUSED */
   2908 int
   2909 sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval)
   2910 {
   2911 	/* {
   2912 		syscallarg(const char *) path;
   2913 		syscallarg(int) pad;
   2914 		syscallarg(off_t) length;
   2915 	} */
   2916 	struct vnode *vp;
   2917 	struct vattr vattr;
   2918 	int error;
   2919 
   2920 	error = namei_simple_user(SCARG(uap, path),
   2921 				NSM_FOLLOW_TRYEMULROOT, &vp);
   2922 	if (error != 0)
   2923 		return (error);
   2924 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2925 	if (vp->v_type == VDIR)
   2926 		error = EISDIR;
   2927 	else if ((error = vn_writechk(vp)) == 0 &&
   2928 	    (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
   2929 		vattr_null(&vattr);
   2930 		vattr.va_size = SCARG(uap, length);
   2931 		error = VOP_SETATTR(vp, &vattr, l->l_cred);
   2932 	}
   2933 	vput(vp);
   2934 	return (error);
   2935 }
   2936 
   2937 /*
   2938  * Truncate a file given a file descriptor.
   2939  */
   2940 /* ARGSUSED */
   2941 int
   2942 sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
   2943 {
   2944 	/* {
   2945 		syscallarg(int) fd;
   2946 		syscallarg(int) pad;
   2947 		syscallarg(off_t) length;
   2948 	} */
   2949 	struct vattr vattr;
   2950 	struct vnode *vp;
   2951 	file_t *fp;
   2952 	int error;
   2953 
   2954 	/* fd_getvnode() will use the descriptor for us */
   2955 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2956 		return (error);
   2957 	if ((fp->f_flag & FWRITE) == 0) {
   2958 		error = EINVAL;
   2959 		goto out;
   2960 	}
   2961 	vp = fp->f_data;
   2962 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2963 	if (vp->v_type == VDIR)
   2964 		error = EISDIR;
   2965 	else if ((error = vn_writechk(vp)) == 0) {
   2966 		vattr_null(&vattr);
   2967 		vattr.va_size = SCARG(uap, length);
   2968 		error = VOP_SETATTR(vp, &vattr, fp->f_cred);
   2969 	}
   2970 	VOP_UNLOCK(vp);
   2971  out:
   2972 	fd_putfile(SCARG(uap, fd));
   2973 	return (error);
   2974 }
   2975 
   2976 /*
   2977  * Sync an open file.
   2978  */
   2979 /* ARGSUSED */
   2980 int
   2981 sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
   2982 {
   2983 	/* {
   2984 		syscallarg(int) fd;
   2985 	} */
   2986 	struct vnode *vp;
   2987 	file_t *fp;
   2988 	int error;
   2989 
   2990 	/* fd_getvnode() will use the descriptor for us */
   2991 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   2992 		return (error);
   2993 	vp = fp->f_data;
   2994 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2995 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
   2996 	VOP_UNLOCK(vp);
   2997 	fd_putfile(SCARG(uap, fd));
   2998 	return (error);
   2999 }
   3000 
   3001 /*
   3002  * Sync a range of file data.  API modeled after that found in AIX.
   3003  *
   3004  * FDATASYNC indicates that we need only save enough metadata to be able
   3005  * to re-read the written data.  Note we duplicate AIX's requirement that
   3006  * the file be open for writing.
   3007  */
   3008 /* ARGSUSED */
   3009 int
   3010 sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
   3011 {
   3012 	/* {
   3013 		syscallarg(int) fd;
   3014 		syscallarg(int) flags;
   3015 		syscallarg(off_t) start;
   3016 		syscallarg(off_t) length;
   3017 	} */
   3018 	struct vnode *vp;
   3019 	file_t *fp;
   3020 	int flags, nflags;
   3021 	off_t s, e, len;
   3022 	int error;
   3023 
   3024 	/* fd_getvnode() will use the descriptor for us */
   3025 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3026 		return (error);
   3027 
   3028 	if ((fp->f_flag & FWRITE) == 0) {
   3029 		error = EBADF;
   3030 		goto out;
   3031 	}
   3032 
   3033 	flags = SCARG(uap, flags);
   3034 	if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
   3035 	    ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
   3036 		error = EINVAL;
   3037 		goto out;
   3038 	}
   3039 	/* Now set up the flags for value(s) to pass to VOP_FSYNC() */
   3040 	if (flags & FDATASYNC)
   3041 		nflags = FSYNC_DATAONLY | FSYNC_WAIT;
   3042 	else
   3043 		nflags = FSYNC_WAIT;
   3044 	if (flags & FDISKSYNC)
   3045 		nflags |= FSYNC_CACHE;
   3046 
   3047 	len = SCARG(uap, length);
   3048 	/* If length == 0, we do the whole file, and s = l = 0 will do that */
   3049 	if (len) {
   3050 		s = SCARG(uap, start);
   3051 		e = s + len;
   3052 		if (e < s) {
   3053 			error = EINVAL;
   3054 			goto out;
   3055 		}
   3056 	} else {
   3057 		e = 0;
   3058 		s = 0;
   3059 	}
   3060 
   3061 	vp = fp->f_data;
   3062 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3063 	error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
   3064 	VOP_UNLOCK(vp);
   3065 out:
   3066 	fd_putfile(SCARG(uap, fd));
   3067 	return (error);
   3068 }
   3069 
   3070 /*
   3071  * Sync the data of an open file.
   3072  */
   3073 /* ARGSUSED */
   3074 int
   3075 sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
   3076 {
   3077 	/* {
   3078 		syscallarg(int) fd;
   3079 	} */
   3080 	struct vnode *vp;
   3081 	file_t *fp;
   3082 	int error;
   3083 
   3084 	/* fd_getvnode() will use the descriptor for us */
   3085 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3086 		return (error);
   3087 	if ((fp->f_flag & FWRITE) == 0) {
   3088 		fd_putfile(SCARG(uap, fd));
   3089 		return (EBADF);
   3090 	}
   3091 	vp = fp->f_data;
   3092 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   3093 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
   3094 	VOP_UNLOCK(vp);
   3095 	fd_putfile(SCARG(uap, fd));
   3096 	return (error);
   3097 }
   3098 
   3099 /*
   3100  * Rename files, (standard) BSD semantics frontend.
   3101  */
   3102 /* ARGSUSED */
   3103 int
   3104 sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
   3105 {
   3106 	/* {
   3107 		syscallarg(const char *) from;
   3108 		syscallarg(const char *) to;
   3109 	} */
   3110 
   3111 	return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 0));
   3112 }
   3113 
   3114 /*
   3115  * Rename files, POSIX semantics frontend.
   3116  */
   3117 /* ARGSUSED */
   3118 int
   3119 sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
   3120 {
   3121 	/* {
   3122 		syscallarg(const char *) from;
   3123 		syscallarg(const char *) to;
   3124 	} */
   3125 
   3126 	return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 1));
   3127 }
   3128 
   3129 /*
   3130  * Rename files.  Source and destination must either both be directories,
   3131  * or both not be directories.  If target is a directory, it must be empty.
   3132  * If `from' and `to' refer to the same object, the value of the `retain'
   3133  * argument is used to determine whether `from' will be
   3134  *
   3135  * (retain == 0)	deleted unless `from' and `to' refer to the same
   3136  *			object in the file system's name space (BSD).
   3137  * (retain == 1)	always retained (POSIX).
   3138  */
   3139 int
   3140 do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
   3141 {
   3142 	struct vnode *tvp, *fvp, *tdvp;
   3143 	struct pathbuf *frompb, *topb;
   3144 	struct nameidata fromnd, tond;
   3145 	struct mount *fs;
   3146 	struct lwp *l = curlwp;
   3147 	struct proc *p;
   3148 	int error;
   3149 
   3150 	error = pathbuf_maybe_copyin(from, seg, &frompb);
   3151 	if (error) {
   3152 		return error;
   3153 	}
   3154 	error = pathbuf_maybe_copyin(to, seg, &topb);
   3155 	if (error) {
   3156 		pathbuf_destroy(frompb);
   3157 		return error;
   3158 	}
   3159 
   3160 	NDINIT(&fromnd, DELETE, LOCKPARENT | TRYEMULROOT | INRENAME,
   3161 	    frompb);
   3162 	if ((error = namei(&fromnd)) != 0) {
   3163 		pathbuf_destroy(frompb);
   3164 		pathbuf_destroy(topb);
   3165 		return (error);
   3166 	}
   3167 	if (fromnd.ni_dvp != fromnd.ni_vp)
   3168 		VOP_UNLOCK(fromnd.ni_dvp);
   3169 	fvp = fromnd.ni_vp;
   3170 
   3171 	fs = fvp->v_mount;
   3172 	error = VFS_RENAMELOCK_ENTER(fs);
   3173 	if (error) {
   3174 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3175 		vrele(fromnd.ni_dvp);
   3176 		vrele(fvp);
   3177 		goto out1;
   3178 	}
   3179 
   3180 	/*
   3181 	 * close, partially, yet another race - ideally we should only
   3182 	 * go as far as getting fromnd.ni_dvp before getting the per-fs
   3183 	 * lock, and then continue to get fromnd.ni_vp, but we can't do
   3184 	 * that with namei as it stands.
   3185 	 *
   3186 	 * This still won't prevent rmdir from nuking fromnd.ni_vp
   3187 	 * under us. The real fix is to get the locks in the right
   3188 	 * order and do the lookups in the right places, but that's a
   3189 	 * major rototill.
   3190 	 *
   3191 	 * Note: this logic (as well as this whole function) is cloned
   3192 	 * in nfs_serv.c. Proceed accordingly.
   3193 	 */
   3194 	vrele(fvp);
   3195 	if ((fromnd.ni_cnd.cn_namelen == 1 &&
   3196 	     fromnd.ni_cnd.cn_nameptr[0] == '.') ||
   3197 	    (fromnd.ni_cnd.cn_namelen == 2 &&
   3198 	     fromnd.ni_cnd.cn_nameptr[0] == '.' &&
   3199 	     fromnd.ni_cnd.cn_nameptr[1] == '.')) {
   3200 		error = EINVAL;
   3201 		VFS_RENAMELOCK_EXIT(fs);
   3202 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3203 		vrele(fromnd.ni_dvp);
   3204 		goto out1;
   3205 	}
   3206 	vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY);
   3207 	error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd, 0);
   3208 	if (error) {
   3209 		VOP_UNLOCK(fromnd.ni_dvp);
   3210 		VFS_RENAMELOCK_EXIT(fs);
   3211 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3212 		vrele(fromnd.ni_dvp);
   3213 		goto out1;
   3214 	}
   3215 	VOP_UNLOCK(fromnd.ni_vp);
   3216 	if (fromnd.ni_dvp != fromnd.ni_vp)
   3217 		VOP_UNLOCK(fromnd.ni_dvp);
   3218 	fvp = fromnd.ni_vp;
   3219 
   3220 	NDINIT(&tond, RENAME,
   3221 	    LOCKPARENT | LOCKLEAF | NOCACHE | TRYEMULROOT
   3222 	      | INRENAME | (fvp->v_type == VDIR ? CREATEDIR : 0),
   3223 	    topb);
   3224 	if ((error = namei(&tond)) != 0) {
   3225 		VFS_RENAMELOCK_EXIT(fs);
   3226 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3227 		vrele(fromnd.ni_dvp);
   3228 		vrele(fvp);
   3229 		goto out1;
   3230 	}
   3231 	tdvp = tond.ni_dvp;
   3232 	tvp = tond.ni_vp;
   3233 
   3234 	if (tvp != NULL) {
   3235 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   3236 			error = ENOTDIR;
   3237 			goto out;
   3238 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   3239 			error = EISDIR;
   3240 			goto out;
   3241 		}
   3242 	}
   3243 
   3244 	if (fvp == tdvp)
   3245 		error = EINVAL;
   3246 
   3247 	/*
   3248 	 * Source and destination refer to the same object.
   3249 	 */
   3250 	if (fvp == tvp) {
   3251 		if (retain)
   3252 			error = -1;
   3253 		else if (fromnd.ni_dvp == tdvp &&
   3254 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   3255 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
   3256 		          tond.ni_cnd.cn_nameptr,
   3257 		          fromnd.ni_cnd.cn_namelen))
   3258 		error = -1;
   3259 	}
   3260 	/*
   3261 	 * Prevent cross-mount operation.
   3262 	 */
   3263 	if (error == 0) {
   3264 		if (tond.ni_dvp->v_mount != fromnd.ni_dvp->v_mount) {
   3265 			error = EXDEV;
   3266 		}
   3267 	}
   3268 #if NVERIEXEC > 0
   3269 	if (!error) {
   3270 		char *f1, *f2;
   3271 		size_t f1_len;
   3272 		size_t f2_len;
   3273 
   3274 		f1_len = fromnd.ni_cnd.cn_namelen + 1;
   3275 		f1 = kmem_alloc(f1_len, KM_SLEEP);
   3276 		strlcpy(f1, fromnd.ni_cnd.cn_nameptr, f1_len);
   3277 
   3278 		f2_len = tond.ni_cnd.cn_namelen + 1;
   3279 		f2 = kmem_alloc(f2_len, KM_SLEEP);
   3280 		strlcpy(f2, tond.ni_cnd.cn_nameptr, f2_len);
   3281 
   3282 		error = veriexec_renamechk(l, fvp, f1, tvp, f2);
   3283 
   3284 		kmem_free(f1, f1_len);
   3285 		kmem_free(f2, f2_len);
   3286 	}
   3287 #endif /* NVERIEXEC > 0 */
   3288 
   3289 out:
   3290 	p = l->l_proc;
   3291 	if (!error) {
   3292 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   3293 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   3294 		VFS_RENAMELOCK_EXIT(fs);
   3295 	} else {
   3296 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   3297 		if (tdvp == tvp)
   3298 			vrele(tdvp);
   3299 		else
   3300 			vput(tdvp);
   3301 		if (tvp)
   3302 			vput(tvp);
   3303 		VFS_RENAMELOCK_EXIT(fs);
   3304 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3305 		vrele(fromnd.ni_dvp);
   3306 		vrele(fvp);
   3307 	}
   3308 out1:
   3309 	pathbuf_destroy(frompb);
   3310 	pathbuf_destroy(topb);
   3311 	return (error == -1 ? 0 : error);
   3312 }
   3313 
   3314 /*
   3315  * Make a directory file.
   3316  */
   3317 /* ARGSUSED */
   3318 int
   3319 sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
   3320 {
   3321 	/* {
   3322 		syscallarg(const char *) path;
   3323 		syscallarg(int) mode;
   3324 	} */
   3325 
   3326 	return do_sys_mkdir(SCARG(uap, path), SCARG(uap, mode), UIO_USERSPACE);
   3327 }
   3328 
   3329 int
   3330 do_sys_mkdir(const char *path, mode_t mode, enum uio_seg seg)
   3331 {
   3332 	struct proc *p = curlwp->l_proc;
   3333 	struct vnode *vp;
   3334 	struct vattr vattr;
   3335 	int error;
   3336 	struct pathbuf *pb;
   3337 	struct nameidata nd;
   3338 
   3339 	/* XXX bollocks, should pass in a pathbuf */
   3340 	error = pathbuf_maybe_copyin(path, seg, &pb);
   3341 	if (error) {
   3342 		return error;
   3343 	}
   3344 
   3345 	NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, pb);
   3346 	if ((error = namei(&nd)) != 0) {
   3347 		pathbuf_destroy(pb);
   3348 		return (error);
   3349 	}
   3350 	vp = nd.ni_vp;
   3351 	if (vp != NULL) {
   3352 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3353 		if (nd.ni_dvp == vp)
   3354 			vrele(nd.ni_dvp);
   3355 		else
   3356 			vput(nd.ni_dvp);
   3357 		vrele(vp);
   3358 		pathbuf_destroy(pb);
   3359 		return (EEXIST);
   3360 	}
   3361 	vattr_null(&vattr);
   3362 	vattr.va_type = VDIR;
   3363 	/* We will read cwdi->cwdi_cmask unlocked. */
   3364 	vattr.va_mode = (mode & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   3365 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   3366 	if (!error)
   3367 		vput(nd.ni_vp);
   3368 	pathbuf_destroy(pb);
   3369 	return (error);
   3370 }
   3371 
   3372 /*
   3373  * Remove a directory file.
   3374  */
   3375 /* ARGSUSED */
   3376 int
   3377 sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
   3378 {
   3379 	/* {
   3380 		syscallarg(const char *) path;
   3381 	} */
   3382 	struct vnode *vp;
   3383 	int error;
   3384 	struct pathbuf *pb;
   3385 	struct nameidata nd;
   3386 
   3387 	error = pathbuf_copyin(SCARG(uap, path), &pb);
   3388 	if (error) {
   3389 		return error;
   3390 	}
   3391 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, pb);
   3392 	if ((error = namei(&nd)) != 0) {
   3393 		pathbuf_destroy(pb);
   3394 		return error;
   3395 	}
   3396 	vp = nd.ni_vp;
   3397 	if (vp->v_type != VDIR) {
   3398 		error = ENOTDIR;
   3399 		goto out;
   3400 	}
   3401 	/*
   3402 	 * No rmdir "." please.
   3403 	 */
   3404 	if (nd.ni_dvp == vp) {
   3405 		error = EINVAL;
   3406 		goto out;
   3407 	}
   3408 	/*
   3409 	 * The root of a mounted filesystem cannot be deleted.
   3410 	 */
   3411 	if ((vp->v_vflag & VV_ROOT) != 0 || vp->v_mountedhere != NULL) {
   3412 		error = EBUSY;
   3413 		goto out;
   3414 	}
   3415 	error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   3416 	pathbuf_destroy(pb);
   3417 	return (error);
   3418 
   3419 out:
   3420 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3421 	if (nd.ni_dvp == vp)
   3422 		vrele(nd.ni_dvp);
   3423 	else
   3424 		vput(nd.ni_dvp);
   3425 	vput(vp);
   3426 	pathbuf_destroy(pb);
   3427 	return (error);
   3428 }
   3429 
   3430 /*
   3431  * Read a block of directory entries in a file system independent format.
   3432  */
   3433 int
   3434 sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
   3435 {
   3436 	/* {
   3437 		syscallarg(int) fd;
   3438 		syscallarg(char *) buf;
   3439 		syscallarg(size_t) count;
   3440 	} */
   3441 	file_t *fp;
   3442 	int error, done;
   3443 
   3444 	/* fd_getvnode() will use the descriptor for us */
   3445 	if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
   3446 		return (error);
   3447 	if ((fp->f_flag & FREAD) == 0) {
   3448 		error = EBADF;
   3449 		goto out;
   3450 	}
   3451 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   3452 			SCARG(uap, count), &done, l, 0, 0);
   3453 	ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
   3454 	*retval = done;
   3455  out:
   3456 	fd_putfile(SCARG(uap, fd));
   3457 	return (error);
   3458 }
   3459 
   3460 /*
   3461  * Set the mode mask for creation of filesystem nodes.
   3462  */
   3463 int
   3464 sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
   3465 {
   3466 	/* {
   3467 		syscallarg(mode_t) newmask;
   3468 	} */
   3469 	struct proc *p = l->l_proc;
   3470 	struct cwdinfo *cwdi;
   3471 
   3472 	/*
   3473 	 * cwdi->cwdi_cmask will be read unlocked elsewhere.  What's
   3474 	 * important is that we serialize changes to the mask.  The
   3475 	 * rw_exit() will issue a write memory barrier on our behalf,
   3476 	 * and force the changes out to other CPUs (as it must use an
   3477 	 * atomic operation, draining the local CPU's store buffers).
   3478 	 */
   3479 	cwdi = p->p_cwdi;
   3480 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   3481 	*retval = cwdi->cwdi_cmask;
   3482 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
   3483 	rw_exit(&cwdi->cwdi_lock);
   3484 
   3485 	return (0);
   3486 }
   3487 
   3488 int
   3489 dorevoke(struct vnode *vp, kauth_cred_t cred)
   3490 {
   3491 	struct vattr vattr;
   3492 	int error;
   3493 
   3494 	if ((error = VOP_GETATTR(vp, &vattr, cred)) != 0)
   3495 		return error;
   3496 	if (kauth_cred_geteuid(cred) == vattr.va_uid ||
   3497 	    (error = kauth_authorize_generic(cred,
   3498 	    KAUTH_GENERIC_ISSUSER, NULL)) == 0)
   3499 		VOP_REVOKE(vp, REVOKEALL);
   3500 	return (error);
   3501 }
   3502 
   3503 /*
   3504  * Void all references to file by ripping underlying filesystem
   3505  * away from vnode.
   3506  */
   3507 /* ARGSUSED */
   3508 int
   3509 sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
   3510 {
   3511 	/* {
   3512 		syscallarg(const char *) path;
   3513 	} */
   3514 	struct vnode *vp;
   3515 	int error;
   3516 
   3517 	error = namei_simple_user(SCARG(uap, path),
   3518 				NSM_FOLLOW_TRYEMULROOT, &vp);
   3519 	if (error != 0)
   3520 		return (error);
   3521 	error = dorevoke(vp, l->l_cred);
   3522 	vrele(vp);
   3523 	return (error);
   3524 }
   3525