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