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