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