Home | History | Annotate | Line # | Download | only in kern
vfs_syscalls.c revision 1.230
      1 /*	$NetBSD: vfs_syscalls.c,v 1.230 2005/08/30 09:37:41 jmmv 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. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)vfs_syscalls.c	8.42 (Berkeley) 7/31/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls.c,v 1.230 2005/08/30 09:37:41 jmmv Exp $");
     41 
     42 #include "opt_compat_netbsd.h"
     43 #include "opt_compat_43.h"
     44 #include "opt_ktrace.h"
     45 #include "opt_verified_exec.h"
     46 #include "fss.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/namei.h>
     51 #include <sys/filedesc.h>
     52 #include <sys/kernel.h>
     53 #include <sys/file.h>
     54 #include <sys/stat.h>
     55 #include <sys/vnode.h>
     56 #include <sys/mount.h>
     57 #include <sys/proc.h>
     58 #include <sys/uio.h>
     59 #include <sys/malloc.h>
     60 #include <sys/dirent.h>
     61 #include <sys/sysctl.h>
     62 #include <sys/sa.h>
     63 #include <sys/syscallargs.h>
     64 #ifdef KTRACE
     65 #include <sys/ktrace.h>
     66 #endif
     67 #ifdef VERIFIED_EXEC
     68 #include <sys/verified_exec.h>
     69 #endif /* VERIFIED_EXEC */
     70 
     71 #include <miscfs/genfs/genfs.h>
     72 #include <miscfs/syncfs/syncfs.h>
     73 
     74 #if NFSS > 0
     75 #include <dev/fssvar.h>
     76 #endif
     77 
     78 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
     79 
     80 static int change_dir(struct nameidata *, struct proc *);
     81 static int change_flags(struct vnode *, u_long, struct proc *);
     82 static int change_mode(struct vnode *, int, struct proc *p);
     83 static int change_owner(struct vnode *, uid_t, gid_t, struct proc *, int);
     84 static int change_utimes(struct vnode *vp, const struct timeval *,
     85 	       struct proc *p);
     86 static int rename_files(const char *, const char *, struct proc *, int);
     87 
     88 void checkdirs(struct vnode *);
     89 
     90 int dovfsusermount = 0;
     91 
     92 /*
     93  * Virtual File System System Calls
     94  */
     95 
     96 /*
     97  * Mount a file system.
     98  */
     99 
    100 #if defined(COMPAT_09) || defined(COMPAT_43)
    101 /*
    102  * This table is used to maintain compatibility with 4.3BSD
    103  * and NetBSD 0.9 mount syscalls.  Note, the order is important!
    104  *
    105  * Do not modify this table. It should only contain filesystems
    106  * supported by NetBSD 0.9 and 4.3BSD.
    107  */
    108 const char * const mountcompatnames[] = {
    109 	NULL,		/* 0 = MOUNT_NONE */
    110 	MOUNT_FFS,	/* 1 = MOUNT_UFS */
    111 	MOUNT_NFS,	/* 2 */
    112 	MOUNT_MFS,	/* 3 */
    113 	MOUNT_MSDOS,	/* 4 */
    114 	MOUNT_CD9660,	/* 5 = MOUNT_ISOFS */
    115 	MOUNT_FDESC,	/* 6 */
    116 	MOUNT_KERNFS,	/* 7 */
    117 	NULL,		/* 8 = MOUNT_DEVFS */
    118 	MOUNT_AFS,	/* 9 */
    119 };
    120 const int nmountcompatnames = sizeof(mountcompatnames) /
    121     sizeof(mountcompatnames[0]);
    122 #endif /* COMPAT_09 || COMPAT_43 */
    123 
    124 /* ARGSUSED */
    125 int
    126 sys_mount(struct lwp *l, void *v, register_t *retval)
    127 {
    128 	struct sys_mount_args /* {
    129 		syscallarg(const char *) type;
    130 		syscallarg(const char *) path;
    131 		syscallarg(int) flags;
    132 		syscallarg(void *) data;
    133 	} */ *uap = v;
    134 	struct proc *p = l->l_proc;
    135 	struct vnode *vp;
    136 	struct mount *mp;
    137 	int error, flag = 0;
    138 	char fstypename[MFSNAMELEN];
    139 	struct vattr va;
    140 	struct nameidata nd;
    141 	struct vfsops *vfs;
    142 
    143 	/*
    144 	 * if MNT_GETARGS is specified, it should be only flag.
    145 	 */
    146 
    147 	if ((SCARG(uap, flags) & MNT_GETARGS) != 0 &&
    148 	    (SCARG(uap, flags) & ~MNT_GETARGS) != 0) {
    149 		return EINVAL;
    150 	}
    151 
    152 	if (dovfsusermount == 0 && (SCARG(uap, flags) & MNT_GETARGS) == 0 &&
    153 	    (error = suser(p->p_ucred, &p->p_acflag)))
    154 		return (error);
    155 	/*
    156 	 * Get vnode to be covered
    157 	 */
    158 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE,
    159 	    SCARG(uap, path), p);
    160 	if ((error = namei(&nd)) != 0)
    161 		return (error);
    162 	vp = nd.ni_vp;
    163 	/*
    164 	 * A lookup in VFS_MOUNT might result in an attempt to
    165 	 * lock this vnode again, so make the lock recursive.
    166 	 */
    167 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
    168 	if (SCARG(uap, flags) & (MNT_UPDATE | MNT_GETARGS)) {
    169 		if ((vp->v_flag & VROOT) == 0) {
    170 			vput(vp);
    171 			return (EINVAL);
    172 		}
    173 		mp = vp->v_mount;
    174 		flag = mp->mnt_flag;
    175 		vfs = mp->mnt_op;
    176 		/*
    177 		 * We only allow the filesystem to be reloaded if it
    178 		 * is currently mounted read-only.
    179 		 */
    180 		if ((SCARG(uap, flags) & MNT_RELOAD) &&
    181 		    ((mp->mnt_flag & MNT_RDONLY) == 0)) {
    182 			vput(vp);
    183 			return (EOPNOTSUPP);	/* Needs translation */
    184 		}
    185 		/*
    186 		 * In "highly secure" mode, don't let the caller do anything
    187 		 * but downgrade a filesystem from read-write to read-only.
    188 		 * (see also below; MNT_UPDATE or MNT_GETARGS is required.)
    189 		 */
    190 		if (securelevel >= 2 &&
    191 		    SCARG(uap, flags) != MNT_GETARGS &&
    192 		    SCARG(uap, flags) !=
    193 		    (mp->mnt_flag | MNT_RDONLY |
    194 		     MNT_RELOAD | MNT_FORCE | MNT_UPDATE)) {
    195 			vput(vp);
    196 			return (EPERM);
    197 		}
    198 		mp->mnt_flag |= SCARG(uap, flags) &
    199 		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
    200 		/*
    201 		 * Only root, or the user that did the original mount is
    202 		 * permitted to update it.
    203 		 */
    204 		if ((mp->mnt_flag & MNT_GETARGS) == 0 &&
    205 		    mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
    206 		    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    207 			vput(vp);
    208 			return (error);
    209 		}
    210 		/*
    211 		 * Do not allow NFS export by non-root users. For non-root
    212 		 * users, silently enforce MNT_NOSUID and MNT_NODEV, and
    213 		 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
    214 		 */
    215 		if (p->p_ucred->cr_uid != 0) {
    216 			if (SCARG(uap, flags) & MNT_EXPORTED) {
    217 				vput(vp);
    218 				return (EPERM);
    219 			}
    220 			SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    221 			if (flag & MNT_NOEXEC)
    222 				SCARG(uap, flags) |= MNT_NOEXEC;
    223 		}
    224 		if (vfs_busy(mp, LK_NOWAIT, 0)) {
    225 			vput(vp);
    226 			return (EPERM);
    227 		}
    228 		goto update;
    229 	} else {
    230 		if (securelevel >= 2) {
    231 			vput(vp);
    232 			return (EPERM);
    233 		}
    234 	}
    235 	/*
    236 	 * If the user is not root, ensure that they own the directory
    237 	 * onto which we are attempting to mount.
    238 	 */
    239 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
    240 	    (va.va_uid != p->p_ucred->cr_uid &&
    241 		(error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
    242 		vput(vp);
    243 		return (error);
    244 	}
    245 	/*
    246 	 * Do not allow NFS export by non-root users. For non-root users,
    247 	 * silently enforce MNT_NOSUID and MNT_NODEV, and MNT_NOEXEC if the
    248 	 * mount point is already MNT_NOEXEC.
    249 	 */
    250 	if (p->p_ucred->cr_uid != 0) {
    251 		if (SCARG(uap, flags) & MNT_EXPORTED) {
    252 			vput(vp);
    253 			return (EPERM);
    254 		}
    255 		SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
    256 		if (vp->v_mount->mnt_flag & MNT_NOEXEC)
    257 			SCARG(uap, flags) |= MNT_NOEXEC;
    258 	}
    259 	if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0) {
    260 		vput(vp);
    261 		return (error);
    262 	}
    263 	if (vp->v_type != VDIR) {
    264 		vput(vp);
    265 		return (ENOTDIR);
    266 	}
    267 	error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
    268 	if (error) {
    269 #if defined(COMPAT_09) || defined(COMPAT_43)
    270 		/*
    271 		 * Historically, filesystem types were identified by numbers.
    272 		 * If we get an integer for the filesystem type instead of a
    273 		 * string, we check to see if it matches one of the historic
    274 		 * filesystem types.
    275 		 */
    276 		u_long fsindex = (u_long)SCARG(uap, type);
    277 		if (fsindex >= nmountcompatnames ||
    278 		    mountcompatnames[fsindex] == NULL) {
    279 			vput(vp);
    280 			return (ENODEV);
    281 		}
    282 		strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
    283 #else
    284 		vput(vp);
    285 		return (error);
    286 #endif
    287 	}
    288 #ifdef	COMPAT_10
    289 	/* Accept `ufs' as an alias for `ffs'. */
    290 	if (!strncmp(fstypename, "ufs", MFSNAMELEN))
    291 		strncpy(fstypename, "ffs", MFSNAMELEN);
    292 #endif
    293 	if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
    294 		vput(vp);
    295 		return (ENODEV);
    296 	}
    297 	if (vp->v_mountedhere != NULL) {
    298 		vput(vp);
    299 		return (EBUSY);
    300 	}
    301 
    302 	/*
    303 	 * Allocate and initialize the file system.
    304 	 */
    305 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    306 		M_MOUNT, M_WAITOK);
    307 	memset((char *)mp, 0, (u_long)sizeof(struct mount));
    308 	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
    309 	simple_lock_init(&mp->mnt_slock);
    310 	(void)vfs_busy(mp, LK_NOWAIT, 0);
    311 	mp->mnt_op = vfs;
    312 	vfs->vfs_refcount++;
    313 	mp->mnt_vnodecovered = vp;
    314 	mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
    315 	mp->mnt_unmounter = NULL;
    316 	mp->mnt_leaf = mp;
    317 
    318 	/*
    319 	 * The underlying file system may refuse the mount for
    320 	 * various reasons.  Allow the user to force it to happen.
    321 	 */
    322 	mp->mnt_flag |= SCARG(uap, flags) & MNT_FORCE;
    323  update:
    324 	if ((SCARG(uap, flags) & MNT_GETARGS) == 0) {
    325 		/*
    326 		 * Set the mount level flags.
    327 		 */
    328 		if (SCARG(uap, flags) & MNT_RDONLY)
    329 			mp->mnt_flag |= MNT_RDONLY;
    330 		else if (mp->mnt_flag & MNT_RDONLY)
    331 			mp->mnt_iflag |= IMNT_WANTRDWR;
    332 		mp->mnt_flag &=
    333 		  ~(MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    334 		    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    335 		    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    336 		    MNT_MAGICLINKS);
    337 		mp->mnt_flag |= SCARG(uap, flags) &
    338 		   (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
    339 		    MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
    340 		    MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM | MNT_SOFTDEP |
    341 		    MNT_IGNORE | MNT_MAGICLINKS);
    342 	}
    343 	/*
    344 	 * Mount the filesystem.
    345 	 */
    346 	error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
    347 	if (mp->mnt_flag & (MNT_UPDATE | MNT_GETARGS)) {
    348 		if (mp->mnt_iflag & IMNT_WANTRDWR)
    349 			mp->mnt_flag &= ~MNT_RDONLY;
    350 		if (error)
    351 			mp->mnt_flag = flag;
    352 		mp->mnt_flag &=~
    353 		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
    354 		mp->mnt_iflag &=~ IMNT_WANTRDWR;
    355 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0) {
    356 			if (mp->mnt_syncer == NULL)
    357 				error = vfs_allocate_syncvnode(mp);
    358 		} else {
    359 			if (mp->mnt_syncer != NULL)
    360 				vfs_deallocate_syncvnode(mp);
    361 		}
    362 		vfs_unbusy(mp);
    363 		VOP_UNLOCK(vp, 0);
    364 		vrele(vp);
    365 		return (error);
    366 	}
    367 	/*
    368 	 * Put the new filesystem on the mount list after root.
    369 	 */
    370 	cache_purge(vp);
    371 	if (!error) {
    372 		mp->mnt_flag &=~
    373 		    (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_GETARGS);
    374 		mp->mnt_iflag &=~ IMNT_WANTRDWR;
    375 		vp->v_mountedhere = mp;
    376 		simple_lock(&mountlist_slock);
    377 		CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    378 		simple_unlock(&mountlist_slock);
    379 		checkdirs(vp);
    380 		VOP_UNLOCK(vp, 0);
    381 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    382 			error = vfs_allocate_syncvnode(mp);
    383 		vfs_unbusy(mp);
    384 		(void) VFS_STATVFS(mp, &mp->mnt_stat, p);
    385 		if ((error = VFS_START(mp, 0, p)))
    386 			vrele(vp);
    387 	} else {
    388 		vp->v_mountedhere = (struct mount *)0;
    389 		vfs->vfs_refcount--;
    390 		vfs_unbusy(mp);
    391 		free(mp, M_MOUNT);
    392 		vput(vp);
    393 	}
    394 	return (error);
    395 }
    396 
    397 /*
    398  * Scan all active processes to see if any of them have a current
    399  * or root directory onto which the new filesystem has just been
    400  * mounted. If so, replace them with the new mount point.
    401  */
    402 void
    403 checkdirs(struct vnode *olddp)
    404 {
    405 	struct cwdinfo *cwdi;
    406 	struct vnode *newdp;
    407 	struct proc *p;
    408 
    409 	if (olddp->v_usecount == 1)
    410 		return;
    411 	if (VFS_ROOT(olddp->v_mountedhere, &newdp))
    412 		panic("mount: lost mount");
    413 	proclist_lock_read();
    414 	PROCLIST_FOREACH(p, &allproc) {
    415 		cwdi = p->p_cwdi;
    416 		if (!cwdi)
    417 			continue;
    418 		if (cwdi->cwdi_cdir == olddp) {
    419 			vrele(cwdi->cwdi_cdir);
    420 			VREF(newdp);
    421 			cwdi->cwdi_cdir = newdp;
    422 		}
    423 		if (cwdi->cwdi_rdir == olddp) {
    424 			vrele(cwdi->cwdi_rdir);
    425 			VREF(newdp);
    426 			cwdi->cwdi_rdir = newdp;
    427 		}
    428 	}
    429 	proclist_unlock_read();
    430 	if (rootvnode == olddp) {
    431 		vrele(rootvnode);
    432 		VREF(newdp);
    433 		rootvnode = newdp;
    434 	}
    435 	vput(newdp);
    436 }
    437 
    438 /*
    439  * Unmount a file system.
    440  *
    441  * Note: unmount takes a path to the vnode mounted on as argument,
    442  * not special file (as before).
    443  */
    444 /* ARGSUSED */
    445 int
    446 sys_unmount(struct lwp *l, void *v, register_t *retval)
    447 {
    448 	struct sys_unmount_args /* {
    449 		syscallarg(const char *) path;
    450 		syscallarg(int) flags;
    451 	} */ *uap = v;
    452 	struct proc *p = l->l_proc;
    453 	struct vnode *vp;
    454 	struct mount *mp;
    455 	int error;
    456 	struct nameidata nd;
    457 
    458 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
    459 	    SCARG(uap, path), p);
    460 	if ((error = namei(&nd)) != 0)
    461 		return (error);
    462 	vp = nd.ni_vp;
    463 	mp = vp->v_mount;
    464 
    465 	/*
    466 	 * Only root, or the user that did the original mount is
    467 	 * permitted to unmount this filesystem.
    468 	 */
    469 	if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
    470 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
    471 		vput(vp);
    472 		return (error);
    473 	}
    474 
    475 	/*
    476 	 * Don't allow unmounting the root file system.
    477 	 */
    478 	if (mp->mnt_flag & MNT_ROOTFS) {
    479 		vput(vp);
    480 		return (EINVAL);
    481 	}
    482 
    483 	/*
    484 	 * Must be the root of the filesystem
    485 	 */
    486 	if ((vp->v_flag & VROOT) == 0) {
    487 		vput(vp);
    488 		return (EINVAL);
    489 	}
    490 	vput(vp);
    491 
    492 	/*
    493 	 * XXX Freeze syncer.  Must do this before locking the
    494 	 * mount point.  See dounmount() for details.
    495 	 */
    496 	lockmgr(&syncer_lock, LK_EXCLUSIVE, NULL);
    497 
    498 	if (vfs_busy(mp, 0, 0)) {
    499 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    500 		return (EBUSY);
    501 	}
    502 
    503 	return (dounmount(mp, SCARG(uap, flags), p));
    504 }
    505 
    506 /*
    507  * Do the actual file system unmount. File system is assumed to have been
    508  * marked busy by the caller.
    509  */
    510 int
    511 dounmount(struct mount *mp, int flags, struct proc *p)
    512 {
    513 	struct vnode *coveredvp;
    514 	int error;
    515 	int async;
    516 	int used_syncer;
    517 
    518 	simple_lock(&mountlist_slock);
    519 	vfs_unbusy(mp);
    520 	used_syncer = (mp->mnt_syncer != NULL);
    521 
    522 	/*
    523 	 * XXX Syncer must be frozen when we get here.  This should really
    524 	 * be done on a per-mountpoint basis, but especially the softdep
    525 	 * code possibly called from the syncer doesn't exactly work on a
    526 	 * per-mountpoint basis, so the softdep code would become a maze
    527 	 * of vfs_busy() calls.
    528 	 *
    529 	 * The caller of dounmount() must acquire syncer_lock because
    530 	 * the syncer itself acquires locks in syncer_lock -> vfs_busy
    531 	 * order, and we must preserve that order to avoid deadlock.
    532 	 *
    533 	 * So, if the file system did not use the syncer, now is
    534 	 * the time to release the syncer_lock.
    535 	 */
    536 	if (used_syncer == 0)
    537 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    538 
    539 	mp->mnt_iflag |= IMNT_UNMOUNT;
    540 	mp->mnt_unmounter = p;
    541 	lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
    542 	vn_start_write(NULL, &mp, V_WAIT);
    543 
    544 	if (mp->mnt_flag & MNT_EXPUBLIC)
    545 		vfs_setpublicfs(NULL, NULL, NULL);
    546 	async = mp->mnt_flag & MNT_ASYNC;
    547 	mp->mnt_flag &= ~MNT_ASYNC;
    548 	cache_purgevfs(mp);	/* remove cache entries for this file sys */
    549 	if (mp->mnt_syncer != NULL)
    550 		vfs_deallocate_syncvnode(mp);
    551 	error = 0;
    552 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    553 #if NFSS > 0
    554 		error = fss_umount_hook(mp, (flags & MNT_FORCE));
    555 #endif
    556 		if (error == 0)
    557 			error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p);
    558 	}
    559 	if (error == 0 || (flags & MNT_FORCE))
    560 		error = VFS_UNMOUNT(mp, flags, p);
    561 	vn_finished_write(mp, 0);
    562 	simple_lock(&mountlist_slock);
    563 	if (error) {
    564 		if ((mp->mnt_flag & (MNT_RDONLY | MNT_ASYNC)) == 0)
    565 			(void) vfs_allocate_syncvnode(mp);
    566 		mp->mnt_iflag &= ~IMNT_UNMOUNT;
    567 		mp->mnt_unmounter = NULL;
    568 		mp->mnt_flag |= async;
    569 		lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
    570 		    &mountlist_slock);
    571 		if (used_syncer)
    572 			lockmgr(&syncer_lock, LK_RELEASE, NULL);
    573 		simple_lock(&mp->mnt_slock);
    574 		while (mp->mnt_wcnt > 0) {
    575 			wakeup(mp);
    576 			ltsleep(&mp->mnt_wcnt, PVFS, "mntwcnt1",
    577 				0, &mp->mnt_slock);
    578 		}
    579 		simple_unlock(&mp->mnt_slock);
    580 		return (error);
    581 	}
    582 	CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
    583 	if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
    584 		coveredvp->v_mountedhere = NULL;
    585 		vrele(coveredvp);
    586 	}
    587 	mp->mnt_op->vfs_refcount--;
    588 	if (LIST_FIRST(&mp->mnt_vnodelist) != NULL)
    589 		panic("unmount: dangling vnode");
    590 	mp->mnt_iflag |= IMNT_GONE;
    591 	lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
    592 	if (used_syncer)
    593 		lockmgr(&syncer_lock, LK_RELEASE, NULL);
    594 	simple_lock(&mp->mnt_slock);
    595 	while (mp->mnt_wcnt > 0) {
    596 		wakeup(mp);
    597 		ltsleep(&mp->mnt_wcnt, PVFS, "mntwcnt2", 0, &mp->mnt_slock);
    598 	}
    599 	simple_unlock(&mp->mnt_slock);
    600 	free(mp, M_MOUNT);
    601 	return (0);
    602 }
    603 
    604 /*
    605  * Sync each mounted filesystem.
    606  */
    607 #ifdef DEBUG
    608 int syncprt = 0;
    609 struct ctldebug debug0 = { "syncprt", &syncprt };
    610 #endif
    611 
    612 /* ARGSUSED */
    613 int
    614 sys_sync(struct lwp *l, void *v, register_t *retval)
    615 {
    616 	struct mount *mp, *nmp;
    617 	int asyncflag;
    618 	struct proc *p = l == NULL ? &proc0 : l->l_proc;
    619 
    620 	simple_lock(&mountlist_slock);
    621 	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
    622 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    623 			nmp = mp->mnt_list.cqe_prev;
    624 			continue;
    625 		}
    626 		if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
    627 		    vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
    628 			asyncflag = mp->mnt_flag & MNT_ASYNC;
    629 			mp->mnt_flag &= ~MNT_ASYNC;
    630 			VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
    631 			if (asyncflag)
    632 				 mp->mnt_flag |= MNT_ASYNC;
    633 			vn_finished_write(mp, 0);
    634 		}
    635 		simple_lock(&mountlist_slock);
    636 		nmp = mp->mnt_list.cqe_prev;
    637 		vfs_unbusy(mp);
    638 
    639 	}
    640 	simple_unlock(&mountlist_slock);
    641 #ifdef DEBUG
    642 	if (syncprt)
    643 		vfs_bufstats();
    644 #endif /* DEBUG */
    645 	return (0);
    646 }
    647 
    648 /*
    649  * Change filesystem quotas.
    650  */
    651 /* ARGSUSED */
    652 int
    653 sys_quotactl(struct lwp *l, void *v, register_t *retval)
    654 {
    655 	struct sys_quotactl_args /* {
    656 		syscallarg(const char *) path;
    657 		syscallarg(int) cmd;
    658 		syscallarg(int) uid;
    659 		syscallarg(caddr_t) arg;
    660 	} */ *uap = v;
    661 	struct proc *p = l->l_proc;
    662 	struct mount *mp;
    663 	int error;
    664 	struct nameidata nd;
    665 
    666 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    667 	if ((error = namei(&nd)) != 0)
    668 		return (error);
    669 	error = vn_start_write(nd.ni_vp, &mp, V_WAIT | V_PCATCH);
    670 	vrele(nd.ni_vp);
    671 	if (error)
    672 		return (error);
    673 	error = VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
    674 	    SCARG(uap, arg), p);
    675 	vn_finished_write(mp, 0);
    676 	return (error);
    677 }
    678 
    679 int
    680 dostatvfs(struct mount *mp, struct statvfs *sp, struct proc *p, int flags,
    681     int root)
    682 {
    683 	struct cwdinfo *cwdi = p->p_cwdi;
    684 	int error = 0;
    685 
    686 	/*
    687 	 * If MNT_NOWAIT or MNT_LAZY is specified, do not
    688 	 * refresh the fsstat cache. MNT_WAIT or MNT_LAZY
    689 	 * overrides MNT_NOWAIT.
    690 	 */
    691 	if (flags == MNT_NOWAIT	|| flags == MNT_LAZY ||
    692 	    (flags != MNT_WAIT && flags != 0)) {
    693 		memcpy(sp, &mp->mnt_stat, sizeof(*sp));
    694 		goto done;
    695 	}
    696 
    697 	/* Get the filesystem stats now */
    698 	memset(sp, 0, sizeof(*sp));
    699 	if ((error = VFS_STATVFS(mp, sp, p)) != 0) {
    700 		return error;
    701 	}
    702 
    703 	if (cwdi->cwdi_rdir == NULL)
    704 		(void)memcpy(&mp->mnt_stat, sp, sizeof(mp->mnt_stat));
    705 done:
    706 	if (cwdi->cwdi_rdir != NULL) {
    707 		size_t len;
    708 		char *bp;
    709 		char *path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
    710 		if (!path)
    711 			return ENOMEM;
    712 
    713 		bp = path + MAXPATHLEN;
    714 		*--bp = '\0';
    715 		error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
    716 		    MAXPATHLEN / 2, 0, p);
    717 		if (error) {
    718 			free(path, M_TEMP);
    719 			return error;
    720 		}
    721 		len = strlen(bp);
    722 		/*
    723 		 * for mount points that are below our root, we can see
    724 		 * them, so we fix up the pathname and return them. The
    725 		 * rest we cannot see, so we don't allow viewing the
    726 		 * data.
    727 		 */
    728 		if (strncmp(bp, sp->f_mntonname, len) == 0) {
    729 			strlcpy(sp->f_mntonname, &sp->f_mntonname[len],
    730 			    sizeof(sp->f_mntonname));
    731 			if (sp->f_mntonname[0] == '\0')
    732 				(void)strlcpy(sp->f_mntonname, "/",
    733 				    sizeof(sp->f_mntonname));
    734 		} else {
    735 			if (root)
    736 				(void)strlcpy(sp->f_mntonname, "/",
    737 				    sizeof(sp->f_mntonname));
    738 			else
    739 				error = EPERM;
    740 		}
    741 		free(path, M_TEMP);
    742 	}
    743 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
    744 	return error;
    745 }
    746 
    747 /*
    748  * Get filesystem statistics.
    749  */
    750 /* ARGSUSED */
    751 int
    752 sys_statvfs1(struct lwp *l, void *v, register_t *retval)
    753 {
    754 	struct sys_statvfs1_args /* {
    755 		syscallarg(const char *) path;
    756 		syscallarg(struct statvfs *) buf;
    757 		syscallarg(int) flags;
    758 	} */ *uap = v;
    759 	struct proc *p = l->l_proc;
    760 	struct mount *mp;
    761 	struct statvfs sbuf;
    762 	int error;
    763 	struct nameidata nd;
    764 
    765 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    766 	if ((error = namei(&nd)) != 0)
    767 		return error;
    768 	mp = nd.ni_vp->v_mount;
    769 	vrele(nd.ni_vp);
    770 	if ((error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 1)) != 0)
    771 		return error;
    772 	return copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf));
    773 }
    774 
    775 /*
    776  * Get filesystem statistics.
    777  */
    778 /* ARGSUSED */
    779 int
    780 sys_fstatvfs1(struct lwp *l, void *v, register_t *retval)
    781 {
    782 	struct sys_fstatvfs1_args /* {
    783 		syscallarg(int) fd;
    784 		syscallarg(struct statvfs *) buf;
    785 		syscallarg(int) flags;
    786 	} */ *uap = v;
    787 	struct proc *p = l->l_proc;
    788 	struct file *fp;
    789 	struct mount *mp;
    790 	struct statvfs sbuf;
    791 	int error;
    792 
    793 	/* getvnode() will use the descriptor for us */
    794 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    795 		return (error);
    796 	mp = ((struct vnode *)fp->f_data)->v_mount;
    797 	if ((error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 1)) != 0)
    798 		goto out;
    799 	error = copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf));
    800  out:
    801 	FILE_UNUSE(fp, p);
    802 	return error;
    803 }
    804 
    805 
    806 /*
    807  * Get statistics on all filesystems.
    808  */
    809 int
    810 sys_getvfsstat(struct lwp *l, void *v, register_t *retval)
    811 {
    812 	struct sys_getvfsstat_args /* {
    813 		syscallarg(struct statvfs *) buf;
    814 		syscallarg(size_t) bufsize;
    815 		syscallarg(int) flags;
    816 	} */ *uap = v;
    817 	int root = 0;
    818 	struct proc *p = l->l_proc;
    819 	struct mount *mp, *nmp;
    820 	struct statvfs sbuf;
    821 	struct statvfs *sfsp;
    822 	size_t count, maxcount;
    823 	int error = 0;
    824 
    825 	maxcount = SCARG(uap, bufsize) / sizeof(struct statvfs);
    826 	sfsp = SCARG(uap, buf);
    827 	simple_lock(&mountlist_slock);
    828 	count = 0;
    829 	for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    830 	     mp = nmp) {
    831 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    832 			nmp = CIRCLEQ_NEXT(mp, mnt_list);
    833 			continue;
    834 		}
    835 		if (sfsp && count < maxcount) {
    836 			error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 0);
    837 			if (error) {
    838 				simple_lock(&mountlist_slock);
    839 				nmp = CIRCLEQ_NEXT(mp, mnt_list);
    840 				vfs_unbusy(mp);
    841 				continue;
    842 			}
    843 			error = copyout(&sbuf, sfsp, sizeof(*sfsp));
    844 			if (error) {
    845 				vfs_unbusy(mp);
    846 				return (error);
    847 			}
    848 			sfsp++;
    849 			root |= strcmp(sbuf.f_mntonname, "/") == 0;
    850 		}
    851 		count++;
    852 		simple_lock(&mountlist_slock);
    853 		nmp = CIRCLEQ_NEXT(mp, mnt_list);
    854 		vfs_unbusy(mp);
    855 	}
    856 	simple_unlock(&mountlist_slock);
    857 	if (root == 0 && p->p_cwdi->cwdi_rdir) {
    858 		/*
    859 		 * fake a root entry
    860 		 */
    861 		if ((error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount, &sbuf, p,
    862 		    SCARG(uap, flags), 1)) != 0)
    863 			return error;
    864 		if (sfsp)
    865 			error = copyout(&sbuf, sfsp, sizeof(*sfsp));
    866 		count++;
    867 	}
    868 	if (sfsp && count > maxcount)
    869 		*retval = maxcount;
    870 	else
    871 		*retval = count;
    872 	return error;
    873 }
    874 
    875 /*
    876  * Change current working directory to a given file descriptor.
    877  */
    878 /* ARGSUSED */
    879 int
    880 sys_fchdir(struct lwp *l, void *v, register_t *retval)
    881 {
    882 	struct sys_fchdir_args /* {
    883 		syscallarg(int) fd;
    884 	} */ *uap = v;
    885 	struct proc *p = l->l_proc;
    886 	struct filedesc *fdp = p->p_fd;
    887 	struct cwdinfo *cwdi = p->p_cwdi;
    888 	struct vnode *vp, *tdp;
    889 	struct mount *mp;
    890 	struct file *fp;
    891 	int error;
    892 
    893 	/* getvnode() will use the descriptor for us */
    894 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    895 		return (error);
    896 	vp = (struct vnode *)fp->f_data;
    897 
    898 	VREF(vp);
    899 	vn_lock(vp,  LK_EXCLUSIVE | LK_RETRY);
    900 	if (vp->v_type != VDIR)
    901 		error = ENOTDIR;
    902 	else
    903 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    904 	while (!error && (mp = vp->v_mountedhere) != NULL) {
    905 		if (vfs_busy(mp, 0, 0))
    906 			continue;
    907 		error = VFS_ROOT(mp, &tdp);
    908 		vfs_unbusy(mp);
    909 		if (error)
    910 			break;
    911 		vput(vp);
    912 		vp = tdp;
    913 	}
    914 	if (error) {
    915 		vput(vp);
    916 		goto out;
    917 	}
    918 	VOP_UNLOCK(vp, 0);
    919 
    920 	/*
    921 	 * Disallow changing to a directory not under the process's
    922 	 * current root directory (if there is one).
    923 	 */
    924 	if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, p)) {
    925 		vrele(vp);
    926 		error = EPERM;	/* operation not permitted */
    927 		goto out;
    928 	}
    929 
    930 	vrele(cwdi->cwdi_cdir);
    931 	cwdi->cwdi_cdir = vp;
    932  out:
    933 	FILE_UNUSE(fp, p);
    934 	return (error);
    935 }
    936 
    937 /*
    938  * Change this process's notion of the root directory to a given file
    939  * descriptor.
    940  */
    941 int
    942 sys_fchroot(struct lwp *l, void *v, register_t *retval)
    943 {
    944 	struct sys_fchroot_args *uap = v;
    945 	struct proc *p = l->l_proc;
    946 	struct filedesc *fdp = p->p_fd;
    947 	struct cwdinfo *cwdi = p->p_cwdi;
    948 	struct vnode	*vp;
    949 	struct file	*fp;
    950 	int		 error;
    951 
    952 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    953 		return error;
    954 	/* getvnode() will use the descriptor for us */
    955 	if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
    956 		return error;
    957 	vp = (struct vnode *) fp->f_data;
    958 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    959 	if (vp->v_type != VDIR)
    960 		error = ENOTDIR;
    961 	else
    962 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
    963 	VOP_UNLOCK(vp, 0);
    964 	if (error)
    965 		goto out;
    966 	VREF(vp);
    967 
    968 	/*
    969 	 * Prevent escaping from chroot by putting the root under
    970 	 * the working directory.  Silently chdir to / if we aren't
    971 	 * already there.
    972 	 */
    973 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
    974 		/*
    975 		 * XXX would be more failsafe to change directory to a
    976 		 * deadfs node here instead
    977 		 */
    978 		vrele(cwdi->cwdi_cdir);
    979 		VREF(vp);
    980 		cwdi->cwdi_cdir = vp;
    981 	}
    982 
    983 	if (cwdi->cwdi_rdir != NULL)
    984 		vrele(cwdi->cwdi_rdir);
    985 	cwdi->cwdi_rdir = vp;
    986  out:
    987 	FILE_UNUSE(fp, p);
    988 	return (error);
    989 }
    990 
    991 /*
    992  * Change current working directory (``.'').
    993  */
    994 /* ARGSUSED */
    995 int
    996 sys_chdir(struct lwp *l, void *v, register_t *retval)
    997 {
    998 	struct sys_chdir_args /* {
    999 		syscallarg(const char *) path;
   1000 	} */ *uap = v;
   1001 	struct proc *p = l->l_proc;
   1002 	struct cwdinfo *cwdi = p->p_cwdi;
   1003 	int error;
   1004 	struct nameidata nd;
   1005 
   1006 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1007 	    SCARG(uap, path), p);
   1008 	if ((error = change_dir(&nd, p)) != 0)
   1009 		return (error);
   1010 	vrele(cwdi->cwdi_cdir);
   1011 	cwdi->cwdi_cdir = nd.ni_vp;
   1012 	return (0);
   1013 }
   1014 
   1015 /*
   1016  * Change notion of root (``/'') directory.
   1017  */
   1018 /* ARGSUSED */
   1019 int
   1020 sys_chroot(struct lwp *l, void *v, register_t *retval)
   1021 {
   1022 	struct sys_chroot_args /* {
   1023 		syscallarg(const char *) path;
   1024 	} */ *uap = v;
   1025 	struct proc *p = l->l_proc;
   1026 	struct cwdinfo *cwdi = p->p_cwdi;
   1027 	struct vnode *vp;
   1028 	int error;
   1029 	struct nameidata nd;
   1030 
   1031 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1032 		return (error);
   1033 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1034 	    SCARG(uap, path), p);
   1035 	if ((error = change_dir(&nd, p)) != 0)
   1036 		return (error);
   1037 	if (cwdi->cwdi_rdir != NULL)
   1038 		vrele(cwdi->cwdi_rdir);
   1039 	vp = nd.ni_vp;
   1040 	cwdi->cwdi_rdir = vp;
   1041 
   1042 	/*
   1043 	 * Prevent escaping from chroot by putting the root under
   1044 	 * the working directory.  Silently chdir to / if we aren't
   1045 	 * already there.
   1046 	 */
   1047 	if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
   1048 		/*
   1049 		 * XXX would be more failsafe to change directory to a
   1050 		 * deadfs node here instead
   1051 		 */
   1052 		vrele(cwdi->cwdi_cdir);
   1053 		VREF(vp);
   1054 		cwdi->cwdi_cdir = vp;
   1055 	}
   1056 
   1057 	return (0);
   1058 }
   1059 
   1060 /*
   1061  * Common routine for chroot and chdir.
   1062  */
   1063 static int
   1064 change_dir(struct nameidata *ndp, struct proc *p)
   1065 {
   1066 	struct vnode *vp;
   1067 	int error;
   1068 
   1069 	if ((error = namei(ndp)) != 0)
   1070 		return (error);
   1071 	vp = ndp->ni_vp;
   1072 	if (vp->v_type != VDIR)
   1073 		error = ENOTDIR;
   1074 	else
   1075 		error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
   1076 
   1077 	if (error)
   1078 		vput(vp);
   1079 	else
   1080 		VOP_UNLOCK(vp, 0);
   1081 	return (error);
   1082 }
   1083 
   1084 /*
   1085  * Check permissions, allocate an open file structure,
   1086  * and call the device open routine if any.
   1087  */
   1088 int
   1089 sys_open(struct lwp *l, void *v, register_t *retval)
   1090 {
   1091 	struct sys_open_args /* {
   1092 		syscallarg(const char *) path;
   1093 		syscallarg(int) flags;
   1094 		syscallarg(int) mode;
   1095 	} */ *uap = v;
   1096 	struct proc *p = l->l_proc;
   1097 	struct cwdinfo *cwdi = p->p_cwdi;
   1098 	struct filedesc *fdp = p->p_fd;
   1099 	struct file *fp;
   1100 	struct vnode *vp;
   1101 	int flags, cmode;
   1102 	int type, indx, error;
   1103 	struct flock lf;
   1104 	struct nameidata nd;
   1105 
   1106 	flags = FFLAGS(SCARG(uap, flags));
   1107 	if ((flags & (FREAD | FWRITE)) == 0)
   1108 		return (EINVAL);
   1109 	/* falloc() will use the file descriptor for us */
   1110 	if ((error = falloc(p, &fp, &indx)) != 0)
   1111 		return (error);
   1112 	cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
   1113 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1114 	l->l_dupfd = -indx - 1;			/* XXX check for fdopen */
   1115 	if ((error = vn_open(&nd, flags, cmode)) != 0) {
   1116 		FILE_UNUSE(fp, p);
   1117 		fdp->fd_ofiles[indx] = NULL;
   1118 		ffree(fp);
   1119 		if ((error == EDUPFD || error == EMOVEFD) &&
   1120 		    l->l_dupfd >= 0 &&			/* XXX from fdopen */
   1121 		    (error =
   1122 			dupfdopen(p, indx, l->l_dupfd, flags, error)) == 0) {
   1123 			*retval = indx;
   1124 			return (0);
   1125 		}
   1126 		if (error == ERESTART)
   1127 			error = EINTR;
   1128 		fdremove(fdp, indx);
   1129 		return (error);
   1130 	}
   1131 	l->l_dupfd = 0;
   1132 	vp = nd.ni_vp;
   1133 	fp->f_flag = flags & FMASK;
   1134 	fp->f_type = DTYPE_VNODE;
   1135 	fp->f_ops = &vnops;
   1136 	fp->f_data = vp;
   1137 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1138 		lf.l_whence = SEEK_SET;
   1139 		lf.l_start = 0;
   1140 		lf.l_len = 0;
   1141 		if (flags & O_EXLOCK)
   1142 			lf.l_type = F_WRLCK;
   1143 		else
   1144 			lf.l_type = F_RDLCK;
   1145 		type = F_FLOCK;
   1146 		if ((flags & FNONBLOCK) == 0)
   1147 			type |= F_WAIT;
   1148 		VOP_UNLOCK(vp, 0);
   1149 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
   1150 		if (error) {
   1151 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1152 			FILE_UNUSE(fp, p);
   1153 			ffree(fp);
   1154 			fdremove(fdp, indx);
   1155 			return (error);
   1156 		}
   1157 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1158 		fp->f_flag |= FHASLOCK;
   1159 	}
   1160 	VOP_UNLOCK(vp, 0);
   1161 	*retval = indx;
   1162 	FILE_SET_MATURE(fp);
   1163 	FILE_UNUSE(fp, p);
   1164 	return (0);
   1165 }
   1166 
   1167 /*
   1168  * Get file handle system call
   1169  */
   1170 int
   1171 sys_getfh(struct lwp *l, void *v, register_t *retval)
   1172 {
   1173 	struct sys_getfh_args /* {
   1174 		syscallarg(char *) fname;
   1175 		syscallarg(fhandle_t *) fhp;
   1176 	} */ *uap = v;
   1177 	struct proc *p = l->l_proc;
   1178 	struct vnode *vp;
   1179 	fhandle_t fh;
   1180 	int error;
   1181 	struct nameidata nd;
   1182 
   1183 	/*
   1184 	 * Must be super user
   1185 	 */
   1186 	error = suser(p->p_ucred, &p->p_acflag);
   1187 	if (error)
   1188 		return (error);
   1189 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   1190 	    SCARG(uap, fname), p);
   1191 	error = namei(&nd);
   1192 	if (error)
   1193 		return (error);
   1194 	vp = nd.ni_vp;
   1195 	memset(&fh, 0, sizeof(fh));
   1196 	fh.fh_fsid = vp->v_mount->mnt_stat.f_fsidx;
   1197 	error = VFS_VPTOFH(vp, &fh.fh_fid);
   1198 	vput(vp);
   1199 	if (error)
   1200 		return (error);
   1201 	error = copyout(&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh));
   1202 	return (error);
   1203 }
   1204 
   1205 /*
   1206  * Open a file given a file handle.
   1207  *
   1208  * Check permissions, allocate an open file structure,
   1209  * and call the device open routine if any.
   1210  */
   1211 int
   1212 sys_fhopen(struct lwp *l, void *v, register_t *retval)
   1213 {
   1214 	struct sys_fhopen_args /* {
   1215 		syscallarg(const fhandle_t *) fhp;
   1216 		syscallarg(int) flags;
   1217 	} */ *uap = v;
   1218 	struct proc *p = l->l_proc;
   1219 	struct filedesc *fdp = p->p_fd;
   1220 	struct file *fp;
   1221 	struct vnode *vp = NULL;
   1222 	struct mount *mp;
   1223 	struct ucred *cred = p->p_ucred;
   1224 	int flags;
   1225 	struct file *nfp;
   1226 	int type, indx, error=0;
   1227 	struct flock lf;
   1228 	struct vattr va;
   1229 	fhandle_t fh;
   1230 
   1231 	/*
   1232 	 * Must be super user
   1233 	 */
   1234 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1235 		return (error);
   1236 
   1237 	flags = FFLAGS(SCARG(uap, flags));
   1238 	if ((flags & (FREAD | FWRITE)) == 0)
   1239 		return (EINVAL);
   1240 	if ((flags & O_CREAT))
   1241 		return (EINVAL);
   1242 	/* falloc() will use the file descriptor for us */
   1243 	if ((error = falloc(p, &nfp, &indx)) != 0)
   1244 		return (error);
   1245 	fp = nfp;
   1246 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1247 		goto bad;
   1248 
   1249 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
   1250 		error = ESTALE;
   1251 		goto bad;
   1252 	}
   1253 
   1254 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) {
   1255 		vp = NULL;	/* most likely unnecessary sanity for bad: */
   1256 		goto bad;
   1257 	}
   1258 
   1259 	/* Now do an effective vn_open */
   1260 
   1261 	if (vp->v_type == VSOCK) {
   1262 		error = EOPNOTSUPP;
   1263 		goto bad;
   1264 	}
   1265 	if (flags & FREAD) {
   1266 		if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
   1267 			goto bad;
   1268 	}
   1269 	if (flags & (FWRITE | O_TRUNC)) {
   1270 		if (vp->v_type == VDIR) {
   1271 			error = EISDIR;
   1272 			goto bad;
   1273 		}
   1274 		if ((error = vn_writechk(vp)) != 0 ||
   1275 		    (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
   1276 			goto bad;
   1277 	}
   1278 	if (flags & O_TRUNC) {
   1279 		if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
   1280 			goto bad;
   1281 		VOP_UNLOCK(vp, 0);			/* XXX */
   1282 		VOP_LEASE(vp, p, cred, LEASE_WRITE);
   1283 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);   /* XXX */
   1284 		VATTR_NULL(&va);
   1285 		va.va_size = 0;
   1286 		error = VOP_SETATTR(vp, &va, cred, p);
   1287 		vn_finished_write(mp, 0);
   1288 		if (error)
   1289 			goto bad;
   1290 	}
   1291 	if ((error = VOP_OPEN(vp, flags, cred, p)) != 0)
   1292 		goto bad;
   1293 	if (vp->v_type == VREG &&
   1294 	    uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
   1295 		error = EIO;
   1296 		goto bad;
   1297 	}
   1298 	if (flags & FWRITE)
   1299 		vp->v_writecount++;
   1300 
   1301 	/* done with modified vn_open, now finish what sys_open does. */
   1302 
   1303 	fp->f_flag = flags & FMASK;
   1304 	fp->f_type = DTYPE_VNODE;
   1305 	fp->f_ops = &vnops;
   1306 	fp->f_data = vp;
   1307 	if (flags & (O_EXLOCK | O_SHLOCK)) {
   1308 		lf.l_whence = SEEK_SET;
   1309 		lf.l_start = 0;
   1310 		lf.l_len = 0;
   1311 		if (flags & O_EXLOCK)
   1312 			lf.l_type = F_WRLCK;
   1313 		else
   1314 			lf.l_type = F_RDLCK;
   1315 		type = F_FLOCK;
   1316 		if ((flags & FNONBLOCK) == 0)
   1317 			type |= F_WAIT;
   1318 		VOP_UNLOCK(vp, 0);
   1319 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
   1320 		if (error) {
   1321 			(void) vn_close(vp, fp->f_flag, fp->f_cred, p);
   1322 			FILE_UNUSE(fp, p);
   1323 			ffree(fp);
   1324 			fdremove(fdp, indx);
   1325 			return (error);
   1326 		}
   1327 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1328 		fp->f_flag |= FHASLOCK;
   1329 	}
   1330 	VOP_UNLOCK(vp, 0);
   1331 	*retval = indx;
   1332 	FILE_SET_MATURE(fp);
   1333 	FILE_UNUSE(fp, p);
   1334 	return (0);
   1335 
   1336 bad:
   1337 	FILE_UNUSE(fp, p);
   1338 	ffree(fp);
   1339 	fdremove(fdp, indx);
   1340 	if (vp != NULL)
   1341 		vput(vp);
   1342 	return (error);
   1343 }
   1344 
   1345 /* ARGSUSED */
   1346 int
   1347 sys_fhstat(struct lwp *l, void *v, register_t *retval)
   1348 {
   1349 	struct sys_fhstat_args /* {
   1350 		syscallarg(const fhandle_t *) fhp;
   1351 		syscallarg(struct stat *) sb;
   1352 	} */ *uap = v;
   1353 	struct proc *p = l->l_proc;
   1354 	struct stat sb;
   1355 	int error;
   1356 	fhandle_t fh;
   1357 	struct mount *mp;
   1358 	struct vnode *vp;
   1359 
   1360 	/*
   1361 	 * Must be super user
   1362 	 */
   1363 	if ((error = suser(p->p_ucred, &p->p_acflag)))
   1364 		return (error);
   1365 
   1366 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1367 		return (error);
   1368 
   1369 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1370 		return (ESTALE);
   1371 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1372 		return (error);
   1373 	error = vn_stat(vp, &sb, p);
   1374 	vput(vp);
   1375 	if (error)
   1376 		return (error);
   1377 	error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
   1378 	return (error);
   1379 }
   1380 
   1381 /* ARGSUSED */
   1382 int
   1383 sys_fhstatvfs1(struct lwp *l, void *v, register_t *retval)
   1384 {
   1385 	struct sys_fhstatvfs1_args /* {
   1386 		syscallarg(const fhandle_t *) fhp;
   1387 		syscallarg(struct statvfs *) buf;
   1388 		syscallarg(int)	flags;
   1389 	} */ *uap = v;
   1390 	struct proc *p = l->l_proc;
   1391 	struct statvfs sbuf;
   1392 	fhandle_t fh;
   1393 	struct mount *mp;
   1394 	struct vnode *vp;
   1395 	int error;
   1396 
   1397 	/*
   1398 	 * Must be super user
   1399 	 */
   1400 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1401 		return error;
   1402 
   1403 	if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
   1404 		return error;
   1405 
   1406 	if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
   1407 		return ESTALE;
   1408 	if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
   1409 		return error;
   1410 
   1411 	mp = vp->v_mount;
   1412 	if ((error = dostatvfs(mp, &sbuf, p, SCARG(uap, flags), 1)) != 0) {
   1413 		vput(vp);
   1414 		return error;
   1415 	}
   1416 	vput(vp);
   1417 	return copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf));
   1418 }
   1419 
   1420 /*
   1421  * Create a special file.
   1422  */
   1423 /* ARGSUSED */
   1424 int
   1425 sys_mknod(struct lwp *l, void *v, register_t *retval)
   1426 {
   1427 	struct sys_mknod_args /* {
   1428 		syscallarg(const char *) path;
   1429 		syscallarg(int) mode;
   1430 		syscallarg(int) dev;
   1431 	} */ *uap = v;
   1432 	struct proc *p = l->l_proc;
   1433 	struct vnode *vp;
   1434 	struct mount *mp;
   1435 	struct vattr vattr;
   1436 	int error;
   1437 	int whiteout = 0;
   1438 	struct nameidata nd;
   1439 
   1440 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1441 		return (error);
   1442 restart:
   1443 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1444 	if ((error = namei(&nd)) != 0)
   1445 		return (error);
   1446 	vp = nd.ni_vp;
   1447 	if (vp != NULL)
   1448 		error = EEXIST;
   1449 	else {
   1450 		VATTR_NULL(&vattr);
   1451 		vattr.va_mode =
   1452 		    (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1453 		vattr.va_rdev = SCARG(uap, dev);
   1454 		whiteout = 0;
   1455 
   1456 		switch (SCARG(uap, mode) & S_IFMT) {
   1457 		case S_IFMT:	/* used by badsect to flag bad sectors */
   1458 			vattr.va_type = VBAD;
   1459 			break;
   1460 		case S_IFCHR:
   1461 			vattr.va_type = VCHR;
   1462 			break;
   1463 		case S_IFBLK:
   1464 			vattr.va_type = VBLK;
   1465 			break;
   1466 		case S_IFWHT:
   1467 			whiteout = 1;
   1468 			break;
   1469 		default:
   1470 			error = EINVAL;
   1471 			break;
   1472 		}
   1473 	}
   1474 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
   1475 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1476 		if (nd.ni_dvp == vp)
   1477 			vrele(nd.ni_dvp);
   1478 		else
   1479 			vput(nd.ni_dvp);
   1480 		if (vp)
   1481 			vrele(vp);
   1482 		if ((error = vn_start_write(NULL, &mp,
   1483 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   1484 			return (error);
   1485 		goto restart;
   1486 	}
   1487 	if (!error) {
   1488 		VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1489 		if (whiteout) {
   1490 			error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
   1491 			if (error)
   1492 				VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1493 			vput(nd.ni_dvp);
   1494 		} else {
   1495 			error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
   1496 						&nd.ni_cnd, &vattr);
   1497 			if (error == 0)
   1498 				vput(nd.ni_vp);
   1499 		}
   1500 	} else {
   1501 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1502 		if (nd.ni_dvp == vp)
   1503 			vrele(nd.ni_dvp);
   1504 		else
   1505 			vput(nd.ni_dvp);
   1506 		if (vp)
   1507 			vrele(vp);
   1508 	}
   1509 	vn_finished_write(mp, 0);
   1510 	return (error);
   1511 }
   1512 
   1513 /*
   1514  * Create a named pipe.
   1515  */
   1516 /* ARGSUSED */
   1517 int
   1518 sys_mkfifo(struct lwp *l, void *v, register_t *retval)
   1519 {
   1520 	struct sys_mkfifo_args /* {
   1521 		syscallarg(const char *) path;
   1522 		syscallarg(int) mode;
   1523 	} */ *uap = v;
   1524 	struct proc *p = l->l_proc;
   1525 	struct mount *mp;
   1526 	struct vattr vattr;
   1527 	int error;
   1528 	struct nameidata nd;
   1529 
   1530 restart:
   1531 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
   1532 	if ((error = namei(&nd)) != 0)
   1533 		return (error);
   1534 	if (nd.ni_vp != NULL) {
   1535 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1536 		if (nd.ni_dvp == nd.ni_vp)
   1537 			vrele(nd.ni_dvp);
   1538 		else
   1539 			vput(nd.ni_dvp);
   1540 		vrele(nd.ni_vp);
   1541 		return (EEXIST);
   1542 	}
   1543 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
   1544 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1545 		if (nd.ni_dvp == nd.ni_vp)
   1546 			vrele(nd.ni_dvp);
   1547 		else
   1548 			vput(nd.ni_dvp);
   1549 		if (nd.ni_vp)
   1550 			vrele(nd.ni_vp);
   1551 		if ((error = vn_start_write(NULL, &mp,
   1552 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   1553 			return (error);
   1554 		goto restart;
   1555 	}
   1556 	VATTR_NULL(&vattr);
   1557 	vattr.va_type = VFIFO;
   1558 	vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
   1559 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1560 	error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   1561 	if (error == 0)
   1562 		vput(nd.ni_vp);
   1563 	vn_finished_write(mp, 0);
   1564 	return (error);
   1565 }
   1566 
   1567 /*
   1568  * Make a hard file link.
   1569  */
   1570 /* ARGSUSED */
   1571 int
   1572 sys_link(struct lwp *l, void *v, register_t *retval)
   1573 {
   1574 	struct sys_link_args /* {
   1575 		syscallarg(const char *) path;
   1576 		syscallarg(const char *) link;
   1577 	} */ *uap = v;
   1578 	struct proc *p = l->l_proc;
   1579 	struct vnode *vp;
   1580 	struct mount *mp;
   1581 	struct nameidata nd;
   1582 	int error;
   1583 
   1584 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   1585 	if ((error = namei(&nd)) != 0)
   1586 		return (error);
   1587 	vp = nd.ni_vp;
   1588 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
   1589 		vrele(vp);
   1590 		return (error);
   1591 	}
   1592 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1593 	if ((error = namei(&nd)) != 0)
   1594 		goto out;
   1595 	if (nd.ni_vp) {
   1596 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1597 		if (nd.ni_dvp == nd.ni_vp)
   1598 			vrele(nd.ni_dvp);
   1599 		else
   1600 			vput(nd.ni_dvp);
   1601 		vrele(nd.ni_vp);
   1602 		error = EEXIST;
   1603 		goto out;
   1604 	}
   1605 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1606 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1607 	error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
   1608 out:
   1609 	vrele(vp);
   1610 	vn_finished_write(mp, 0);
   1611 	return (error);
   1612 }
   1613 
   1614 /*
   1615  * Make a symbolic link.
   1616  */
   1617 /* ARGSUSED */
   1618 int
   1619 sys_symlink(struct lwp *l, void *v, register_t *retval)
   1620 {
   1621 	struct sys_symlink_args /* {
   1622 		syscallarg(const char *) path;
   1623 		syscallarg(const char *) link;
   1624 	} */ *uap = v;
   1625 	struct proc *p = l->l_proc;
   1626 	struct mount *mp;
   1627 	struct vattr vattr;
   1628 	char *path;
   1629 	int error;
   1630 	struct nameidata nd;
   1631 
   1632 	path = PNBUF_GET();
   1633 	error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
   1634 	if (error)
   1635 		goto out;
   1636 restart:
   1637 	NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
   1638 	if ((error = namei(&nd)) != 0)
   1639 		goto out;
   1640 	if (nd.ni_vp) {
   1641 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1642 		if (nd.ni_dvp == nd.ni_vp)
   1643 			vrele(nd.ni_dvp);
   1644 		else
   1645 			vput(nd.ni_dvp);
   1646 		vrele(nd.ni_vp);
   1647 		error = EEXIST;
   1648 		goto out;
   1649 	}
   1650 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
   1651 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1652 		if (nd.ni_dvp == nd.ni_vp)
   1653 			vrele(nd.ni_dvp);
   1654 		else
   1655 			vput(nd.ni_dvp);
   1656 		if ((error = vn_start_write(NULL, &mp,
   1657 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   1658 			return (error);
   1659 		goto restart;
   1660 	}
   1661 	VATTR_NULL(&vattr);
   1662 	vattr.va_type = VLNK;
   1663 	vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
   1664 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1665 	error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
   1666 	if (error == 0)
   1667 		vput(nd.ni_vp);
   1668 	vn_finished_write(mp, 0);
   1669 out:
   1670 	PNBUF_PUT(path);
   1671 	return (error);
   1672 }
   1673 
   1674 /*
   1675  * Delete a whiteout from the filesystem.
   1676  */
   1677 /* ARGSUSED */
   1678 int
   1679 sys_undelete(struct lwp *l, void *v, register_t *retval)
   1680 {
   1681 	struct sys_undelete_args /* {
   1682 		syscallarg(const char *) path;
   1683 	} */ *uap = v;
   1684 	struct proc *p = l->l_proc;
   1685 	int error;
   1686 	struct mount *mp;
   1687 	struct nameidata nd;
   1688 
   1689 restart:
   1690 	NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
   1691 	    SCARG(uap, path), p);
   1692 	error = namei(&nd);
   1693 	if (error)
   1694 		return (error);
   1695 
   1696 	if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
   1697 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1698 		if (nd.ni_dvp == nd.ni_vp)
   1699 			vrele(nd.ni_dvp);
   1700 		else
   1701 			vput(nd.ni_dvp);
   1702 		if (nd.ni_vp)
   1703 			vrele(nd.ni_vp);
   1704 		return (EEXIST);
   1705 	}
   1706 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
   1707 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1708 		if (nd.ni_dvp == nd.ni_vp)
   1709 			vrele(nd.ni_dvp);
   1710 		else
   1711 			vput(nd.ni_dvp);
   1712 		if ((error = vn_start_write(NULL, &mp,
   1713 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   1714 			return (error);
   1715 		goto restart;
   1716 	}
   1717 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1718 	if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
   1719 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1720 	vput(nd.ni_dvp);
   1721 	vn_finished_write(mp, 0);
   1722 	return (error);
   1723 }
   1724 
   1725 /*
   1726  * Delete a name from the filesystem.
   1727  */
   1728 /* ARGSUSED */
   1729 int
   1730 sys_unlink(struct lwp *l, void *v, register_t *retval)
   1731 {
   1732 	struct sys_unlink_args /* {
   1733 		syscallarg(const char *) path;
   1734 	} */ *uap = v;
   1735 	struct proc *p = l->l_proc;
   1736 	struct mount *mp;
   1737 	struct vnode *vp;
   1738 	int error;
   1739 	struct nameidata nd;
   1740 
   1741 restart:
   1742 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   1743 	    SCARG(uap, path), p);
   1744 	if ((error = namei(&nd)) != 0)
   1745 		return (error);
   1746 	vp = nd.ni_vp;
   1747 
   1748 	/*
   1749 	 * The root of a mounted filesystem cannot be deleted.
   1750 	 */
   1751 	if (vp->v_flag & VROOT) {
   1752 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1753 		if (nd.ni_dvp == vp)
   1754 			vrele(nd.ni_dvp);
   1755 		else
   1756 			vput(nd.ni_dvp);
   1757 		vput(vp);
   1758 		error = EBUSY;
   1759 		goto out;
   1760 	}
   1761 
   1762 #ifdef VERIFIED_EXEC
   1763 	/* Handle remove requests for veriexec entries. */
   1764 	if ((error = veriexec_removechk(p, vp, nd.ni_dirp)) != 0) {
   1765 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1766 		if (nd.ni_dvp == vp)
   1767 			vrele(nd.ni_dvp);
   1768 		else
   1769 			vput(nd.ni_dvp);
   1770 		vput(vp);
   1771 		goto out;
   1772 	}
   1773 #endif /* VERIFIED_EXEC */
   1774 
   1775 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
   1776 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   1777 		if (nd.ni_dvp == vp)
   1778 			vrele(nd.ni_dvp);
   1779 		else
   1780 			vput(nd.ni_dvp);
   1781 		vput(vp);
   1782 		if ((error = vn_start_write(NULL, &mp,
   1783 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   1784 			return (error);
   1785 		goto restart;
   1786 	}
   1787 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   1788 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   1789 	error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   1790 	vn_finished_write(mp, 0);
   1791 out:
   1792 	return (error);
   1793 }
   1794 
   1795 /*
   1796  * Reposition read/write file offset.
   1797  */
   1798 int
   1799 sys_lseek(struct lwp *l, void *v, register_t *retval)
   1800 {
   1801 	struct sys_lseek_args /* {
   1802 		syscallarg(int) fd;
   1803 		syscallarg(int) pad;
   1804 		syscallarg(off_t) offset;
   1805 		syscallarg(int) whence;
   1806 	} */ *uap = v;
   1807 	struct proc *p = l->l_proc;
   1808 	struct ucred *cred = p->p_ucred;
   1809 	struct filedesc *fdp = p->p_fd;
   1810 	struct file *fp;
   1811 	struct vnode *vp;
   1812 	struct vattr vattr;
   1813 	off_t newoff;
   1814 	int error;
   1815 
   1816 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
   1817 		return (EBADF);
   1818 
   1819 	FILE_USE(fp);
   1820 
   1821 	vp = (struct vnode *)fp->f_data;
   1822 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1823 		error = ESPIPE;
   1824 		goto out;
   1825 	}
   1826 
   1827 	switch (SCARG(uap, whence)) {
   1828 	case SEEK_CUR:
   1829 		newoff = fp->f_offset + SCARG(uap, offset);
   1830 		break;
   1831 	case SEEK_END:
   1832 		error = VOP_GETATTR(vp, &vattr, cred, p);
   1833 		if (error)
   1834 			goto out;
   1835 		newoff = SCARG(uap, offset) + vattr.va_size;
   1836 		break;
   1837 	case SEEK_SET:
   1838 		newoff = SCARG(uap, offset);
   1839 		break;
   1840 	default:
   1841 		error = EINVAL;
   1842 		goto out;
   1843 	}
   1844 	if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
   1845 		goto out;
   1846 
   1847 	*(off_t *)retval = fp->f_offset = newoff;
   1848  out:
   1849 	FILE_UNUSE(fp, p);
   1850 	return (error);
   1851 }
   1852 
   1853 /*
   1854  * Positional read system call.
   1855  */
   1856 int
   1857 sys_pread(struct lwp *l, void *v, register_t *retval)
   1858 {
   1859 	struct sys_pread_args /* {
   1860 		syscallarg(int) fd;
   1861 		syscallarg(void *) buf;
   1862 		syscallarg(size_t) nbyte;
   1863 		syscallarg(off_t) offset;
   1864 	} */ *uap = v;
   1865 	struct proc *p = l->l_proc;
   1866 	struct filedesc *fdp = p->p_fd;
   1867 	struct file *fp;
   1868 	struct vnode *vp;
   1869 	off_t offset;
   1870 	int error, fd = SCARG(uap, fd);
   1871 
   1872 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1873 		return (EBADF);
   1874 
   1875 	if ((fp->f_flag & FREAD) == 0) {
   1876 		simple_unlock(&fp->f_slock);
   1877 		return (EBADF);
   1878 	}
   1879 
   1880 	FILE_USE(fp);
   1881 
   1882 	vp = (struct vnode *)fp->f_data;
   1883 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1884 		error = ESPIPE;
   1885 		goto out;
   1886 	}
   1887 
   1888 	offset = SCARG(uap, offset);
   1889 
   1890 	/*
   1891 	 * XXX This works because no file systems actually
   1892 	 * XXX take any action on the seek operation.
   1893 	 */
   1894 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1895 		goto out;
   1896 
   1897 	/* dofileread() will unuse the descriptor for us */
   1898 	return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   1899 	    &offset, 0, retval));
   1900 
   1901  out:
   1902 	FILE_UNUSE(fp, p);
   1903 	return (error);
   1904 }
   1905 
   1906 /*
   1907  * Positional scatter read system call.
   1908  */
   1909 int
   1910 sys_preadv(struct lwp *l, void *v, register_t *retval)
   1911 {
   1912 	struct sys_preadv_args /* {
   1913 		syscallarg(int) fd;
   1914 		syscallarg(const struct iovec *) iovp;
   1915 		syscallarg(int) iovcnt;
   1916 		syscallarg(off_t) offset;
   1917 	} */ *uap = v;
   1918 	struct proc *p = l->l_proc;
   1919 	struct filedesc *fdp = p->p_fd;
   1920 	struct file *fp;
   1921 	struct vnode *vp;
   1922 	off_t offset;
   1923 	int error, fd = SCARG(uap, fd);
   1924 
   1925 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1926 		return (EBADF);
   1927 
   1928 	if ((fp->f_flag & FREAD) == 0) {
   1929 		simple_unlock(&fp->f_slock);
   1930 		return (EBADF);
   1931 	}
   1932 
   1933 	FILE_USE(fp);
   1934 
   1935 	vp = (struct vnode *)fp->f_data;
   1936 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1937 		error = ESPIPE;
   1938 		goto out;
   1939 	}
   1940 
   1941 	offset = SCARG(uap, offset);
   1942 
   1943 	/*
   1944 	 * XXX This works because no file systems actually
   1945 	 * XXX take any action on the seek operation.
   1946 	 */
   1947 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   1948 		goto out;
   1949 
   1950 	/* dofilereadv() will unuse the descriptor for us */
   1951 	return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   1952 	    &offset, 0, retval));
   1953 
   1954  out:
   1955 	FILE_UNUSE(fp, p);
   1956 	return (error);
   1957 }
   1958 
   1959 /*
   1960  * Positional write system call.
   1961  */
   1962 int
   1963 sys_pwrite(struct lwp *l, void *v, register_t *retval)
   1964 {
   1965 	struct sys_pwrite_args /* {
   1966 		syscallarg(int) fd;
   1967 		syscallarg(const void *) buf;
   1968 		syscallarg(size_t) nbyte;
   1969 		syscallarg(off_t) offset;
   1970 	} */ *uap = v;
   1971 	struct proc *p = l->l_proc;
   1972 	struct filedesc *fdp = p->p_fd;
   1973 	struct file *fp;
   1974 	struct vnode *vp;
   1975 	off_t offset;
   1976 	int error, fd = SCARG(uap, fd);
   1977 
   1978 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1979 		return (EBADF);
   1980 
   1981 	if ((fp->f_flag & FWRITE) == 0) {
   1982 		simple_unlock(&fp->f_slock);
   1983 		return (EBADF);
   1984 	}
   1985 
   1986 	FILE_USE(fp);
   1987 
   1988 	vp = (struct vnode *)fp->f_data;
   1989 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   1990 		error = ESPIPE;
   1991 		goto out;
   1992 	}
   1993 
   1994 	offset = SCARG(uap, offset);
   1995 
   1996 	/*
   1997 	 * XXX This works because no file systems actually
   1998 	 * XXX take any action on the seek operation.
   1999 	 */
   2000 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   2001 		goto out;
   2002 
   2003 	/* dofilewrite() will unuse the descriptor for us */
   2004 	return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
   2005 	    &offset, 0, retval));
   2006 
   2007  out:
   2008 	FILE_UNUSE(fp, p);
   2009 	return (error);
   2010 }
   2011 
   2012 /*
   2013  * Positional gather write system call.
   2014  */
   2015 int
   2016 sys_pwritev(struct lwp *l, void *v, register_t *retval)
   2017 {
   2018 	struct sys_pwritev_args /* {
   2019 		syscallarg(int) fd;
   2020 		syscallarg(const struct iovec *) iovp;
   2021 		syscallarg(int) iovcnt;
   2022 		syscallarg(off_t) offset;
   2023 	} */ *uap = v;
   2024 	struct proc *p = l->l_proc;
   2025 	struct filedesc *fdp = p->p_fd;
   2026 	struct file *fp;
   2027 	struct vnode *vp;
   2028 	off_t offset;
   2029 	int error, fd = SCARG(uap, fd);
   2030 
   2031 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   2032 		return (EBADF);
   2033 
   2034 	if ((fp->f_flag & FWRITE) == 0) {
   2035 		simple_unlock(&fp->f_slock);
   2036 		return (EBADF);
   2037 	}
   2038 
   2039 	FILE_USE(fp);
   2040 
   2041 	vp = (struct vnode *)fp->f_data;
   2042 	if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
   2043 		error = ESPIPE;
   2044 		goto out;
   2045 	}
   2046 
   2047 	offset = SCARG(uap, offset);
   2048 
   2049 	/*
   2050 	 * XXX This works because no file systems actually
   2051 	 * XXX take any action on the seek operation.
   2052 	 */
   2053 	if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
   2054 		goto out;
   2055 
   2056 	/* dofilewritev() will unuse the descriptor for us */
   2057 	return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
   2058 	    &offset, 0, retval));
   2059 
   2060  out:
   2061 	FILE_UNUSE(fp, p);
   2062 	return (error);
   2063 }
   2064 
   2065 /*
   2066  * Check access permissions.
   2067  */
   2068 int
   2069 sys_access(struct lwp *l, void *v, register_t *retval)
   2070 {
   2071 	struct sys_access_args /* {
   2072 		syscallarg(const char *) path;
   2073 		syscallarg(int) flags;
   2074 	} */ *uap = v;
   2075 	struct proc *p = l->l_proc;
   2076 	struct ucred *cred;
   2077 	struct vnode *vp;
   2078 	int error, flags;
   2079 	struct nameidata nd;
   2080 
   2081 	cred = crdup(p->p_ucred);
   2082 	cred->cr_uid = p->p_cred->p_ruid;
   2083 	cred->cr_gid = p->p_cred->p_rgid;
   2084 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   2085 	    SCARG(uap, path), p);
   2086 	/* Override default credentials */
   2087 	nd.ni_cnd.cn_cred = cred;
   2088 	if ((error = namei(&nd)) != 0)
   2089 		goto out;
   2090 	vp = nd.ni_vp;
   2091 
   2092 	/* Flags == 0 means only check for existence. */
   2093 	if (SCARG(uap, flags)) {
   2094 		flags = 0;
   2095 		if (SCARG(uap, flags) & R_OK)
   2096 			flags |= VREAD;
   2097 		if (SCARG(uap, flags) & W_OK)
   2098 			flags |= VWRITE;
   2099 		if (SCARG(uap, flags) & X_OK)
   2100 			flags |= VEXEC;
   2101 
   2102 		error = VOP_ACCESS(vp, flags, cred, p);
   2103 		if (!error && (flags & VWRITE))
   2104 			error = vn_writechk(vp);
   2105 	}
   2106 	vput(vp);
   2107 out:
   2108 	crfree(cred);
   2109 	return (error);
   2110 }
   2111 
   2112 /*
   2113  * Get file status; this version follows links.
   2114  */
   2115 /* ARGSUSED */
   2116 int
   2117 sys___stat30(struct lwp *l, void *v, register_t *retval)
   2118 {
   2119 	struct sys___stat30_args /* {
   2120 		syscallarg(const char *) path;
   2121 		syscallarg(struct stat *) ub;
   2122 	} */ *uap = v;
   2123 	struct proc *p = l->l_proc;
   2124 	struct stat sb;
   2125 	int error;
   2126 	struct nameidata nd;
   2127 
   2128 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   2129 	    SCARG(uap, path), p);
   2130 	if ((error = namei(&nd)) != 0)
   2131 		return (error);
   2132 	error = vn_stat(nd.ni_vp, &sb, p);
   2133 	vput(nd.ni_vp);
   2134 	if (error)
   2135 		return (error);
   2136 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2137 	return (error);
   2138 }
   2139 
   2140 /*
   2141  * Get file status; this version does not follow links.
   2142  */
   2143 /* ARGSUSED */
   2144 int
   2145 sys___lstat30(struct lwp *l, void *v, register_t *retval)
   2146 {
   2147 	struct sys___lstat30_args /* {
   2148 		syscallarg(const char *) path;
   2149 		syscallarg(struct stat *) ub;
   2150 	} */ *uap = v;
   2151 	struct proc *p = l->l_proc;
   2152 	struct stat sb;
   2153 	int error;
   2154 	struct nameidata nd;
   2155 
   2156 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2157 	    SCARG(uap, path), p);
   2158 	if ((error = namei(&nd)) != 0)
   2159 		return (error);
   2160 	error = vn_stat(nd.ni_vp, &sb, p);
   2161 	vput(nd.ni_vp);
   2162 	if (error)
   2163 		return (error);
   2164 	error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
   2165 	return (error);
   2166 }
   2167 
   2168 /*
   2169  * Get configurable pathname variables.
   2170  */
   2171 /* ARGSUSED */
   2172 int
   2173 sys_pathconf(struct lwp *l, void *v, register_t *retval)
   2174 {
   2175 	struct sys_pathconf_args /* {
   2176 		syscallarg(const char *) path;
   2177 		syscallarg(int) name;
   2178 	} */ *uap = v;
   2179 	struct proc *p = l->l_proc;
   2180 	int error;
   2181 	struct nameidata nd;
   2182 
   2183 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
   2184 	    SCARG(uap, path), p);
   2185 	if ((error = namei(&nd)) != 0)
   2186 		return (error);
   2187 	error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
   2188 	vput(nd.ni_vp);
   2189 	return (error);
   2190 }
   2191 
   2192 /*
   2193  * Return target name of a symbolic link.
   2194  */
   2195 /* ARGSUSED */
   2196 int
   2197 sys_readlink(struct lwp *l, void *v, register_t *retval)
   2198 {
   2199 	struct sys_readlink_args /* {
   2200 		syscallarg(const char *) path;
   2201 		syscallarg(char *) buf;
   2202 		syscallarg(size_t) count;
   2203 	} */ *uap = v;
   2204 	struct proc *p = l->l_proc;
   2205 	struct vnode *vp;
   2206 	struct iovec aiov;
   2207 	struct uio auio;
   2208 	int error;
   2209 	struct nameidata nd;
   2210 
   2211 	NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
   2212 	    SCARG(uap, path), p);
   2213 	if ((error = namei(&nd)) != 0)
   2214 		return (error);
   2215 	vp = nd.ni_vp;
   2216 	if (vp->v_type != VLNK)
   2217 		error = EINVAL;
   2218 	else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
   2219 	    (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
   2220 		aiov.iov_base = SCARG(uap, buf);
   2221 		aiov.iov_len = SCARG(uap, count);
   2222 		auio.uio_iov = &aiov;
   2223 		auio.uio_iovcnt = 1;
   2224 		auio.uio_offset = 0;
   2225 		auio.uio_rw = UIO_READ;
   2226 		auio.uio_segflg = UIO_USERSPACE;
   2227 		auio.uio_procp = p;
   2228 		auio.uio_resid = SCARG(uap, count);
   2229 		error = VOP_READLINK(vp, &auio, p->p_ucred);
   2230 	}
   2231 	vput(vp);
   2232 	*retval = SCARG(uap, count) - auio.uio_resid;
   2233 	return (error);
   2234 }
   2235 
   2236 /*
   2237  * Change flags of a file given a path name.
   2238  */
   2239 /* ARGSUSED */
   2240 int
   2241 sys_chflags(struct lwp *l, void *v, register_t *retval)
   2242 {
   2243 	struct sys_chflags_args /* {
   2244 		syscallarg(const char *) path;
   2245 		syscallarg(u_long) flags;
   2246 	} */ *uap = v;
   2247 	struct proc *p = l->l_proc;
   2248 	struct vnode *vp;
   2249 	int error;
   2250 	struct nameidata nd;
   2251 
   2252 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2253 	if ((error = namei(&nd)) != 0)
   2254 		return (error);
   2255 	vp = nd.ni_vp;
   2256 	error = change_flags(vp, SCARG(uap, flags), p);
   2257 	vput(vp);
   2258 	return (error);
   2259 }
   2260 
   2261 /*
   2262  * Change flags of a file given a file descriptor.
   2263  */
   2264 /* ARGSUSED */
   2265 int
   2266 sys_fchflags(struct lwp *l, void *v, register_t *retval)
   2267 {
   2268 	struct sys_fchflags_args /* {
   2269 		syscallarg(int) fd;
   2270 		syscallarg(u_long) flags;
   2271 	} */ *uap = v;
   2272 	struct proc *p = l->l_proc;
   2273 	struct vnode *vp;
   2274 	struct file *fp;
   2275 	int error;
   2276 
   2277 	/* getvnode() will use the descriptor for us */
   2278 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2279 		return (error);
   2280 	vp = (struct vnode *)fp->f_data;
   2281 	error = change_flags(vp, SCARG(uap, flags), p);
   2282 	VOP_UNLOCK(vp, 0);
   2283 	FILE_UNUSE(fp, p);
   2284 	return (error);
   2285 }
   2286 
   2287 /*
   2288  * Change flags of a file given a path name; this version does
   2289  * not follow links.
   2290  */
   2291 int
   2292 sys_lchflags(struct lwp *l, void *v, register_t *retval)
   2293 {
   2294 	struct sys_lchflags_args /* {
   2295 		syscallarg(const char *) path;
   2296 		syscallarg(u_long) flags;
   2297 	} */ *uap = v;
   2298 	struct proc *p = l->l_proc;
   2299 	struct vnode *vp;
   2300 	int error;
   2301 	struct nameidata nd;
   2302 
   2303 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2304 	if ((error = namei(&nd)) != 0)
   2305 		return (error);
   2306 	vp = nd.ni_vp;
   2307 	error = change_flags(vp, SCARG(uap, flags), p);
   2308 	vput(vp);
   2309 	return (error);
   2310 }
   2311 
   2312 /*
   2313  * Common routine to change flags of a file.
   2314  */
   2315 int
   2316 change_flags(struct vnode *vp, u_long flags, struct proc *p)
   2317 {
   2318 	struct mount *mp;
   2319 	struct vattr vattr;
   2320 	int error;
   2321 
   2322 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
   2323 		return (error);
   2324 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2325 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2326 	/*
   2327 	 * Non-superusers cannot change the flags on devices, even if they
   2328 	 * own them.
   2329 	 */
   2330 	if (suser(p->p_ucred, &p->p_acflag) != 0) {
   2331 		if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2332 			goto out;
   2333 		if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
   2334 			error = EINVAL;
   2335 			goto out;
   2336 		}
   2337 	}
   2338 	VATTR_NULL(&vattr);
   2339 	vattr.va_flags = flags;
   2340 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2341 out:
   2342 	vn_finished_write(mp, 0);
   2343 	return (error);
   2344 }
   2345 
   2346 /*
   2347  * Change mode of a file given path name; this version follows links.
   2348  */
   2349 /* ARGSUSED */
   2350 int
   2351 sys_chmod(struct lwp *l, void *v, register_t *retval)
   2352 {
   2353 	struct sys_chmod_args /* {
   2354 		syscallarg(const char *) path;
   2355 		syscallarg(int) mode;
   2356 	} */ *uap = v;
   2357 	struct proc *p = l->l_proc;
   2358 	int error;
   2359 	struct nameidata nd;
   2360 
   2361 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2362 	if ((error = namei(&nd)) != 0)
   2363 		return (error);
   2364 
   2365 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2366 
   2367 	vrele(nd.ni_vp);
   2368 	return (error);
   2369 }
   2370 
   2371 /*
   2372  * Change mode of a file given a file descriptor.
   2373  */
   2374 /* ARGSUSED */
   2375 int
   2376 sys_fchmod(struct lwp *l, void *v, register_t *retval)
   2377 {
   2378 	struct sys_fchmod_args /* {
   2379 		syscallarg(int) fd;
   2380 		syscallarg(int) mode;
   2381 	} */ *uap = v;
   2382 	struct proc *p = l->l_proc;
   2383 	struct file *fp;
   2384 	int error;
   2385 
   2386 	/* getvnode() will use the descriptor for us */
   2387 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2388 		return (error);
   2389 
   2390 	error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
   2391 	FILE_UNUSE(fp, p);
   2392 	return (error);
   2393 }
   2394 
   2395 /*
   2396  * Change mode of a file given path name; this version does not follow links.
   2397  */
   2398 /* ARGSUSED */
   2399 int
   2400 sys_lchmod(struct lwp *l, void *v, register_t *retval)
   2401 {
   2402 	struct sys_lchmod_args /* {
   2403 		syscallarg(const char *) path;
   2404 		syscallarg(int) mode;
   2405 	} */ *uap = v;
   2406 	struct proc *p = l->l_proc;
   2407 	int error;
   2408 	struct nameidata nd;
   2409 
   2410 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2411 	if ((error = namei(&nd)) != 0)
   2412 		return (error);
   2413 
   2414 	error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
   2415 
   2416 	vrele(nd.ni_vp);
   2417 	return (error);
   2418 }
   2419 
   2420 /*
   2421  * Common routine to set mode given a vnode.
   2422  */
   2423 static int
   2424 change_mode(struct vnode *vp, int mode, struct proc *p)
   2425 {
   2426 	struct mount *mp;
   2427 	struct vattr vattr;
   2428 	int error;
   2429 
   2430 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
   2431 		return (error);
   2432 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2433 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2434 	VATTR_NULL(&vattr);
   2435 	vattr.va_mode = mode & ALLPERMS;
   2436 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2437 	VOP_UNLOCK(vp, 0);
   2438 	vn_finished_write(mp, 0);
   2439 	return (error);
   2440 }
   2441 
   2442 /*
   2443  * Set ownership given a path name; this version follows links.
   2444  */
   2445 /* ARGSUSED */
   2446 int
   2447 sys_chown(struct lwp *l, void *v, register_t *retval)
   2448 {
   2449 	struct sys_chown_args /* {
   2450 		syscallarg(const char *) path;
   2451 		syscallarg(uid_t) uid;
   2452 		syscallarg(gid_t) gid;
   2453 	} */ *uap = v;
   2454 	struct proc *p = l->l_proc;
   2455 	int error;
   2456 	struct nameidata nd;
   2457 
   2458 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2459 	if ((error = namei(&nd)) != 0)
   2460 		return (error);
   2461 
   2462 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2463 
   2464 	vrele(nd.ni_vp);
   2465 	return (error);
   2466 }
   2467 
   2468 /*
   2469  * Set ownership given a path name; this version follows links.
   2470  * Provides POSIX semantics.
   2471  */
   2472 /* ARGSUSED */
   2473 int
   2474 sys___posix_chown(struct lwp *l, void *v, register_t *retval)
   2475 {
   2476 	struct sys_chown_args /* {
   2477 		syscallarg(const char *) path;
   2478 		syscallarg(uid_t) uid;
   2479 		syscallarg(gid_t) gid;
   2480 	} */ *uap = v;
   2481 	struct proc *p = l->l_proc;
   2482 	int error;
   2483 	struct nameidata nd;
   2484 
   2485 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2486 	if ((error = namei(&nd)) != 0)
   2487 		return (error);
   2488 
   2489 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2490 
   2491 	vrele(nd.ni_vp);
   2492 	return (error);
   2493 }
   2494 
   2495 /*
   2496  * Set ownership given a file descriptor.
   2497  */
   2498 /* ARGSUSED */
   2499 int
   2500 sys_fchown(struct lwp *l, void *v, register_t *retval)
   2501 {
   2502 	struct sys_fchown_args /* {
   2503 		syscallarg(int) fd;
   2504 		syscallarg(uid_t) uid;
   2505 		syscallarg(gid_t) gid;
   2506 	} */ *uap = v;
   2507 	struct proc *p = l->l_proc;
   2508 	int error;
   2509 	struct file *fp;
   2510 
   2511 	/* getvnode() will use the descriptor for us */
   2512 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2513 		return (error);
   2514 
   2515 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2516 	    SCARG(uap, gid), p, 0);
   2517 	FILE_UNUSE(fp, p);
   2518 	return (error);
   2519 }
   2520 
   2521 /*
   2522  * Set ownership given a file descriptor, providing POSIX/XPG semantics.
   2523  */
   2524 /* ARGSUSED */
   2525 int
   2526 sys___posix_fchown(struct lwp *l, void *v, register_t *retval)
   2527 {
   2528 	struct sys_fchown_args /* {
   2529 		syscallarg(int) fd;
   2530 		syscallarg(uid_t) uid;
   2531 		syscallarg(gid_t) gid;
   2532 	} */ *uap = v;
   2533 	struct proc *p = l->l_proc;
   2534 	int error;
   2535 	struct file *fp;
   2536 
   2537 	/* getvnode() will use the descriptor for us */
   2538 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2539 		return (error);
   2540 
   2541 	error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
   2542 	    SCARG(uap, gid), p, 1);
   2543 	FILE_UNUSE(fp, p);
   2544 	return (error);
   2545 }
   2546 
   2547 /*
   2548  * Set ownership given a path name; this version does not follow links.
   2549  */
   2550 /* ARGSUSED */
   2551 int
   2552 sys_lchown(struct lwp *l, void *v, register_t *retval)
   2553 {
   2554 	struct sys_lchown_args /* {
   2555 		syscallarg(const char *) path;
   2556 		syscallarg(uid_t) uid;
   2557 		syscallarg(gid_t) gid;
   2558 	} */ *uap = v;
   2559 	struct proc *p = l->l_proc;
   2560 	int error;
   2561 	struct nameidata nd;
   2562 
   2563 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2564 	if ((error = namei(&nd)) != 0)
   2565 		return (error);
   2566 
   2567 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
   2568 
   2569 	vrele(nd.ni_vp);
   2570 	return (error);
   2571 }
   2572 
   2573 /*
   2574  * Set ownership given a path name; this version does not follow links.
   2575  * Provides POSIX/XPG semantics.
   2576  */
   2577 /* ARGSUSED */
   2578 int
   2579 sys___posix_lchown(struct lwp *l, void *v, register_t *retval)
   2580 {
   2581 	struct sys_lchown_args /* {
   2582 		syscallarg(const char *) path;
   2583 		syscallarg(uid_t) uid;
   2584 		syscallarg(gid_t) gid;
   2585 	} */ *uap = v;
   2586 	struct proc *p = l->l_proc;
   2587 	int error;
   2588 	struct nameidata nd;
   2589 
   2590 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2591 	if ((error = namei(&nd)) != 0)
   2592 		return (error);
   2593 
   2594 	error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
   2595 
   2596 	vrele(nd.ni_vp);
   2597 	return (error);
   2598 }
   2599 
   2600 /*
   2601  * Common routine to set ownership given a vnode.
   2602  */
   2603 static int
   2604 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct proc *p,
   2605     int posix_semantics)
   2606 {
   2607 	struct mount *mp;
   2608 	struct vattr vattr;
   2609 	mode_t newmode;
   2610 	int error;
   2611 
   2612 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
   2613 		return (error);
   2614 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2615 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2616 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   2617 		goto out;
   2618 
   2619 #define CHANGED(x) ((int)(x) != -1)
   2620 	newmode = vattr.va_mode;
   2621 	if (posix_semantics) {
   2622 		/*
   2623 		 * POSIX/XPG semantics: if the caller is not the super-user,
   2624 		 * clear set-user-id and set-group-id bits.  Both POSIX and
   2625 		 * the XPG consider the behaviour for calls by the super-user
   2626 		 * implementation-defined; we leave the set-user-id and set-
   2627 		 * group-id settings intact in that case.
   2628 		 */
   2629 		if (suser(p->p_ucred, NULL) != 0)
   2630 			newmode &= ~(S_ISUID | S_ISGID);
   2631 	} else {
   2632 		/*
   2633 		 * NetBSD semantics: when changing owner and/or group,
   2634 		 * clear the respective bit(s).
   2635 		 */
   2636 		if (CHANGED(uid))
   2637 			newmode &= ~S_ISUID;
   2638 		if (CHANGED(gid))
   2639 			newmode &= ~S_ISGID;
   2640 	}
   2641 	/* Update va_mode iff altered. */
   2642 	if (vattr.va_mode == newmode)
   2643 		newmode = VNOVAL;
   2644 
   2645 	VATTR_NULL(&vattr);
   2646 	vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
   2647 	vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
   2648 	vattr.va_mode = newmode;
   2649 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2650 #undef CHANGED
   2651 
   2652 out:
   2653 	VOP_UNLOCK(vp, 0);
   2654 	vn_finished_write(mp, 0);
   2655 	return (error);
   2656 }
   2657 
   2658 /*
   2659  * Set the access and modification times given a path name; this
   2660  * version follows links.
   2661  */
   2662 /* ARGSUSED */
   2663 int
   2664 sys_utimes(struct lwp *l, void *v, register_t *retval)
   2665 {
   2666 	struct sys_utimes_args /* {
   2667 		syscallarg(const char *) path;
   2668 		syscallarg(const struct timeval *) tptr;
   2669 	} */ *uap = v;
   2670 	struct proc *p = l->l_proc;
   2671 	int error;
   2672 	struct nameidata nd;
   2673 
   2674 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2675 	if ((error = namei(&nd)) != 0)
   2676 		return (error);
   2677 
   2678 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2679 
   2680 	vrele(nd.ni_vp);
   2681 	return (error);
   2682 }
   2683 
   2684 /*
   2685  * Set the access and modification times given a file descriptor.
   2686  */
   2687 /* ARGSUSED */
   2688 int
   2689 sys_futimes(struct lwp *l, void *v, register_t *retval)
   2690 {
   2691 	struct sys_futimes_args /* {
   2692 		syscallarg(int) fd;
   2693 		syscallarg(const struct timeval *) tptr;
   2694 	} */ *uap = v;
   2695 	struct proc *p = l->l_proc;
   2696 	int error;
   2697 	struct file *fp;
   2698 
   2699 	/* getvnode() will use the descriptor for us */
   2700 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2701 		return (error);
   2702 
   2703 	error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
   2704 	FILE_UNUSE(fp, p);
   2705 	return (error);
   2706 }
   2707 
   2708 /*
   2709  * Set the access and modification times given a path name; this
   2710  * version does not follow links.
   2711  */
   2712 /* ARGSUSED */
   2713 int
   2714 sys_lutimes(struct lwp *l, void *v, register_t *retval)
   2715 {
   2716 	struct sys_lutimes_args /* {
   2717 		syscallarg(const char *) path;
   2718 		syscallarg(const struct timeval *) tptr;
   2719 	} */ *uap = v;
   2720 	struct proc *p = l->l_proc;
   2721 	int error;
   2722 	struct nameidata nd;
   2723 
   2724 	NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2725 	if ((error = namei(&nd)) != 0)
   2726 		return (error);
   2727 
   2728 	error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
   2729 
   2730 	vrele(nd.ni_vp);
   2731 	return (error);
   2732 }
   2733 
   2734 /*
   2735  * Common routine to set access and modification times given a vnode.
   2736  */
   2737 static int
   2738 change_utimes(struct vnode *vp, const struct timeval *tptr, struct proc *p)
   2739 {
   2740 	struct timeval tv[2];
   2741 	struct mount *mp;
   2742 	struct vattr vattr;
   2743 	int error;
   2744 
   2745 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
   2746 		return (error);
   2747 	VATTR_NULL(&vattr);
   2748 	if (tptr == NULL) {
   2749 		microtime(&tv[0]);
   2750 		tv[1] = tv[0];
   2751 		vattr.va_vaflags |= VA_UTIMES_NULL;
   2752 	} else {
   2753 		error = copyin(tptr, tv, sizeof(tv));
   2754 		if (error)
   2755 			goto out;
   2756 	}
   2757 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2758 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2759 	vattr.va_atime.tv_sec = tv[0].tv_sec;
   2760 	vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
   2761 	vattr.va_mtime.tv_sec = tv[1].tv_sec;
   2762 	vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
   2763 	error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2764 	VOP_UNLOCK(vp, 0);
   2765 out:
   2766 	vn_finished_write(mp, 0);
   2767 	return (error);
   2768 }
   2769 
   2770 /*
   2771  * Truncate a file given its path name.
   2772  */
   2773 /* ARGSUSED */
   2774 int
   2775 sys_truncate(struct lwp *l, void *v, register_t *retval)
   2776 {
   2777 	struct sys_truncate_args /* {
   2778 		syscallarg(const char *) path;
   2779 		syscallarg(int) pad;
   2780 		syscallarg(off_t) length;
   2781 	} */ *uap = v;
   2782 	struct proc *p = l->l_proc;
   2783 	struct vnode *vp;
   2784 	struct mount *mp;
   2785 	struct vattr vattr;
   2786 	int error;
   2787 	struct nameidata nd;
   2788 
   2789 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   2790 	if ((error = namei(&nd)) != 0)
   2791 		return (error);
   2792 	vp = nd.ni_vp;
   2793 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
   2794 		vrele(vp);
   2795 		return (error);
   2796 	}
   2797 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2798 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2799 	if (vp->v_type == VDIR)
   2800 		error = EISDIR;
   2801 	else if ((error = vn_writechk(vp)) == 0 &&
   2802 	    (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
   2803 		VATTR_NULL(&vattr);
   2804 		vattr.va_size = SCARG(uap, length);
   2805 		error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
   2806 	}
   2807 	vput(vp);
   2808 	vn_finished_write(mp, 0);
   2809 	return (error);
   2810 }
   2811 
   2812 /*
   2813  * Truncate a file given a file descriptor.
   2814  */
   2815 /* ARGSUSED */
   2816 int
   2817 sys_ftruncate(struct lwp *l, void *v, register_t *retval)
   2818 {
   2819 	struct sys_ftruncate_args /* {
   2820 		syscallarg(int) fd;
   2821 		syscallarg(int) pad;
   2822 		syscallarg(off_t) length;
   2823 	} */ *uap = v;
   2824 	struct proc *p = l->l_proc;
   2825 	struct mount *mp;
   2826 	struct vattr vattr;
   2827 	struct vnode *vp;
   2828 	struct file *fp;
   2829 	int error;
   2830 
   2831 	/* getvnode() will use the descriptor for us */
   2832 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2833 		return (error);
   2834 	if ((fp->f_flag & FWRITE) == 0) {
   2835 		error = EINVAL;
   2836 		goto out;
   2837 	}
   2838 	vp = (struct vnode *)fp->f_data;
   2839 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
   2840 		FILE_UNUSE(fp, p);
   2841 		return (error);
   2842 	}
   2843 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   2844 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2845 	if (vp->v_type == VDIR)
   2846 		error = EISDIR;
   2847 	else if ((error = vn_writechk(vp)) == 0) {
   2848 		VATTR_NULL(&vattr);
   2849 		vattr.va_size = SCARG(uap, length);
   2850 		error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
   2851 	}
   2852 	VOP_UNLOCK(vp, 0);
   2853 	vn_finished_write(mp, 0);
   2854  out:
   2855 	FILE_UNUSE(fp, p);
   2856 	return (error);
   2857 }
   2858 
   2859 /*
   2860  * Sync an open file.
   2861  */
   2862 /* ARGSUSED */
   2863 int
   2864 sys_fsync(struct lwp *l, void *v, register_t *retval)
   2865 {
   2866 	struct sys_fsync_args /* {
   2867 		syscallarg(int) fd;
   2868 	} */ *uap = v;
   2869 	struct proc *p = l->l_proc;
   2870 	struct vnode *vp;
   2871 	struct mount *mp;
   2872 	struct file *fp;
   2873 	int error;
   2874 
   2875 	/* getvnode() will use the descriptor for us */
   2876 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2877 		return (error);
   2878 	vp = (struct vnode *)fp->f_data;
   2879 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
   2880 		FILE_UNUSE(fp, p);
   2881 		return (error);
   2882 	}
   2883 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2884 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, p);
   2885 	if (error == 0 && bioops.io_fsync != NULL &&
   2886 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   2887 		(*bioops.io_fsync)(vp, 0);
   2888 	VOP_UNLOCK(vp, 0);
   2889 	vn_finished_write(mp, 0);
   2890 	FILE_UNUSE(fp, p);
   2891 	return (error);
   2892 }
   2893 
   2894 /*
   2895  * Sync a range of file data.  API modeled after that found in AIX.
   2896  *
   2897  * FDATASYNC indicates that we need only save enough metadata to be able
   2898  * to re-read the written data.  Note we duplicate AIX's requirement that
   2899  * the file be open for writing.
   2900  */
   2901 /* ARGSUSED */
   2902 int
   2903 sys_fsync_range(struct lwp *l, void *v, register_t *retval)
   2904 {
   2905 	struct sys_fsync_range_args /* {
   2906 		syscallarg(int) fd;
   2907 		syscallarg(int) flags;
   2908 		syscallarg(off_t) start;
   2909 		syscallarg(off_t) length;
   2910 	} */ *uap = v;
   2911 	struct proc *p = l->l_proc;
   2912 	struct vnode *vp;
   2913 	struct file *fp;
   2914 	int flags, nflags;
   2915 	off_t s, e, len;
   2916 	int error;
   2917 
   2918 	/* getvnode() will use the descriptor for us */
   2919 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2920 		return (error);
   2921 
   2922 	if ((fp->f_flag & FWRITE) == 0) {
   2923 		FILE_UNUSE(fp, p);
   2924 		return (EBADF);
   2925 	}
   2926 
   2927 	flags = SCARG(uap, flags);
   2928 	if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
   2929 	    ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
   2930 		return (EINVAL);
   2931 	}
   2932 	/* Now set up the flags for value(s) to pass to VOP_FSYNC() */
   2933 	if (flags & FDATASYNC)
   2934 		nflags = FSYNC_DATAONLY | FSYNC_WAIT;
   2935 	else
   2936 		nflags = FSYNC_WAIT;
   2937 	if (flags & FDISKSYNC)
   2938 		nflags |= FSYNC_CACHE;
   2939 
   2940 	len = SCARG(uap, length);
   2941 	/* If length == 0, we do the whole file, and s = l = 0 will do that */
   2942 	if (len) {
   2943 		s = SCARG(uap, start);
   2944 		e = s + len;
   2945 		if (e < s) {
   2946 			FILE_UNUSE(fp, p);
   2947 			return (EINVAL);
   2948 		}
   2949 	} else {
   2950 		e = 0;
   2951 		s = 0;
   2952 	}
   2953 
   2954 	vp = (struct vnode *)fp->f_data;
   2955 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2956 	error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e, p);
   2957 
   2958 	if (error == 0 && bioops.io_fsync != NULL &&
   2959 	    vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
   2960 		(*bioops.io_fsync)(vp, nflags);
   2961 
   2962 	VOP_UNLOCK(vp, 0);
   2963 	FILE_UNUSE(fp, p);
   2964 	return (error);
   2965 }
   2966 
   2967 /*
   2968  * Sync the data of an open file.
   2969  */
   2970 /* ARGSUSED */
   2971 int
   2972 sys_fdatasync(struct lwp *l, void *v, register_t *retval)
   2973 {
   2974 	struct sys_fdatasync_args /* {
   2975 		syscallarg(int) fd;
   2976 	} */ *uap = v;
   2977 	struct proc *p = l->l_proc;
   2978 	struct vnode *vp;
   2979 	struct file *fp;
   2980 	int error;
   2981 
   2982 	/* getvnode() will use the descriptor for us */
   2983 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   2984 		return (error);
   2985 	if ((fp->f_flag & FWRITE) == 0) {
   2986 		FILE_UNUSE(fp, p);
   2987 		return (EBADF);
   2988 	}
   2989 	vp = (struct vnode *)fp->f_data;
   2990 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2991 	error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, p);
   2992 	VOP_UNLOCK(vp, 0);
   2993 	FILE_UNUSE(fp, p);
   2994 	return (error);
   2995 }
   2996 
   2997 /*
   2998  * Rename files, (standard) BSD semantics frontend.
   2999  */
   3000 /* ARGSUSED */
   3001 int
   3002 sys_rename(struct lwp *l, void *v, register_t *retval)
   3003 {
   3004 	struct sys_rename_args /* {
   3005 		syscallarg(const char *) from;
   3006 		syscallarg(const char *) to;
   3007 	} */ *uap = v;
   3008 	struct proc *p = l->l_proc;
   3009 
   3010 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
   3011 }
   3012 
   3013 /*
   3014  * Rename files, POSIX semantics frontend.
   3015  */
   3016 /* ARGSUSED */
   3017 int
   3018 sys___posix_rename(struct lwp *l, void *v, register_t *retval)
   3019 {
   3020 	struct sys___posix_rename_args /* {
   3021 		syscallarg(const char *) from;
   3022 		syscallarg(const char *) to;
   3023 	} */ *uap = v;
   3024 	struct proc *p = l->l_proc;
   3025 
   3026 	return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
   3027 }
   3028 
   3029 /*
   3030  * Rename files.  Source and destination must either both be directories,
   3031  * or both not be directories.  If target is a directory, it must be empty.
   3032  * If `from' and `to' refer to the same object, the value of the `retain'
   3033  * argument is used to determine whether `from' will be
   3034  *
   3035  * (retain == 0)	deleted unless `from' and `to' refer to the same
   3036  *			object in the file system's name space (BSD).
   3037  * (retain == 1)	always retained (POSIX).
   3038  */
   3039 static int
   3040 rename_files(const char *from, const char *to, struct proc *p, int retain)
   3041 {
   3042 	struct mount *mp = NULL;
   3043 	struct vnode *tvp, *fvp, *tdvp;
   3044 	struct nameidata fromnd, tond;
   3045 	int error;
   3046 
   3047 	NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
   3048 	    from, p);
   3049 	if ((error = namei(&fromnd)) != 0)
   3050 		return (error);
   3051 	fvp = fromnd.ni_vp;
   3052 	error = vn_start_write(fvp, &mp, V_WAIT | V_PCATCH);
   3053 	if (error != 0) {
   3054 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3055 		vrele(fromnd.ni_dvp);
   3056 		vrele(fvp);
   3057 		if (fromnd.ni_startdir)
   3058 			vrele(fromnd.ni_startdir);
   3059 		PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
   3060 		return (error);
   3061 	}
   3062 	NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
   3063 	    (fvp->v_type == VDIR ? CREATEDIR : 0), UIO_USERSPACE, to, p);
   3064 	if ((error = namei(&tond)) != 0) {
   3065 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3066 		vrele(fromnd.ni_dvp);
   3067 		vrele(fvp);
   3068 		goto out1;
   3069 	}
   3070 	tdvp = tond.ni_dvp;
   3071 	tvp = tond.ni_vp;
   3072 
   3073 	if (tvp != NULL) {
   3074 		if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
   3075 			error = ENOTDIR;
   3076 			goto out;
   3077 		} else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
   3078 			error = EISDIR;
   3079 			goto out;
   3080 		}
   3081 	}
   3082 
   3083 	if (fvp == tdvp)
   3084 		error = EINVAL;
   3085 
   3086 	/*
   3087 	 * Source and destination refer to the same object.
   3088 	 */
   3089 	if (fvp == tvp) {
   3090 		if (retain)
   3091 			error = -1;
   3092 		else if (fromnd.ni_dvp == tdvp &&
   3093 		    fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
   3094 		    !memcmp(fromnd.ni_cnd.cn_nameptr,
   3095 		          tond.ni_cnd.cn_nameptr,
   3096 		          fromnd.ni_cnd.cn_namelen))
   3097 		error = -1;
   3098 	}
   3099 
   3100 #ifdef VERIFIED_EXEC
   3101 	if (!error)
   3102 		error = veriexec_renamechk(fvp, fromnd.ni_dirp, tond.ni_dirp);
   3103 #endif /* VERIFIED_EXEC */
   3104 
   3105 out:
   3106 	if (!error) {
   3107 		VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
   3108 		if (fromnd.ni_dvp != tdvp)
   3109 			VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   3110 		if (tvp) {
   3111 			VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
   3112 		}
   3113 		error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
   3114 				   tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
   3115 	} else {
   3116 		VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
   3117 		if (tdvp == tvp)
   3118 			vrele(tdvp);
   3119 		else
   3120 			vput(tdvp);
   3121 		if (tvp)
   3122 			vput(tvp);
   3123 		VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
   3124 		vrele(fromnd.ni_dvp);
   3125 		vrele(fvp);
   3126 	}
   3127 	vrele(tond.ni_startdir);
   3128 	PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
   3129 out1:
   3130 	vn_finished_write(mp, 0);
   3131 	if (fromnd.ni_startdir)
   3132 		vrele(fromnd.ni_startdir);
   3133 	PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
   3134 	return (error == -1 ? 0 : error);
   3135 }
   3136 
   3137 /*
   3138  * Make a directory file.
   3139  */
   3140 /* ARGSUSED */
   3141 int
   3142 sys_mkdir(struct lwp *l, void *v, register_t *retval)
   3143 {
   3144 	struct sys_mkdir_args /* {
   3145 		syscallarg(const char *) path;
   3146 		syscallarg(int) mode;
   3147 	} */ *uap = v;
   3148 	struct proc *p = l->l_proc;
   3149 	struct mount *mp;
   3150 	struct vnode *vp;
   3151 	struct vattr vattr;
   3152 	int error;
   3153 	struct nameidata nd;
   3154 
   3155 restart:
   3156 	NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR, UIO_USERSPACE,
   3157 	    SCARG(uap, path), p);
   3158 	if ((error = namei(&nd)) != 0)
   3159 		return (error);
   3160 	vp = nd.ni_vp;
   3161 	if (vp != NULL) {
   3162 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3163 		if (nd.ni_dvp == vp)
   3164 			vrele(nd.ni_dvp);
   3165 		else
   3166 			vput(nd.ni_dvp);
   3167 		vrele(vp);
   3168 		return (EEXIST);
   3169 	}
   3170 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
   3171 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3172 		if (nd.ni_dvp == vp)
   3173 			vrele(nd.ni_dvp);
   3174 		else
   3175 			vput(nd.ni_dvp);
   3176 		if ((error = vn_start_write(NULL, &mp,
   3177 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   3178 			return (error);
   3179 		goto restart;
   3180 	}
   3181 	VATTR_NULL(&vattr);
   3182 	vattr.va_type = VDIR;
   3183 	vattr.va_mode =
   3184 	    (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
   3185 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   3186 	error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
   3187 	if (!error)
   3188 		vput(nd.ni_vp);
   3189 	vn_finished_write(mp, 0);
   3190 	return (error);
   3191 }
   3192 
   3193 /*
   3194  * Remove a directory file.
   3195  */
   3196 /* ARGSUSED */
   3197 int
   3198 sys_rmdir(struct lwp *l, void *v, register_t *retval)
   3199 {
   3200 	struct sys_rmdir_args /* {
   3201 		syscallarg(const char *) path;
   3202 	} */ *uap = v;
   3203 	struct proc *p = l->l_proc;
   3204 	struct mount *mp;
   3205 	struct vnode *vp;
   3206 	int error;
   3207 	struct nameidata nd;
   3208 
   3209 restart:
   3210 	NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
   3211 	    SCARG(uap, path), p);
   3212 	if ((error = namei(&nd)) != 0)
   3213 		return (error);
   3214 	vp = nd.ni_vp;
   3215 	if (vp->v_type != VDIR) {
   3216 		error = ENOTDIR;
   3217 		goto out;
   3218 	}
   3219 	/*
   3220 	 * No rmdir "." please.
   3221 	 */
   3222 	if (nd.ni_dvp == vp) {
   3223 		error = EINVAL;
   3224 		goto out;
   3225 	}
   3226 	/*
   3227 	 * The root of a mounted filesystem cannot be deleted.
   3228 	 */
   3229 	if (vp->v_flag & VROOT) {
   3230 		error = EBUSY;
   3231 		goto out;
   3232 	}
   3233 	if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
   3234 		VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3235 		if (nd.ni_dvp == vp)
   3236 			vrele(nd.ni_dvp);
   3237 		else
   3238 			vput(nd.ni_dvp);
   3239 		vput(vp);
   3240 		if ((error = vn_start_write(NULL, &mp,
   3241 		    V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
   3242 			return (error);
   3243 		goto restart;
   3244 	}
   3245 	VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
   3246 	VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
   3247 	error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
   3248 	vn_finished_write(mp, 0);
   3249 	return (error);
   3250 
   3251 out:
   3252 	VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
   3253 	if (nd.ni_dvp == vp)
   3254 		vrele(nd.ni_dvp);
   3255 	else
   3256 		vput(nd.ni_dvp);
   3257 	vput(vp);
   3258 	return (error);
   3259 }
   3260 
   3261 /*
   3262  * Read a block of directory entries in a file system independent format.
   3263  */
   3264 int
   3265 sys___getdents30(struct lwp *l, void *v, register_t *retval)
   3266 {
   3267 	struct sys___getdents30_args /* {
   3268 		syscallarg(int) fd;
   3269 		syscallarg(char *) buf;
   3270 		syscallarg(size_t) count;
   3271 	} */ *uap = v;
   3272 	struct proc *p = l->l_proc;
   3273 	struct file *fp;
   3274 	int error, done;
   3275 
   3276 	/* getvnode() will use the descriptor for us */
   3277 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
   3278 		return (error);
   3279 	if ((fp->f_flag & FREAD) == 0) {
   3280 		error = EBADF;
   3281 		goto out;
   3282 	}
   3283 	error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
   3284 			SCARG(uap, count), &done, p, 0, 0);
   3285 #ifdef KTRACE
   3286 	if (!error && KTRPOINT(p, KTR_GENIO)) {
   3287 		struct iovec iov;
   3288 		iov.iov_base = SCARG(uap, buf);
   3289 		iov.iov_len = done;
   3290 		ktrgenio(p, SCARG(uap, fd), UIO_READ, &iov, done, 0);
   3291 	}
   3292 #endif
   3293 	*retval = done;
   3294  out:
   3295 	FILE_UNUSE(fp, p);
   3296 	return (error);
   3297 }
   3298 
   3299 /*
   3300  * Set the mode mask for creation of filesystem nodes.
   3301  */
   3302 int
   3303 sys_umask(struct lwp *l, void *v, register_t *retval)
   3304 {
   3305 	struct sys_umask_args /* {
   3306 		syscallarg(mode_t) newmask;
   3307 	} */ *uap = v;
   3308 	struct proc *p = l->l_proc;
   3309 	struct cwdinfo *cwdi;
   3310 
   3311 	cwdi = p->p_cwdi;
   3312 	*retval = cwdi->cwdi_cmask;
   3313 	cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
   3314 	return (0);
   3315 }
   3316 
   3317 /*
   3318  * Void all references to file by ripping underlying filesystem
   3319  * away from vnode.
   3320  */
   3321 /* ARGSUSED */
   3322 int
   3323 sys_revoke(struct lwp *l, void *v, register_t *retval)
   3324 {
   3325 	struct sys_revoke_args /* {
   3326 		syscallarg(const char *) path;
   3327 	} */ *uap = v;
   3328 	struct proc *p = l->l_proc;
   3329 	struct mount *mp;
   3330 	struct vnode *vp;
   3331 	struct vattr vattr;
   3332 	int error;
   3333 	struct nameidata nd;
   3334 
   3335 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
   3336 	if ((error = namei(&nd)) != 0)
   3337 		return (error);
   3338 	vp = nd.ni_vp;
   3339 	if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
   3340 		goto out;
   3341 	if (p->p_ucred->cr_uid != vattr.va_uid &&
   3342 	    (error = suser(p->p_ucred, &p->p_acflag)) != 0)
   3343 		goto out;
   3344 	if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
   3345 		goto out;
   3346 	if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
   3347 		VOP_REVOKE(vp, REVOKEALL);
   3348 	vn_finished_write(mp, 0);
   3349 out:
   3350 	vrele(vp);
   3351 	return (error);
   3352 }
   3353 
   3354 /*
   3355  * Convert a user file descriptor to a kernel file entry.
   3356  */
   3357 int
   3358 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
   3359 {
   3360 	struct vnode *vp;
   3361 	struct file *fp;
   3362 
   3363 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   3364 		return (EBADF);
   3365 
   3366 	FILE_USE(fp);
   3367 
   3368 	if (fp->f_type != DTYPE_VNODE) {
   3369 		FILE_UNUSE(fp, NULL);
   3370 		return (EINVAL);
   3371 	}
   3372 
   3373 	vp = (struct vnode *)fp->f_data;
   3374 	if (vp->v_type == VBAD) {
   3375 		FILE_UNUSE(fp, NULL);
   3376 		return (EBADF);
   3377 	}
   3378 
   3379 	*fpp = fp;
   3380 	return (0);
   3381 }
   3382