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