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