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