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