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