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