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