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