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