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