Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.148
      1 /*	$NetBSD: vfs_syscalls.c,v 1.148 1999/11/15 18:49:09 fvdl 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)vfs_syscalls.c	8.42 (Berkeley) 7/31/95
     41  */
     42 
     43 #include "opt_compat_netbsd.h"
     44 #include "opt_compat_43.h"
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/namei.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/kernel.h>
     51 #include <sys/file.h>
     52 #include <sys/stat.h>
     53 #include <sys/vnode.h>
     54 #include <sys/mount.h>
     55 #include <sys/proc.h>
     56 #include <sys/uio.h>
     57 #include <sys/malloc.h>
     58 #include <sys/dirent.h>
     59 
     60 #include <sys/syscallargs.h>
     61 
     62 #include <vm/vm.h>
     63 #include <sys/sysctl.h>
     64 
     65 #include <miscfs/genfs/genfs.h>
     66 #include <miscfs/syncfs/syncfs.h>
     67 
     68 #include <uvm/uvm_extern.h>
     69 
     70 static int change_dir __P((struct nameidata *, struct proc *));
     71 static int change_mode __P((struct vnode *, int, struct proc *p));
     72 static int change_owner __P((struct vnode *, uid_t, gid_t, struct proc *,
     73     int));
     74 static int change_utimes __P((struct vnode *vp, const struct timeval *,
     75 	       struct proc *p));
     76 static int rename_files __P((const char *, const char *, struct proc *, int));
     77 
     78 void checkdirs __P((struct vnode *));
     79 int dounmount __P((struct mount *, int, struct proc *));
     80 
     81 /*
     82  * Virtual File System System Calls
     83  */
     84 
     85 /*
     86  * Mount a file system.
     87  */
     88 
     89 /*
     90  * This table is used to maintain compatibility with 4.3BSD
     91  * and NetBSD 0.9 mount syscalls.  Note, the order is important!
     92  *
     93  * Also note that not all of these had actual numbers in 4.3BSD
     94  * or NetBSD 0.9!
     95  */
     96 const char *mountcompatnames[] = {
     97 	NULL,		/* 0 = MOUNT_NONE */
     98 	MOUNT_FFS,	/* 1 */
     99 	MOUNT_NFS,	/* 2 */
    100 	MOUNT_MFS,	/* 3 */
    101 	MOUNT_MSDOS,	/* 4 */
    102 	MOUNT_LFS,	/* 5 */
    103 	NULL,		/* 6 = MOUNT_LOFS */
    104 	MOUNT_FDESC,	/* 7 */
    105 	MOUNT_PORTAL,	/* 8 */
    106 	MOUNT_NULL,	/* 9 */
    107 	MOUNT_UMAP,	/* 10 */
    108 	MOUNT_KERNFS,	/* 11 */
    109 	MOUNT_PROCFS,	/* 12 */
    110 	MOUNT_AFS,	/* 13 */
    111 	MOUNT_CD9660,	/* 14 = MOUNT_ISOFS */
    112 	MOUNT_UNION,	/* 15 */
    113 	MOUNT_ADOSFS,	/* 16 */
    114 	MOUNT_EXT2FS,	/* 17 */
    115 	MOUNT_CODA,	/* 18 */
    116 	MOUNT_FILECORE,	/* 19 */
    117 	MOUNT_NTFS,	/* 20 */
    118 };
    119 const int nmountcompatnames = sizeof(mountcompatnames) /
    120     sizeof(mountcompatnames[0]);
    121 
    122 /* ARGSUSED */
    123 int
    124 sys_mount(p, v, retval)
    125 	struct proc *p;
    126 	void *v;
    127 	register_t *retval;
    128 {
    129 	register struct sys_mount_args /* {
    130 		syscallarg(const char *) type;
    131 		syscallarg(const char *) path;
    132 		syscallarg(int) flags;
    133 		syscallarg(void *) data;
    134 	} */ *uap = v;
    135 	struct vnode *vp;
    136 	struct mount *mp;
    137 	int error, flag = 0;
    138 	char fstypename[MFSNAMELEN];
    139 	struct vattr va;
    140 	struct nameidata nd;
    141 	struct vfsops *vfs;
    142 
    143 	/*
    144 	 * Get vnode to be covered
    145 	 */
    146 	NDINIT(&nd, LOOKUP, FOLLOW , UIO_USERSPACE,
    147 	    SCARG(uap, path), p);
    148 	if ((error = namei(&nd)) != 0)
    149 		return (error);
    150 	vp = nd.ni_vp;
    151 	/*
    152 	 * A lookup in VFS_MOUNT might result in an attempt to
    153 	 * lock this vnode again, so make the lock resursive.
    154 	 */
    155 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
    156 	if (SCARG(uap, flags) & MNT_UPDATE) {
    157 		if ((vp->v_flag & VROOT) == 0) {
    158 			vput(vp);
    159 			return (EINVAL);
    160 		}
    161 		mp = vp->v_mount;
    162 		flag = mp->mnt_flag;
    163 		vfs = mp->mnt_op;
    164 		/*
    165 		 * We only allow the filesystem to be reloaded if it
    166 		 * is currently mounted read-only.
    167 		 */
    168 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
    169 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
    170 			vput(vp);
    171 			return (EOPNOTSUPP);	/* Needs translation */
    172 		}
    173 		/*
    174 		 * In "highly secure" mode, don't let the caller do anything
    175 		 * but downgrade a filesystem from read-write to read-only.
    176 		 * (see also below; MNT_UPDATE is required.)
    177 		 */
    178 		if (securelevel >= 2 &&
    179 		    (SCARG(uap, flags) !=
    180 		    (mp->mnt_flag | MNT_RDONLY |
    181 		    MNT_RELOAD | MNT_FORCE | MNT_UPDATE))) {
    182 			vput(vp);
    183 			return (EPERM);
    184 		}
    185 		mp->mnt_flag |=
    186 		    SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    187 		/*
    188 		 * Only root, or the user that did the original mount is
    189 		 * permitted to update it.
    190 		 */
    191 		if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
    192 		    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    193 			vput(vp);
    194 			return (error);
    195 		}
    196 		/*
    197 		 * Do not allow NFS export by non-root users. For non-root
    198 		 * users, silently enforce MNT_NOSUID and MNT_NODEV, and
    199 		 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
    200 		 */
    201 		if (p->p_ucred->cr_uid != 0) {
    202 			if (SCARG(uap, flags) & MNT_EXPORTED) {
    203 				vput(vp);
    204 				return (EPERM);
    205 			}
    206 			SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    207 			if (flag & MNT_NOEXEC)
    208 				SCARG(uap, flags) |= MNT_NOEXEC;
    209 		}
    210 		if (vfs_busy(mp, LK_NOWAIT, 0)) {
    211 			vput(vp);
    212 			return (EPERM);
    213 		}
    214 		VOP_UNLOCK(vp, 0);
    215 		goto update;
    216 	} else {
    217 		if (securelevel >= 2)
    218 			return (EPERM);
    219 	}
    220 	/*
    221 	 * If the user is not root, ensure that they own the directory
    222 	 * onto which we are attempting to mount.
    223 	 */
    224 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
    225 	    (va.va_uid != p->p_ucred->cr_uid &&
    226 		(error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
    227 		vput(vp);
    228 		return (error);
    229 	}
    230 	/*
    231 	 * Do not allow NFS export by non-root users. For non-root users,
    232 	 * silently enforce MNT_NOSUID and MNT_NODEV, and MNT_NOEXEC if the
    233 	 * mount point is already MNT_NOEXEC.
    234 	 */
    235 	if (p->p_ucred->cr_uid != 0) {
    236 		if (SCARG(uap, flags) & MNT_EXPORTED) {
    237 			vput(vp);
    238 			return (EPERM);
    239 		}
    240 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    241 		if (vp->v_mount->mnt_flag & MNT_NOEXEC)
    242 			SCARG(uap, flags) |= MNT_NOEXEC;
    243 	}
    244 	if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
    245 		return (error);
    246 	if (vp->v_type != VDIR) {
    247 		vput(vp);
    248 		return (ENOTDIR);
    249 	}
    250 	error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
    251 	if (error) {
    252 #if defined(COMPAT_09) || defined(COMPAT_43)
    253 		/*
    254 		 * Historically filesystem types were identified by number.
    255 		 * If we get an integer for the filesystem type instead of a
    256 		 * string, we check to see if it matches one of the historic
    257 		 * filesystem types.
    258 		 */
    259 		u_long fsindex = (u_long)SCARG(uap, type);
    260 		if (fsindex >= nmountcompatnames ||
    261 		    mountcompatnames[fsindex] == NULL) {
    262 			vput(vp);
    263 			return (ENODEV);
    264 		}
    265 		strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
    266 #else
    267 		vput(vp);
    268 		return (error);
    269 #endif
    270 	}
    271 #ifdef	COMPAT_10
    272 	/* Accept `ufs' as an alias for `ffs'. */
    273 	if (!strncmp(fstypename, "ufs", MFSNAMELEN))
    274 		strncpy(fstypename, "ffs", MFSNAMELEN);
    275 #endif
    276 	if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
    277 		vput(vp);
    278 		return (ENODEV);
    279 	}
    280 	if (vp->v_mountedhere != NULL) {
    281 		vput(vp);
    282 		return (EBUSY);
    283 	}
    284 
    285 	/*
    286 	 * Allocate and initialize the file system.
    287 	 */
    288 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    289 		M_MOUNT, M_WAITOK);
    290 	memset((char *)mp, 0, (u_long)sizeof(struct mount));
    291 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
    292 	(void)vfs_busy(mp, LK_NOWAIT, 0);
    293 	mp->mnt_op = vfs;
    294 	vfs->vfs_refcount++;
    295 	mp->mnt_vnodecovered = vp;
    296 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
    297 	mp->mnt_unmounter = NULL;
    298 update:
    299 	/*
    300 	 * Set the mount level flags.
    301 	 */
    302 	if (SCARG(uap, flags) & MNT_RDONLY)
    303 		mp->mnt_flag |= MNT_RDONLY;
    304 	else if (mp->mnt_flag & MNT_RDONLY)
    305 		mp->mnt_flag |= MNT_WANTRDWR;
    306 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    307 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    308 	    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM);
    309 	mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
    310 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
    311 	    MNT_NOCOREDUMP | MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM);
    312 	/*
    313 	 * Mount the filesystem.
    314 	 */
    315 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
    316 	if (mp->mnt_flag & MNT_UPDATE) {
    317 		vrele(vp);
    318 		if (mp->mnt_flag & MNT_WANTRDWR)
    319 			mp->mnt_flag &= ~MNT_RDONLY;
    320 		mp->mnt_flag &=~
    321 		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
    322 		if (error)
    323 			mp->mnt_flag = flag;
    324 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    325 			if (mp->mnt_syncer == NULL)
    326 				error = vfs_allocate_syncvnode(mp);
    327 		} else {
    328 			if (mp->mnt_syncer != NULL)
    329 				vgone(mp->mnt_syncer);
    330 			mp->mnt_syncer = NULL;
    331 		}
    332 		vfs_unbusy(mp);
    333 		return (error);
    334 	}
    335 	/*
    336 	 * Put the new filesystem on the mount list after root.
    337 	 */
    338 	cache_purge(vp);
    339 	if (!error) {
    340 		vp->v_mountedhere = mp;
    341 		simple_lock(&mountlist_slock);
    342 		CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    343 		simple_unlock(&mountlist_slock);
    344 		checkdirs(vp);
    345 		VOP_UNLOCK(vp, 0);
    346 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
    347 			error = vfs_allocate_syncvnode(mp);
    348 		vfs_unbusy(mp);
    349 		(void) VFS_STATFS(mp, &mp->mnt_stat, p);
    350 		if ((error = VFS_START(mp, 0, p)))
    351 			vrele(vp);
    352 	} else {
    353 		vp->v_mountedhere = (struct mount *)0;
    354 		vfs->vfs_refcount--;
    355 		vfs_unbusy(mp);
    356 		free((caddr_t)mp, M_MOUNT);
    357 		vput(vp);
    358 	}
    359 	return (error);
    360 }
    361 
    362 /*
    363  * Scan all active processes to see if any of them have a current
    364  * or root directory onto which the new filesystem has just been
    365  * mounted. If so, replace them with the new mount point.
    366  */
    367 void
    368 checkdirs(olddp)
    369 	struct vnode *olddp;
    370 {
    371 	struct cwdinfo *cwdi;
    372 	struct vnode *newdp;
    373 	struct proc *p;
    374 
    375 	if (olddp->v_usecount == 1)
    376 		return;
    377 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    378 		panic("mount: lost mount");
    379 	proclist_lock_read();
    380 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    381 		cwdi = p->p_cwdi;
    382 		if (cwdi->cwdi_cdir == olddp) {
    383 			vrele(cwdi->cwdi_cdir);
    384 			VREF(newdp);
    385 			cwdi->cwdi_cdir = newdp;
    386 		}
    387 		if (cwdi->cwdi_rdir == olddp) {
    388 			vrele(cwdi->cwdi_rdir);
    389 			VREF(newdp);
    390 			cwdi->cwdi_rdir = newdp;
    391 		}
    392 	}
    393 	proclist_unlock_read();
    394 	if (rootvnode == olddp) {
    395 		vrele(rootvnode);
    396 		VREF(newdp);
    397 		rootvnode = newdp;
    398 	}
    399 	vput(newdp);
    400 }
    401 
    402 /*
    403  * Unmount a file system.
    404  *
    405  * Note: unmount takes a path to the vnode mounted on as argument,
    406  * not special file (as before).
    407  */
    408 /* ARGSUSED */
    409 int
    410 sys_unmount(p, v, retval)
    411 	struct proc *p;
    412 	void *v;
    413 	register_t *retval;
    414 {
    415 	register struct sys_unmount_args /* {
    416 		syscallarg(const char *) path;
    417 		syscallarg(int) flags;
    418 	} */ *uap = v;
    419 	register struct vnode *vp;
    420 	struct mount *mp;
    421 	int error;
    422 	struct nameidata nd;
    423 
    424 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    425 	    SCARG(uap, path), p);
    426 	if ((error = namei(&nd)) != 0)
    427 		return (error);
    428 	vp = nd.ni_vp;
    429 	mp = vp->v_mount;
    430 
    431 	/*
    432 	 * Only root, or the user that did the original mount is
    433 	 * permitted to unmount this filesystem.
    434 	 */
    435 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
    436 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    437 		vput(vp);
    438 		return (error);
    439 	}
    440 
    441 	/*
    442 	 * Don't allow unmounting the root file system.
    443 	 */
    444 	if (mp->mnt_flag & MNT_ROOTFS) {
    445 		vput(vp);
    446 		return (EINVAL);
    447 	}
    448 
    449 	/*
    450 	 * Must be the root of the filesystem
    451 	 */
    452 	if ((vp->v_flag & VROOT) == 0) {
    453 		vput(vp);
    454 		return (EINVAL);
    455 	}
    456 	vput(vp);
    457 
    458 	if (vfs_busy(mp, 0, 0))
    459 		return (EBUSY);
    460 
    461 	return (dounmount(mp, SCARG(uap, flags), p));
    462 }
    463 
    464 /*
    465  * Do the actual file system unmount. File system is assumed to have been
    466  * marked busy by the caller.
    467  */
    468 int
    469 dounmount(mp, flags, p)
    470 	register struct mount *mp;
    471 	int flags;
    472 	struct proc *p;
    473 {
    474 	struct vnode *coveredvp;
    475 	int error;
    476 	int async;
    477 
    478 	simple_lock(&mountlist_slock);
    479 	vfs_unbusy(mp);
    480 	/*
    481 	 * XXX Freeze syncer. This should really be done on a mountpoint
    482 	 * basis, but especially the softdep code possibly called from
    483 	 * the syncer doesn't exactly work on a per-mountpoint basis,
    484 	 * so the softdep code would become a maze of vfs_busy calls.
    485 	 */
    486 	lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
    487 
    488 	mp->mnt_flag |= MNT_UNMOUNT;
    489 	mp->mnt_unmounter = p;
    490 	lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
    491 	if (mp->mnt_flag & MNT_EXPUBLIC)
    492 		vfs_setpublicfs(NULL, NULL, NULL);
    493 	async = mp->mnt_flag & MNT_ASYNC;
    494 	mp->mnt_flag &=~ MNT_ASYNC;
    495 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    496 	if (mp->mnt_syncer != NULL)
    497 		vgone(mp->mnt_syncer);
    498 	if (((mp->mnt_flag & MNT_RDONLY) ||
    499 	    (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
    500 	    (flags & MNT_FORCE))
    501 		error = VFS_UNMOUNT(mp, flags, p);
    502 	simple_lock(&mountlist_slock);
    503 	if (error) {
    504 		if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL)
    505 			(void) vfs_allocate_syncvnode(mp);
    506 		mp->mnt_flag &= ~MNT_UNMOUNT;
    507 		mp->mnt_unmounter = NULL;
    508 		mp->mnt_flag |= async;
    509 		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
    510 		    &mountlist_slock);
    511 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    512 		while(mp->mnt_wcnt > 0) {
    513 			wakeup((caddr_t)mp);
    514 			tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1", 0);
    515 		}
    516 		return (error);
    517 	}
    518 	CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
    519 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    520 		coveredvp->v_mountedhere = NULL;
    521 		vrele(coveredvp);
    522 	}
    523 	mp->mnt_op->vfs_refcount--;
    524 	if (mp->mnt_vnodelist.lh_first != NULL)
    525 		panic("unmount: dangling vnode");
    526 	mp->mnt_flag |= MNT_GONE;
    527 	lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
    528 	lockmgr(&syncer_lock, LK_RELEASE, NULL);
    529 	while(mp->mnt_wcnt > 0) {
    530 		wakeup((caddr_t)mp);
    531 		tsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0);
    532 	}
    533 	free((caddr_t)mp, M_MOUNT);
    534 	return (0);
    535 }
    536 
    537 /*
    538  * Sync each mounted filesystem.
    539  */
    540 #ifdef DEBUG
    541 int syncprt = 0;
    542 struct ctldebug debug0 = { "syncprt", &syncprt };
    543 #endif
    544 
    545 /* ARGSUSED */
    546 int
    547 sys_sync(p, v, retval)
    548 	struct proc *p;
    549 	void *v;
    550 	register_t *retval;
    551 {
    552 	register struct mount *mp, *nmp;
    553 	int asyncflag;
    554 
    555 	simple_lock(&mountlist_slock);
    556 	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
    557 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    558 			nmp = mp->mnt_list.cqe_prev;
    559 			continue;
    560 		}
    561 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    562 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    563 			mp->mnt_flag &= ~MNT_ASYNC;
    564 			uvm_vnp_sync(mp);
    565 			VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
    566 			if (asyncflag)
    567 				 mp->mnt_flag |= MNT_ASYNC;
    568 		}
    569 		simple_lock(&mountlist_slock);
    570 		nmp = mp->mnt_list.cqe_prev;
    571 		vfs_unbusy(mp);
    572 
    573 	}
    574 	simple_unlock(&mountlist_slock);
    575 #ifdef DEBUG
    576 	if (syncprt)
    577 		vfs_bufstats();
    578 #endif /* DEBUG */
    579 	return (0);
    580 }
    581 
    582 /*
    583  * Change filesystem quotas.
    584  */
    585 /* ARGSUSED */
    586 int
    587 sys_quotactl(p, v, retval)
    588 	struct proc *p;
    589 	void *v;
    590 	register_t *retval;
    591 {
    592 	register struct sys_quotactl_args /* {
    593 		syscallarg(const char *) path;
    594 		syscallarg(int) cmd;
    595 		syscallarg(int) uid;
    596 		syscallarg(caddr_t) arg;
    597 	} */ *uap = v;
    598 	register struct mount *mp;
    599 	int error;
    600 	struct nameidata nd;
    601 
    602 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    603 	if ((error = namei(&nd)) != 0)
    604 		return (error);
    605 	mp = nd.ni_vp->v_mount;
    606 	vrele(nd.ni_vp);
    607 	return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
    608 	    SCARG(uap, arg), p));
    609 }
    610 
    611 /*
    612  * Get filesystem statistics.
    613  */
    614 /* ARGSUSED */
    615 int
    616 sys_statfs(p, v, retval)
    617 	struct proc *p;
    618 	void *v;
    619 	register_t *retval;
    620 {
    621 	register struct sys_statfs_args /* {
    622 		syscallarg(const char *) path;
    623 		syscallarg(struct statfs *) buf;
    624 	} */ *uap = v;
    625 	register struct mount *mp;
    626 	register struct statfs *sp;
    627 	int error;
    628 	struct nameidata nd;
    629 
    630 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    631 	if ((error = namei(&nd)) != 0)
    632 		return (error);
    633 	mp = nd.ni_vp->v_mount;
    634 	sp = &mp->mnt_stat;
    635 	vrele(nd.ni_vp);
    636 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    637 		return (error);
    638 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    639 	sp->f_oflags = sp->f_flags & 0xffff;
    640 	return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
    641 }
    642 
    643 /*
    644  * Get filesystem statistics.
    645  */
    646 /* ARGSUSED */
    647 int
    648 sys_fstatfs(p, v, retval)
    649 	struct proc *p;
    650 	void *v;
    651 	register_t *retval;
    652 {
    653 	register struct sys_fstatfs_args /* {
    654 		syscallarg(int) fd;
    655 		syscallarg(struct statfs *) buf;
    656 	} */ *uap = v;
    657 	struct file *fp;
    658 	struct mount *mp;
    659 	register struct statfs *sp;
    660 	int error;
    661 
    662 	/* getvnode() will use the descriptor for us */
    663 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    664 		return (error);
    665 	mp = ((struct vnode *)fp->f_data)->v_mount;
    666 	sp = &mp->mnt_stat;
    667 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    668 		goto out;
    669 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    670 	sp->f_oflags = sp->f_flags & 0xffff;
    671 	error = copyout(sp, SCARG(uap, buf), sizeof(*sp));
    672  out:
    673 	FILE_UNUSE(fp, p);
    674 	return (error);
    675 }
    676 
    677 /*
    678  * Get statistics on all filesystems.
    679  */
    680 int
    681 sys_getfsstat(p, v, retval)
    682 	struct proc *p;
    683 	void *v;
    684 	register_t *retval;
    685 {
    686 	register struct sys_getfsstat_args /* {
    687 		syscallarg(struct statfs *) buf;
    688 		syscallarg(long) bufsize;
    689 		syscallarg(int) flags;
    690 	} */ *uap = v;
    691 	register struct mount *mp, *nmp;
    692 	register struct statfs *sp;
    693 	caddr_t sfsp;
    694 	long count, maxcount, error;
    695 
    696 	maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
    697 	sfsp = (caddr_t)SCARG(uap, buf);
    698 	simple_lock(&mountlist_slock);
    699 	count = 0;
    700 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    701 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    702 			nmp = mp->mnt_list.cqe_next;
    703 			continue;
    704 		}
    705 		if (sfsp && count < maxcount) {
    706 			sp = &mp->mnt_stat;
    707 			/*
    708 			 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    709 			 * refresh the fsstat cache. MNT_WAIT or MNT_LAXY
    710 			 * overrides MNT_NOWAIT.
    711 			 */
    712 			if (SCARG(uap, flags) != MNT_NOWAIT &&
    713 			    SCARG(uap, flags) != MNT_LAZY &&
    714 			    (SCARG(uap, flags) == MNT_WAIT ||
    715 			     SCARG(uap, flags) == 0) &&
    716 			    (error = VFS_STATFS(mp, sp, p)) != 0) {
    717 				simple_lock(&mountlist_slock);
    718 				nmp = mp->mnt_list.cqe_next;
    719 				vfs_unbusy(mp);
    720 				continue;
    721 			}
    722 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    723 			sp->f_oflags = sp->f_flags & 0xffff;
    724 			error = copyout(sp, sfsp, sizeof(*sp));
    725 			if (error) {
    726 				vfs_unbusy(mp);
    727 				return (error);
    728 			}
    729 			sfsp += sizeof(*sp);
    730 		}
    731 		count++;
    732 		simple_lock(&mountlist_slock);
    733 		nmp = mp->mnt_list.cqe_next;
    734 		vfs_unbusy(mp);
    735 	}
    736 	simple_unlock(&mountlist_slock);
    737 	if (sfsp && count > maxcount)
    738 		*retval = maxcount;
    739 	else
    740 		*retval = count;
    741 	return (0);
    742 }
    743 
    744 /*
    745  * Change current working directory to a given file descriptor.
    746  */
    747 /* ARGSUSED */
    748 int
    749 sys_fchdir(p, v, retval)
    750 	struct proc *p;
    751 	void *v;
    752 	register_t *retval;
    753 {
    754 	struct sys_fchdir_args /* {
    755 		syscallarg(int) fd;
    756 	} */ *uap = v;
    757 	struct filedesc *fdp = p->p_fd;
    758 	struct cwdinfo *cwdi = p->p_cwdi;
    759 	struct vnode *vp, *tdp;
    760 	struct mount *mp;
    761 	struct file *fp;
    762 	int error;
    763 
    764 	/* getvnode() will use the descriptor for us */
    765 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    766 		return (error);
    767 	vp = (struct vnode *)fp->f_data;
    768 
    769 	VREF(vp);
    770 	vn_lock(vp,  LK_EXCLUSIVE | LK_RETRY);
    771 	if (vp->v_type != VDIR)
    772 		error = ENOTDIR;
    773 	else
    774 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    775 	while (!error && (mp = vp->v_mountedhere) != NULL) {
    776 		if (vfs_busy(mp, 0, 0))
    777 			continue;
    778 		error = VFS_ROOT(mp, &tdp);
    779 		vfs_unbusy(mp);
    780 		if (error)
    781 			break;
    782 		vput(vp);
    783 		vp = tdp;
    784 	}
    785 	if (error) {
    786 		vput(vp);
    787 		goto out;
    788 	}
    789 	VOP_UNLOCK(vp, 0);
    790 
    791 	/*
    792 	 * Disallow changing to a directory not under the process's
    793 	 * current root directory (if there is one).
    794 	 */
    795 	if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, p)) {
    796 		vrele(vp);
    797 		error = EPERM;	/* operation not permitted */
    798 		goto out;
    799 	}
    800 
    801 	vrele(cwdi->cwdi_cdir);
    802 	cwdi->cwdi_cdir = vp;
    803  out:
    804 	FILE_UNUSE(fp, p);
    805 	return (error);
    806 }
    807 
    808 /*
    809  * Change this process's notion of the root directory to a given file descriptor.
    810  */
    811 
    812 int
    813 sys_fchroot(p, v, retval)
    814 	struct proc *p;
    815 	void *v;
    816 	register_t *retval;
    817 {
    818 	struct sys_fchroot_args *uap = v;
    819 	struct filedesc *fdp = p->p_fd;
    820 	struct cwdinfo *cwdi = p->p_cwdi;
    821 	struct vnode	*vp;
    822 	struct file	*fp;
    823 	int		 error;
    824 
    825 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    826 		return error;
    827 	/* getvnode() will use the descriptor for us */
    828 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    829 		return error;
    830 	vp = (struct vnode *) fp->f_data;
    831 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    832 	if (vp->v_type != VDIR)
    833 		error = ENOTDIR;
    834 	else
    835 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    836 	VOP_UNLOCK(vp, 0);
    837 	if (error)
    838 		goto out;
    839 	VREF(vp);
    840 
    841 	/*
    842 	 * Prevent escaping from chroot by putting the root under
    843 	 * the working directory.  Silently chdir to / if we aren't
    844 	 * already there.
    845 	 */
    846 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
    847 		/*
    848 		 * XXX would be more failsafe to change directory to a
    849 		 * deadfs node here instead
    850 		 */
    851 		vrele(cwdi->cwdi_cdir);
    852 		VREF(vp);
    853 		cwdi->cwdi_cdir = vp;
    854 	}
    855 
    856 	if (cwdi->cwdi_rdir != NULL)
    857 		vrele(cwdi->cwdi_rdir);
    858 	cwdi->cwdi_rdir = vp;
    859  out:
    860 	FILE_UNUSE(fp, p);
    861 	return (error);
    862 }
    863 
    864 
    865 
    866 /*
    867  * Change current working directory (``.'').
    868  */
    869 /* ARGSUSED */
    870 int
    871 sys_chdir(p, v, retval)
    872 	struct proc *p;
    873 	void *v;
    874 	register_t *retval;
    875 {
    876 	struct sys_chdir_args /* {
    877 		syscallarg(const char *) path;
    878 	} */ *uap = v;
    879 	struct cwdinfo *cwdi = p->p_cwdi;
    880 	int error;
    881 	struct nameidata nd;
    882 
    883 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    884 	    SCARG(uap, path), p);
    885 	if ((error = change_dir(&nd, p)) != 0)
    886 		return (error);
    887 	vrele(cwdi->cwdi_cdir);
    888 	cwdi->cwdi_cdir = nd.ni_vp;
    889 	return (0);
    890 }
    891 
    892 /*
    893  * Change notion of root (``/'') directory.
    894  */
    895 /* ARGSUSED */
    896 int
    897 sys_chroot(p, v, retval)
    898 	struct proc *p;
    899 	void *v;
    900 	register_t *retval;
    901 {
    902 	struct sys_chroot_args /* {
    903 		syscallarg(const char *) path;
    904 	} */ *uap = v;
    905 	struct cwdinfo *cwdi = p->p_cwdi;
    906 	struct vnode *vp;
    907 	int error;
    908 	struct nameidata nd;
    909 
    910 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    911 		return (error);
    912 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    913 	    SCARG(uap, path), p);
    914 	if ((error = change_dir(&nd, p)) != 0)
    915 		return (error);
    916 	if (cwdi->cwdi_rdir != NULL)
    917 		vrele(cwdi->cwdi_rdir);
    918 	vp = nd.ni_vp;
    919 	cwdi->cwdi_rdir = vp;
    920 
    921 	/*
    922 	 * Prevent escaping from chroot by putting the root under
    923 	 * the working directory.  Silently chdir to / if we aren't
    924 	 * already there.
    925 	 */
    926 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
    927 		/*
    928 		 * XXX would be more failsafe to change directory to a
    929 		 * deadfs node here instead
    930 		 */
    931 		vrele(cwdi->cwdi_cdir);
    932 		VREF(vp);
    933 		cwdi->cwdi_cdir = vp;
    934 	}
    935 
    936 	return (0);
    937 }
    938 
    939 /*
    940  * Common routine for chroot and chdir.
    941  */
    942 static int
    943 change_dir(ndp, p)
    944 	register struct nameidata *ndp;
    945 	struct proc *p;
    946 {
    947 	struct vnode *vp;
    948 	int error;
    949 
    950 	if ((error = namei(ndp)) != 0)
    951 		return (error);
    952 	vp = ndp->ni_vp;
    953 	if (vp->v_type != VDIR)
    954 		error = ENOTDIR;
    955 	else
    956 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    957 
    958 	if (error)
    959 		vput(vp);
    960 	else
    961 		VOP_UNLOCK(vp, 0);
    962 	return (error);
    963 }
    964 
    965 /*
    966  * Check permissions, allocate an open file structure,
    967  * and call the device open routine if any.
    968  */
    969 int
    970 sys_open(p, v, retval)
    971 	struct proc *p;
    972 	void *v;
    973 	register_t *retval;
    974 {
    975 	register struct sys_open_args /* {
    976 		syscallarg(const char *) path;
    977 		syscallarg(int) flags;
    978 		syscallarg(int) mode;
    979 	} */ *uap = v;
    980 	struct cwdinfo *cwdi = p->p_cwdi;
    981 	struct filedesc *fdp = p->p_fd;
    982 	struct file *fp;
    983 	struct vnode *vp;
    984 	int flags, cmode;
    985 	int type, indx, error;
    986 	struct flock lf;
    987 	struct nameidata nd;
    988 	extern struct fileops vnops;
    989 
    990 	flags = FFLAGS(SCARG(uap, flags));
    991 	if ((flags & (FREAD | FWRITE)) == 0)
    992 		return (EINVAL);
    993 	/* falloc() will use the file descriptor for us */
    994 	if ((error = falloc(p, &fp, &indx)) != 0)
    995 		return (error);
    996 	cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
    997 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    998 	p->p_dupfd = -indx - 1;			/* XXX check for fdopen */
    999 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
   1000 		FILE_UNUSE(fp, p);
   1001 		ffree(fp);
   1002 		if ((error == ENODEV || error == ENXIO) &&
   1003 		    p->p_dupfd >= 0 &&			/* XXX from fdopen */
   1004 		    (error =
   1005 			dupfdopen(p, indx, p->p_dupfd, flags, error)) == 0) {
   1006 			*retval = indx;
   1007 			return (0);
   1008 		}
   1009 		if (error == ERESTART)
   1010 			error = EINTR;
   1011 		fdp->fd_ofiles[indx] = NULL;
   1012 		return (error);
   1013 	}
   1014 	p->p_dupfd = 0;
   1015 	vp = nd.ni_vp;
   1016 	fp->f_flag = flags & FMASK;
   1017 	fp->f_type = DTYPE_VNODE;
   1018 	fp->f_ops = &vnops;
   1019 	fp->f_data = (caddr_t)vp;
   1020 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1021 		lf.l_whence = SEEK_SET;
   1022 		lf.l_start = 0;
   1023 		lf.l_len = 0;
   1024 		if (flags & O_EXLOCK)
   1025 			lf.l_type = F_WRLCK;
   1026 		else
   1027 			lf.l_type = F_RDLCK;
   1028 		type = F_FLOCK;
   1029 		if ((flags & FNONBLOCK) == 0)
   1030 			type |= F_WAIT;
   1031 		VOP_UNLOCK(vp, 0);
   1032 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
   1033 		if (error) {
   1034 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1035 			FILE_UNUSE(fp, p);
   1036 			ffree(fp);
   1037 			fdp->fd_ofiles[indx] = NULL;
   1038 			return (error);
   1039 		}
   1040 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1041 		fp->f_flag |= FHASLOCK;
   1042 	}
   1043 	VOP_UNLOCK(vp, 0);
   1044 	*retval = indx;
   1045 	FILE_UNUSE(fp, p);
   1046 	return (0);
   1047 }
   1048 
   1049 /*
   1050  * Get file handle system call
   1051  */
   1052 int
   1053 sys_getfh(p, v, retval)
   1054 	struct proc *p;
   1055 	register void *v;
   1056 	register_t *retval;
   1057 {
   1058 	register struct sys_getfh_args /* {
   1059 		syscallarg(char *) fname;
   1060 		syscallarg(fhandle_t *) fhp;
   1061 	} */ *uap = v;
   1062 	register struct vnode *vp;
   1063 	fhandle_t fh;
   1064 	int error;
   1065 	struct nameidata nd;
   1066 
   1067 	/*
   1068 	 * Must be super user
   1069 	 */
   1070 	error = suser(p->p_ucred, &p->p_acflag);
   1071 	if (error)
   1072 		return (error);
   1073 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1074 	    SCARG(uap, fname), p);
   1075 	error = namei(&nd);
   1076 	if (error)
   1077 		return (error);
   1078 	vp = nd.ni_vp;
   1079 	memset((caddr_t)&fh, 0, sizeof(fh));
   1080 	fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
   1081 	error = VFS_VPTOFH(vp, &fh.fh_fid);
   1082 	vput(vp);
   1083 	if (error)
   1084 		return (error);
   1085 	error = copyout((caddr_t)&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh));
   1086 	return (error);
   1087 }
   1088 
   1089 /*
   1090  * Open a file given a file handle.
   1091  *
   1092  * Check permissions, allocate an open file structure,
   1093  * and call the device open routine if any.
   1094  */
   1095 int
   1096 sys_fhopen(p, v, retval)
   1097 	struct proc *p;
   1098 	void *v;
   1099 	register_t *retval;
   1100 {
   1101 	register struct sys_fhopen_args /* {
   1102 		syscallarg(const fhandle_t *) fhp;
   1103 		syscallarg(int) flags;
   1104 	} */ *uap = v;
   1105 	struct filedesc *fdp = p->p_fd;
   1106 	struct file *fp;
   1107 	struct vnode *vp = NULL;
   1108 	struct mount *mp;
   1109 	struct ucred *cred = p->p_ucred;
   1110 	int flags;
   1111 	struct file *nfp;
   1112 	int type, indx, error=0;
   1113 	struct flock lf;
   1114 	struct vattr va;
   1115 	fhandle_t fh;
   1116 	extern struct fileops vnops;
   1117 
   1118 	/*
   1119 	 * Must be super user
   1120 	 */
   1121 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1122 		return (error);
   1123 
   1124 	flags = FFLAGS(SCARG(uap, flags));
   1125 	if ((flags & (FREAD | FWRITE)) == 0)
   1126 		return (EINVAL);
   1127 	if ((flags & O_CREAT))
   1128 		return (EINVAL);
   1129 	/* falloc() will use the file descriptor for us */
   1130 	if ((error = falloc(p, &nfp, &indx)) != 0)
   1131 		return (error);
   1132 	fp = nfp;
   1133 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1134 		goto bad;
   1135 
   1136 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
   1137 		error = ESTALE;
   1138 		goto bad;
   1139 	}
   1140 
   1141 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) {
   1142 		vp = NULL;	/* most likely unnecessary sanity for bad: */
   1143 		goto bad;
   1144 	}
   1145 
   1146 	/* Now do an effective vn_open */
   1147 
   1148 	if (vp->v_type == VSOCK) {
   1149 		error = EOPNOTSUPP;
   1150 		goto bad;
   1151 	}
   1152 	if (flags & FREAD) {
   1153 		if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
   1154 			goto bad;
   1155 	}
   1156 	if (flags & (FWRITE | O_TRUNC)) {
   1157 		if (vp->v_type == VDIR) {
   1158 			error = EISDIR;
   1159 			goto bad;
   1160 		}
   1161 		if ((error = vn_writechk(vp)) != 0 ||
   1162 		    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
   1163 			goto bad;
   1164 	}
   1165 	if (flags & O_TRUNC) {
   1166 		VOP_UNLOCK(vp, 0);			/* XXX */
   1167 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1168 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
   1169 		VATTR_NULL(&va);
   1170 		va.va_size = 0;
   1171 		if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
   1172 			goto bad;
   1173 	}
   1174 	if ((error = VOP_OPEN(vp, flags, cred, p)) != 0)
   1175 		goto bad;
   1176 	if (flags & FWRITE)
   1177 		vp->v_writecount++;
   1178 
   1179 	/* done with modified vn_open, now finish what sys_open does. */
   1180 
   1181 	fp->f_flag = flags & FMASK;
   1182 	fp->f_type = DTYPE_VNODE;
   1183 	fp->f_ops = &vnops;
   1184 	fp->f_data = (caddr_t)vp;
   1185 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1186 		lf.l_whence = SEEK_SET;
   1187 		lf.l_start = 0;
   1188 		lf.l_len = 0;
   1189 		if (flags & O_EXLOCK)
   1190 			lf.l_type = F_WRLCK;
   1191 		else
   1192 			lf.l_type = F_RDLCK;
   1193 		type = F_FLOCK;
   1194 		if ((flags & FNONBLOCK) == 0)
   1195 			type |= F_WAIT;
   1196 		VOP_UNLOCK(vp, 0);
   1197 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
   1198 		if (error) {
   1199 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1200 			FILE_UNUSE(fp, p);
   1201 			ffree(fp);
   1202 			fdp->fd_ofiles[indx] = NULL;
   1203 			return (error);
   1204 		}
   1205 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1206 		fp->f_flag |= FHASLOCK;
   1207 	}
   1208 	VOP_UNLOCK(vp, 0);
   1209 	*retval = indx;
   1210 	FILE_UNUSE(fp, p);
   1211 	return (0);
   1212 
   1213 bad:
   1214 	FILE_UNUSE(fp, p);
   1215 	ffree(fp);
   1216 	fdp->fd_ofiles[indx] = NULL;
   1217 	if (vp != NULL)
   1218 		vput(vp);
   1219 	return (error);
   1220 }
   1221 
   1222 /* ARGSUSED */
   1223 int
   1224 sys_fhstat(p, v, retval)
   1225 	struct proc *p;
   1226 	void *v;
   1227 	register_t *retval;
   1228 {
   1229 	register struct sys_fhstat_args /* {
   1230 		syscallarg(const fhandle_t *) fhp;
   1231 		syscallarg(struct stat *) sb;
   1232 	} */ *uap = v;
   1233 	struct stat sb;
   1234 	int error;
   1235 	fhandle_t fh;
   1236 	struct mount *mp;
   1237 	struct vnode *vp;
   1238 
   1239 	/*
   1240 	 * Must be super user
   1241 	 */
   1242 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1243 		return (error);
   1244 
   1245 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1246 		return (error);
   1247 
   1248 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1249 		return (ESTALE);
   1250 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1251 		return (error);
   1252 	error = vn_stat(vp, &sb, p);
   1253 	vput(vp);
   1254 	if (error)
   1255 		return (error);
   1256 	error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
   1257 	return (error);
   1258 }
   1259 
   1260 /* ARGSUSED */
   1261 int
   1262 sys_fhstatfs(p, v, retval)
   1263 	struct proc *p;
   1264 	void *v;
   1265 	register_t *retval;
   1266 {
   1267 	register struct sys_fhstatfs_args /*
   1268 		syscallarg(const fhandle_t *) fhp;
   1269 		syscallarg(struct statfs *) buf;
   1270 	} */ *uap = v;
   1271 	struct statfs sp;
   1272 	fhandle_t fh;
   1273 	struct mount *mp;
   1274 	struct vnode *vp;
   1275 	int error;
   1276 
   1277 	/*
   1278 	 * Must be super user
   1279 	 */
   1280 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1281 		return (error);
   1282 
   1283 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1284 		return (error);
   1285 
   1286 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1287 		return (ESTALE);
   1288 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1289 		return (error);
   1290 	mp = vp->v_mount;
   1291 	vput(vp);
   1292 	if ((error = VFS_STATFS(mp, &sp, p)) != 0)
   1293 		return (error);
   1294 	sp.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
   1295 	sp.f_oflags = sp.f_flags & 0xffff;
   1296 	return (copyout(&sp, SCARG(uap, buf), sizeof(sp)));
   1297 }
   1298 
   1299 /*
   1300  * Create a special file.
   1301  */
   1302 /* ARGSUSED */
   1303 int
   1304 sys_mknod(p, v, retval)
   1305 	struct proc *p;
   1306 	void *v;
   1307 	register_t *retval;
   1308 {
   1309 	register struct sys_mknod_args /* {
   1310 		syscallarg(const char *) path;
   1311 		syscallarg(int) mode;
   1312 		syscallarg(int) dev;
   1313 	} */ *uap = v;
   1314 	register struct vnode *vp;
   1315 	struct vattr vattr;
   1316 	int error;
   1317 	int whiteout = 0;
   1318 	struct nameidata nd;
   1319 
   1320 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1321 		return (error);
   1322 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1323 	if ((error = namei(&nd)) != 0)
   1324 		return (error);
   1325 	vp = nd.ni_vp;
   1326 	if (vp != NULL)
   1327 		error = EEXIST;
   1328 	else {
   1329 		VATTR_NULL(&vattr);
   1330 		vattr.va_mode =
   1331 		    (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1332 		vattr.va_rdev = SCARG(uap, dev);
   1333 		whiteout = 0;
   1334 
   1335 		switch (SCARG(uap, mode) & S_IFMT) {
   1336 		case S_IFMT:	/* used by badsect to flag bad sectors */
   1337 			vattr.va_type = VBAD;
   1338 			break;
   1339 		case S_IFCHR:
   1340 			vattr.va_type = VCHR;
   1341 			break;
   1342 		case S_IFBLK:
   1343 			vattr.va_type = VBLK;
   1344 			break;
   1345 		case S_IFWHT:
   1346 			whiteout = 1;
   1347 			break;
   1348 		default:
   1349 			error = EINVAL;
   1350 			break;
   1351 		}
   1352 	}
   1353 	if (!error) {
   1354 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1355 		if (whiteout) {
   1356 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
   1357 			if (error)
   1358 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1359 			vput(nd.ni_dvp);
   1360 		} else {
   1361 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
   1362 						&nd.ni_cnd, &vattr);
   1363 		}
   1364 	} else {
   1365 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1366 		if (nd.ni_dvp == vp)
   1367 			vrele(nd.ni_dvp);
   1368 		else
   1369 			vput(nd.ni_dvp);
   1370 		if (vp)
   1371 			vrele(vp);
   1372 	}
   1373 	return (error);
   1374 }
   1375 
   1376 /*
   1377  * Create a named pipe.
   1378  */
   1379 /* ARGSUSED */
   1380 int
   1381 sys_mkfifo(p, v, retval)
   1382 	struct proc *p;
   1383 	void *v;
   1384 	register_t *retval;
   1385 {
   1386 	register struct sys_mkfifo_args /* {
   1387 		syscallarg(const char *) path;
   1388 		syscallarg(int) mode;
   1389 	} */ *uap = v;
   1390 	struct vattr vattr;
   1391 	int error;
   1392 	struct nameidata nd;
   1393 
   1394 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1395 	if ((error = namei(&nd)) != 0)
   1396 		return (error);
   1397 	if (nd.ni_vp != NULL) {
   1398 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1399 		if (nd.ni_dvp == nd.ni_vp)
   1400 			vrele(nd.ni_dvp);
   1401 		else
   1402 			vput(nd.ni_dvp);
   1403 		vrele(nd.ni_vp);
   1404 		return (EEXIST);
   1405 	}
   1406 	VATTR_NULL(&vattr);
   1407 	vattr.va_type = VFIFO;
   1408 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1409 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1410 	return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
   1411 }
   1412 
   1413 /*
   1414  * Make a hard file link.
   1415  */
   1416 /* ARGSUSED */
   1417 int
   1418 sys_link(p, v, retval)
   1419 	struct proc *p;
   1420 	void *v;
   1421 	register_t *retval;
   1422 {
   1423 	register struct sys_link_args /* {
   1424 		syscallarg(const char *) path;
   1425 		syscallarg(const char *) link;
   1426 	} */ *uap = v;
   1427 	register struct vnode *vp;
   1428 	struct nameidata nd;
   1429 	int error;
   1430 
   1431 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1432 	if ((error = namei(&nd)) != 0)
   1433 		return (error);
   1434 	vp = nd.ni_vp;
   1435 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1436 	if ((error = namei(&nd)) != 0)
   1437 		goto out;
   1438 	if (nd.ni_vp) {
   1439 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1440 		if (nd.ni_dvp == nd.ni_vp)
   1441 			vrele(nd.ni_dvp);
   1442 		else
   1443 			vput(nd.ni_dvp);
   1444 		vrele(nd.ni_vp);
   1445 		error = EEXIST;
   1446 		goto out;
   1447 	}
   1448 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1449 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1450 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1451 out:
   1452 	vrele(vp);
   1453 	return (error);
   1454 }
   1455 
   1456 /*
   1457  * Make a symbolic link.
   1458  */
   1459 /* ARGSUSED */
   1460 int
   1461 sys_symlink(p, v, retval)
   1462 	struct proc *p;
   1463 	void *v;
   1464 	register_t *retval;
   1465 {
   1466 	register struct sys_symlink_args /* {
   1467 		syscallarg(const char *) path;
   1468 		syscallarg(const char *) link;
   1469 	} */ *uap = v;
   1470 	struct vattr vattr;
   1471 	char *path;
   1472 	int error;
   1473 	struct nameidata nd;
   1474 
   1475 	MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
   1476 	error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
   1477 	if (error)
   1478 		goto out;
   1479 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1480 	if ((error = namei(&nd)) != 0)
   1481 		goto out;
   1482 	if (nd.ni_vp) {
   1483 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1484 		if (nd.ni_dvp == nd.ni_vp)
   1485 			vrele(nd.ni_dvp);
   1486 		else
   1487 			vput(nd.ni_dvp);
   1488 		vrele(nd.ni_vp);
   1489 		error = EEXIST;
   1490 		goto out;
   1491 	}
   1492 	VATTR_NULL(&vattr);
   1493 	vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
   1494 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1495 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   1496 out:
   1497 	FREE(path, M_NAMEI);
   1498 	return (error);
   1499 }
   1500 
   1501 /*
   1502  * Delete a whiteout from the filesystem.
   1503  */
   1504 /* ARGSUSED */
   1505 int
   1506 sys_undelete(p, v, retval)
   1507 	struct proc *p;
   1508 	void *v;
   1509 	register_t *retval;
   1510 {
   1511 	register struct sys_undelete_args /* {
   1512 		syscallarg(const char *) path;
   1513 	} */ *uap = v;
   1514 	int error;
   1515 	struct nameidata nd;
   1516 
   1517 	NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
   1518 	    SCARG(uap, path), p);
   1519 	error = namei(&nd);
   1520 	if (error)
   1521 		return (error);
   1522 
   1523 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   1524 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1525 		if (nd.ni_dvp == nd.ni_vp)
   1526 			vrele(nd.ni_dvp);
   1527 		else
   1528 			vput(nd.ni_dvp);
   1529 		if (nd.ni_vp)
   1530 			vrele(nd.ni_vp);
   1531 		return (EEXIST);
   1532 	}
   1533 
   1534 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1535 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   1536 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1537 	vput(nd.ni_dvp);
   1538 	return (error);
   1539 }
   1540 
   1541 /*
   1542  * Delete a name from the filesystem.
   1543  */
   1544 /* ARGSUSED */
   1545 int
   1546 sys_unlink(p, v, retval)
   1547 	struct proc *p;
   1548 	void *v;
   1549 	register_t *retval;
   1550 {
   1551 	struct sys_unlink_args /* {
   1552 		syscallarg(const char *) path;
   1553 	} */ *uap = v;
   1554 	register struct vnode *vp;
   1555 	int error;
   1556 	struct nameidata nd;
   1557 
   1558 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1559 	    SCARG(uap, path), p);
   1560 	if ((error = namei(&nd)) != 0)
   1561 		return (error);
   1562 	vp = nd.ni_vp;
   1563 
   1564 	/*
   1565 	 * The root of a mounted filesystem cannot be deleted.
   1566 	 */
   1567 	if (vp->v_flag & VROOT) {
   1568 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1569 		if (nd.ni_dvp == vp)
   1570 			vrele(nd.ni_dvp);
   1571 		else
   1572 			vput(nd.ni_dvp);
   1573 		vput(vp);
   1574 		error = EBUSY;
   1575 		goto out;
   1576 	}
   1577 
   1578 	(void)uvm_vnp_uncache(vp);
   1579 
   1580 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1581 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1582 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1583 out:
   1584 	return (error);
   1585 }
   1586 
   1587 /*
   1588  * Reposition read/write file offset.
   1589  */
   1590 int
   1591 sys_lseek(p, v, retval)
   1592 	struct proc *p;
   1593 	void *v;
   1594 	register_t *retval;
   1595 {
   1596 	register struct sys_lseek_args /* {
   1597 		syscallarg(int) fd;
   1598 		syscallarg(int) pad;
   1599 		syscallarg(off_t) offset;
   1600 		syscallarg(int) whence;
   1601 	} */ *uap = v;
   1602 	struct ucred *cred = p->p_ucred;
   1603 	register struct filedesc *fdp = p->p_fd;
   1604 	register struct file *fp;
   1605 	struct vnode *vp;
   1606 	struct vattr vattr;
   1607 	register off_t newoff;
   1608 	int error;
   1609 
   1610 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
   1611 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
   1612 	    (fp->f_iflags & FIF_WANTCLOSE) != 0)
   1613 		return (EBADF);
   1614 
   1615 	FILE_USE(fp);
   1616 
   1617 	vp = (struct vnode *)fp->f_data;
   1618 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1619 		error = ESPIPE;
   1620 		goto out;
   1621 	}
   1622 
   1623 	switch (SCARG(uap, whence)) {
   1624 	case SEEK_CUR:
   1625 		newoff = fp->f_offset + SCARG(uap, offset);
   1626 		break;
   1627 	case SEEK_END:
   1628 		error = VOP_GETATTR(vp, &vattr, cred, p);
   1629 		if (error)
   1630 			goto out;
   1631 		newoff = SCARG(uap, offset) + vattr.va_size;
   1632 		break;
   1633 	case SEEK_SET:
   1634 		newoff = SCARG(uap, offset);
   1635 		break;
   1636 	default:
   1637 		error = EINVAL;
   1638 		goto out;
   1639 	}
   1640 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
   1641 		goto out;
   1642 
   1643 	*(off_t *)retval = fp->f_offset = newoff;
   1644  out:
   1645 	FILE_UNUSE(fp, p);
   1646 	return (error);
   1647 }
   1648 
   1649 /*
   1650  * Positional read system call.
   1651  */
   1652 int
   1653 sys_pread(p, v, retval)
   1654 	struct proc *p;
   1655 	void *v;
   1656 	register_t *retval;
   1657 {
   1658 	struct sys_pread_args /* {
   1659 		syscallarg(int) fd;
   1660 		syscallarg(void *) buf;
   1661 		syscallarg(size_t) nbyte;
   1662 		syscallarg(off_t) offset;
   1663 	} */ *uap = v;
   1664 	struct filedesc *fdp = p->p_fd;
   1665 	struct file *fp;
   1666 	struct vnode *vp;
   1667 	off_t offset;
   1668 	int error, fd = SCARG(uap, fd);
   1669 
   1670 	if ((u_int)fd >= fdp->fd_nfiles ||
   1671 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   1672 	    (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
   1673 	    (fp->f_flag & FREAD) == 0)
   1674 		return (EBADF);
   1675 
   1676 	FILE_USE(fp);
   1677 
   1678 	vp = (struct vnode *)fp->f_data;
   1679 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1680 		error = ESPIPE;
   1681 		goto out;
   1682 	}
   1683 
   1684 	offset = SCARG(uap, offset);
   1685 
   1686 	/*
   1687 	 * XXX This works because no file systems actually
   1688 	 * XXX take any action on the seek operation.
   1689 	 */
   1690 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1691 		goto out;
   1692 
   1693 	/* dofileread() will unuse the descriptor for us */
   1694 	return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   1695 	    &offset, 0, retval));
   1696 
   1697  out:
   1698 	FILE_UNUSE(fp, p);
   1699 	return (error);
   1700 }
   1701 
   1702 /*
   1703  * Positional scatter read system call.
   1704  */
   1705 int
   1706 sys_preadv(p, v, retval)
   1707 	struct proc *p;
   1708 	void *v;
   1709 	register_t *retval;
   1710 {
   1711 	struct sys_preadv_args /* {
   1712 		syscallarg(int) fd;
   1713 		syscallarg(const struct iovec *) iovp;
   1714 		syscallarg(int) iovcnt;
   1715 		syscallarg(off_t) offset;
   1716 	} */ *uap = v;
   1717 	struct filedesc *fdp = p->p_fd;
   1718 	struct file *fp;
   1719 	struct vnode *vp;
   1720 	off_t offset;
   1721 	int error, fd = SCARG(uap, fd);
   1722 
   1723 	if ((u_int)fd >= fdp->fd_nfiles ||
   1724 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   1725 	    (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
   1726 	    (fp->f_flag & FREAD) == 0)
   1727 		return (EBADF);
   1728 
   1729 	FILE_USE(fp);
   1730 
   1731 	vp = (struct vnode *)fp->f_data;
   1732 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1733 		error = ESPIPE;
   1734 		goto out;
   1735 	}
   1736 
   1737 	offset = SCARG(uap, offset);
   1738 
   1739 	/*
   1740 	 * XXX This works because no file systems actually
   1741 	 * XXX take any action on the seek operation.
   1742 	 */
   1743 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1744 		goto out;
   1745 
   1746 	/* dofilereadv() will unuse the descriptor for us */
   1747 	return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   1748 	    &offset, 0, retval));
   1749 
   1750  out:
   1751 	FILE_UNUSE(fp, p);
   1752 	return (error);
   1753 }
   1754 
   1755 /*
   1756  * Positional write system call.
   1757  */
   1758 int
   1759 sys_pwrite(p, v, retval)
   1760 	struct proc *p;
   1761 	void *v;
   1762 	register_t *retval;
   1763 {
   1764 	struct sys_pwrite_args /* {
   1765 		syscallarg(int) fd;
   1766 		syscallarg(const void *) buf;
   1767 		syscallarg(size_t) nbyte;
   1768 		syscallarg(off_t) offset;
   1769 	} */ *uap = v;
   1770 	struct filedesc *fdp = p->p_fd;
   1771 	struct file *fp;
   1772 	struct vnode *vp;
   1773 	off_t offset;
   1774 	int error, fd = SCARG(uap, fd);
   1775 
   1776 	if ((u_int)fd >= fdp->fd_nfiles ||
   1777 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   1778 	    (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
   1779 	    (fp->f_flag & FWRITE) == 0)
   1780 		return (EBADF);
   1781 
   1782 	FILE_USE(fp);
   1783 
   1784 	vp = (struct vnode *)fp->f_data;
   1785 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1786 		error = ESPIPE;
   1787 		goto out;
   1788 	}
   1789 
   1790 	offset = SCARG(uap, offset);
   1791 
   1792 	/*
   1793 	 * XXX This works because no file systems actually
   1794 	 * XXX take any action on the seek operation.
   1795 	 */
   1796 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1797 		goto out;
   1798 
   1799 	/* dofilewrite() will unuse the descriptor for us */
   1800 	return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   1801 	    &offset, 0, retval));
   1802 
   1803  out:
   1804 	FILE_UNUSE(fp, p);
   1805 	return (error);
   1806 }
   1807 
   1808 /*
   1809  * Positional gather write system call.
   1810  */
   1811 int
   1812 sys_pwritev(p, v, retval)
   1813 	struct proc *p;
   1814 	void *v;
   1815 	register_t *retval;
   1816 {
   1817 	struct sys_pwritev_args /* {
   1818 		syscallarg(int) fd;
   1819 		syscallarg(const struct iovec *) iovp;
   1820 		syscallarg(int) iovcnt;
   1821 		syscallarg(off_t) offset;
   1822 	} */ *uap = v;
   1823 	struct filedesc *fdp = p->p_fd;
   1824 	struct file *fp;
   1825 	struct vnode *vp;
   1826 	off_t offset;
   1827 	int error, fd = SCARG(uap, fd);
   1828 
   1829 	if ((u_int)fd >= fdp->fd_nfiles ||
   1830 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   1831 	    (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
   1832 	    (fp->f_flag & FWRITE) == 0)
   1833 		return (EBADF);
   1834 
   1835 	FILE_USE(fp);
   1836 
   1837 	vp = (struct vnode *)fp->f_data;
   1838 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1839 		error = ESPIPE;
   1840 		goto out;
   1841 	}
   1842 
   1843 	offset = SCARG(uap, offset);
   1844 
   1845 	/*
   1846 	 * XXX This works because no file systems actually
   1847 	 * XXX take any action on the seek operation.
   1848 	 */
   1849 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1850 		goto out;
   1851 
   1852 	/* dofilewritev() will unuse the descriptor for us */
   1853 	return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   1854 	    &offset, 0, retval));
   1855 
   1856  out:
   1857 	FILE_UNUSE(fp, p);
   1858 	return (error);
   1859 }
   1860 
   1861 /*
   1862  * Check access permissions.
   1863  */
   1864 int
   1865 sys_access(p, v, retval)
   1866 	struct proc *p;
   1867 	void *v;
   1868 	register_t *retval;
   1869 {
   1870 	register struct sys_access_args /* {
   1871 		syscallarg(const char *) path;
   1872 		syscallarg(int) flags;
   1873 	} */ *uap = v;
   1874 	register struct ucred *cred = p->p_ucred;
   1875 	register struct vnode *vp;
   1876 	int error, flags, t_gid, t_uid;
   1877 	struct nameidata nd;
   1878 
   1879 	t_uid = cred->cr_uid;
   1880 	t_gid = cred->cr_gid;
   1881 	cred->cr_uid = p->p_cred->p_ruid;
   1882 	cred->cr_gid = p->p_cred->p_rgid;
   1883 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1884 	    SCARG(uap, path), p);
   1885 	if ((error = namei(&nd)) != 0)
   1886 		goto out1;
   1887 	vp = nd.ni_vp;
   1888 
   1889 	/* Flags == 0 means only check for existence. */
   1890 	if (SCARG(uap, flags)) {
   1891 		flags = 0;
   1892 		if (SCARG(uap, flags) & R_OK)
   1893 			flags |= VREAD;
   1894 		if (SCARG(uap, flags) & W_OK)
   1895 			flags |= VWRITE;
   1896 		if (SCARG(uap, flags) & X_OK)
   1897 			flags |= VEXEC;
   1898 
   1899 		error = VOP_ACCESS(vp, flags, cred, p);
   1900 		if (!error && (flags & VWRITE))
   1901 			error = vn_writechk(vp);
   1902 	}
   1903 	vput(vp);
   1904 out1:
   1905 	cred->cr_uid = t_uid;
   1906 	cred->cr_gid = t_gid;
   1907 	return (error);
   1908 }
   1909 
   1910 /*
   1911  * Get file status; this version follows links.
   1912  */
   1913 /* ARGSUSED */
   1914 int
   1915 sys___stat13(p, v, retval)
   1916 	struct proc *p;
   1917 	void *v;
   1918 	register_t *retval;
   1919 {
   1920 	register struct sys___stat13_args /* {
   1921 		syscallarg(const char *) path;
   1922 		syscallarg(struct stat *) ub;
   1923 	} */ *uap = v;
   1924 	struct stat sb;
   1925 	int error;
   1926 	struct nameidata nd;
   1927 
   1928 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1929 	    SCARG(uap, path), p);
   1930 	if ((error = namei(&nd)) != 0)
   1931 		return (error);
   1932 	error = vn_stat(nd.ni_vp, &sb, p);
   1933 	vput(nd.ni_vp);
   1934 	if (error)
   1935 		return (error);
   1936 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   1937 	return (error);
   1938 }
   1939 
   1940 /*
   1941  * Get file status; this version does not follow links.
   1942  */
   1943 /* ARGSUSED */
   1944 int
   1945 sys___lstat13(p, v, retval)
   1946 	struct proc *p;
   1947 	void *v;
   1948 	register_t *retval;
   1949 {
   1950 	register struct sys___lstat13_args /* {
   1951 		syscallarg(const char *) path;
   1952 		syscallarg(struct stat *) ub;
   1953 	} */ *uap = v;
   1954 	struct stat sb;
   1955 	int error;
   1956 	struct nameidata nd;
   1957 
   1958 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   1959 	    SCARG(uap, path), p);
   1960 	if ((error = namei(&nd)) != 0)
   1961 		return (error);
   1962 	error = vn_stat(nd.ni_vp, &sb, p);
   1963 	vput(nd.ni_vp);
   1964 	if (error)
   1965 		return (error);
   1966 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   1967 	return (error);
   1968 }
   1969 
   1970 /*
   1971  * Get configurable pathname variables.
   1972  */
   1973 /* ARGSUSED */
   1974 int
   1975 sys_pathconf(p, v, retval)
   1976 	struct proc *p;
   1977 	void *v;
   1978 	register_t *retval;
   1979 {
   1980 	register struct sys_pathconf_args /* {
   1981 		syscallarg(const char *) path;
   1982 		syscallarg(int) name;
   1983 	} */ *uap = v;
   1984 	int error;
   1985 	struct nameidata nd;
   1986 
   1987 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1988 	    SCARG(uap, path), p);
   1989 	if ((error = namei(&nd)) != 0)
   1990 		return (error);
   1991 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   1992 	vput(nd.ni_vp);
   1993 	return (error);
   1994 }
   1995 
   1996 /*
   1997  * Return target name of a symbolic link.
   1998  */
   1999 /* ARGSUSED */
   2000 int
   2001 sys_readlink(p, v, retval)
   2002 	struct proc *p;
   2003 	void *v;
   2004 	register_t *retval;
   2005 {
   2006 	register struct sys_readlink_args /* {
   2007 		syscallarg(const char *) path;
   2008 		syscallarg(char *) buf;
   2009 		syscallarg(size_t) count;
   2010 	} */ *uap = v;
   2011 	register struct vnode *vp;
   2012 	struct iovec aiov;
   2013 	struct uio auio;
   2014 	int error;
   2015 	struct nameidata nd;
   2016 
   2017 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2018 	    SCARG(uap, path), p);
   2019 	if ((error = namei(&nd)) != 0)
   2020 		return (error);
   2021 	vp = nd.ni_vp;
   2022 	if (vp->v_type != VLNK)
   2023 		error = EINVAL;
   2024 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   2025 	    (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
   2026 		aiov.iov_base = SCARG(uap, buf);
   2027 		aiov.iov_len = SCARG(uap, count);
   2028 		auio.uio_iov = &aiov;
   2029 		auio.uio_iovcnt = 1;
   2030 		auio.uio_offset = 0;
   2031 		auio.uio_rw = UIO_READ;
   2032 		auio.uio_segflg = UIO_USERSPACE;
   2033 		auio.uio_procp = p;
   2034 		auio.uio_resid = SCARG(uap, count);
   2035 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   2036 	}
   2037 	vput(vp);
   2038 	*retval = SCARG(uap, count) - auio.uio_resid;
   2039 	return (error);
   2040 }
   2041 
   2042 /*
   2043  * Change flags of a file given a path name.
   2044  */
   2045 /* ARGSUSED */
   2046 int
   2047 sys_chflags(p, v, retval)
   2048 	struct proc *p;
   2049 	void *v;
   2050 	register_t *retval;
   2051 {
   2052 	register struct sys_chflags_args /* {
   2053 		syscallarg(const char *) path;
   2054 		syscallarg(u_long) flags;
   2055 	} */ *uap = v;
   2056 	register struct vnode *vp;
   2057 	struct vattr vattr;
   2058 	int error;
   2059 	struct nameidata nd;
   2060 
   2061 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2062 	if ((error = namei(&nd)) != 0)
   2063 		return (error);
   2064 	vp = nd.ni_vp;
   2065 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2066 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2067 	/* Non-superusers cannot change the flags on devices, even if they
   2068 	   own them. */
   2069 	if (suser(p->p_ucred, &p->p_acflag)) {
   2070 		if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2071 			goto out;
   2072 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2073 			error = EINVAL;
   2074 			goto out;
   2075 		}
   2076 	}
   2077 	VATTR_NULL(&vattr);
   2078 	vattr.va_flags = SCARG(uap, flags);
   2079 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2080 out:
   2081 	vput(vp);
   2082 	return (error);
   2083 }
   2084 
   2085 /*
   2086  * Change flags of a file given a file descriptor.
   2087  */
   2088 /* ARGSUSED */
   2089 int
   2090 sys_fchflags(p, v, retval)
   2091 	struct proc *p;
   2092 	void *v;
   2093 	register_t *retval;
   2094 {
   2095 	register struct sys_fchflags_args /* {
   2096 		syscallarg(int) fd;
   2097 		syscallarg(u_long) flags;
   2098 	} */ *uap = v;
   2099 	struct vattr vattr;
   2100 	struct vnode *vp;
   2101 	struct file *fp;
   2102 	int error;
   2103 
   2104 	/* getvnode() will use the descriptor for us */
   2105 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2106 		return (error);
   2107 	vp = (struct vnode *)fp->f_data;
   2108 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2109 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2110 	/* Non-superusers cannot change the flags on devices, even if they
   2111 	   own them. */
   2112 	if (suser(p->p_ucred, &p->p_acflag)) {
   2113 		if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
   2114 		    != 0)
   2115 			goto out;
   2116 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2117 			error = EINVAL;
   2118 			goto out;
   2119 		}
   2120 	}
   2121 	VATTR_NULL(&vattr);
   2122 	vattr.va_flags = SCARG(uap, flags);
   2123 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2124 out:
   2125 	VOP_UNLOCK(vp, 0);
   2126 	FILE_UNUSE(fp, p);
   2127 	return (error);
   2128 }
   2129 
   2130 /*
   2131  * Change mode of a file given path name; this version follows links.
   2132  */
   2133 /* ARGSUSED */
   2134 int
   2135 sys_chmod(p, v, retval)
   2136 	struct proc *p;
   2137 	void *v;
   2138 	register_t *retval;
   2139 {
   2140 	register struct sys_chmod_args /* {
   2141 		syscallarg(const char *) path;
   2142 		syscallarg(int) mode;
   2143 	} */ *uap = v;
   2144 	int error;
   2145 	struct nameidata nd;
   2146 
   2147 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2148 	if ((error = namei(&nd)) != 0)
   2149 		return (error);
   2150 
   2151 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2152 
   2153 	vrele(nd.ni_vp);
   2154 	return (error);
   2155 }
   2156 
   2157 /*
   2158  * Change mode of a file given a file descriptor.
   2159  */
   2160 /* ARGSUSED */
   2161 int
   2162 sys_fchmod(p, v, retval)
   2163 	struct proc *p;
   2164 	void *v;
   2165 	register_t *retval;
   2166 {
   2167 	register struct sys_fchmod_args /* {
   2168 		syscallarg(int) fd;
   2169 		syscallarg(int) mode;
   2170 	} */ *uap = v;
   2171 	struct file *fp;
   2172 	int error;
   2173 
   2174 	/* getvnode() will use the descriptor for us */
   2175 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2176 		return (error);
   2177 
   2178 	error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
   2179 	FILE_UNUSE(fp, p);
   2180 	return (error);
   2181 }
   2182 
   2183 /*
   2184  * Change mode of a file given path name; this version does not follow links.
   2185  */
   2186 /* ARGSUSED */
   2187 int
   2188 sys_lchmod(p, v, retval)
   2189 	struct proc *p;
   2190 	void *v;
   2191 	register_t *retval;
   2192 {
   2193 	register struct sys_lchmod_args /* {
   2194 		syscallarg(const char *) path;
   2195 		syscallarg(int) mode;
   2196 	} */ *uap = v;
   2197 	int error;
   2198 	struct nameidata nd;
   2199 
   2200 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2201 	if ((error = namei(&nd)) != 0)
   2202 		return (error);
   2203 
   2204 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2205 
   2206 	vrele(nd.ni_vp);
   2207 	return (error);
   2208 }
   2209 
   2210 /*
   2211  * Common routine to set mode given a vnode.
   2212  */
   2213 static int
   2214 change_mode(vp, mode, p)
   2215 	struct vnode *vp;
   2216 	int mode;
   2217 	struct proc *p;
   2218 {
   2219 	struct vattr vattr;
   2220 	int error;
   2221 
   2222 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2223 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2224 	VATTR_NULL(&vattr);
   2225 	vattr.va_mode = mode & ALLPERMS;
   2226 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2227 	VOP_UNLOCK(vp, 0);
   2228 	return (error);
   2229 }
   2230 
   2231 /*
   2232  * Set ownership given a path name; this version follows links.
   2233  */
   2234 /* ARGSUSED */
   2235 int
   2236 sys_chown(p, v, retval)
   2237 	struct proc *p;
   2238 	void *v;
   2239 	register_t *retval;
   2240 {
   2241 	register struct sys_chown_args /* {
   2242 		syscallarg(const char *) path;
   2243 		syscallarg(uid_t) uid;
   2244 		syscallarg(gid_t) gid;
   2245 	} */ *uap = v;
   2246 	int error;
   2247 	struct nameidata nd;
   2248 
   2249 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2250 	if ((error = namei(&nd)) != 0)
   2251 		return (error);
   2252 
   2253 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2254 
   2255 	vrele(nd.ni_vp);
   2256 	return (error);
   2257 }
   2258 
   2259 /*
   2260  * Set ownership given a path name; this version follows links.
   2261  * Provides POSIX semantics.
   2262  */
   2263 /* ARGSUSED */
   2264 int
   2265 sys___posix_chown(p, v, retval)
   2266 	struct proc *p;
   2267 	void *v;
   2268 	register_t *retval;
   2269 {
   2270 	register struct sys_chown_args /* {
   2271 		syscallarg(const char *) path;
   2272 		syscallarg(uid_t) uid;
   2273 		syscallarg(gid_t) gid;
   2274 	} */ *uap = v;
   2275 	int error;
   2276 	struct nameidata nd;
   2277 
   2278 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2279 	if ((error = namei(&nd)) != 0)
   2280 		return (error);
   2281 
   2282 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2283 
   2284 	vrele(nd.ni_vp);
   2285 	return (error);
   2286 }
   2287 
   2288 /*
   2289  * Set ownership given a file descriptor.
   2290  */
   2291 /* ARGSUSED */
   2292 int
   2293 sys_fchown(p, v, retval)
   2294 	struct proc *p;
   2295 	void *v;
   2296 	register_t *retval;
   2297 {
   2298 	register struct sys_fchown_args /* {
   2299 		syscallarg(int) fd;
   2300 		syscallarg(uid_t) uid;
   2301 		syscallarg(gid_t) gid;
   2302 	} */ *uap = v;
   2303 	int error;
   2304 	struct file *fp;
   2305 
   2306 	/* getvnode() will use the descriptor for us */
   2307 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2308 		return (error);
   2309 
   2310 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2311 	    SCARG(uap, gid), p, 0);
   2312 	FILE_UNUSE(fp, p);
   2313 	return (error);
   2314 }
   2315 
   2316 /*
   2317  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   2318  */
   2319 /* ARGSUSED */
   2320 int
   2321 sys___posix_fchown(p, v, retval)
   2322 	struct proc *p;
   2323 	void *v;
   2324 	register_t *retval;
   2325 {
   2326 	register struct sys_fchown_args /* {
   2327 		syscallarg(int) fd;
   2328 		syscallarg(uid_t) uid;
   2329 		syscallarg(gid_t) gid;
   2330 	} */ *uap = v;
   2331 	int error;
   2332 	struct file *fp;
   2333 
   2334 	/* getvnode() will use the descriptor for us */
   2335 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2336 		return (error);
   2337 
   2338 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2339 	    SCARG(uap, gid), p, 1);
   2340 	FILE_UNUSE(fp, p);
   2341 	return (error);
   2342 }
   2343 
   2344 /*
   2345  * Set ownership given a path name; this version does not follow links.
   2346  */
   2347 /* ARGSUSED */
   2348 int
   2349 sys_lchown(p, v, retval)
   2350 	struct proc *p;
   2351 	void *v;
   2352 	register_t *retval;
   2353 {
   2354 	register struct sys_lchown_args /* {
   2355 		syscallarg(const char *) path;
   2356 		syscallarg(uid_t) uid;
   2357 		syscallarg(gid_t) gid;
   2358 	} */ *uap = v;
   2359 	int error;
   2360 	struct nameidata nd;
   2361 
   2362 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2363 	if ((error = namei(&nd)) != 0)
   2364 		return (error);
   2365 
   2366 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2367 
   2368 	vrele(nd.ni_vp);
   2369 	return (error);
   2370 }
   2371 
   2372 /*
   2373  * Set ownership given a path name; this version does not follow links.
   2374  * Provides POSIX/XPG semantics.
   2375  */
   2376 /* ARGSUSED */
   2377 int
   2378 sys___posix_lchown(p, v, retval)
   2379 	struct proc *p;
   2380 	void *v;
   2381 	register_t *retval;
   2382 {
   2383 	register struct sys_lchown_args /* {
   2384 		syscallarg(const char *) path;
   2385 		syscallarg(uid_t) uid;
   2386 		syscallarg(gid_t) gid;
   2387 	} */ *uap = v;
   2388 	int error;
   2389 	struct nameidata nd;
   2390 
   2391 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2392 	if ((error = namei(&nd)) != 0)
   2393 		return (error);
   2394 
   2395 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2396 
   2397 	vrele(nd.ni_vp);
   2398 	return (error);
   2399 }
   2400 
   2401 /*
   2402  * Common routine to set ownership given a vnode.
   2403  */
   2404 static int
   2405 change_owner(vp, uid, gid, p, posix_semantics)
   2406 	register struct vnode *vp;
   2407 	uid_t uid;
   2408 	gid_t gid;
   2409 	struct proc *p;
   2410 	int posix_semantics;
   2411 {
   2412 	struct vattr vattr;
   2413 	mode_t newmode;
   2414 	int error;
   2415 
   2416 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2417 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2418 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2419 		goto out;
   2420 
   2421 #define CHANGED(x) ((x) != -1)
   2422 	newmode = vattr.va_mode;
   2423 	if (posix_semantics) {
   2424 		/*
   2425 		 * POSIX/XPG semantics: if the caller is not the super-user,
   2426 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   2427 		 * the XPG consider the behaviour for calls by the super-user
   2428 		 * implementation-defined; we leave the set-user-id and set-
   2429 		 * group-id settings intact in that case.
   2430 		 */
   2431 		if (suser(p->p_ucred, NULL) != 0)
   2432 			newmode &= ~(S_ISUID | S_ISGID);
   2433 	} else {
   2434 		/*
   2435 		 * NetBSD semantics: when changing owner and/or group,
   2436 		 * clear the respective bit(s).
   2437 		 */
   2438 		if (CHANGED(uid))
   2439 			newmode &= ~S_ISUID;
   2440 		if (CHANGED(gid))
   2441 			newmode &= ~S_ISGID;
   2442 	}
   2443 	/* Update va_mode iff altered. */
   2444 	if (vattr.va_mode == newmode)
   2445 		newmode = VNOVAL;
   2446 
   2447 	VATTR_NULL(&vattr);
   2448 	vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
   2449 	vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
   2450 	vattr.va_mode = newmode;
   2451 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2452 #undef CHANGED
   2453 
   2454 out:
   2455 	VOP_UNLOCK(vp, 0);
   2456 	return (error);
   2457 }
   2458 
   2459 /*
   2460  * Set the access and modification times given a path name; this
   2461  * version follows links.
   2462  */
   2463 /* ARGSUSED */
   2464 int
   2465 sys_utimes(p, v, retval)
   2466 	struct proc *p;
   2467 	void *v;
   2468 	register_t *retval;
   2469 {
   2470 	register struct sys_utimes_args /* {
   2471 		syscallarg(const char *) path;
   2472 		syscallarg(const struct timeval *) tptr;
   2473 	} */ *uap = v;
   2474 	int error;
   2475 	struct nameidata nd;
   2476 
   2477 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2478 	if ((error = namei(&nd)) != 0)
   2479 		return (error);
   2480 
   2481 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2482 
   2483 	vrele(nd.ni_vp);
   2484 	return (error);
   2485 }
   2486 
   2487 /*
   2488  * Set the access and modification times given a file descriptor.
   2489  */
   2490 /* ARGSUSED */
   2491 int
   2492 sys_futimes(p, v, retval)
   2493 	struct proc *p;
   2494 	void *v;
   2495 	register_t *retval;
   2496 {
   2497 	register struct sys_futimes_args /* {
   2498 		syscallarg(int) fd;
   2499 		syscallarg(const struct timeval *) tptr;
   2500 	} */ *uap = v;
   2501 	int error;
   2502 	struct file *fp;
   2503 
   2504 	/* getvnode() will use the descriptor for us */
   2505 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2506 		return (error);
   2507 
   2508 	error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
   2509 	FILE_UNUSE(fp, p);
   2510 	return (error);
   2511 }
   2512 
   2513 /*
   2514  * Set the access and modification times given a path name; this
   2515  * version does not follow links.
   2516  */
   2517 /* ARGSUSED */
   2518 int
   2519 sys_lutimes(p, v, retval)
   2520 	struct proc *p;
   2521 	void *v;
   2522 	register_t *retval;
   2523 {
   2524 	register struct sys_lutimes_args /* {
   2525 		syscallarg(const char *) path;
   2526 		syscallarg(const struct timeval *) tptr;
   2527 	} */ *uap = v;
   2528 	int error;
   2529 	struct nameidata nd;
   2530 
   2531 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2532 	if ((error = namei(&nd)) != 0)
   2533 		return (error);
   2534 
   2535 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2536 
   2537 	vrele(nd.ni_vp);
   2538 	return (error);
   2539 }
   2540 
   2541 /*
   2542  * Common routine to set access and modification times given a vnode.
   2543  */
   2544 static int
   2545 change_utimes(vp, tptr, p)
   2546 	struct vnode *vp;
   2547 	const struct timeval *tptr;
   2548 	struct proc *p;
   2549 {
   2550 	struct timeval tv[2];
   2551 	struct vattr vattr;
   2552 	int error;
   2553 
   2554 	VATTR_NULL(&vattr);
   2555 	if (tptr == NULL) {
   2556 		microtime(&tv[0]);
   2557 		tv[1] = tv[0];
   2558 		vattr.va_vaflags |= VA_UTIMES_NULL;
   2559 	} else {
   2560 		error = copyin(tptr, tv, sizeof(tv));
   2561 		if (error)
   2562 			return (error);
   2563 	}
   2564 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2565 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2566 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   2567 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   2568 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   2569 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   2570 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2571 	VOP_UNLOCK(vp, 0);
   2572 	return (error);
   2573 }
   2574 
   2575 /*
   2576  * Truncate a file given its path name.
   2577  */
   2578 /* ARGSUSED */
   2579 int
   2580 sys_truncate(p, v, retval)
   2581 	struct proc *p;
   2582 	void *v;
   2583 	register_t *retval;
   2584 {
   2585 	register struct sys_truncate_args /* {
   2586 		syscallarg(const char *) path;
   2587 		syscallarg(int) pad;
   2588 		syscallarg(off_t) length;
   2589 	} */ *uap = v;
   2590 	register struct vnode *vp;
   2591 	struct vattr vattr;
   2592 	int error;
   2593 	struct nameidata nd;
   2594 
   2595 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2596 	if ((error = namei(&nd)) != 0)
   2597 		return (error);
   2598 	vp = nd.ni_vp;
   2599 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2600 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2601 	if (vp->v_type == VDIR)
   2602 		error = EISDIR;
   2603 	else if ((error = vn_writechk(vp)) == 0 &&
   2604 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   2605 		VATTR_NULL(&vattr);
   2606 		vattr.va_size = SCARG(uap, length);
   2607 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2608 	}
   2609 	vput(vp);
   2610 	return (error);
   2611 }
   2612 
   2613 /*
   2614  * Truncate a file given a file descriptor.
   2615  */
   2616 /* ARGSUSED */
   2617 int
   2618 sys_ftruncate(p, v, retval)
   2619 	struct proc *p;
   2620 	void *v;
   2621 	register_t *retval;
   2622 {
   2623 	register struct sys_ftruncate_args /* {
   2624 		syscallarg(int) fd;
   2625 		syscallarg(int) pad;
   2626 		syscallarg(off_t) length;
   2627 	} */ *uap = v;
   2628 	struct vattr vattr;
   2629 	struct vnode *vp;
   2630 	struct file *fp;
   2631 	int error;
   2632 
   2633 	/* getvnode() will use the descriptor for us */
   2634 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2635 		return (error);
   2636 	if ((fp->f_flag & FWRITE) == 0) {
   2637 		error = EINVAL;
   2638 		goto out;
   2639 	}
   2640 	vp = (struct vnode *)fp->f_data;
   2641 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2642 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2643 	if (vp->v_type == VDIR)
   2644 		error = EISDIR;
   2645 	else if ((error = vn_writechk(vp)) == 0) {
   2646 		VATTR_NULL(&vattr);
   2647 		vattr.va_size = SCARG(uap, length);
   2648 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   2649 	}
   2650 	VOP_UNLOCK(vp, 0);
   2651  out:
   2652 	FILE_UNUSE(fp, p);
   2653 	return (error);
   2654 }
   2655 
   2656 /*
   2657  * Sync an open file.
   2658  */
   2659 /* ARGSUSED */
   2660 int
   2661 sys_fsync(p, v, retval)
   2662 	struct proc *p;
   2663 	void *v;
   2664 	register_t *retval;
   2665 {
   2666 	struct sys_fsync_args /* {
   2667 		syscallarg(int) fd;
   2668 	} */ *uap = v;
   2669 	register struct vnode *vp;
   2670 	struct file *fp;
   2671 	int error;
   2672 
   2673 	/* getvnode() will use the descriptor for us */
   2674 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2675 		return (error);
   2676 	vp = (struct vnode *)fp->f_data;
   2677 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2678 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, p);
   2679 	if (error == 0 && bioops.io_fsync != NULL &&
   2680 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   2681 		(*bioops.io_fsync)(vp);
   2682 	VOP_UNLOCK(vp, 0);
   2683 	FILE_UNUSE(fp, p);
   2684 	return (error);
   2685 }
   2686 
   2687 /*
   2688  * Sync the data of an open file.
   2689  */
   2690 /* ARGSUSED */
   2691 int
   2692 sys_fdatasync(p, v, retval)
   2693 	struct proc *p;
   2694 	void *v;
   2695 	register_t *retval;
   2696 {
   2697 	struct sys_fdatasync_args /* {
   2698 		syscallarg(int) fd;
   2699 	} */ *uap = v;
   2700 	struct vnode *vp;
   2701 	struct file *fp;
   2702 	int error;
   2703 
   2704 	/* getvnode() will use the descriptor for us */
   2705 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2706 		return (error);
   2707 	vp = (struct vnode *)fp->f_data;
   2708 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2709 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, p);
   2710 	if (error == 0 && bioops.io_fsync != NULL)
   2711 		(*bioops.io_fsync)(vp);
   2712 	VOP_UNLOCK(vp, 0);
   2713 	FILE_UNUSE(fp, p);
   2714 	return (error);
   2715 }
   2716 
   2717 /*
   2718  * Rename files, (standard) BSD semantics frontend.
   2719  */
   2720 /* ARGSUSED */
   2721 int
   2722 sys_rename(p, v, retval)
   2723 	struct proc *p;
   2724 	void *v;
   2725 	register_t *retval;
   2726 {
   2727 	register struct sys_rename_args /* {
   2728 		syscallarg(const char *) from;
   2729 		syscallarg(const char *) to;
   2730 	} */ *uap = v;
   2731 
   2732 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
   2733 }
   2734 
   2735 /*
   2736  * Rename files, POSIX semantics frontend.
   2737  */
   2738 /* ARGSUSED */
   2739 int
   2740 sys___posix_rename(p, v, retval)
   2741 	struct proc *p;
   2742 	void *v;
   2743 	register_t *retval;
   2744 {
   2745 	register struct sys___posix_rename_args /* {
   2746 		syscallarg(const char *) from;
   2747 		syscallarg(const char *) to;
   2748 	} */ *uap = v;
   2749 
   2750 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
   2751 }
   2752 
   2753 /*
   2754  * Rename files.  Source and destination must either both be directories,
   2755  * or both not be directories.  If target is a directory, it must be empty.
   2756  * If `from' and `to' refer to the same object, the value of the `retain'
   2757  * argument is used to determine whether `from' will be
   2758  *
   2759  * (retain == 0)	deleted unless `from' and `to' refer to the same
   2760  *			object in the file system's name space (BSD).
   2761  * (retain == 1)	always retained (POSIX).
   2762  */
   2763 static int
   2764 rename_files(from, to, p, retain)
   2765 	const char *from, *to;
   2766 	struct proc *p;
   2767 	int retain;
   2768 {
   2769 	register struct vnode *tvp, *fvp, *tdvp;
   2770 	struct nameidata fromnd, tond;
   2771 	int error;
   2772 
   2773 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   2774 	    from, p);
   2775 	if ((error = namei(&fromnd)) != 0)
   2776 		return (error);
   2777 	fvp = fromnd.ni_vp;
   2778 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
   2779 	    UIO_USERSPACE, to, p);
   2780 	if ((error = namei(&tond)) != 0) {
   2781 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2782 		vrele(fromnd.ni_dvp);
   2783 		vrele(fvp);
   2784 		goto out1;
   2785 	}
   2786 	tdvp = tond.ni_dvp;
   2787 	tvp = tond.ni_vp;
   2788 
   2789 	if (tvp != NULL) {
   2790 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   2791 			error = ENOTDIR;
   2792 			goto out;
   2793 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   2794 			error = EISDIR;
   2795 			goto out;
   2796 		}
   2797 	}
   2798 
   2799 	if (fvp == tdvp)
   2800 		error = EINVAL;
   2801 
   2802 	/*
   2803 	 * Source and destination refer to the same object.
   2804 	 */
   2805 	if (fvp == tvp) {
   2806 		if (retain)
   2807 			error = -1;
   2808 		else if (fromnd.ni_dvp == tdvp &&
   2809 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   2810 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
   2811 		          tond.ni_cnd.cn_nameptr,
   2812 		          fromnd.ni_cnd.cn_namelen))
   2813 		error = -1;
   2814 	}
   2815 
   2816 out:
   2817 	if (!error) {
   2818 		VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
   2819 		if (fromnd.ni_dvp != tdvp)
   2820 			VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2821 		if (tvp) {
   2822 			(void)uvm_vnp_uncache(tvp);
   2823 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   2824 		}
   2825 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   2826 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   2827 	} else {
   2828 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   2829 		if (tdvp == tvp)
   2830 			vrele(tdvp);
   2831 		else
   2832 			vput(tdvp);
   2833 		if (tvp)
   2834 			vput(tvp);
   2835 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2836 		vrele(fromnd.ni_dvp);
   2837 		vrele(fvp);
   2838 	}
   2839 	vrele(tond.ni_startdir);
   2840 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   2841 out1:
   2842 	if (fromnd.ni_startdir)
   2843 		vrele(fromnd.ni_startdir);
   2844 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   2845 	return (error == -1 ? 0 : error);
   2846 }
   2847 
   2848 /*
   2849  * Make a directory file.
   2850  */
   2851 /* ARGSUSED */
   2852 int
   2853 sys_mkdir(p, v, retval)
   2854 	struct proc *p;
   2855 	void *v;
   2856 	register_t *retval;
   2857 {
   2858 	register struct sys_mkdir_args /* {
   2859 		syscallarg(const char *) path;
   2860 		syscallarg(int) mode;
   2861 	} */ *uap = v;
   2862 	register struct vnode *vp;
   2863 	struct vattr vattr;
   2864 	int error;
   2865 	struct nameidata nd;
   2866 
   2867 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   2868 	if ((error = namei(&nd)) != 0)
   2869 		return (error);
   2870 	vp = nd.ni_vp;
   2871 	if (vp != NULL) {
   2872 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2873 		if (nd.ni_dvp == vp)
   2874 			vrele(nd.ni_dvp);
   2875 		else
   2876 			vput(nd.ni_dvp);
   2877 		vrele(vp);
   2878 		return (EEXIST);
   2879 	}
   2880 	VATTR_NULL(&vattr);
   2881 	vattr.va_type = VDIR;
   2882 	vattr.va_mode =
   2883 	    (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   2884 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2885 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   2886 	if (!error)
   2887 		vput(nd.ni_vp);
   2888 	return (error);
   2889 }
   2890 
   2891 /*
   2892  * Remove a directory file.
   2893  */
   2894 /* ARGSUSED */
   2895 int
   2896 sys_rmdir(p, v, retval)
   2897 	struct proc *p;
   2898 	void *v;
   2899 	register_t *retval;
   2900 {
   2901 	struct sys_rmdir_args /* {
   2902 		syscallarg(const char *) path;
   2903 	} */ *uap = v;
   2904 	register struct vnode *vp;
   2905 	int error;
   2906 	struct nameidata nd;
   2907 
   2908 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   2909 	    SCARG(uap, path), p);
   2910 	if ((error = namei(&nd)) != 0)
   2911 		return (error);
   2912 	vp = nd.ni_vp;
   2913 	if (vp->v_type != VDIR) {
   2914 		error = ENOTDIR;
   2915 		goto out;
   2916 	}
   2917 	/*
   2918 	 * No rmdir "." please.
   2919 	 */
   2920 	if (nd.ni_dvp == vp) {
   2921 		error = EINVAL;
   2922 		goto out;
   2923 	}
   2924 	/*
   2925 	 * The root of a mounted filesystem cannot be deleted.
   2926 	 */
   2927 	if (vp->v_flag & VROOT)
   2928 		error = EBUSY;
   2929 out:
   2930 	if (!error) {
   2931 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2932 		VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2933 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   2934 	} else {
   2935 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2936 		if (nd.ni_dvp == vp)
   2937 			vrele(nd.ni_dvp);
   2938 		else
   2939 			vput(nd.ni_dvp);
   2940 		vput(vp);
   2941 	}
   2942 	return (error);
   2943 }
   2944 
   2945 /*
   2946  * Read a block of directory entries in a file system independent format.
   2947  */
   2948 int
   2949 sys_getdents(p, v, retval)
   2950 	struct proc *p;
   2951 	void *v;
   2952 	register_t *retval;
   2953 {
   2954 	register struct sys_getdents_args /* {
   2955 		syscallarg(int) fd;
   2956 		syscallarg(char *) buf;
   2957 		syscallarg(size_t) count;
   2958 	} */ *uap = v;
   2959 	struct file *fp;
   2960 	int error, done;
   2961 
   2962 	/* getvnode() will use the descriptor for us */
   2963 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2964 		return (error);
   2965 	if ((fp->f_flag & FREAD) == 0) {
   2966 		error = EBADF;
   2967 		goto out;
   2968 	}
   2969 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   2970 			SCARG(uap, count), &done, p, 0, 0);
   2971 	*retval = done;
   2972  out:
   2973 	FILE_UNUSE(fp, p);
   2974 	return (error);
   2975 }
   2976 
   2977 /*
   2978  * Set the mode mask for creation of filesystem nodes.
   2979  */
   2980 int
   2981 sys_umask(p, v, retval)
   2982 	struct proc *p;
   2983 	void *v;
   2984 	register_t *retval;
   2985 {
   2986 	struct sys_umask_args /* {
   2987 		syscallarg(mode_t) newmask;
   2988 	} */ *uap = v;
   2989 	struct cwdinfo *cwdi;
   2990 
   2991 	cwdi = p->p_cwdi;
   2992 	*retval = cwdi->cwdi_cmask;
   2993 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
   2994 	return (0);
   2995 }
   2996 
   2997 /*
   2998  * Void all references to file by ripping underlying filesystem
   2999  * away from vnode.
   3000  */
   3001 /* ARGSUSED */
   3002 int
   3003 sys_revoke(p, v, retval)
   3004 	struct proc *p;
   3005 	void *v;
   3006 	register_t *retval;
   3007 {
   3008 	register struct sys_revoke_args /* {
   3009 		syscallarg(const char *) path;
   3010 	} */ *uap = v;
   3011 	register struct vnode *vp;
   3012 	struct vattr vattr;
   3013 	int error;
   3014 	struct nameidata nd;
   3015 
   3016 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   3017 	if ((error = namei(&nd)) != 0)
   3018 		return (error);
   3019 	vp = nd.ni_vp;
   3020 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   3021 		goto out;
   3022 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   3023 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
   3024 		goto out;
   3025 	if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
   3026 		VOP_REVOKE(vp, REVOKEALL);
   3027 out:
   3028 	vrele(vp);
   3029 	return (error);
   3030 }
   3031 
   3032 /*
   3033  * Convert a user file descriptor to a kernel file entry.
   3034  */
   3035 int
   3036 getvnode(fdp, fd, fpp)
   3037 	struct filedesc *fdp;
   3038 	int fd;
   3039 	struct file **fpp;
   3040 {
   3041 	struct vnode *vp;
   3042 	struct file *fp;
   3043 
   3044 	if ((u_int)fd >= fdp->fd_nfiles ||
   3045 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
   3046 	    (fp->f_iflags & FIF_WANTCLOSE) != 0)
   3047 		return (EBADF);
   3048 
   3049 	FILE_USE(fp);
   3050 
   3051 	if (fp->f_type != DTYPE_VNODE) {
   3052 		FILE_UNUSE(fp, NULL);
   3053 		return (EINVAL);
   3054 	}
   3055 
   3056 	vp = (struct vnode *)fp->f_data;
   3057 	if (vp->v_type == VBAD) {
   3058 		FILE_UNUSE(fp, NULL);
   3059 		return (EBADF);
   3060 	}
   3061 
   3062 	*fpp = fp;
   3063 	return (0);
   3064 }
   3065