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