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