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