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