Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.118
      1 /*	$NetBSD: vfs_syscalls.c,v 1.118 1998/06/22 22:01:04 sommerfe 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_uvm.h"
     44 #include "opt_fifo.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 #if defined(UVM)
     66 #include <uvm/uvm_extern.h>
     67 #endif
     68 
     69 static int change_dir __P((struct nameidata *, struct proc *));
     70 static int change_mode __P((struct vnode *, int, struct proc *p));
     71 static int change_owner __P((struct vnode *, uid_t, gid_t, struct proc *,
     72     int));
     73 static int change_utimes __P((struct vnode *vp, const struct timeval *,
     74 	       struct proc *p));
     75 static int rename_files __P((const char *, const char *, struct proc *, int));
     76 
     77 void checkdirs __P((struct vnode *));
     78 int dounmount __P((struct mount *, int, struct proc *));
     79 
     80 /*
     81  * Virtual File System System Calls
     82  */
     83 
     84 /*
     85  * Mount a file system.
     86  */
     87 
     88 #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44)
     89 /*
     90  * This table is used to maintain compatibility with 4.3BSD
     91  * and NetBSD 0.9 mount syscalls.  Note, the order is important!
     92  */
     93 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 };
    113 const int nmountcompatnames = sizeof(mountcompatnames) /
    114     sizeof(mountcompatnames[0]);
    115 #endif /* COMPAT_09 || COMPAT_43 */
    116 
    117 /* ARGSUSED */
    118 int
    119 sys_mount(p, v, retval)
    120 	struct proc *p;
    121 	void *v;
    122 	register_t *retval;
    123 {
    124 	register struct sys_mount_args /* {
    125 		syscallarg(const char *) type;
    126 		syscallarg(const char *) path;
    127 		syscallarg(int) flags;
    128 		syscallarg(void *) data;
    129 	} */ *uap = v;
    130 	struct vnode *vp;
    131 	struct mount *mp;
    132 	int error, flag = 0;
    133 	char fstypename[MFSNAMELEN];
    134 	struct vattr va;
    135 	struct nameidata nd;
    136 	struct vfsops *vfs;
    137 
    138 	/*
    139 	 * Get vnode to be covered
    140 	 */
    141 	NDINIT(&nd, LOOKUP, FOLLOW , UIO_USERSPACE,
    142 	    SCARG(uap, path), p);
    143 	if ((error = namei(&nd)) != 0)
    144 		return (error);
    145 	vp = nd.ni_vp;
    146 	/*
    147 	 * A lookup in VFS_MOUNT might result in an attempt to
    148 	 * lock this vnode again, so make the lock resursive.
    149 	 */
    150 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
    151 	if (SCARG(uap, flags) & MNT_UPDATE) {
    152 		if ((vp->v_flag & VROOT) == 0) {
    153 			vput(vp);
    154 			return (EINVAL);
    155 		}
    156 		mp = vp->v_mount;
    157 		flag = mp->mnt_flag;
    158 		vfs = mp->mnt_op;
    159 		/*
    160 		 * We only allow the filesystem to be reloaded if it
    161 		 * is currently mounted read-only.
    162 		 */
    163 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
    164 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
    165 			vput(vp);
    166 			return (EOPNOTSUPP);	/* Needs translation */
    167 		}
    168 		mp->mnt_flag |=
    169 		    SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
    170 		/*
    171 		 * Only root, or the user that did the original mount is
    172 		 * permitted to update it.
    173 		 */
    174 		if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
    175 		    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    176 			vput(vp);
    177 			return (error);
    178 		}
    179 		/*
    180 		 * Do not allow NFS export by non-root users. Silently
    181 		 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
    182 		 */
    183 		if (p->p_ucred->cr_uid != 0) {
    184 			if (SCARG(uap, flags) & MNT_EXPORTED) {
    185 				vput(vp);
    186 				return (EPERM);
    187 			}
    188 			SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    189 		}
    190 		if (vfs_busy(mp, LK_NOWAIT, 0)) {
    191 			vput(vp);
    192 			return (EPERM);
    193 		}
    194 		VOP_UNLOCK(vp, 0);
    195 		goto update;
    196 	}
    197 	/*
    198 	 * If the user is not root, ensure that they own the directory
    199 	 * onto which we are attempting to mount.
    200 	 */
    201 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
    202 	    (va.va_uid != p->p_ucred->cr_uid &&
    203 		(error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
    204 		vput(vp);
    205 		return (error);
    206 	}
    207 	/*
    208 	 * Do not allow NFS export by non-root users. Silently
    209 	 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
    210 	 */
    211 	if (p->p_ucred->cr_uid != 0) {
    212 		if (SCARG(uap, flags) & MNT_EXPORTED) {
    213 			vput(vp);
    214 			return (EPERM);
    215 		}
    216 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    217 	}
    218 	if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
    219 		return (error);
    220 	if (vp->v_type != VDIR) {
    221 		vput(vp);
    222 		return (ENOTDIR);
    223 	}
    224 	error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
    225 	if (error) {
    226 #if defined(COMPAT_09) || defined(COMPAT_43)
    227 		/*
    228 		 * Historically filesystem types were identified by number.
    229 		 * If we get an integer for the filesystem type instead of a
    230 		 * string, we check to see if it matches one of the historic
    231 		 * filesystem types.
    232 		 */
    233 		u_long fsindex = (u_long)SCARG(uap, type);
    234 		if (fsindex >= nmountcompatnames ||
    235 		    mountcompatnames[fsindex] == NULL) {
    236 			vput(vp);
    237 			return (ENODEV);
    238 		}
    239 		strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
    240 #else
    241 		vput(vp);
    242 		return (error);
    243 #endif
    244 	}
    245 #ifdef	COMPAT_10
    246 	/* Accept `ufs' as an alias for `ffs'. */
    247 	if (!strncmp(fstypename, "ufs", MFSNAMELEN))
    248 		strncpy(fstypename, "ffs", MFSNAMELEN);
    249 #endif
    250 	if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
    251 		vput(vp);
    252 		return (ENODEV);
    253 	}
    254 	if (vp->v_mountedhere != NULL) {
    255 		vput(vp);
    256 		return (EBUSY);
    257 	}
    258 
    259 	/*
    260 	 * Allocate and initialize the file system.
    261 	 */
    262 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    263 		M_MOUNT, M_WAITOK);
    264 	bzero((char *)mp, (u_long)sizeof(struct mount));
    265 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
    266 	(void)vfs_busy(mp, LK_NOWAIT, 0);
    267 	mp->mnt_op = vfs;
    268 	vfs->vfs_refcount++;
    269 	vp->v_mountedhere = mp;
    270 	mp->mnt_vnodecovered = vp;
    271 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
    272 update:
    273 	/*
    274 	 * Set the mount level flags.
    275 	 */
    276 	if (SCARG(uap, flags) & MNT_RDONLY)
    277 		mp->mnt_flag |= MNT_RDONLY;
    278 	else if (mp->mnt_flag & MNT_RDONLY)
    279 		mp->mnt_flag |= MNT_WANTRDWR;
    280 	mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    281 	    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    282 	    MNT_NOATIME | MNT_SYMPERM);
    283 	mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
    284 	    MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
    285 	    MNT_NOCOREDUMP | MNT_NOATIME | MNT_SYMPERM);
    286 	/*
    287 	 * Mount the filesystem.
    288 	 */
    289 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
    290 	if (mp->mnt_flag & MNT_UPDATE) {
    291 		vrele(vp);
    292 		if (mp->mnt_flag & MNT_WANTRDWR)
    293 			mp->mnt_flag &= ~MNT_RDONLY;
    294 		mp->mnt_flag &=~
    295 		    (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
    296 		if (error)
    297 			mp->mnt_flag = flag;
    298 		vfs_unbusy(mp);
    299 		return (error);
    300 	}
    301 	/*
    302 	 * Put the new filesystem on the mount list after root.
    303 	 */
    304 	cache_purge(vp);
    305 	if (!error) {
    306 		simple_lock(&mountlist_slock);
    307 		CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    308 		simple_unlock(&mountlist_slock);
    309 		checkdirs(vp);
    310 		VOP_UNLOCK(vp, 0);
    311 		vfs_unbusy(mp);
    312 		(void) VFS_STATFS(mp, &mp->mnt_stat, p);
    313 		if ((error = VFS_START(mp, 0, p)))
    314 			vrele(vp);
    315 	} else {
    316 		mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
    317 		vfs->vfs_refcount--;
    318 		vfs_unbusy(mp);
    319 		free((caddr_t)mp, M_MOUNT);
    320 		vput(vp);
    321 	}
    322 	return (error);
    323 }
    324 
    325 /*
    326  * Scan all active processes to see if any of them have a current
    327  * or root directory onto which the new filesystem has just been
    328  * mounted. If so, replace them with the new mount point.
    329  */
    330 void
    331 checkdirs(olddp)
    332 	struct vnode *olddp;
    333 {
    334 	struct filedesc *fdp;
    335 	struct vnode *newdp;
    336 	struct proc *p;
    337 
    338 	if (olddp->v_usecount == 1)
    339 		return;
    340 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    341 		panic("mount: lost mount");
    342 	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
    343 		fdp = p->p_fd;
    344 		if (fdp->fd_cdir == olddp) {
    345 			vrele(fdp->fd_cdir);
    346 			VREF(newdp);
    347 			fdp->fd_cdir = newdp;
    348 		}
    349 		if (fdp->fd_rdir == olddp) {
    350 			vrele(fdp->fd_rdir);
    351 			VREF(newdp);
    352 			fdp->fd_rdir = newdp;
    353 		}
    354 	}
    355 	if (rootvnode == olddp) {
    356 		vrele(rootvnode);
    357 		VREF(newdp);
    358 		rootvnode = newdp;
    359 	}
    360 	vput(newdp);
    361 }
    362 
    363 /*
    364  * Unmount a file system.
    365  *
    366  * Note: unmount takes a path to the vnode mounted on as argument,
    367  * not special file (as before).
    368  */
    369 /* ARGSUSED */
    370 int
    371 sys_unmount(p, v, retval)
    372 	struct proc *p;
    373 	void *v;
    374 	register_t *retval;
    375 {
    376 	register struct sys_unmount_args /* {
    377 		syscallarg(const char *) path;
    378 		syscallarg(int) flags;
    379 	} */ *uap = v;
    380 	register struct vnode *vp;
    381 	struct mount *mp;
    382 	int error;
    383 	struct nameidata nd;
    384 
    385 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    386 	    SCARG(uap, path), p);
    387 	if ((error = namei(&nd)) != 0)
    388 		return (error);
    389 	vp = nd.ni_vp;
    390 	mp = vp->v_mount;
    391 
    392 	/*
    393 	 * Only root, or the user that did the original mount is
    394 	 * permitted to unmount this filesystem.
    395 	 */
    396 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
    397 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    398 		vput(vp);
    399 		return (error);
    400 	}
    401 
    402 	/*
    403 	 * Don't allow unmounting the root file system.
    404 	 */
    405 	if (mp->mnt_flag & MNT_ROOTFS) {
    406 		vput(vp);
    407 		return (EINVAL);
    408 	}
    409 
    410 	/*
    411 	 * Must be the root of the filesystem
    412 	 */
    413 	if ((vp->v_flag & VROOT) == 0) {
    414 		vput(vp);
    415 		return (EINVAL);
    416 	}
    417 	vput(vp);
    418 
    419 	if (vfs_busy(mp, 0, 0))
    420 		return (EBUSY);
    421 
    422 	return (dounmount(mp, SCARG(uap, flags), p));
    423 }
    424 
    425 /*
    426  * Do the actual file system unmount. File system is assumed to have been
    427  * marked busy by the caller.
    428  */
    429 int
    430 dounmount(mp, flags, p)
    431 	register struct mount *mp;
    432 	int flags;
    433 	struct proc *p;
    434 {
    435 	struct vnode *coveredvp;
    436 	int error;
    437 
    438 	simple_lock(&mountlist_slock);
    439 	mp->mnt_flag |= MNT_UNMOUNT;
    440 	vfs_unbusy(mp);
    441 	lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
    442 	if (mp->mnt_flag & MNT_EXPUBLIC)
    443 		vfs_setpublicfs(NULL, NULL, NULL);
    444 	mp->mnt_flag &=~ MNT_ASYNC;
    445 #if !defined(UVM)
    446 	vnode_pager_umount(mp);	/* release cached vnodes */
    447 #endif
    448 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    449 	if (((mp->mnt_flag & MNT_RDONLY) ||
    450 	    (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
    451 	    (flags & MNT_FORCE))
    452 		error = VFS_UNMOUNT(mp, flags, p);
    453 	simple_lock(&mountlist_slock);
    454 	if (error) {
    455 		mp->mnt_flag &= ~MNT_UNMOUNT;
    456 		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
    457 		    &mountlist_slock);
    458 		if (mp->mnt_flag & MNT_MWAIT)
    459 			wakeup((caddr_t)mp);
    460 		 return (error);
    461 	}
    462 	CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
    463 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    464 		coveredvp->v_mountedhere = NULL;
    465 		vrele(coveredvp);
    466 	}
    467 	mp->mnt_op->vfs_refcount--;
    468 	if (mp->mnt_vnodelist.lh_first != NULL)
    469 		panic("unmount: dangling vnode");
    470 	lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
    471 	if (mp->mnt_flag & MNT_MWAIT)
    472 		wakeup((caddr_t)mp);
    473 	free((caddr_t)mp, M_MOUNT);
    474 	return (0);
    475 }
    476 
    477 /*
    478  * Sync each mounted filesystem.
    479  */
    480 #ifdef DEBUG
    481 int syncprt = 0;
    482 struct ctldebug debug0 = { "syncprt", &syncprt };
    483 #endif
    484 
    485 /* ARGSUSED */
    486 int
    487 sys_sync(p, v, retval)
    488 	struct proc *p;
    489 	void *v;
    490 	register_t *retval;
    491 {
    492 	register struct mount *mp, *nmp;
    493 	int asyncflag;
    494 
    495 	simple_lock(&mountlist_slock);
    496 	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
    497 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    498 			nmp = mp->mnt_list.cqe_prev;
    499 			continue;
    500 		}
    501 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    502 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    503 			mp->mnt_flag &= ~MNT_ASYNC;
    504 #if defined(UVM)
    505 			uvm_vnp_sync(mp);
    506 #else
    507 			vnode_pager_sync(mp);
    508 #endif
    509 			VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
    510 			if (asyncflag)
    511 				 mp->mnt_flag |= MNT_ASYNC;
    512 		}
    513 		simple_lock(&mountlist_slock);
    514 		nmp = mp->mnt_list.cqe_prev;
    515 		vfs_unbusy(mp);
    516 
    517 	}
    518 	simple_unlock(&mountlist_slock);
    519 #ifdef DEBUG
    520 	if (syncprt)
    521 		vfs_bufstats();
    522 #endif /* DEBUG */
    523 	return (0);
    524 }
    525 
    526 /*
    527  * Change filesystem quotas.
    528  */
    529 /* ARGSUSED */
    530 int
    531 sys_quotactl(p, v, retval)
    532 	struct proc *p;
    533 	void *v;
    534 	register_t *retval;
    535 {
    536 	register struct sys_quotactl_args /* {
    537 		syscallarg(const char *) path;
    538 		syscallarg(int) cmd;
    539 		syscallarg(int) uid;
    540 		syscallarg(caddr_t) arg;
    541 	} */ *uap = v;
    542 	register struct mount *mp;
    543 	int error;
    544 	struct nameidata nd;
    545 
    546 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    547 	if ((error = namei(&nd)) != 0)
    548 		return (error);
    549 	mp = nd.ni_vp->v_mount;
    550 	vrele(nd.ni_vp);
    551 	return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
    552 	    SCARG(uap, arg), p));
    553 }
    554 
    555 /*
    556  * Get filesystem statistics.
    557  */
    558 /* ARGSUSED */
    559 int
    560 sys_statfs(p, v, retval)
    561 	struct proc *p;
    562 	void *v;
    563 	register_t *retval;
    564 {
    565 	register struct sys_statfs_args /* {
    566 		syscallarg(const char *) path;
    567 		syscallarg(struct statfs *) buf;
    568 	} */ *uap = v;
    569 	register struct mount *mp;
    570 	register struct statfs *sp;
    571 	int error;
    572 	struct nameidata nd;
    573 
    574 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    575 	if ((error = namei(&nd)) != 0)
    576 		return (error);
    577 	mp = nd.ni_vp->v_mount;
    578 	sp = &mp->mnt_stat;
    579 	vrele(nd.ni_vp);
    580 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    581 		return (error);
    582 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    583 	return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
    584 }
    585 
    586 /*
    587  * Get filesystem statistics.
    588  */
    589 /* ARGSUSED */
    590 int
    591 sys_fstatfs(p, v, retval)
    592 	struct proc *p;
    593 	void *v;
    594 	register_t *retval;
    595 {
    596 	register struct sys_fstatfs_args /* {
    597 		syscallarg(int) fd;
    598 		syscallarg(struct statfs *) buf;
    599 	} */ *uap = v;
    600 	struct file *fp;
    601 	struct mount *mp;
    602 	register struct statfs *sp;
    603 	int error;
    604 
    605 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    606 		return (error);
    607 	mp = ((struct vnode *)fp->f_data)->v_mount;
    608 	sp = &mp->mnt_stat;
    609 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    610 		return (error);
    611 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    612 	return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
    613 }
    614 
    615 /*
    616  * Get statistics on all filesystems.
    617  */
    618 int
    619 sys_getfsstat(p, v, retval)
    620 	struct proc *p;
    621 	void *v;
    622 	register_t *retval;
    623 {
    624 	register struct sys_getfsstat_args /* {
    625 		syscallarg(struct statfs *) buf;
    626 		syscallarg(long) bufsize;
    627 		syscallarg(int) flags;
    628 	} */ *uap = v;
    629 	register struct mount *mp, *nmp;
    630 	register struct statfs *sp;
    631 	caddr_t sfsp;
    632 	long count, maxcount, error;
    633 
    634 	maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
    635 	sfsp = (caddr_t)SCARG(uap, buf);
    636 	simple_lock(&mountlist_slock);
    637 	count = 0;
    638 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    639 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    640 			nmp = mp->mnt_list.cqe_next;
    641 			continue;
    642 		}
    643 		if (sfsp && count < maxcount) {
    644 			sp = &mp->mnt_stat;
    645 			/*
    646 			 * If MNT_NOWAIT is specified, do not refresh the
    647 			 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
    648 			 */
    649 			if (((SCARG(uap, flags) & MNT_NOWAIT) == 0 ||
    650 			    (SCARG(uap, flags) & MNT_WAIT)) &&
    651 			    (error = VFS_STATFS(mp, sp, p)) != 0) {
    652 				simple_lock(&mountlist_slock);
    653 				nmp = mp->mnt_list.cqe_next;
    654 				vfs_unbusy(mp);
    655 				continue;
    656 			}
    657 			sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    658 			error = copyout(sp, sfsp, sizeof(*sp));
    659 			if (error)
    660 				return (error);
    661 			sfsp += sizeof(*sp);
    662 		}
    663 		count++;
    664 		simple_lock(&mountlist_slock);
    665 		nmp = mp->mnt_list.cqe_next;
    666 		vfs_unbusy(mp);
    667 	}
    668 	simple_unlock(&mountlist_slock);
    669 	if (sfsp && count > maxcount)
    670 		*retval = maxcount;
    671 	else
    672 		*retval = count;
    673 	return (0);
    674 }
    675 
    676 /*
    677  * Change current working directory to a given file descriptor.
    678  */
    679 /* ARGSUSED */
    680 int
    681 sys_fchdir(p, v, retval)
    682 	struct proc *p;
    683 	void *v;
    684 	register_t *retval;
    685 {
    686 	struct sys_fchdir_args /* {
    687 		syscallarg(int) fd;
    688 	} */ *uap = v;
    689 	register struct filedesc *fdp = p->p_fd;
    690 	struct vnode *vp, *tdp;
    691 	struct mount *mp;
    692 	struct file *fp;
    693 	int error;
    694 
    695 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    696 		return (error);
    697 	vp = (struct vnode *)fp->f_data;
    698 	VREF(vp);
    699 	vn_lock(vp,  LK_EXCLUSIVE | LK_RETRY);
    700 	if (vp->v_type != VDIR)
    701 		error = ENOTDIR;
    702 	else
    703 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    704 	while (!error && (mp = vp->v_mountedhere) != NULL) {
    705 		if (vfs_busy(mp, 0, 0))
    706 			continue;
    707 		error = VFS_ROOT(mp, &tdp);
    708 		vfs_unbusy(mp);
    709 		if (error)
    710 			break;
    711 		vput(vp);
    712 		vp = tdp;
    713 	}
    714 	if (error) {
    715 		vput(vp);
    716 		return (error);
    717 	}
    718 	VOP_UNLOCK(vp, 0);
    719 	vrele(fdp->fd_cdir);
    720 	fdp->fd_cdir = vp;
    721 	return (0);
    722 }
    723 
    724 /*
    725  * Change current working directory (``.'').
    726  */
    727 /* ARGSUSED */
    728 int
    729 sys_chdir(p, v, retval)
    730 	struct proc *p;
    731 	void *v;
    732 	register_t *retval;
    733 {
    734 	struct sys_chdir_args /* {
    735 		syscallarg(const char *) path;
    736 	} */ *uap = v;
    737 	register struct filedesc *fdp = p->p_fd;
    738 	int error;
    739 	struct nameidata nd;
    740 
    741 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    742 	    SCARG(uap, path), p);
    743 	if ((error = change_dir(&nd, p)) != 0)
    744 		return (error);
    745 	vrele(fdp->fd_cdir);
    746 	fdp->fd_cdir = nd.ni_vp;
    747 	return (0);
    748 }
    749 
    750 /*
    751  * Change notion of root (``/'') directory.
    752  */
    753 /* ARGSUSED */
    754 int
    755 sys_chroot(p, v, retval)
    756 	struct proc *p;
    757 	void *v;
    758 	register_t *retval;
    759 {
    760 	struct sys_chroot_args /* {
    761 		syscallarg(const char *) path;
    762 	} */ *uap = v;
    763 	register struct filedesc *fdp = p->p_fd;
    764 	int error;
    765 	struct nameidata nd;
    766 
    767 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    768 		return (error);
    769 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    770 	    SCARG(uap, path), p);
    771 	if ((error = change_dir(&nd, p)) != 0)
    772 		return (error);
    773 	if (fdp->fd_rdir != NULL)
    774 		vrele(fdp->fd_rdir);
    775 	fdp->fd_rdir = nd.ni_vp;
    776 	return (0);
    777 }
    778 
    779 /*
    780  * Common routine for chroot and chdir.
    781  */
    782 static int
    783 change_dir(ndp, p)
    784 	register struct nameidata *ndp;
    785 	struct proc *p;
    786 {
    787 	struct vnode *vp;
    788 	int error;
    789 
    790 	if ((error = namei(ndp)) != 0)
    791 		return (error);
    792 	vp = ndp->ni_vp;
    793 	if (vp->v_type != VDIR)
    794 		error = ENOTDIR;
    795 	else
    796 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    797 
    798 	if (error)
    799 		vput(vp);
    800 	else
    801 		VOP_UNLOCK(vp, 0);
    802 	return (error);
    803 }
    804 
    805 /*
    806  * Check permissions, allocate an open file structure,
    807  * and call the device open routine if any.
    808  */
    809 int
    810 sys_open(p, v, retval)
    811 	struct proc *p;
    812 	void *v;
    813 	register_t *retval;
    814 {
    815 	register struct sys_open_args /* {
    816 		syscallarg(const char *) path;
    817 		syscallarg(int) flags;
    818 		syscallarg(int) mode;
    819 	} */ *uap = v;
    820 	register struct filedesc *fdp = p->p_fd;
    821 	register struct file *fp;
    822 	register struct vnode *vp;
    823 	int flags, cmode;
    824 	struct file *nfp;
    825 	int type, indx, error;
    826 	struct flock lf;
    827 	struct nameidata nd;
    828 	extern struct fileops vnops;
    829 
    830 	flags = FFLAGS(SCARG(uap, flags));
    831 	if ((flags & (FREAD | FWRITE)) == 0)
    832 		return (EINVAL);
    833 	if ((error = falloc(p, &nfp, &indx)) != 0)
    834 		return (error);
    835 	fp = nfp;
    836 	cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
    837 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    838 	p->p_dupfd = -indx - 1;			/* XXX check for fdopen */
    839 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
    840 		ffree(fp);
    841 		if ((error == ENODEV || error == ENXIO) &&
    842 		    p->p_dupfd >= 0 &&			/* XXX from fdopen */
    843 		    (error =
    844 			dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
    845 			*retval = indx;
    846 			return (0);
    847 		}
    848 		if (error == ERESTART)
    849 			error = EINTR;
    850 		fdp->fd_ofiles[indx] = NULL;
    851 		return (error);
    852 	}
    853 	p->p_dupfd = 0;
    854 	vp = nd.ni_vp;
    855 	fp->f_flag = flags & FMASK;
    856 	fp->f_type = DTYPE_VNODE;
    857 	fp->f_ops = &vnops;
    858 	fp->f_data = (caddr_t)vp;
    859 	if (flags & (O_EXLOCK | O_SHLOCK)) {
    860 		lf.l_whence = SEEK_SET;
    861 		lf.l_start = 0;
    862 		lf.l_len = 0;
    863 		if (flags & O_EXLOCK)
    864 			lf.l_type = F_WRLCK;
    865 		else
    866 			lf.l_type = F_RDLCK;
    867 		type = F_FLOCK;
    868 		if ((flags & FNONBLOCK) == 0)
    869 			type |= F_WAIT;
    870 		VOP_UNLOCK(vp, 0);
    871 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
    872 		if (error) {
    873 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
    874 			ffree(fp);
    875 			fdp->fd_ofiles[indx] = NULL;
    876 			return (error);
    877 		}
    878 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    879 		fp->f_flag |= FHASLOCK;
    880 	}
    881 	VOP_UNLOCK(vp, 0);
    882 	*retval = indx;
    883 	return (0);
    884 }
    885 
    886 /*
    887  * Create a special file.
    888  */
    889 /* ARGSUSED */
    890 int
    891 sys_mknod(p, v, retval)
    892 	struct proc *p;
    893 	void *v;
    894 	register_t *retval;
    895 {
    896 	register struct sys_mknod_args /* {
    897 		syscallarg(const char *) path;
    898 		syscallarg(int) mode;
    899 		syscallarg(int) dev;
    900 	} */ *uap = v;
    901 	register struct vnode *vp;
    902 	struct vattr vattr;
    903 	int error;
    904 	int whiteout = 0;
    905 	struct nameidata nd;
    906 
    907 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    908 		return (error);
    909 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
    910 	if ((error = namei(&nd)) != 0)
    911 		return (error);
    912 	vp = nd.ni_vp;
    913 	if (vp != NULL)
    914 		error = EEXIST;
    915 	else {
    916 		VATTR_NULL(&vattr);
    917 		vattr.va_mode =
    918 		    (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
    919 		vattr.va_rdev = SCARG(uap, dev);
    920 		whiteout = 0;
    921 
    922 		switch (SCARG(uap, mode) & S_IFMT) {
    923 		case S_IFMT:	/* used by badsect to flag bad sectors */
    924 			vattr.va_type = VBAD;
    925 			break;
    926 		case S_IFCHR:
    927 			vattr.va_type = VCHR;
    928 			break;
    929 		case S_IFBLK:
    930 			vattr.va_type = VBLK;
    931 			break;
    932 		case S_IFWHT:
    933 			whiteout = 1;
    934 			break;
    935 		default:
    936 			error = EINVAL;
    937 			break;
    938 		}
    939 	}
    940 	if (!error) {
    941 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
    942 		if (whiteout) {
    943 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
    944 			if (error)
    945 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    946 			vput(nd.ni_dvp);
    947 		} else {
    948 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
    949 						&nd.ni_cnd, &vattr);
    950 		}
    951 	} else {
    952 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    953 		if (nd.ni_dvp == vp)
    954 			vrele(nd.ni_dvp);
    955 		else
    956 			vput(nd.ni_dvp);
    957 		if (vp)
    958 			vrele(vp);
    959 	}
    960 	return (error);
    961 }
    962 
    963 /*
    964  * Create a named pipe.
    965  */
    966 /* ARGSUSED */
    967 int
    968 sys_mkfifo(p, v, retval)
    969 	struct proc *p;
    970 	void *v;
    971 	register_t *retval;
    972 {
    973 #ifndef FIFO
    974 	return (EOPNOTSUPP);
    975 #else
    976 	register struct sys_mkfifo_args /* {
    977 		syscallarg(const char *) path;
    978 		syscallarg(int) mode;
    979 	} */ *uap = v;
    980 	struct vattr vattr;
    981 	int error;
    982 	struct nameidata nd;
    983 
    984 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
    985 	if ((error = namei(&nd)) != 0)
    986 		return (error);
    987 	if (nd.ni_vp != NULL) {
    988 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
    989 		if (nd.ni_dvp == nd.ni_vp)
    990 			vrele(nd.ni_dvp);
    991 		else
    992 			vput(nd.ni_dvp);
    993 		vrele(nd.ni_vp);
    994 		return (EEXIST);
    995 	}
    996 	VATTR_NULL(&vattr);
    997 	vattr.va_type = VFIFO;
    998 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
    999 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1000 	return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
   1001 #endif /* FIFO */
   1002 }
   1003 
   1004 /*
   1005  * Make a hard file link.
   1006  */
   1007 /* ARGSUSED */
   1008 int
   1009 sys_link(p, v, retval)
   1010 	struct proc *p;
   1011 	void *v;
   1012 	register_t *retval;
   1013 {
   1014 	register struct sys_link_args /* {
   1015 		syscallarg(const char *) path;
   1016 		syscallarg(const char *) link;
   1017 	} */ *uap = v;
   1018 	register struct vnode *vp;
   1019 	struct nameidata nd;
   1020 	int error;
   1021 
   1022 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1023 	if ((error = namei(&nd)) != 0)
   1024 		return (error);
   1025 	vp = nd.ni_vp;
   1026 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1027 	if ((error = namei(&nd)) != 0)
   1028 		goto out;
   1029 	if (nd.ni_vp) {
   1030 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1031 		if (nd.ni_dvp == nd.ni_vp)
   1032 			vrele(nd.ni_dvp);
   1033 		else
   1034 			vput(nd.ni_dvp);
   1035 		vrele(nd.ni_vp);
   1036 		error = EEXIST;
   1037 		goto out;
   1038 	}
   1039 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1040 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1041 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1042 out:
   1043 	vrele(vp);
   1044 	return (error);
   1045 }
   1046 
   1047 /*
   1048  * Make a symbolic link.
   1049  */
   1050 /* ARGSUSED */
   1051 int
   1052 sys_symlink(p, v, retval)
   1053 	struct proc *p;
   1054 	void *v;
   1055 	register_t *retval;
   1056 {
   1057 	register struct sys_symlink_args /* {
   1058 		syscallarg(const char *) path;
   1059 		syscallarg(const char *) link;
   1060 	} */ *uap = v;
   1061 	struct vattr vattr;
   1062 	char *path;
   1063 	int error;
   1064 	struct nameidata nd;
   1065 
   1066 	MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
   1067 	error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
   1068 	if (error)
   1069 		goto out;
   1070 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1071 	if ((error = namei(&nd)) != 0)
   1072 		goto out;
   1073 	if (nd.ni_vp) {
   1074 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1075 		if (nd.ni_dvp == nd.ni_vp)
   1076 			vrele(nd.ni_dvp);
   1077 		else
   1078 			vput(nd.ni_dvp);
   1079 		vrele(nd.ni_vp);
   1080 		error = EEXIST;
   1081 		goto out;
   1082 	}
   1083 	VATTR_NULL(&vattr);
   1084 	vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
   1085 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1086 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   1087 out:
   1088 	FREE(path, M_NAMEI);
   1089 	return (error);
   1090 }
   1091 
   1092 /*
   1093  * Delete a whiteout from the filesystem.
   1094  */
   1095 /* ARGSUSED */
   1096 int
   1097 sys_undelete(p, v, retval)
   1098 	struct proc *p;
   1099 	void *v;
   1100 	register_t *retval;
   1101 {
   1102 	register struct sys_undelete_args /* {
   1103 		syscallarg(const char *) path;
   1104 	} */ *uap = v;
   1105 	int error;
   1106 	struct nameidata nd;
   1107 
   1108 	NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
   1109 	    SCARG(uap, path), p);
   1110 	error = namei(&nd);
   1111 	if (error)
   1112 		return (error);
   1113 
   1114 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   1115 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1116 		if (nd.ni_dvp == nd.ni_vp)
   1117 			vrele(nd.ni_dvp);
   1118 		else
   1119 			vput(nd.ni_dvp);
   1120 		if (nd.ni_vp)
   1121 			vrele(nd.ni_vp);
   1122 		return (EEXIST);
   1123 	}
   1124 
   1125 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1126 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   1127 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1128 	vput(nd.ni_dvp);
   1129 	return (error);
   1130 }
   1131 
   1132 /*
   1133  * Delete a name from the filesystem.
   1134  */
   1135 /* ARGSUSED */
   1136 int
   1137 sys_unlink(p, v, retval)
   1138 	struct proc *p;
   1139 	void *v;
   1140 	register_t *retval;
   1141 {
   1142 	struct sys_unlink_args /* {
   1143 		syscallarg(const char *) path;
   1144 	} */ *uap = v;
   1145 	register struct vnode *vp;
   1146 	int error;
   1147 	struct nameidata nd;
   1148 
   1149 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1150 	    SCARG(uap, path), p);
   1151 	if ((error = namei(&nd)) != 0)
   1152 		return (error);
   1153 	vp = nd.ni_vp;
   1154 
   1155 	/*
   1156 	 * The root of a mounted filesystem cannot be deleted.
   1157 	 */
   1158 	if (vp->v_flag & VROOT) {
   1159 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1160 		if (nd.ni_dvp == vp)
   1161 			vrele(nd.ni_dvp);
   1162 		else
   1163 			vput(nd.ni_dvp);
   1164 		vput(vp);
   1165 		error = EBUSY;
   1166 		goto out;
   1167 	}
   1168 
   1169 #if defined(UVM)
   1170 	(void)uvm_vnp_uncache(vp);
   1171 #else
   1172 	(void)vnode_pager_uncache(vp);
   1173 #endif
   1174 
   1175 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1176 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1177 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1178 out:
   1179 	return (error);
   1180 }
   1181 
   1182 /*
   1183  * Reposition read/write file offset.
   1184  */
   1185 int
   1186 sys_lseek(p, v, retval)
   1187 	struct proc *p;
   1188 	void *v;
   1189 	register_t *retval;
   1190 {
   1191 	register struct sys_lseek_args /* {
   1192 		syscallarg(int) fd;
   1193 		syscallarg(int) pad;
   1194 		syscallarg(off_t) offset;
   1195 		syscallarg(int) whence;
   1196 	} */ *uap = v;
   1197 	struct ucred *cred = p->p_ucred;
   1198 	register struct filedesc *fdp = p->p_fd;
   1199 	register struct file *fp;
   1200 	struct vnode *vp;
   1201 	struct vattr vattr;
   1202 	register off_t newoff;
   1203 	int error;
   1204 
   1205 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
   1206 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
   1207 		return (EBADF);
   1208 
   1209 	vp = (struct vnode *)fp->f_data;
   1210 	if (fp->f_type != DTYPE_VNODE
   1211 #ifdef FIFO
   1212 	    || vp->v_type == VFIFO
   1213 #endif
   1214 	)
   1215 		return (ESPIPE);
   1216 
   1217 	switch (SCARG(uap, whence)) {
   1218 	case SEEK_CUR:
   1219 		newoff = fp->f_offset + SCARG(uap, offset);
   1220 		break;
   1221 	case SEEK_END:
   1222 		error = VOP_GETATTR(vp, &vattr, cred, p);
   1223 		if (error)
   1224 			return (error);
   1225 		newoff = SCARG(uap, offset) + vattr.va_size;
   1226 		break;
   1227 	case SEEK_SET:
   1228 		newoff = SCARG(uap, offset);
   1229 		break;
   1230 	default:
   1231 		return (EINVAL);
   1232 	}
   1233 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
   1234 		return (error);
   1235 
   1236 	*(off_t *)retval = fp->f_offset = newoff;
   1237 	return (0);
   1238 }
   1239 
   1240 /*
   1241  * Check access permissions.
   1242  */
   1243 int
   1244 sys_access(p, v, retval)
   1245 	struct proc *p;
   1246 	void *v;
   1247 	register_t *retval;
   1248 {
   1249 	register struct sys_access_args /* {
   1250 		syscallarg(const char *) path;
   1251 		syscallarg(int) flags;
   1252 	} */ *uap = v;
   1253 	register struct ucred *cred = p->p_ucred;
   1254 	register struct vnode *vp;
   1255 	int error, flags, t_gid, t_uid;
   1256 	struct nameidata nd;
   1257 
   1258 	t_uid = cred->cr_uid;
   1259 	t_gid = cred->cr_gid;
   1260 	cred->cr_uid = p->p_cred->p_ruid;
   1261 	cred->cr_gid = p->p_cred->p_rgid;
   1262 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1263 	    SCARG(uap, path), p);
   1264 	if ((error = namei(&nd)) != 0)
   1265 		goto out1;
   1266 	vp = nd.ni_vp;
   1267 
   1268 	/* Flags == 0 means only check for existence. */
   1269 	if (SCARG(uap, flags)) {
   1270 		flags = 0;
   1271 		if (SCARG(uap, flags) & R_OK)
   1272 			flags |= VREAD;
   1273 		if (SCARG(uap, flags) & W_OK)
   1274 			flags |= VWRITE;
   1275 		if (SCARG(uap, flags) & X_OK)
   1276 			flags |= VEXEC;
   1277 		if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
   1278 			error = VOP_ACCESS(vp, flags, cred, p);
   1279 	}
   1280 	vput(vp);
   1281 out1:
   1282 	cred->cr_uid = t_uid;
   1283 	cred->cr_gid = t_gid;
   1284 	return (error);
   1285 }
   1286 
   1287 /*
   1288  * Get file status; this version follows links.
   1289  */
   1290 /* ARGSUSED */
   1291 int
   1292 sys___stat13(p, v, retval)
   1293 	struct proc *p;
   1294 	void *v;
   1295 	register_t *retval;
   1296 {
   1297 	register struct sys___stat13_args /* {
   1298 		syscallarg(const char *) path;
   1299 		syscallarg(struct stat *) ub;
   1300 	} */ *uap = v;
   1301 	struct stat sb;
   1302 	int error;
   1303 	struct nameidata nd;
   1304 
   1305 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1306 	    SCARG(uap, path), p);
   1307 	if ((error = namei(&nd)) != 0)
   1308 		return (error);
   1309 	error = vn_stat(nd.ni_vp, &sb, p);
   1310 	vput(nd.ni_vp);
   1311 	if (error)
   1312 		return (error);
   1313 	error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
   1314 	return (error);
   1315 }
   1316 
   1317 /*
   1318  * Get file status; this version does not follow links.
   1319  */
   1320 /* ARGSUSED */
   1321 int
   1322 sys___lstat13(p, v, retval)
   1323 	struct proc *p;
   1324 	void *v;
   1325 	register_t *retval;
   1326 {
   1327 	register struct sys___lstat13_args /* {
   1328 		syscallarg(const char *) path;
   1329 		syscallarg(struct stat *) ub;
   1330 	} */ *uap = v;
   1331 	struct stat sb;
   1332 	int error;
   1333 	struct nameidata nd;
   1334 
   1335 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   1336 	    SCARG(uap, path), p);
   1337 	if ((error = namei(&nd)) != 0)
   1338 		return (error);
   1339 	error = vn_stat(nd.ni_vp, &sb, p);
   1340 	vput(nd.ni_vp);
   1341 	if (error)
   1342 		return (error);
   1343 	error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
   1344 	return (error);
   1345 }
   1346 
   1347 /*
   1348  * Get configurable pathname variables.
   1349  */
   1350 /* ARGSUSED */
   1351 int
   1352 sys_pathconf(p, v, retval)
   1353 	struct proc *p;
   1354 	void *v;
   1355 	register_t *retval;
   1356 {
   1357 	register struct sys_pathconf_args /* {
   1358 		syscallarg(const char *) path;
   1359 		syscallarg(int) name;
   1360 	} */ *uap = v;
   1361 	int error;
   1362 	struct nameidata nd;
   1363 
   1364 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1365 	    SCARG(uap, path), p);
   1366 	if ((error = namei(&nd)) != 0)
   1367 		return (error);
   1368 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   1369 	vput(nd.ni_vp);
   1370 	return (error);
   1371 }
   1372 
   1373 /*
   1374  * Return target name of a symbolic link.
   1375  */
   1376 /* ARGSUSED */
   1377 int
   1378 sys_readlink(p, v, retval)
   1379 	struct proc *p;
   1380 	void *v;
   1381 	register_t *retval;
   1382 {
   1383 	register struct sys_readlink_args /* {
   1384 		syscallarg(const char *) path;
   1385 		syscallarg(char *) buf;
   1386 		syscallarg(size_t) count;
   1387 	} */ *uap = v;
   1388 	register struct vnode *vp;
   1389 	struct iovec aiov;
   1390 	struct uio auio;
   1391 	int error;
   1392 	struct nameidata nd;
   1393 
   1394 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   1395 	    SCARG(uap, path), p);
   1396 	if ((error = namei(&nd)) != 0)
   1397 		return (error);
   1398 	vp = nd.ni_vp;
   1399 	if (vp->v_type != VLNK)
   1400 		error = EINVAL;
   1401 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   1402 	    (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
   1403 		aiov.iov_base = SCARG(uap, buf);
   1404 		aiov.iov_len = SCARG(uap, count);
   1405 		auio.uio_iov = &aiov;
   1406 		auio.uio_iovcnt = 1;
   1407 		auio.uio_offset = 0;
   1408 		auio.uio_rw = UIO_READ;
   1409 		auio.uio_segflg = UIO_USERSPACE;
   1410 		auio.uio_procp = p;
   1411 		auio.uio_resid = SCARG(uap, count);
   1412 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   1413 	}
   1414 	vput(vp);
   1415 	*retval = SCARG(uap, count) - auio.uio_resid;
   1416 	return (error);
   1417 }
   1418 
   1419 /*
   1420  * Change flags of a file given a path name.
   1421  */
   1422 /* ARGSUSED */
   1423 int
   1424 sys_chflags(p, v, retval)
   1425 	struct proc *p;
   1426 	void *v;
   1427 	register_t *retval;
   1428 {
   1429 	register struct sys_chflags_args /* {
   1430 		syscallarg(const char *) path;
   1431 		syscallarg(u_long) flags;
   1432 	} */ *uap = v;
   1433 	register struct vnode *vp;
   1434 	struct vattr vattr;
   1435 	int error;
   1436 	struct nameidata nd;
   1437 
   1438 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1439 	if ((error = namei(&nd)) != 0)
   1440 		return (error);
   1441 	vp = nd.ni_vp;
   1442 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1443 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1444 	VATTR_NULL(&vattr);
   1445 	vattr.va_flags = SCARG(uap, flags);
   1446 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1447 	vput(vp);
   1448 	return (error);
   1449 }
   1450 
   1451 /*
   1452  * Change flags of a file given a file descriptor.
   1453  */
   1454 /* ARGSUSED */
   1455 int
   1456 sys_fchflags(p, v, retval)
   1457 	struct proc *p;
   1458 	void *v;
   1459 	register_t *retval;
   1460 {
   1461 	register struct sys_fchflags_args /* {
   1462 		syscallarg(int) fd;
   1463 		syscallarg(u_long) flags;
   1464 	} */ *uap = v;
   1465 	struct vattr vattr;
   1466 	struct vnode *vp;
   1467 	struct file *fp;
   1468 	int error;
   1469 
   1470 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1471 		return (error);
   1472 	vp = (struct vnode *)fp->f_data;
   1473 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1474 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1475 	VATTR_NULL(&vattr);
   1476 	vattr.va_flags = SCARG(uap, flags);
   1477 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1478 	VOP_UNLOCK(vp, 0);
   1479 	return (error);
   1480 }
   1481 
   1482 /*
   1483  * Change mode of a file given path name; this version follows links.
   1484  */
   1485 /* ARGSUSED */
   1486 int
   1487 sys_chmod(p, v, retval)
   1488 	struct proc *p;
   1489 	void *v;
   1490 	register_t *retval;
   1491 {
   1492 	register struct sys_chmod_args /* {
   1493 		syscallarg(const char *) path;
   1494 		syscallarg(int) mode;
   1495 	} */ *uap = v;
   1496 	int error;
   1497 	struct nameidata nd;
   1498 
   1499 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1500 	if ((error = namei(&nd)) != 0)
   1501 		return (error);
   1502 
   1503 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   1504 
   1505 	vrele(nd.ni_vp);
   1506 	return (error);
   1507 }
   1508 
   1509 /*
   1510  * Change mode of a file given a file descriptor.
   1511  */
   1512 /* ARGSUSED */
   1513 int
   1514 sys_fchmod(p, v, retval)
   1515 	struct proc *p;
   1516 	void *v;
   1517 	register_t *retval;
   1518 {
   1519 	register struct sys_fchmod_args /* {
   1520 		syscallarg(int) fd;
   1521 		syscallarg(int) mode;
   1522 	} */ *uap = v;
   1523 	struct file *fp;
   1524 	int error;
   1525 
   1526 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1527 		return (error);
   1528 
   1529 	return (change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p));
   1530 }
   1531 
   1532 /*
   1533  * Change mode of a file given path name; this version does not follow links.
   1534  */
   1535 /* ARGSUSED */
   1536 int
   1537 sys_lchmod(p, v, retval)
   1538 	struct proc *p;
   1539 	void *v;
   1540 	register_t *retval;
   1541 {
   1542 	register struct sys_lchmod_args /* {
   1543 		syscallarg(const char *) path;
   1544 		syscallarg(int) mode;
   1545 	} */ *uap = v;
   1546 	int error;
   1547 	struct nameidata nd;
   1548 
   1549 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1550 	if ((error = namei(&nd)) != 0)
   1551 		return (error);
   1552 
   1553 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   1554 
   1555 	vrele(nd.ni_vp);
   1556 	return (error);
   1557 }
   1558 
   1559 /*
   1560  * Common routine to set mode given a vnode.
   1561  */
   1562 static int
   1563 change_mode(vp, mode, p)
   1564 	struct vnode *vp;
   1565 	int mode;
   1566 	struct proc *p;
   1567 {
   1568 	struct vattr vattr;
   1569 	int error;
   1570 
   1571 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1572 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1573 	VATTR_NULL(&vattr);
   1574 	vattr.va_mode = mode & ALLPERMS;
   1575 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1576 	VOP_UNLOCK(vp, 0);
   1577 	return (error);
   1578 }
   1579 
   1580 /*
   1581  * Set ownership given a path name; this version follows links.
   1582  */
   1583 /* ARGSUSED */
   1584 int
   1585 sys_chown(p, v, retval)
   1586 	struct proc *p;
   1587 	void *v;
   1588 	register_t *retval;
   1589 {
   1590 	register struct sys_chown_args /* {
   1591 		syscallarg(const char *) path;
   1592 		syscallarg(uid_t) uid;
   1593 		syscallarg(gid_t) gid;
   1594 	} */ *uap = v;
   1595 	int error;
   1596 	struct nameidata nd;
   1597 
   1598 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1599 	if ((error = namei(&nd)) != 0)
   1600 		return (error);
   1601 
   1602 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   1603 
   1604 	vrele(nd.ni_vp);
   1605 	return (error);
   1606 }
   1607 
   1608 /*
   1609  * Set ownership given a path name; this version follows links.
   1610  * Provides POSIX semantics.
   1611  */
   1612 /* ARGSUSED */
   1613 int
   1614 sys___posix_chown(p, v, retval)
   1615 	struct proc *p;
   1616 	void *v;
   1617 	register_t *retval;
   1618 {
   1619 	register struct sys_chown_args /* {
   1620 		syscallarg(const char *) path;
   1621 		syscallarg(uid_t) uid;
   1622 		syscallarg(gid_t) gid;
   1623 	} */ *uap = v;
   1624 	int error;
   1625 	struct nameidata nd;
   1626 
   1627 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1628 	if ((error = namei(&nd)) != 0)
   1629 		return (error);
   1630 
   1631 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   1632 
   1633 	vrele(nd.ni_vp);
   1634 	return (error);
   1635 }
   1636 
   1637 /*
   1638  * Set ownership given a file descriptor.
   1639  */
   1640 /* ARGSUSED */
   1641 int
   1642 sys_fchown(p, v, retval)
   1643 	struct proc *p;
   1644 	void *v;
   1645 	register_t *retval;
   1646 {
   1647 	register struct sys_fchown_args /* {
   1648 		syscallarg(int) fd;
   1649 		syscallarg(uid_t) uid;
   1650 		syscallarg(gid_t) gid;
   1651 	} */ *uap = v;
   1652 	int error;
   1653 	struct file *fp;
   1654 
   1655 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1656 		return (error);
   1657 
   1658 	return (change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   1659 	    SCARG(uap, gid), p, 0));
   1660 }
   1661 
   1662 /*
   1663  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   1664  */
   1665 /* ARGSUSED */
   1666 int
   1667 sys___posix_fchown(p, v, retval)
   1668 	struct proc *p;
   1669 	void *v;
   1670 	register_t *retval;
   1671 {
   1672 	register struct sys_fchown_args /* {
   1673 		syscallarg(int) fd;
   1674 		syscallarg(uid_t) uid;
   1675 		syscallarg(gid_t) gid;
   1676 	} */ *uap = v;
   1677 	int error;
   1678 	struct file *fp;
   1679 
   1680 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1681 		return (error);
   1682 
   1683 	return (change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   1684 	    SCARG(uap, gid), p, 1));
   1685 }
   1686 
   1687 /*
   1688  * Set ownership given a path name; this version does not follow links.
   1689  */
   1690 /* ARGSUSED */
   1691 int
   1692 sys_lchown(p, v, retval)
   1693 	struct proc *p;
   1694 	void *v;
   1695 	register_t *retval;
   1696 {
   1697 	register struct sys_lchown_args /* {
   1698 		syscallarg(const char *) path;
   1699 		syscallarg(uid_t) uid;
   1700 		syscallarg(gid_t) gid;
   1701 	} */ *uap = v;
   1702 	int error;
   1703 	struct nameidata nd;
   1704 
   1705 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1706 	if ((error = namei(&nd)) != 0)
   1707 		return (error);
   1708 
   1709 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   1710 
   1711 	vrele(nd.ni_vp);
   1712 	return (error);
   1713 }
   1714 
   1715 /*
   1716  * Set ownership given a path name; this version does not follow links.
   1717  * Provides POSIX/XPG semantics.
   1718  */
   1719 /* ARGSUSED */
   1720 int
   1721 sys___posix_lchown(p, v, retval)
   1722 	struct proc *p;
   1723 	void *v;
   1724 	register_t *retval;
   1725 {
   1726 	register struct sys_lchown_args /* {
   1727 		syscallarg(const char *) path;
   1728 		syscallarg(uid_t) uid;
   1729 		syscallarg(gid_t) gid;
   1730 	} */ *uap = v;
   1731 	int error;
   1732 	struct nameidata nd;
   1733 
   1734 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1735 	if ((error = namei(&nd)) != 0)
   1736 		return (error);
   1737 
   1738 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   1739 
   1740 	vrele(nd.ni_vp);
   1741 	return (error);
   1742 }
   1743 
   1744 /*
   1745  * Common routine to set ownership given a vnode.
   1746  */
   1747 static int
   1748 change_owner(vp, uid, gid, p, posix_semantics)
   1749 	register struct vnode *vp;
   1750 	uid_t uid;
   1751 	gid_t gid;
   1752 	struct proc *p;
   1753 	int posix_semantics;
   1754 {
   1755 	struct vattr vattr;
   1756 	mode_t newmode;
   1757 	int error;
   1758 
   1759 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1760 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1761 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   1762 		goto out;
   1763 
   1764 #define CHANGED(x) ((x) != -1)
   1765 	newmode = vattr.va_mode;
   1766 	if (posix_semantics) {
   1767 		/*
   1768 		 * POSIX/XPG semantics: if the caller is not the super-user,
   1769 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   1770 		 * the XPG consider the behaviour for calls by the super-user
   1771 		 * implementation-defined; we leave the set-user-id and set-
   1772 		 * group-id settings intact in that case.
   1773 		 */
   1774 		if (suser(p->p_ucred, NULL) != 0)
   1775 			newmode &= ~(S_ISUID | S_ISGID);
   1776 	} else {
   1777 		/*
   1778 		 * NetBSD semantics: when changing owner and/or group,
   1779 		 * clear the respective bit(s).
   1780 		 */
   1781 		if (CHANGED(uid))
   1782 			newmode &= ~S_ISUID;
   1783 		if (CHANGED(gid))
   1784 			newmode &= ~S_ISGID;
   1785 	}
   1786 	/* Update va_mode iff altered. */
   1787 	if (vattr.va_mode == newmode)
   1788 		newmode = VNOVAL;
   1789 
   1790 	VATTR_NULL(&vattr);
   1791 	vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
   1792 	vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
   1793 	vattr.va_mode = newmode;
   1794 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1795 #undef CHANGED
   1796 
   1797 out:
   1798 	VOP_UNLOCK(vp, 0);
   1799 	return (error);
   1800 }
   1801 
   1802 /*
   1803  * Set the access and modification times given a path name; this
   1804  * version follows links.
   1805  */
   1806 /* ARGSUSED */
   1807 int
   1808 sys_utimes(p, v, retval)
   1809 	struct proc *p;
   1810 	void *v;
   1811 	register_t *retval;
   1812 {
   1813 	register struct sys_utimes_args /* {
   1814 		syscallarg(const char *) path;
   1815 		syscallarg(const struct timeval *) tptr;
   1816 	} */ *uap = v;
   1817 	int error;
   1818 	struct nameidata nd;
   1819 
   1820 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1821 	if ((error = namei(&nd)) != 0)
   1822 		return (error);
   1823 
   1824 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   1825 
   1826 	vrele(nd.ni_vp);
   1827 	return (error);
   1828 }
   1829 
   1830 /*
   1831  * Set the access and modification times given a file descriptor.
   1832  */
   1833 /* ARGSUSED */
   1834 int
   1835 sys_futimes(p, v, retval)
   1836 	struct proc *p;
   1837 	void *v;
   1838 	register_t *retval;
   1839 {
   1840 	register struct sys_futimes_args /* {
   1841 		syscallarg(int) fd;
   1842 		syscallarg(const struct timeval *) tptr;
   1843 	} */ *uap = v;
   1844 	int error;
   1845 	struct file *fp;
   1846 
   1847 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1848 		return (error);
   1849 
   1850 	return (change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr),
   1851 	    p));
   1852 }
   1853 
   1854 /*
   1855  * Set the access and modification times given a path name; this
   1856  * version does not follow links.
   1857  */
   1858 /* ARGSUSED */
   1859 int
   1860 sys_lutimes(p, v, retval)
   1861 	struct proc *p;
   1862 	void *v;
   1863 	register_t *retval;
   1864 {
   1865 	register struct sys_lutimes_args /* {
   1866 		syscallarg(const char *) path;
   1867 		syscallarg(const struct timeval *) tptr;
   1868 	} */ *uap = v;
   1869 	int error;
   1870 	struct nameidata nd;
   1871 
   1872 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1873 	if ((error = namei(&nd)) != 0)
   1874 		return (error);
   1875 
   1876 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   1877 
   1878 	vrele(nd.ni_vp);
   1879 	return (error);
   1880 }
   1881 
   1882 /*
   1883  * Common routine to set access and modification times given a vnode.
   1884  */
   1885 static int
   1886 change_utimes(vp, tptr, p)
   1887 	struct vnode *vp;
   1888 	const struct timeval *tptr;
   1889 	struct proc *p;
   1890 {
   1891 	struct timeval tv[2];
   1892 	struct vattr vattr;
   1893 	int error;
   1894 
   1895 	VATTR_NULL(&vattr);
   1896 	if (tptr == NULL) {
   1897 		microtime(&tv[0]);
   1898 		tv[1] = tv[0];
   1899 		vattr.va_vaflags |= VA_UTIMES_NULL;
   1900 	} else {
   1901 		error = copyin(tptr, tv, sizeof (tv));
   1902 		if (error)
   1903 			return (error);
   1904 	}
   1905 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1906 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1907 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   1908 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   1909 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   1910 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   1911 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1912 	VOP_UNLOCK(vp, 0);
   1913 	return (error);
   1914 }
   1915 
   1916 /*
   1917  * Truncate a file given its path name.
   1918  */
   1919 /* ARGSUSED */
   1920 int
   1921 sys_truncate(p, v, retval)
   1922 	struct proc *p;
   1923 	void *v;
   1924 	register_t *retval;
   1925 {
   1926 	register struct sys_truncate_args /* {
   1927 		syscallarg(const char *) path;
   1928 		syscallarg(int) pad;
   1929 		syscallarg(off_t) length;
   1930 	} */ *uap = v;
   1931 	register struct vnode *vp;
   1932 	struct vattr vattr;
   1933 	int error;
   1934 	struct nameidata nd;
   1935 
   1936 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1937 	if ((error = namei(&nd)) != 0)
   1938 		return (error);
   1939 	vp = nd.ni_vp;
   1940 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1941 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1942 	if (vp->v_type == VDIR)
   1943 		error = EISDIR;
   1944 	else if ((error = vn_writechk(vp)) == 0 &&
   1945 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   1946 		VATTR_NULL(&vattr);
   1947 		vattr.va_size = SCARG(uap, length);
   1948 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   1949 	}
   1950 	vput(vp);
   1951 	return (error);
   1952 }
   1953 
   1954 /*
   1955  * Truncate a file given a file descriptor.
   1956  */
   1957 /* ARGSUSED */
   1958 int
   1959 sys_ftruncate(p, v, retval)
   1960 	struct proc *p;
   1961 	void *v;
   1962 	register_t *retval;
   1963 {
   1964 	register struct sys_ftruncate_args /* {
   1965 		syscallarg(int) fd;
   1966 		syscallarg(int) pad;
   1967 		syscallarg(off_t) length;
   1968 	} */ *uap = v;
   1969 	struct vattr vattr;
   1970 	struct vnode *vp;
   1971 	struct file *fp;
   1972 	int error;
   1973 
   1974 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   1975 		return (error);
   1976 	if ((fp->f_flag & FWRITE) == 0)
   1977 		return (EINVAL);
   1978 	vp = (struct vnode *)fp->f_data;
   1979 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1980 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1981 	if (vp->v_type == VDIR)
   1982 		error = EISDIR;
   1983 	else if ((error = vn_writechk(vp)) == 0) {
   1984 		VATTR_NULL(&vattr);
   1985 		vattr.va_size = SCARG(uap, length);
   1986 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   1987 	}
   1988 	VOP_UNLOCK(vp, 0);
   1989 	return (error);
   1990 }
   1991 
   1992 /*
   1993  * Sync an open file.
   1994  */
   1995 /* ARGSUSED */
   1996 int
   1997 sys_fsync(p, v, retval)
   1998 	struct proc *p;
   1999 	void *v;
   2000 	register_t *retval;
   2001 {
   2002 	struct sys_fsync_args /* {
   2003 		syscallarg(int) fd;
   2004 	} */ *uap = v;
   2005 	register struct vnode *vp;
   2006 	struct file *fp;
   2007 	int error;
   2008 
   2009 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2010 		return (error);
   2011 	vp = (struct vnode *)fp->f_data;
   2012 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2013 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, p);
   2014 	VOP_UNLOCK(vp, 0);
   2015 	return (error);
   2016 }
   2017 
   2018 /*
   2019  * Sync the data of an open file.
   2020  */
   2021 /* ARGSUSED */
   2022 int
   2023 sys_fdatasync(p, v, retval)
   2024 	struct proc *p;
   2025 	void *v;
   2026 	register_t *retval;
   2027 {
   2028 	struct sys_fdatasync_args /* {
   2029 		syscallarg(int) fd;
   2030 	} */ *uap = v;
   2031 	struct vnode *vp;
   2032 	struct file *fp;
   2033 	int error;
   2034 
   2035 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2036 		return (error);
   2037 	vp = (struct vnode *)fp->f_data;
   2038 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2039 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, p);
   2040 	VOP_UNLOCK(vp, 0);
   2041 	return (error);
   2042 }
   2043 
   2044 /*
   2045  * Rename files, (standard) BSD semantics frontend.
   2046  */
   2047 /* ARGSUSED */
   2048 int
   2049 sys_rename(p, v, retval)
   2050 	struct proc *p;
   2051 	void *v;
   2052 	register_t *retval;
   2053 {
   2054 	register struct sys_rename_args /* {
   2055 		syscallarg(const char *) from;
   2056 		syscallarg(const char *) to;
   2057 	} */ *uap = v;
   2058 
   2059 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
   2060 }
   2061 
   2062 /*
   2063  * Rename files, POSIX semantics frontend.
   2064  */
   2065 /* ARGSUSED */
   2066 int
   2067 sys___posix_rename(p, v, retval)
   2068 	struct proc *p;
   2069 	void *v;
   2070 	register_t *retval;
   2071 {
   2072 	register struct sys___posix_rename_args /* {
   2073 		syscallarg(const char *) from;
   2074 		syscallarg(const char *) to;
   2075 	} */ *uap = v;
   2076 
   2077 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
   2078 }
   2079 
   2080 /*
   2081  * Rename files.  Source and destination must either both be directories,
   2082  * or both not be directories.  If target is a directory, it must be empty.
   2083  * If `from' and `to' refer to the same object, the value of the `retain'
   2084  * argument is used to determine whether `from' will be
   2085  *
   2086  * (retain == 0)	deleted unless `from' and `to' refer to the same
   2087  *			object in the file system's name space (BSD).
   2088  * (retain == 1)	always retained (POSIX).
   2089  */
   2090 static int
   2091 rename_files(from, to, p, retain)
   2092 	const char *from, *to;
   2093 	struct proc *p;
   2094 	int retain;
   2095 {
   2096 	register struct vnode *tvp, *fvp, *tdvp;
   2097 	struct nameidata fromnd, tond;
   2098 	int error;
   2099 
   2100 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   2101 	    from, p);
   2102 	if ((error = namei(&fromnd)) != 0)
   2103 		return (error);
   2104 	fvp = fromnd.ni_vp;
   2105 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
   2106 	    UIO_USERSPACE, to, p);
   2107 	if ((error = namei(&tond)) != 0) {
   2108 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2109 		vrele(fromnd.ni_dvp);
   2110 		vrele(fvp);
   2111 		goto out1;
   2112 	}
   2113 	tdvp = tond.ni_dvp;
   2114 	tvp = tond.ni_vp;
   2115 
   2116 	if (tvp != NULL) {
   2117 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   2118 			error = ENOTDIR;
   2119 			goto out;
   2120 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   2121 			error = EISDIR;
   2122 			goto out;
   2123 		}
   2124 	}
   2125 
   2126 	if (fvp == tdvp)
   2127 		error = EINVAL;
   2128 
   2129 	/*
   2130 	 * Source and destination refer to the same object.
   2131 	 */
   2132 	if (fvp == tvp) {
   2133 		if (retain)
   2134 			error = -1;
   2135 		else if (fromnd.ni_dvp == tdvp &&
   2136 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   2137 		    !bcmp(fromnd.ni_cnd.cn_nameptr,
   2138 		          tond.ni_cnd.cn_nameptr,
   2139 		          fromnd.ni_cnd.cn_namelen))
   2140 		error = -1;
   2141 	}
   2142 
   2143 out:
   2144 	if (!error) {
   2145 		VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
   2146 		if (fromnd.ni_dvp != tdvp)
   2147 			VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2148 		if (tvp) {
   2149 #if defined(UVM)
   2150 			(void)uvm_vnp_uncache(tvp);
   2151 #else
   2152 			(void)vnode_pager_uncache(tvp);
   2153 #endif
   2154 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   2155 		}
   2156 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   2157 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   2158 	} else {
   2159 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   2160 		if (tdvp == tvp)
   2161 			vrele(tdvp);
   2162 		else
   2163 			vput(tdvp);
   2164 		if (tvp)
   2165 			vput(tvp);
   2166 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   2167 		vrele(fromnd.ni_dvp);
   2168 		vrele(fvp);
   2169 	}
   2170 	vrele(tond.ni_startdir);
   2171 	FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
   2172 out1:
   2173 	if (fromnd.ni_startdir)
   2174 		vrele(fromnd.ni_startdir);
   2175 	FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
   2176 	return (error == -1 ? 0 : error);
   2177 }
   2178 
   2179 /*
   2180  * Make a directory file.
   2181  */
   2182 /* ARGSUSED */
   2183 int
   2184 sys_mkdir(p, v, retval)
   2185 	struct proc *p;
   2186 	void *v;
   2187 	register_t *retval;
   2188 {
   2189 	register struct sys_mkdir_args /* {
   2190 		syscallarg(const char *) path;
   2191 		syscallarg(int) mode;
   2192 	} */ *uap = v;
   2193 	register struct vnode *vp;
   2194 	struct vattr vattr;
   2195 	int error;
   2196 	struct nameidata nd;
   2197 
   2198 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   2199 	if ((error = namei(&nd)) != 0)
   2200 		return (error);
   2201 	vp = nd.ni_vp;
   2202 	if (vp != NULL) {
   2203 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2204 		if (nd.ni_dvp == vp)
   2205 			vrele(nd.ni_dvp);
   2206 		else
   2207 			vput(nd.ni_dvp);
   2208 		vrele(vp);
   2209 		return (EEXIST);
   2210 	}
   2211 	VATTR_NULL(&vattr);
   2212 	vattr.va_type = VDIR;
   2213 	vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
   2214 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2215 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   2216 	if (!error)
   2217 		vput(nd.ni_vp);
   2218 	return (error);
   2219 }
   2220 
   2221 /*
   2222  * Remove a directory file.
   2223  */
   2224 /* ARGSUSED */
   2225 int
   2226 sys_rmdir(p, v, retval)
   2227 	struct proc *p;
   2228 	void *v;
   2229 	register_t *retval;
   2230 {
   2231 	struct sys_rmdir_args /* {
   2232 		syscallarg(const char *) path;
   2233 	} */ *uap = v;
   2234 	register struct vnode *vp;
   2235 	int error;
   2236 	struct nameidata nd;
   2237 
   2238 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   2239 	    SCARG(uap, path), p);
   2240 	if ((error = namei(&nd)) != 0)
   2241 		return (error);
   2242 	vp = nd.ni_vp;
   2243 	if (vp->v_type != VDIR) {
   2244 		error = ENOTDIR;
   2245 		goto out;
   2246 	}
   2247 	/*
   2248 	 * No rmdir "." please.
   2249 	 */
   2250 	if (nd.ni_dvp == vp) {
   2251 		error = EINVAL;
   2252 		goto out;
   2253 	}
   2254 	/*
   2255 	 * The root of a mounted filesystem cannot be deleted.
   2256 	 */
   2257 	if (vp->v_flag & VROOT)
   2258 		error = EBUSY;
   2259 out:
   2260 	if (!error) {
   2261 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   2262 		VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2263 		error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   2264 	} else {
   2265 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   2266 		if (nd.ni_dvp == vp)
   2267 			vrele(nd.ni_dvp);
   2268 		else
   2269 			vput(nd.ni_dvp);
   2270 		vput(vp);
   2271 	}
   2272 	return (error);
   2273 }
   2274 
   2275 /*
   2276  * Read a block of directory entries in a file system independent format.
   2277  */
   2278 int
   2279 sys_getdents(p, v, retval)
   2280 	struct proc *p;
   2281 	void *v;
   2282 	register_t *retval;
   2283 {
   2284 	register struct sys_getdents_args /* {
   2285 		syscallarg(int) fd;
   2286 		syscallarg(char *) buf;
   2287 		syscallarg(size_t) count;
   2288 	} */ *uap = v;
   2289 	struct file *fp;
   2290 	int error, done;
   2291 
   2292 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2293 		return (error);
   2294 	if ((fp->f_flag & FREAD) == 0)
   2295 		return (EBADF);
   2296 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   2297 			SCARG(uap, count), &done, p, 0, 0);
   2298 	*retval = done;
   2299 	return (error);
   2300 }
   2301 
   2302 /*
   2303  * Set the mode mask for creation of filesystem nodes.
   2304  */
   2305 int
   2306 sys_umask(p, v, retval)
   2307 	struct proc *p;
   2308 	void *v;
   2309 	register_t *retval;
   2310 {
   2311 	struct sys_umask_args /* {
   2312 		syscallarg(mode_t) newmask;
   2313 	} */ *uap = v;
   2314 	register struct filedesc *fdp;
   2315 
   2316 	fdp = p->p_fd;
   2317 	*retval = fdp->fd_cmask;
   2318 	fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
   2319 	return (0);
   2320 }
   2321 
   2322 /*
   2323  * Void all references to file by ripping underlying filesystem
   2324  * away from vnode.
   2325  */
   2326 /* ARGSUSED */
   2327 int
   2328 sys_revoke(p, v, retval)
   2329 	struct proc *p;
   2330 	void *v;
   2331 	register_t *retval;
   2332 {
   2333 	register struct sys_revoke_args /* {
   2334 		syscallarg(const char *) path;
   2335 	} */ *uap = v;
   2336 	register struct vnode *vp;
   2337 	struct vattr vattr;
   2338 	int error;
   2339 	struct nameidata nd;
   2340 
   2341 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2342 	if ((error = namei(&nd)) != 0)
   2343 		return (error);
   2344 	vp = nd.ni_vp;
   2345 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2346 		goto out;
   2347 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   2348 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
   2349 		goto out;
   2350 	if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
   2351 		VOP_REVOKE(vp, REVOKEALL);
   2352 out:
   2353 	vrele(vp);
   2354 	return (error);
   2355 }
   2356 
   2357 /*
   2358  * Convert a user file descriptor to a kernel file entry.
   2359  */
   2360 int
   2361 getvnode(fdp, fd, fpp)
   2362 	struct filedesc *fdp;
   2363 	int fd;
   2364 	struct file **fpp;
   2365 {
   2366 	struct vnode *vp;
   2367 	struct file *fp;
   2368 
   2369 	if ((u_int)fd >= fdp->fd_nfiles ||
   2370 	    (fp = fdp->fd_ofiles[fd]) == NULL)
   2371 		return (EBADF);
   2372 	if (fp->f_type != DTYPE_VNODE)
   2373 		return (EINVAL);
   2374 	vp = (struct vnode *)fp->f_data;
   2375 	if (vp->v_type == VBAD)
   2376 		return (EBADF);
   2377 	*fpp = fp;
   2378 	return (0);
   2379 }
   2380