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