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