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