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