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