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