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