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