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