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