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