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