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