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