Home | History | Annotate | Line # | Download | only in kern
vfs_vnode.c revision 1.105.2.7
      1 /*	$NetBSD: vfs_vnode.c,v 1.105.2.7 2020/01/25 22:38:51 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997-2011, 2019 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1989, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  * (c) UNIX System Laboratories, Inc.
     37  * All or some portions of this file are derived from material licensed
     38  * to the University of California by American Telephone and Telegraph
     39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  * the permission of UNIX System Laboratories, Inc.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
     67  */
     68 
     69 /*
     70  * The vnode cache subsystem.
     71  *
     72  * Life-cycle
     73  *
     74  *	Normally, there are two points where new vnodes are created:
     75  *	VOP_CREATE(9) and VOP_LOOKUP(9).  The life-cycle of a vnode
     76  *	starts in one of the following ways:
     77  *
     78  *	- Allocation, via vcache_get(9) or vcache_new(9).
     79  *	- Reclamation of inactive vnode, via vcache_vget(9).
     80  *
     81  *	Recycle from a free list, via getnewvnode(9) -> getcleanvnode(9)
     82  *	was another, traditional way.  Currently, only the draining thread
     83  *	recycles the vnodes.  This behaviour might be revisited.
     84  *
     85  *	The life-cycle ends when the last reference is dropped, usually
     86  *	in VOP_REMOVE(9).  In such case, VOP_INACTIVE(9) is called to inform
     87  *	the file system that vnode is inactive.  Via this call, file system
     88  *	indicates whether vnode can be recycled (usually, it checks its own
     89  *	references, e.g. count of links, whether the file was removed).
     90  *
     91  *	Depending on indication, vnode can be put into a free list (cache),
     92  *	or cleaned via vcache_reclaim, which calls VOP_RECLAIM(9) to
     93  *	disassociate underlying file system from the vnode, and finally
     94  *	destroyed.
     95  *
     96  * Vnode state
     97  *
     98  *	Vnode is always in one of six states:
     99  *	- MARKER	This is a marker vnode to help list traversal.  It
    100  *			will never change its state.
    101  *	- LOADING	Vnode is associating underlying file system and not
    102  *			yet ready to use.
    103  *	- LOADED	Vnode has associated underlying file system and is
    104  *			ready to use.
    105  *	- BLOCKED	Vnode is active but cannot get new references.
    106  *	- RECLAIMING	Vnode is disassociating from the underlying file
    107  *			system.
    108  *	- RECLAIMED	Vnode has disassociated from underlying file system
    109  *			and is dead.
    110  *
    111  *	Valid state changes are:
    112  *	LOADING -> LOADED
    113  *			Vnode has been initialised in vcache_get() or
    114  *			vcache_new() and is ready to use.
    115  *	LOADED -> RECLAIMING
    116  *			Vnode starts disassociation from underlying file
    117  *			system in vcache_reclaim().
    118  *	RECLAIMING -> RECLAIMED
    119  *			Vnode finished disassociation from underlying file
    120  *			system in vcache_reclaim().
    121  *	LOADED -> BLOCKED
    122  *			vcache_rekey*() is changing the vnode key.
    123  *	BLOCKED -> LOADED
    124  *			The block condition is over.
    125  *	LOADING -> RECLAIMED
    126  *			Either vcache_get() or vcache_new() failed to
    127  *			associate the underlying file system or vcache_rekey*()
    128  *			drops a vnode used as placeholder.
    129  *
    130  *	Of these states LOADING, BLOCKED and RECLAIMING are intermediate
    131  *	and it is possible to wait for state change.
    132  *
    133  *	State is protected with v_interlock with one exception:
    134  *	to change from LOADING both v_interlock and vcache_lock must be held
    135  *	so it is possible to check "state == LOADING" without holding
    136  *	v_interlock.  See vcache_get() for details.
    137  *
    138  * Reference counting
    139  *
    140  *	Vnode is considered active, if reference count (vnode_t::v_usecount)
    141  *	is non-zero.  It is maintained using: vref(9) and vrele(9), as well
    142  *	as vput(9), routines.  Common points holding references are e.g.
    143  *	file openings, current working directory, mount points, etc.
    144  *
    145  * Note on v_usecount & v_holdcnt and their locking
    146  *
    147  *	At nearly all points it is known that the counts could be zero,
    148  *	the vnode_t::v_interlock will be held.  To change the counts away
    149  *	from zero, the interlock must be held.  To change from a non-zero
    150  *	value to zero, again the interlock must be held.
    151  *
    152  *	Changing the usecount from a non-zero value to a non-zero value can
    153  *	safely be done using atomic operations, without the interlock held.
    154  */
    155 
    156 #include <sys/cdefs.h>
    157 __KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.105.2.7 2020/01/25 22:38:51 ad Exp $");
    158 
    159 #ifdef _KERNEL_OPT
    160 #include "opt_pax.h"
    161 #endif
    162 
    163 #include <sys/param.h>
    164 #include <sys/kernel.h>
    165 
    166 #include <sys/atomic.h>
    167 #include <sys/buf.h>
    168 #include <sys/conf.h>
    169 #include <sys/device.h>
    170 #include <sys/hash.h>
    171 #include <sys/kauth.h>
    172 #include <sys/kmem.h>
    173 #include <sys/kthread.h>
    174 #include <sys/module.h>
    175 #include <sys/mount.h>
    176 #include <sys/namei.h>
    177 #include <sys/pax.h>
    178 #include <sys/syscallargs.h>
    179 #include <sys/sysctl.h>
    180 #include <sys/systm.h>
    181 #include <sys/vnode_impl.h>
    182 #include <sys/wapbl.h>
    183 #include <sys/fstrans.h>
    184 
    185 #include <uvm/uvm.h>
    186 #include <uvm/uvm_readahead.h>
    187 #include <uvm/uvm_stat.h>
    188 
    189 /* Flags to vrelel. */
    190 #define	VRELEL_ASYNC	0x0001	/* Always defer to vrele thread. */
    191 
    192 #define	LRU_VRELE	0
    193 #define	LRU_FREE	1
    194 #define	LRU_HOLD	2
    195 #define	LRU_COUNT	3
    196 
    197 /*
    198  * There are three lru lists: one holds vnodes waiting for async release,
    199  * one is for vnodes which have no buffer/page references and one for those
    200  * which do (i.e.  v_holdcnt is non-zero).  We put the lists into a single,
    201  * private cache line as vnodes migrate between them while under the same
    202  * lock (vdrain_lock).
    203  */
    204 u_int			numvnodes		__cacheline_aligned;
    205 static vnodelst_t	lru_list[LRU_COUNT]	__cacheline_aligned;
    206 static kmutex_t		vdrain_lock		__cacheline_aligned;
    207 static kcondvar_t	vdrain_cv;
    208 static int		vdrain_gen;
    209 static kcondvar_t	vdrain_gen_cv;
    210 static bool		vdrain_retry;
    211 static lwp_t *		vdrain_lwp;
    212 SLIST_HEAD(hashhead, vnode_impl);
    213 static kmutex_t		vcache_lock		__cacheline_aligned;
    214 static kcondvar_t	vcache_cv;
    215 static u_int		vcache_hashsize;
    216 static u_long		vcache_hashmask;
    217 static struct hashhead	*vcache_hashtab;
    218 static pool_cache_t	vcache_pool;
    219 static void		lru_requeue(vnode_t *, vnodelst_t *);
    220 static vnodelst_t *	lru_which(vnode_t *);
    221 static vnode_impl_t *	vcache_alloc(void);
    222 static void		vcache_dealloc(vnode_impl_t *);
    223 static void		vcache_free(vnode_impl_t *);
    224 static void		vcache_init(void);
    225 static void		vcache_reinit(void);
    226 static void		vcache_reclaim(vnode_t *);
    227 static void		vrelel(vnode_t *, int, int);
    228 static void		vdrain_thread(void *);
    229 static void		vnpanic(vnode_t *, const char *, ...)
    230     __printflike(2, 3);
    231 
    232 /* Routines having to do with the management of the vnode table. */
    233 extern struct mount	*dead_rootmount;
    234 extern int		(**dead_vnodeop_p)(void *);
    235 extern int		(**spec_vnodeop_p)(void *);
    236 extern struct vfsops	dead_vfsops;
    237 
    238 /* Vnode state operations and diagnostics. */
    239 
    240 #if defined(DIAGNOSTIC)
    241 
    242 #define VSTATE_VALID(state) \
    243 	((state) != VS_ACTIVE && (state) != VS_MARKER)
    244 #define VSTATE_GET(vp) \
    245 	vstate_assert_get((vp), __func__, __LINE__)
    246 #define VSTATE_CHANGE(vp, from, to) \
    247 	vstate_assert_change((vp), (from), (to), __func__, __LINE__)
    248 #define VSTATE_WAIT_STABLE(vp) \
    249 	vstate_assert_wait_stable((vp), __func__, __LINE__)
    250 
    251 void
    252 _vstate_assert(vnode_t *vp, enum vnode_state state, const char *func, int line,
    253     bool has_lock)
    254 {
    255 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    256 
    257 	if (!has_lock) {
    258 		/*
    259 		 * Prevent predictive loads from the CPU, but check the state
    260 		 * without loooking first.
    261 		 */
    262 		membar_enter();
    263 		if (state == VS_ACTIVE && vp->v_usecount > 0 &&
    264 		    (vip->vi_state == VS_LOADED || vip->vi_state == VS_BLOCKED))
    265 			return;
    266 		if (vip->vi_state == state)
    267 			return;
    268 		mutex_enter((vp)->v_interlock);
    269 	}
    270 
    271 	KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
    272 
    273 	if ((state == VS_ACTIVE && vp->v_usecount > 0 &&
    274 	    (vip->vi_state == VS_LOADED || vip->vi_state == VS_BLOCKED)) ||
    275 	    vip->vi_state == state) {
    276 		if (!has_lock)
    277 			mutex_exit((vp)->v_interlock);
    278 		return;
    279 	}
    280 	vnpanic(vp, "state is %s, usecount %d, expected %s at %s:%d",
    281 	    vstate_name(vip->vi_state), vp->v_usecount,
    282 	    vstate_name(state), func, line);
    283 }
    284 
    285 static enum vnode_state
    286 vstate_assert_get(vnode_t *vp, const char *func, int line)
    287 {
    288 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    289 
    290 	KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
    291 	if (! VSTATE_VALID(vip->vi_state))
    292 		vnpanic(vp, "state is %s at %s:%d",
    293 		    vstate_name(vip->vi_state), func, line);
    294 
    295 	return vip->vi_state;
    296 }
    297 
    298 static void
    299 vstate_assert_wait_stable(vnode_t *vp, const char *func, int line)
    300 {
    301 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    302 
    303 	KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
    304 	if (! VSTATE_VALID(vip->vi_state))
    305 		vnpanic(vp, "state is %s at %s:%d",
    306 		    vstate_name(vip->vi_state), func, line);
    307 
    308 	while (vip->vi_state != VS_LOADED && vip->vi_state != VS_RECLAIMED)
    309 		cv_wait(&vp->v_cv, vp->v_interlock);
    310 
    311 	if (! VSTATE_VALID(vip->vi_state))
    312 		vnpanic(vp, "state is %s at %s:%d",
    313 		    vstate_name(vip->vi_state), func, line);
    314 }
    315 
    316 static void
    317 vstate_assert_change(vnode_t *vp, enum vnode_state from, enum vnode_state to,
    318     const char *func, int line)
    319 {
    320 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    321 
    322 	KASSERTMSG(mutex_owned(vp->v_interlock), "at %s:%d", func, line);
    323 	if (from == VS_LOADING)
    324 		KASSERTMSG(mutex_owned(&vcache_lock), "at %s:%d", func, line);
    325 
    326 	if (! VSTATE_VALID(from))
    327 		vnpanic(vp, "from is %s at %s:%d",
    328 		    vstate_name(from), func, line);
    329 	if (! VSTATE_VALID(to))
    330 		vnpanic(vp, "to is %s at %s:%d",
    331 		    vstate_name(to), func, line);
    332 	if (vip->vi_state != from)
    333 		vnpanic(vp, "from is %s, expected %s at %s:%d\n",
    334 		    vstate_name(vip->vi_state), vstate_name(from), func, line);
    335 	if ((from == VS_BLOCKED || to == VS_BLOCKED) && vp->v_usecount != 1)
    336 		vnpanic(vp, "%s to %s with usecount %d at %s:%d",
    337 		    vstate_name(from), vstate_name(to), vp->v_usecount,
    338 		    func, line);
    339 
    340 	vip->vi_state = to;
    341 	if (from == VS_LOADING)
    342 		cv_broadcast(&vcache_cv);
    343 	if (to == VS_LOADED || to == VS_RECLAIMED)
    344 		cv_broadcast(&vp->v_cv);
    345 }
    346 
    347 #else /* defined(DIAGNOSTIC) */
    348 
    349 #define VSTATE_GET(vp) \
    350 	(VNODE_TO_VIMPL((vp))->vi_state)
    351 #define VSTATE_CHANGE(vp, from, to) \
    352 	vstate_change((vp), (from), (to))
    353 #define VSTATE_WAIT_STABLE(vp) \
    354 	vstate_wait_stable((vp))
    355 void
    356 _vstate_assert(vnode_t *vp, enum vnode_state state, const char *func, int line,
    357     bool has_lock)
    358 {
    359 
    360 }
    361 
    362 static void
    363 vstate_wait_stable(vnode_t *vp)
    364 {
    365 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    366 
    367 	while (vip->vi_state != VS_LOADED && vip->vi_state != VS_RECLAIMED)
    368 		cv_wait(&vp->v_cv, vp->v_interlock);
    369 }
    370 
    371 static void
    372 vstate_change(vnode_t *vp, enum vnode_state from, enum vnode_state to)
    373 {
    374 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    375 
    376 	vip->vi_state = to;
    377 	if (from == VS_LOADING)
    378 		cv_broadcast(&vcache_cv);
    379 	if (to == VS_LOADED || to == VS_RECLAIMED)
    380 		cv_broadcast(&vp->v_cv);
    381 }
    382 
    383 #endif /* defined(DIAGNOSTIC) */
    384 
    385 void
    386 vfs_vnode_sysinit(void)
    387 {
    388 	int error __diagused, i;
    389 
    390 	dead_rootmount = vfs_mountalloc(&dead_vfsops, NULL);
    391 	KASSERT(dead_rootmount != NULL);
    392 	dead_rootmount->mnt_iflag |= IMNT_MPSAFE;
    393 
    394 	mutex_init(&vdrain_lock, MUTEX_DEFAULT, IPL_NONE);
    395 	for (i = 0; i < LRU_COUNT; i++) {
    396 		TAILQ_INIT(&lru_list[i]);
    397 	}
    398 	vcache_init();
    399 
    400 	cv_init(&vdrain_cv, "vdrain");
    401 	cv_init(&vdrain_gen_cv, "vdrainwt");
    402 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vdrain_thread,
    403 	    NULL, &vdrain_lwp, "vdrain");
    404 	KASSERTMSG((error == 0), "kthread_create(vdrain) failed: %d", error);
    405 }
    406 
    407 /*
    408  * Allocate a new marker vnode.
    409  */
    410 vnode_t *
    411 vnalloc_marker(struct mount *mp)
    412 {
    413 	vnode_impl_t *vip;
    414 	vnode_t *vp;
    415 
    416 	vip = pool_cache_get(vcache_pool, PR_WAITOK);
    417 	memset(vip, 0, sizeof(*vip));
    418 	vp = VIMPL_TO_VNODE(vip);
    419 	uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 0);
    420 	vp->v_mount = mp;
    421 	vp->v_type = VBAD;
    422 	vip->vi_state = VS_MARKER;
    423 
    424 	return vp;
    425 }
    426 
    427 /*
    428  * Free a marker vnode.
    429  */
    430 void
    431 vnfree_marker(vnode_t *vp)
    432 {
    433 	vnode_impl_t *vip;
    434 
    435 	vip = VNODE_TO_VIMPL(vp);
    436 	KASSERT(vip->vi_state == VS_MARKER);
    437 	uvm_obj_destroy(&vp->v_uobj, true);
    438 	pool_cache_put(vcache_pool, vip);
    439 }
    440 
    441 /*
    442  * Test a vnode for being a marker vnode.
    443  */
    444 bool
    445 vnis_marker(vnode_t *vp)
    446 {
    447 
    448 	return (VNODE_TO_VIMPL(vp)->vi_state == VS_MARKER);
    449 }
    450 
    451 /*
    452  * Return the lru list this node should be on.
    453  */
    454 static vnodelst_t *
    455 lru_which(vnode_t *vp)
    456 {
    457 
    458 	KASSERT(mutex_owned(vp->v_interlock));
    459 
    460 	if (vp->v_holdcnt > 0)
    461 		return &lru_list[LRU_HOLD];
    462 	else
    463 		return &lru_list[LRU_FREE];
    464 }
    465 
    466 /*
    467  * Put vnode to end of given list.
    468  * Both the current and the new list may be NULL, used on vnode alloc/free.
    469  * Adjust numvnodes and signal vdrain thread if there is work.
    470  */
    471 static void
    472 lru_requeue(vnode_t *vp, vnodelst_t *listhd)
    473 {
    474 	vnode_impl_t *vip;
    475 	int d;
    476 
    477 	/*
    478 	 * If the vnode is on the correct list, and was put there recently,
    479 	 * then leave it be, thus avoiding huge cache and lock contention.
    480 	 */
    481 	vip = VNODE_TO_VIMPL(vp);
    482 	if (listhd == vip->vi_lrulisthd &&
    483 	    (hardclock_ticks - vip->vi_lrulisttm) < hz) {
    484 	    	return;
    485 	}
    486 
    487 	mutex_enter(&vdrain_lock);
    488 	d = 0;
    489 	if (vip->vi_lrulisthd != NULL)
    490 		TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist);
    491 	else
    492 		d++;
    493 	vip->vi_lrulisthd = listhd;
    494 	vip->vi_lrulisttm = hardclock_ticks;
    495 	if (vip->vi_lrulisthd != NULL)
    496 		TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist);
    497 	else
    498 		d--;
    499 	if (d != 0) {
    500 		/*
    501 		 * Looks strange?  This is not a bug.  Don't store
    502 		 * numvnodes unless there is a change - avoid false
    503 		 * sharing on MP.
    504 		 */
    505 		numvnodes += d;
    506 	}
    507 	if (numvnodes > desiredvnodes || listhd == &lru_list[LRU_VRELE])
    508 		cv_broadcast(&vdrain_cv);
    509 	mutex_exit(&vdrain_lock);
    510 }
    511 
    512 /*
    513  * Release deferred vrele vnodes for this mount.
    514  * Called with file system suspended.
    515  */
    516 void
    517 vrele_flush(struct mount *mp)
    518 {
    519 	vnode_impl_t *vip, *marker;
    520 	vnode_t *vp;
    521 
    522 	KASSERT(fstrans_is_owner(mp));
    523 
    524 	marker = VNODE_TO_VIMPL(vnalloc_marker(NULL));
    525 
    526 	mutex_enter(&vdrain_lock);
    527 	TAILQ_INSERT_HEAD(&lru_list[LRU_VRELE], marker, vi_lrulist);
    528 
    529 	while ((vip = TAILQ_NEXT(marker, vi_lrulist))) {
    530 		TAILQ_REMOVE(&lru_list[LRU_VRELE], marker, vi_lrulist);
    531 		TAILQ_INSERT_AFTER(&lru_list[LRU_VRELE], vip, marker,
    532 		    vi_lrulist);
    533 		vp = VIMPL_TO_VNODE(vip);
    534 		if (vnis_marker(vp))
    535 			continue;
    536 
    537 		KASSERT(vip->vi_lrulisthd == &lru_list[LRU_VRELE]);
    538 		TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist);
    539 		vip->vi_lrulisthd = &lru_list[LRU_HOLD];
    540 		vip->vi_lrulisttm = hardclock_ticks;
    541 		TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist);
    542 		mutex_exit(&vdrain_lock);
    543 
    544 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    545 		mutex_enter(vp->v_interlock);
    546 		vrelel(vp, 0, LK_EXCLUSIVE);
    547 
    548 		mutex_enter(&vdrain_lock);
    549 	}
    550 
    551 	TAILQ_REMOVE(&lru_list[LRU_VRELE], marker, vi_lrulist);
    552 	mutex_exit(&vdrain_lock);
    553 
    554 	vnfree_marker(VIMPL_TO_VNODE(marker));
    555 }
    556 
    557 /*
    558  * Reclaim a cached vnode.  Used from vdrain_thread only.
    559  */
    560 static __inline void
    561 vdrain_remove(vnode_t *vp)
    562 {
    563 	struct mount *mp;
    564 
    565 	KASSERT(mutex_owned(&vdrain_lock));
    566 
    567 	/* Probe usecount (unlocked). */
    568 	if (vp->v_usecount > 0)
    569 		return;
    570 	/* Try v_interlock -- we lock the wrong direction! */
    571 	if (!mutex_tryenter(vp->v_interlock))
    572 		return;
    573 	/* Probe usecount and state. */
    574 	if (vp->v_usecount > 0 || VSTATE_GET(vp) != VS_LOADED) {
    575 		mutex_exit(vp->v_interlock);
    576 		return;
    577 	}
    578 	mp = vp->v_mount;
    579 	if (fstrans_start_nowait(mp) != 0) {
    580 		mutex_exit(vp->v_interlock);
    581 		return;
    582 	}
    583 	vdrain_retry = true;
    584 	mutex_exit(&vdrain_lock);
    585 
    586 	if (vcache_vget(vp) == 0) {
    587 		if (!vrecycle(vp)) {
    588 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    589 			mutex_enter(vp->v_interlock);
    590 			vrelel(vp, 0, LK_EXCLUSIVE);
    591 		}
    592 	}
    593 	fstrans_done(mp);
    594 
    595 	mutex_enter(&vdrain_lock);
    596 }
    597 
    598 /*
    599  * Release a cached vnode.  Used from vdrain_thread only.
    600  */
    601 static __inline void
    602 vdrain_vrele(vnode_t *vp)
    603 {
    604 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
    605 	struct mount *mp;
    606 
    607 	KASSERT(mutex_owned(&vdrain_lock));
    608 
    609 	mp = vp->v_mount;
    610 	if (fstrans_start_nowait(mp) != 0)
    611 		return;
    612 
    613 	/*
    614 	 * First remove the vnode from the vrele list.
    615 	 * Put it on the last lru list, the last vrele()
    616 	 * will put it back onto the right list before
    617 	 * its v_usecount reaches zero.
    618 	 */
    619 	KASSERT(vip->vi_lrulisthd == &lru_list[LRU_VRELE]);
    620 	TAILQ_REMOVE(vip->vi_lrulisthd, vip, vi_lrulist);
    621 	vip->vi_lrulisthd = &lru_list[LRU_HOLD];
    622 	vip->vi_lrulisttm = hardclock_ticks;
    623 	TAILQ_INSERT_TAIL(vip->vi_lrulisthd, vip, vi_lrulist);
    624 
    625 	vdrain_retry = true;
    626 	mutex_exit(&vdrain_lock);
    627 
    628 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    629 	mutex_enter(vp->v_interlock);
    630 	vrelel(vp, 0, LK_EXCLUSIVE);
    631 	fstrans_done(mp);
    632 
    633 	mutex_enter(&vdrain_lock);
    634 }
    635 
    636 /*
    637  * Helper thread to keep the number of vnodes below desiredvnodes
    638  * and release vnodes from asynchronous vrele.
    639  */
    640 static void
    641 vdrain_thread(void *cookie)
    642 {
    643 	int i;
    644 	u_int target;
    645 	vnode_impl_t *vip, *marker;
    646 
    647 	marker = VNODE_TO_VIMPL(vnalloc_marker(NULL));
    648 
    649 	mutex_enter(&vdrain_lock);
    650 
    651 	for (;;) {
    652 		vdrain_retry = false;
    653 		target = desiredvnodes - desiredvnodes/10;
    654 
    655 		for (i = 0; i < LRU_COUNT; i++) {
    656 			TAILQ_INSERT_HEAD(&lru_list[i], marker, vi_lrulist);
    657 			while ((vip = TAILQ_NEXT(marker, vi_lrulist))) {
    658 				TAILQ_REMOVE(&lru_list[i], marker, vi_lrulist);
    659 				TAILQ_INSERT_AFTER(&lru_list[i], vip, marker,
    660 				    vi_lrulist);
    661 				if (vnis_marker(VIMPL_TO_VNODE(vip)))
    662 					continue;
    663 				if (i == LRU_VRELE)
    664 					vdrain_vrele(VIMPL_TO_VNODE(vip));
    665 				else if (numvnodes < target)
    666 					break;
    667 				else
    668 					vdrain_remove(VIMPL_TO_VNODE(vip));
    669 			}
    670 			TAILQ_REMOVE(&lru_list[i], marker, vi_lrulist);
    671 		}
    672 
    673 		if (vdrain_retry) {
    674 			mutex_exit(&vdrain_lock);
    675 			yield();
    676 			mutex_enter(&vdrain_lock);
    677 		} else {
    678 			vdrain_gen++;
    679 			cv_broadcast(&vdrain_gen_cv);
    680 			cv_wait(&vdrain_cv, &vdrain_lock);
    681 		}
    682 	}
    683 }
    684 
    685 /*
    686  * Try to drop reference on a vnode.  Abort if we are releasing the
    687  * last reference.  Note: this _must_ succeed if not the last reference.
    688  */
    689 static bool
    690 vtryrele(vnode_t *vp)
    691 {
    692 	u_int use, next;
    693 
    694 	for (use = vp->v_usecount;; use = next) {
    695 		if (__predict_false(use == 1)) {
    696 			return false;
    697 		}
    698 		KASSERT(use > 1);
    699 		next = atomic_cas_uint(&vp->v_usecount, use, use - 1);
    700 		if (__predict_true(next == use)) {
    701 			return true;
    702 		}
    703 	}
    704 }
    705 
    706 /*
    707  * vput: unlock and release the reference.
    708  */
    709 void
    710 vput(vnode_t *vp)
    711 {
    712 	int lktype;
    713 
    714 	/*
    715 	 * Do an unlocked check of v_usecount.  If it looks like we're not
    716 	 * about to drop the last reference, then unlock the vnode and try
    717 	 * to drop the reference.  If it ends up being the last reference
    718 	 * after all, we dropped the lock when we shouldn't have.  vrelel()
    719 	 * can fix it all up.  Most of the time this will all go to plan.
    720 	 */
    721 	if (vp->v_usecount > 1) {
    722 		VOP_UNLOCK(vp);
    723 		if (vtryrele(vp)) {
    724 			return;
    725 		}
    726 		lktype = LK_NONE;
    727 	} else if ((vp->v_vflag & VV_LOCKSWORK) == 0) {
    728 		lktype = LK_EXCLUSIVE;
    729 	} else {
    730 		lktype = VOP_ISLOCKED(vp);
    731 		KASSERT(lktype != LK_NONE);
    732 	}
    733 	mutex_enter(vp->v_interlock);
    734 	vrelel(vp, 0, lktype);
    735 }
    736 
    737 /*
    738  * Vnode release.  If reference count drops to zero, call inactive
    739  * routine and either return to freelist or free to the pool.
    740  */
    741 static void
    742 vrelel(vnode_t *vp, int flags, int lktype)
    743 {
    744 	const bool async = ((flags & VRELEL_ASYNC) != 0);
    745 	bool recycle, defer;
    746 	int error;
    747 
    748 	KASSERT(mutex_owned(vp->v_interlock));
    749 
    750 	if (__predict_false(vp->v_op == dead_vnodeop_p &&
    751 	    VSTATE_GET(vp) != VS_RECLAIMED)) {
    752 		vnpanic(vp, "dead but not clean");
    753 	}
    754 
    755 	/*
    756 	 * If not the last reference, just drop the reference count
    757 	 * and unlock.
    758 	 */
    759 	if (vtryrele(vp)) {
    760 		if (lktype != LK_NONE) {
    761 			VOP_UNLOCK(vp);
    762 		}
    763 		mutex_exit(vp->v_interlock);
    764 		return;
    765 	}
    766 	if (vp->v_usecount <= 0 || vp->v_writecount != 0) {
    767 		vnpanic(vp, "%s: bad ref count", __func__);
    768 	}
    769 
    770 #ifdef DIAGNOSTIC
    771 	if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
    772 	    vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) {
    773 		vprint("vrelel: missing VOP_CLOSE()", vp);
    774 	}
    775 #endif
    776 
    777 	/*
    778 	 * First try to get the vnode locked for VOP_INACTIVE().
    779 	 * Defer vnode release to vdrain_thread if caller requests
    780 	 * it explicitly, is the pagedaemon or the lock failed.
    781 	 */
    782 	defer = false;
    783 	if ((curlwp == uvm.pagedaemon_lwp) || async) {
    784 		defer = true;
    785 	} else if (lktype == LK_SHARED) {
    786 		/* Excellent chance of getting, if the last ref. */
    787 		error = vn_lock(vp, LK_UPGRADE | LK_RETRY |
    788 		    LK_NOWAIT);
    789 		if (error != 0) {
    790 			defer = true;
    791 		} else {
    792 			lktype = LK_EXCLUSIVE;
    793 		}
    794 	} else if (lktype == LK_NONE) {
    795 		/* Excellent chance of getting, if the last ref. */
    796 		error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY |
    797 		    LK_NOWAIT);
    798 		if (error != 0) {
    799 			defer = true;
    800 		} else {
    801 			lktype = LK_EXCLUSIVE;
    802 		}
    803 	}
    804 	KASSERT(mutex_owned(vp->v_interlock));
    805 	if (defer) {
    806 		/*
    807 		 * Defer reclaim to the kthread; it's not safe to
    808 		 * clean it here.  We donate it our last reference.
    809 		 */
    810 		if (lktype != LK_NONE) {
    811 			VOP_UNLOCK(vp);
    812 		}
    813 		lru_requeue(vp, &lru_list[LRU_VRELE]);
    814 		mutex_exit(vp->v_interlock);
    815 		return;
    816 	}
    817 	KASSERT(lktype == LK_EXCLUSIVE);
    818 
    819 	/*
    820 	 * If not clean, deactivate the vnode, but preserve
    821 	 * our reference across the call to VOP_INACTIVE().
    822 	 */
    823 	if (VSTATE_GET(vp) == VS_RECLAIMED) {
    824 		VOP_UNLOCK(vp);
    825 	} else {
    826 		/*
    827 		 * If VOP_INACTIVE() indicates that the described file has
    828 		 * been deleted, then recycle the vnode.  Note that
    829 		 * VOP_INACTIVE() will not drop the vnode lock.
    830 		 *
    831 		 * If the file has been deleted, this is a lingering
    832 		 * reference and there is no need to worry about new
    833 		 * references looking to do real work with the vnode (as it
    834 		 * will have been purged from directories, caches, etc).
    835 		 */
    836 		recycle = false;
    837 		mutex_exit(vp->v_interlock);
    838 		VOP_INACTIVE(vp, &recycle);
    839 		mutex_enter(vp->v_interlock);
    840 		if (!recycle) {
    841 			VOP_UNLOCK(vp);
    842 			if (vtryrele(vp)) {
    843 				mutex_exit(vp->v_interlock);
    844 				return;
    845 			}
    846 		}
    847 
    848 		/* Take care of space accounting. */
    849 		if ((vp->v_iflag & VI_EXECMAP) != 0 &&
    850 		    vp->v_uobj.uo_npages != 0) {
    851 			cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages);
    852 			cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages);
    853 		}
    854 		vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
    855 		vp->v_vflag &= ~VV_MAPPED;
    856 
    857 		/*
    858 		 * Recycle the vnode if the file is now unused (unlinked),
    859 		 * otherwise just free it.
    860 		 */
    861 		if (recycle) {
    862 			VSTATE_ASSERT(vp, VS_LOADED);
    863 			/* vcache_reclaim drops the lock. */
    864 			vcache_reclaim(vp);
    865 		}
    866 		KASSERT(vp->v_usecount > 0);
    867 	}
    868 
    869 	if (atomic_dec_uint_nv(&vp->v_usecount) != 0) {
    870 		/* Gained another reference while being reclaimed. */
    871 		mutex_exit(vp->v_interlock);
    872 		return;
    873 	}
    874 
    875 	if (VSTATE_GET(vp) == VS_RECLAIMED && vp->v_holdcnt == 0) {
    876 		/*
    877 		 * It's clean so destroy it.  It isn't referenced
    878 		 * anywhere since it has been reclaimed.
    879 		 */
    880 		vcache_free(VNODE_TO_VIMPL(vp));
    881 	} else {
    882 		/*
    883 		 * Otherwise, put it back onto the freelist.  It
    884 		 * can't be destroyed while still associated with
    885 		 * a file system.
    886 		 */
    887 		lru_requeue(vp, lru_which(vp));
    888 		mutex_exit(vp->v_interlock);
    889 	}
    890 }
    891 
    892 void
    893 vrele(vnode_t *vp)
    894 {
    895 
    896 	if (vtryrele(vp)) {
    897 		return;
    898 	}
    899 	mutex_enter(vp->v_interlock);
    900 	vrelel(vp, 0, LK_NONE);
    901 }
    902 
    903 /*
    904  * Asynchronous vnode release, vnode is released in different context.
    905  */
    906 void
    907 vrele_async(vnode_t *vp)
    908 {
    909 
    910 	if (vtryrele(vp)) {
    911 		return;
    912 	}
    913 	mutex_enter(vp->v_interlock);
    914 	vrelel(vp, VRELEL_ASYNC, LK_NONE);
    915 }
    916 
    917 /*
    918  * Vnode reference, where a reference is already held by some other
    919  * object (for example, a file structure).
    920  *
    921  * NB: we have lockless code sequences that rely on this not blocking.
    922  */
    923 void
    924 vref(vnode_t *vp)
    925 {
    926 
    927 	KASSERT(vp->v_usecount != 0);
    928 
    929 	atomic_inc_uint(&vp->v_usecount);
    930 }
    931 
    932 /*
    933  * Page or buffer structure gets a reference.
    934  * Called with v_interlock held.
    935  */
    936 void
    937 vholdl(vnode_t *vp)
    938 {
    939 
    940 	KASSERT(mutex_owned(vp->v_interlock));
    941 
    942 	if (atomic_inc_uint_nv(&vp->v_holdcnt) == 1 && vp->v_usecount == 0)
    943 		lru_requeue(vp, lru_which(vp));
    944 }
    945 
    946 /*
    947  * Page or buffer structure gets a reference.
    948  */
    949 void
    950 vhold(vnode_t *vp)
    951 {
    952 	int hold, next;
    953 
    954 	for (hold = vp->v_holdcnt;; hold = next) {
    955 		if (__predict_false(hold == 0)) {
    956 			break;
    957 		}
    958 		next = atomic_cas_uint(&vp->v_holdcnt, hold, hold + 1);
    959 		if (__predict_true(next == hold)) {
    960 			return;
    961 		}
    962 	}
    963 
    964 	mutex_enter(vp->v_interlock);
    965 	vholdl(vp);
    966 	mutex_exit(vp->v_interlock);
    967 }
    968 
    969 /*
    970  * Page or buffer structure frees a reference.
    971  * Called with v_interlock held.
    972  */
    973 void
    974 holdrelel(vnode_t *vp)
    975 {
    976 
    977 	KASSERT(mutex_owned(vp->v_interlock));
    978 
    979 	if (vp->v_holdcnt <= 0) {
    980 		vnpanic(vp, "%s: holdcnt vp %p", __func__, vp);
    981 	}
    982 
    983 	if (atomic_dec_uint_nv(&vp->v_holdcnt) == 0 && vp->v_usecount == 0)
    984 		lru_requeue(vp, lru_which(vp));
    985 }
    986 
    987 /*
    988  * Page or buffer structure frees a reference.
    989  */
    990 void
    991 holdrele(vnode_t *vp)
    992 {
    993 	int hold, next;
    994 
    995 	for (hold = vp->v_holdcnt;; hold = next) {
    996 		if (__predict_false(hold == 1)) {
    997 			break;
    998 		}
    999 		KASSERT(hold > 1);
   1000 		next = atomic_cas_uint(&vp->v_holdcnt, hold, hold - 1);
   1001 		if (__predict_true(next == hold)) {
   1002 			return;
   1003 		}
   1004 	}
   1005 
   1006 	mutex_enter(vp->v_interlock);
   1007 	holdrelel(vp);
   1008 	mutex_exit(vp->v_interlock);
   1009 }
   1010 
   1011 /*
   1012  * Recycle an unused vnode if caller holds the last reference.
   1013  */
   1014 bool
   1015 vrecycle(vnode_t *vp)
   1016 {
   1017 	int error __diagused;
   1018 
   1019 	mutex_enter(vp->v_interlock);
   1020 
   1021 	/* Make sure we hold the last reference. */
   1022 	VSTATE_WAIT_STABLE(vp);
   1023 	if (vp->v_usecount != 1) {
   1024 		mutex_exit(vp->v_interlock);
   1025 		return false;
   1026 	}
   1027 
   1028 	/* If the vnode is already clean we're done. */
   1029 	if (VSTATE_GET(vp) != VS_LOADED) {
   1030 		VSTATE_ASSERT(vp, VS_RECLAIMED);
   1031 		vrelel(vp, 0, LK_NONE);
   1032 		return true;
   1033 	}
   1034 
   1035 	/* Prevent further references until the vnode is locked. */
   1036 	VSTATE_CHANGE(vp, VS_LOADED, VS_BLOCKED);
   1037 	mutex_exit(vp->v_interlock);
   1038 
   1039 	/*
   1040 	 * On a leaf file system this lock will always succeed as we hold
   1041 	 * the last reference and prevent further references.
   1042 	 * On layered file systems waiting for the lock would open a can of
   1043 	 * deadlocks as the lower vnodes may have other active references.
   1044 	 */
   1045 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWAIT);
   1046 
   1047 	mutex_enter(vp->v_interlock);
   1048 	VSTATE_CHANGE(vp, VS_BLOCKED, VS_LOADED);
   1049 
   1050 	if (error) {
   1051 		mutex_exit(vp->v_interlock);
   1052 		return false;
   1053 	}
   1054 
   1055 	KASSERT(vp->v_usecount == 1);
   1056 	vcache_reclaim(vp);
   1057 	vrelel(vp, 0, LK_NONE);
   1058 
   1059 	return true;
   1060 }
   1061 
   1062 /*
   1063  * Helper for vrevoke() to propagate suspension from lastmp
   1064  * to thismp.  Both args may be NULL.
   1065  * Returns the currently suspended file system or NULL.
   1066  */
   1067 static struct mount *
   1068 vrevoke_suspend_next(struct mount *lastmp, struct mount *thismp)
   1069 {
   1070 	int error;
   1071 
   1072 	if (lastmp == thismp)
   1073 		return thismp;
   1074 
   1075 	if (lastmp != NULL)
   1076 		vfs_resume(lastmp);
   1077 
   1078 	if (thismp == NULL)
   1079 		return NULL;
   1080 
   1081 	do {
   1082 		error = vfs_suspend(thismp, 0);
   1083 	} while (error == EINTR || error == ERESTART);
   1084 
   1085 	if (error == 0)
   1086 		return thismp;
   1087 
   1088 	KASSERT(error == EOPNOTSUPP);
   1089 	return NULL;
   1090 }
   1091 
   1092 /*
   1093  * Eliminate all activity associated with the requested vnode
   1094  * and with all vnodes aliased to the requested vnode.
   1095  */
   1096 void
   1097 vrevoke(vnode_t *vp)
   1098 {
   1099 	struct mount *mp;
   1100 	vnode_t *vq;
   1101 	enum vtype type;
   1102 	dev_t dev;
   1103 
   1104 	KASSERT(vp->v_usecount > 0);
   1105 
   1106 	mp = vrevoke_suspend_next(NULL, vp->v_mount);
   1107 
   1108 	mutex_enter(vp->v_interlock);
   1109 	VSTATE_WAIT_STABLE(vp);
   1110 	if (VSTATE_GET(vp) == VS_RECLAIMED) {
   1111 		mutex_exit(vp->v_interlock);
   1112 	} else if (vp->v_type != VBLK && vp->v_type != VCHR) {
   1113 		atomic_inc_uint(&vp->v_usecount);
   1114 		mutex_exit(vp->v_interlock);
   1115 		vgone(vp);
   1116 	} else {
   1117 		dev = vp->v_rdev;
   1118 		type = vp->v_type;
   1119 		mutex_exit(vp->v_interlock);
   1120 
   1121 		while (spec_node_lookup_by_dev(type, dev, &vq) == 0) {
   1122 			mp = vrevoke_suspend_next(mp, vq->v_mount);
   1123 			vgone(vq);
   1124 		}
   1125 	}
   1126 	vrevoke_suspend_next(mp, NULL);
   1127 }
   1128 
   1129 /*
   1130  * Eliminate all activity associated with a vnode in preparation for
   1131  * reuse.  Drops a reference from the vnode.
   1132  */
   1133 void
   1134 vgone(vnode_t *vp)
   1135 {
   1136 	int lktype;
   1137 
   1138 	KASSERT(vp->v_mount == dead_rootmount || fstrans_is_owner(vp->v_mount));
   1139 
   1140 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1141 	lktype = LK_EXCLUSIVE;
   1142 	mutex_enter(vp->v_interlock);
   1143 	VSTATE_WAIT_STABLE(vp);
   1144 	if (VSTATE_GET(vp) == VS_LOADED) {
   1145 		vcache_reclaim(vp);
   1146 		lktype = LK_NONE;
   1147 	}
   1148 	VSTATE_ASSERT(vp, VS_RECLAIMED);
   1149 	vrelel(vp, 0, lktype);
   1150 }
   1151 
   1152 static inline uint32_t
   1153 vcache_hash(const struct vcache_key *key)
   1154 {
   1155 	uint32_t hash = HASH32_BUF_INIT;
   1156 
   1157 	KASSERT(key->vk_key_len > 0);
   1158 
   1159 	hash = hash32_buf(&key->vk_mount, sizeof(struct mount *), hash);
   1160 	hash = hash32_buf(key->vk_key, key->vk_key_len, hash);
   1161 	return hash;
   1162 }
   1163 
   1164 static void
   1165 vcache_init(void)
   1166 {
   1167 
   1168 	vcache_pool = pool_cache_init(sizeof(vnode_impl_t), coherency_unit,
   1169 	    0, 0, "vcachepl", NULL, IPL_NONE, NULL, NULL, NULL);
   1170 	KASSERT(vcache_pool != NULL);
   1171 	mutex_init(&vcache_lock, MUTEX_DEFAULT, IPL_NONE);
   1172 	cv_init(&vcache_cv, "vcache");
   1173 	vcache_hashsize = desiredvnodes;
   1174 	vcache_hashtab = hashinit(desiredvnodes, HASH_SLIST, true,
   1175 	    &vcache_hashmask);
   1176 }
   1177 
   1178 static void
   1179 vcache_reinit(void)
   1180 {
   1181 	int i;
   1182 	uint32_t hash;
   1183 	u_long oldmask, newmask;
   1184 	struct hashhead *oldtab, *newtab;
   1185 	vnode_impl_t *vip;
   1186 
   1187 	newtab = hashinit(desiredvnodes, HASH_SLIST, true, &newmask);
   1188 	mutex_enter(&vcache_lock);
   1189 	oldtab = vcache_hashtab;
   1190 	oldmask = vcache_hashmask;
   1191 	vcache_hashsize = desiredvnodes;
   1192 	vcache_hashtab = newtab;
   1193 	vcache_hashmask = newmask;
   1194 	for (i = 0; i <= oldmask; i++) {
   1195 		while ((vip = SLIST_FIRST(&oldtab[i])) != NULL) {
   1196 			SLIST_REMOVE(&oldtab[i], vip, vnode_impl, vi_hash);
   1197 			hash = vcache_hash(&vip->vi_key);
   1198 			SLIST_INSERT_HEAD(&newtab[hash & vcache_hashmask],
   1199 			    vip, vi_hash);
   1200 		}
   1201 	}
   1202 	mutex_exit(&vcache_lock);
   1203 	hashdone(oldtab, HASH_SLIST, oldmask);
   1204 }
   1205 
   1206 static inline vnode_impl_t *
   1207 vcache_hash_lookup(const struct vcache_key *key, uint32_t hash)
   1208 {
   1209 	struct hashhead *hashp;
   1210 	vnode_impl_t *vip;
   1211 
   1212 	KASSERT(mutex_owned(&vcache_lock));
   1213 
   1214 	hashp = &vcache_hashtab[hash & vcache_hashmask];
   1215 	SLIST_FOREACH(vip, hashp, vi_hash) {
   1216 		if (key->vk_mount != vip->vi_key.vk_mount)
   1217 			continue;
   1218 		if (key->vk_key_len != vip->vi_key.vk_key_len)
   1219 			continue;
   1220 		if (memcmp(key->vk_key, vip->vi_key.vk_key, key->vk_key_len))
   1221 			continue;
   1222 		return vip;
   1223 	}
   1224 	return NULL;
   1225 }
   1226 
   1227 /*
   1228  * Allocate a new, uninitialized vcache node.
   1229  */
   1230 static vnode_impl_t *
   1231 vcache_alloc(void)
   1232 {
   1233 	vnode_impl_t *vip;
   1234 	vnode_t *vp;
   1235 
   1236 	vip = pool_cache_get(vcache_pool, PR_WAITOK);
   1237 	memset(vip, 0, sizeof(*vip));
   1238 
   1239 	rw_init(&vip->vi_lock);
   1240 
   1241 	vp = VIMPL_TO_VNODE(vip);
   1242 	uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 0);
   1243 	cv_init(&vp->v_cv, "vnode");
   1244 	cache_vnode_init(vp);
   1245 
   1246 	vp->v_usecount = 1;
   1247 	vp->v_type = VNON;
   1248 	vp->v_size = vp->v_writesize = VSIZENOTSET;
   1249 
   1250 	vip->vi_state = VS_LOADING;
   1251 
   1252 	lru_requeue(vp, &lru_list[LRU_FREE]);
   1253 
   1254 	return vip;
   1255 }
   1256 
   1257 /*
   1258  * Deallocate a vcache node in state VS_LOADING.
   1259  *
   1260  * vcache_lock held on entry and released on return.
   1261  */
   1262 static void
   1263 vcache_dealloc(vnode_impl_t *vip)
   1264 {
   1265 	vnode_t *vp;
   1266 
   1267 	KASSERT(mutex_owned(&vcache_lock));
   1268 
   1269 	vp = VIMPL_TO_VNODE(vip);
   1270 	vfs_ref(dead_rootmount);
   1271 	vfs_insmntque(vp, dead_rootmount);
   1272 	mutex_enter(vp->v_interlock);
   1273 	vp->v_op = dead_vnodeop_p;
   1274 	VSTATE_CHANGE(vp, VS_LOADING, VS_RECLAIMED);
   1275 	mutex_exit(&vcache_lock);
   1276 	vrelel(vp, 0, LK_NONE);
   1277 }
   1278 
   1279 /*
   1280  * Free an unused, unreferenced vcache node.
   1281  * v_interlock locked on entry.
   1282  */
   1283 static void
   1284 vcache_free(vnode_impl_t *vip)
   1285 {
   1286 	vnode_t *vp;
   1287 
   1288 	vp = VIMPL_TO_VNODE(vip);
   1289 	KASSERT(mutex_owned(vp->v_interlock));
   1290 
   1291 	KASSERT(vp->v_usecount == 0);
   1292 	KASSERT(vp->v_holdcnt == 0);
   1293 	KASSERT(vp->v_writecount == 0);
   1294 	lru_requeue(vp, NULL);
   1295 	mutex_exit(vp->v_interlock);
   1296 
   1297 	vfs_insmntque(vp, NULL);
   1298 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1299 		spec_node_destroy(vp);
   1300 
   1301 	rw_destroy(&vip->vi_lock);
   1302 	uvm_obj_destroy(&vp->v_uobj, true);
   1303 	cv_destroy(&vp->v_cv);
   1304 	cache_vnode_fini(vp);
   1305 	pool_cache_put(vcache_pool, vip);
   1306 }
   1307 
   1308 /*
   1309  * Try to get an initial reference on this cached vnode.
   1310  * Returns zero on success,  ENOENT if the vnode has been reclaimed and
   1311  * EBUSY if the vnode state is unstable.
   1312  *
   1313  * v_interlock locked on entry and unlocked on exit.
   1314  */
   1315 int
   1316 vcache_tryvget(vnode_t *vp)
   1317 {
   1318 	int error = 0;
   1319 
   1320 	KASSERT(mutex_owned(vp->v_interlock));
   1321 
   1322 	if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED))
   1323 		error = ENOENT;
   1324 	else if (__predict_false(VSTATE_GET(vp) != VS_LOADED))
   1325 		error = EBUSY;
   1326 	else if (vp->v_usecount == 0)
   1327 		vp->v_usecount = 1;
   1328 	else
   1329 		atomic_inc_uint(&vp->v_usecount);
   1330 
   1331 	mutex_exit(vp->v_interlock);
   1332 
   1333 	return error;
   1334 }
   1335 
   1336 /*
   1337  * Try to get an initial reference on this cached vnode.
   1338  * Returns zero on success and  ENOENT if the vnode has been reclaimed.
   1339  * Will wait for the vnode state to be stable.
   1340  *
   1341  * v_interlock locked on entry and unlocked on exit.
   1342  */
   1343 int
   1344 vcache_vget(vnode_t *vp)
   1345 {
   1346 
   1347 	KASSERT(mutex_owned(vp->v_interlock));
   1348 
   1349 	/* Increment hold count to prevent vnode from disappearing. */
   1350 	vp->v_holdcnt++;
   1351 	VSTATE_WAIT_STABLE(vp);
   1352 	vp->v_holdcnt--;
   1353 
   1354 	/* If this was the last reference to a reclaimed vnode free it now. */
   1355 	if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED)) {
   1356 		if (vp->v_holdcnt == 0 && vp->v_usecount == 0)
   1357 			vcache_free(VNODE_TO_VIMPL(vp));
   1358 		else
   1359 			mutex_exit(vp->v_interlock);
   1360 		return ENOENT;
   1361 	}
   1362 	VSTATE_ASSERT(vp, VS_LOADED);
   1363 	if (vp->v_usecount == 0)
   1364 		vp->v_usecount = 1;
   1365 	else
   1366 		atomic_inc_uint(&vp->v_usecount);
   1367 	mutex_exit(vp->v_interlock);
   1368 
   1369 	return 0;
   1370 }
   1371 
   1372 /*
   1373  * Get a vnode / fs node pair by key and return it referenced through vpp.
   1374  */
   1375 int
   1376 vcache_get(struct mount *mp, const void *key, size_t key_len,
   1377     struct vnode **vpp)
   1378 {
   1379 	int error;
   1380 	uint32_t hash;
   1381 	const void *new_key;
   1382 	struct vnode *vp;
   1383 	struct vcache_key vcache_key;
   1384 	vnode_impl_t *vip, *new_vip;
   1385 
   1386 	new_key = NULL;
   1387 	*vpp = NULL;
   1388 
   1389 	vcache_key.vk_mount = mp;
   1390 	vcache_key.vk_key = key;
   1391 	vcache_key.vk_key_len = key_len;
   1392 	hash = vcache_hash(&vcache_key);
   1393 
   1394 again:
   1395 	mutex_enter(&vcache_lock);
   1396 	vip = vcache_hash_lookup(&vcache_key, hash);
   1397 
   1398 	/* If found, take a reference or retry. */
   1399 	if (__predict_true(vip != NULL)) {
   1400 		/*
   1401 		 * If the vnode is loading we cannot take the v_interlock
   1402 		 * here as it might change during load (see uvm_obj_setlock()).
   1403 		 * As changing state from VS_LOADING requires both vcache_lock
   1404 		 * and v_interlock it is safe to test with vcache_lock held.
   1405 		 *
   1406 		 * Wait for vnodes changing state from VS_LOADING and retry.
   1407 		 */
   1408 		if (__predict_false(vip->vi_state == VS_LOADING)) {
   1409 			cv_wait(&vcache_cv, &vcache_lock);
   1410 			mutex_exit(&vcache_lock);
   1411 			goto again;
   1412 		}
   1413 		vp = VIMPL_TO_VNODE(vip);
   1414 		mutex_enter(vp->v_interlock);
   1415 		mutex_exit(&vcache_lock);
   1416 		error = vcache_vget(vp);
   1417 		if (error == ENOENT)
   1418 			goto again;
   1419 		if (error == 0)
   1420 			*vpp = vp;
   1421 		KASSERT((error != 0) == (*vpp == NULL));
   1422 		return error;
   1423 	}
   1424 	mutex_exit(&vcache_lock);
   1425 
   1426 	/* Allocate and initialize a new vcache / vnode pair. */
   1427 	error = vfs_busy(mp);
   1428 	if (error)
   1429 		return error;
   1430 	new_vip = vcache_alloc();
   1431 	new_vip->vi_key = vcache_key;
   1432 	vp = VIMPL_TO_VNODE(new_vip);
   1433 	mutex_enter(&vcache_lock);
   1434 	vip = vcache_hash_lookup(&vcache_key, hash);
   1435 	if (vip == NULL) {
   1436 		SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
   1437 		    new_vip, vi_hash);
   1438 		vip = new_vip;
   1439 	}
   1440 
   1441 	/* If another thread beat us inserting this node, retry. */
   1442 	if (vip != new_vip) {
   1443 		vcache_dealloc(new_vip);
   1444 		vfs_unbusy(mp);
   1445 		goto again;
   1446 	}
   1447 	mutex_exit(&vcache_lock);
   1448 
   1449 	/* Load the fs node.  Exclusive as new_node is VS_LOADING. */
   1450 	error = VFS_LOADVNODE(mp, vp, key, key_len, &new_key);
   1451 	if (error) {
   1452 		mutex_enter(&vcache_lock);
   1453 		SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
   1454 		    new_vip, vnode_impl, vi_hash);
   1455 		vcache_dealloc(new_vip);
   1456 		vfs_unbusy(mp);
   1457 		KASSERT(*vpp == NULL);
   1458 		return error;
   1459 	}
   1460 	KASSERT(new_key != NULL);
   1461 	KASSERT(memcmp(key, new_key, key_len) == 0);
   1462 	KASSERT(vp->v_op != NULL);
   1463 	vfs_insmntque(vp, mp);
   1464 	if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
   1465 		vp->v_vflag |= VV_MPSAFE;
   1466 	vfs_ref(mp);
   1467 	vfs_unbusy(mp);
   1468 
   1469 	/* Finished loading, finalize node. */
   1470 	mutex_enter(&vcache_lock);
   1471 	new_vip->vi_key.vk_key = new_key;
   1472 	mutex_enter(vp->v_interlock);
   1473 	VSTATE_CHANGE(vp, VS_LOADING, VS_LOADED);
   1474 	mutex_exit(vp->v_interlock);
   1475 	mutex_exit(&vcache_lock);
   1476 	*vpp = vp;
   1477 	return 0;
   1478 }
   1479 
   1480 /*
   1481  * Create a new vnode / fs node pair and return it referenced through vpp.
   1482  */
   1483 int
   1484 vcache_new(struct mount *mp, struct vnode *dvp, struct vattr *vap,
   1485     kauth_cred_t cred, void *extra, struct vnode **vpp)
   1486 {
   1487 	int error;
   1488 	uint32_t hash;
   1489 	struct vnode *vp, *ovp;
   1490 	vnode_impl_t *vip, *ovip;
   1491 
   1492 	*vpp = NULL;
   1493 
   1494 	/* Allocate and initialize a new vcache / vnode pair. */
   1495 	error = vfs_busy(mp);
   1496 	if (error)
   1497 		return error;
   1498 	vip = vcache_alloc();
   1499 	vip->vi_key.vk_mount = mp;
   1500 	vp = VIMPL_TO_VNODE(vip);
   1501 
   1502 	/* Create and load the fs node. */
   1503 	error = VFS_NEWVNODE(mp, dvp, vp, vap, cred, extra,
   1504 	    &vip->vi_key.vk_key_len, &vip->vi_key.vk_key);
   1505 	if (error) {
   1506 		mutex_enter(&vcache_lock);
   1507 		vcache_dealloc(vip);
   1508 		vfs_unbusy(mp);
   1509 		KASSERT(*vpp == NULL);
   1510 		return error;
   1511 	}
   1512 	KASSERT(vp->v_op != NULL);
   1513 	KASSERT((vip->vi_key.vk_key_len == 0) == (mp == dead_rootmount));
   1514 	if (vip->vi_key.vk_key_len > 0) {
   1515 		KASSERT(vip->vi_key.vk_key != NULL);
   1516 		hash = vcache_hash(&vip->vi_key);
   1517 
   1518 		/*
   1519 		 * Wait for previous instance to be reclaimed,
   1520 		 * then insert new node.
   1521 		 */
   1522 		mutex_enter(&vcache_lock);
   1523 		while ((ovip = vcache_hash_lookup(&vip->vi_key, hash))) {
   1524 			ovp = VIMPL_TO_VNODE(ovip);
   1525 			mutex_enter(ovp->v_interlock);
   1526 			mutex_exit(&vcache_lock);
   1527 			error = vcache_vget(ovp);
   1528 			KASSERT(error == ENOENT);
   1529 			mutex_enter(&vcache_lock);
   1530 		}
   1531 		SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
   1532 		    vip, vi_hash);
   1533 		mutex_exit(&vcache_lock);
   1534 	}
   1535 	vfs_insmntque(vp, mp);
   1536 	if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
   1537 		vp->v_vflag |= VV_MPSAFE;
   1538 	vfs_ref(mp);
   1539 	vfs_unbusy(mp);
   1540 
   1541 	/* Finished loading, finalize node. */
   1542 	mutex_enter(&vcache_lock);
   1543 	mutex_enter(vp->v_interlock);
   1544 	VSTATE_CHANGE(vp, VS_LOADING, VS_LOADED);
   1545 	mutex_exit(&vcache_lock);
   1546 	mutex_exit(vp->v_interlock);
   1547 	*vpp = vp;
   1548 	return 0;
   1549 }
   1550 
   1551 /*
   1552  * Prepare key change: update old cache nodes key and lock new cache node.
   1553  * Return an error if the new node already exists.
   1554  */
   1555 int
   1556 vcache_rekey_enter(struct mount *mp, struct vnode *vp,
   1557     const void *old_key, size_t old_key_len,
   1558     const void *new_key, size_t new_key_len)
   1559 {
   1560 	uint32_t old_hash, new_hash;
   1561 	struct vcache_key old_vcache_key, new_vcache_key;
   1562 	vnode_impl_t *vip, *new_vip;
   1563 
   1564 	old_vcache_key.vk_mount = mp;
   1565 	old_vcache_key.vk_key = old_key;
   1566 	old_vcache_key.vk_key_len = old_key_len;
   1567 	old_hash = vcache_hash(&old_vcache_key);
   1568 
   1569 	new_vcache_key.vk_mount = mp;
   1570 	new_vcache_key.vk_key = new_key;
   1571 	new_vcache_key.vk_key_len = new_key_len;
   1572 	new_hash = vcache_hash(&new_vcache_key);
   1573 
   1574 	new_vip = vcache_alloc();
   1575 	new_vip->vi_key = new_vcache_key;
   1576 
   1577 	/* Insert locked new node used as placeholder. */
   1578 	mutex_enter(&vcache_lock);
   1579 	vip = vcache_hash_lookup(&new_vcache_key, new_hash);
   1580 	if (vip != NULL) {
   1581 		vcache_dealloc(new_vip);
   1582 		return EEXIST;
   1583 	}
   1584 	SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
   1585 	    new_vip, vi_hash);
   1586 
   1587 	/* Replace old nodes key with the temporary copy. */
   1588 	vip = vcache_hash_lookup(&old_vcache_key, old_hash);
   1589 	KASSERT(vip != NULL);
   1590 	KASSERT(VIMPL_TO_VNODE(vip) == vp);
   1591 	KASSERT(vip->vi_key.vk_key != old_vcache_key.vk_key);
   1592 	vip->vi_key = old_vcache_key;
   1593 	mutex_exit(&vcache_lock);
   1594 	return 0;
   1595 }
   1596 
   1597 /*
   1598  * Key change complete: update old node and remove placeholder.
   1599  */
   1600 void
   1601 vcache_rekey_exit(struct mount *mp, struct vnode *vp,
   1602     const void *old_key, size_t old_key_len,
   1603     const void *new_key, size_t new_key_len)
   1604 {
   1605 	uint32_t old_hash, new_hash;
   1606 	struct vcache_key old_vcache_key, new_vcache_key;
   1607 	vnode_impl_t *vip, *new_vip;
   1608 	struct vnode *new_vp;
   1609 
   1610 	old_vcache_key.vk_mount = mp;
   1611 	old_vcache_key.vk_key = old_key;
   1612 	old_vcache_key.vk_key_len = old_key_len;
   1613 	old_hash = vcache_hash(&old_vcache_key);
   1614 
   1615 	new_vcache_key.vk_mount = mp;
   1616 	new_vcache_key.vk_key = new_key;
   1617 	new_vcache_key.vk_key_len = new_key_len;
   1618 	new_hash = vcache_hash(&new_vcache_key);
   1619 
   1620 	mutex_enter(&vcache_lock);
   1621 
   1622 	/* Lookup old and new node. */
   1623 	vip = vcache_hash_lookup(&old_vcache_key, old_hash);
   1624 	KASSERT(vip != NULL);
   1625 	KASSERT(VIMPL_TO_VNODE(vip) == vp);
   1626 
   1627 	new_vip = vcache_hash_lookup(&new_vcache_key, new_hash);
   1628 	KASSERT(new_vip != NULL);
   1629 	KASSERT(new_vip->vi_key.vk_key_len == new_key_len);
   1630 	new_vp = VIMPL_TO_VNODE(new_vip);
   1631 	mutex_enter(new_vp->v_interlock);
   1632 	VSTATE_ASSERT(VIMPL_TO_VNODE(new_vip), VS_LOADING);
   1633 	mutex_exit(new_vp->v_interlock);
   1634 
   1635 	/* Rekey old node and put it onto its new hashlist. */
   1636 	vip->vi_key = new_vcache_key;
   1637 	if (old_hash != new_hash) {
   1638 		SLIST_REMOVE(&vcache_hashtab[old_hash & vcache_hashmask],
   1639 		    vip, vnode_impl, vi_hash);
   1640 		SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
   1641 		    vip, vi_hash);
   1642 	}
   1643 
   1644 	/* Remove new node used as placeholder. */
   1645 	SLIST_REMOVE(&vcache_hashtab[new_hash & vcache_hashmask],
   1646 	    new_vip, vnode_impl, vi_hash);
   1647 	vcache_dealloc(new_vip);
   1648 }
   1649 
   1650 /*
   1651  * Disassociate the underlying file system from a vnode.
   1652  *
   1653  * Must be called with vnode locked and will return unlocked.
   1654  * Must be called with the interlock held, and will return with it held.
   1655  */
   1656 static void
   1657 vcache_reclaim(vnode_t *vp)
   1658 {
   1659 	lwp_t *l = curlwp;
   1660 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
   1661 	struct mount *mp = vp->v_mount;
   1662 	uint32_t hash;
   1663 	uint8_t temp_buf[64], *temp_key;
   1664 	size_t temp_key_len;
   1665 	bool recycle, active;
   1666 	int error;
   1667 
   1668 	KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
   1669 	    VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1670 	KASSERT(mutex_owned(vp->v_interlock));
   1671 	KASSERT(vp->v_usecount != 0);
   1672 
   1673 	active = (vp->v_usecount > 1);
   1674 	temp_key_len = vip->vi_key.vk_key_len;
   1675 	/*
   1676 	 * Prevent the vnode from being recycled or brought into use
   1677 	 * while we clean it out.
   1678 	 */
   1679 	VSTATE_CHANGE(vp, VS_LOADED, VS_RECLAIMING);
   1680 	if ((vp->v_iflag & VI_EXECMAP) != 0 && vp->v_uobj.uo_npages != 0) {
   1681 		cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages);
   1682 		cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages);
   1683 	}
   1684 	vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
   1685 	mutex_exit(vp->v_interlock);
   1686 
   1687 	/*
   1688 	 * With vnode state set to reclaiming, purge name cache immediately
   1689 	 * to prevent new handles on vnode, and wait for existing threads
   1690 	 * trying to get a handle to notice VS_RECLAIMED status and abort.
   1691 	 */
   1692 	cache_purge(vp);
   1693 
   1694 	/* Replace the vnode key with a temporary copy. */
   1695 	if (vip->vi_key.vk_key_len > sizeof(temp_buf)) {
   1696 		temp_key = kmem_alloc(temp_key_len, KM_SLEEP);
   1697 	} else {
   1698 		temp_key = temp_buf;
   1699 	}
   1700 	if (vip->vi_key.vk_key_len > 0) {
   1701 		mutex_enter(&vcache_lock);
   1702 		memcpy(temp_key, vip->vi_key.vk_key, temp_key_len);
   1703 		vip->vi_key.vk_key = temp_key;
   1704 		mutex_exit(&vcache_lock);
   1705 	}
   1706 
   1707 	fstrans_start(mp);
   1708 
   1709 	/*
   1710 	 * Clean out any cached data associated with the vnode.
   1711 	 * If purging an active vnode, it must be closed and
   1712 	 * deactivated before being reclaimed.
   1713 	 */
   1714 	error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
   1715 	if (error != 0) {
   1716 		if (wapbl_vphaswapbl(vp))
   1717 			WAPBL_DISCARD(wapbl_vptomp(vp));
   1718 		error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
   1719 	}
   1720 	KASSERTMSG((error == 0), "vinvalbuf failed: %d", error);
   1721 	KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
   1722 	if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
   1723 		 spec_node_revoke(vp);
   1724 	}
   1725 
   1726 	/*
   1727 	 * Disassociate the underlying file system from the vnode.
   1728 	 * VOP_INACTIVE leaves the vnode locked; VOP_RECLAIM unlocks
   1729 	 * the vnode, and may destroy the vnode so that VOP_UNLOCK
   1730 	 * would no longer function.
   1731 	 */
   1732 	VOP_INACTIVE(vp, &recycle);
   1733 	KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
   1734 	    VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1735 	if (VOP_RECLAIM(vp)) {
   1736 		vnpanic(vp, "%s: cannot reclaim", __func__);
   1737 	}
   1738 
   1739 	KASSERT(vp->v_data == NULL);
   1740 	KASSERT(vp->v_uobj.uo_npages == 0);
   1741 
   1742 	if (vp->v_type == VREG && vp->v_ractx != NULL) {
   1743 		uvm_ra_freectx(vp->v_ractx);
   1744 		vp->v_ractx = NULL;
   1745 	}
   1746 
   1747 	if (vip->vi_key.vk_key_len > 0) {
   1748 	/* Remove from vnode cache. */
   1749 		hash = vcache_hash(&vip->vi_key);
   1750 		mutex_enter(&vcache_lock);
   1751 		KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash));
   1752 		SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
   1753 		    vip, vnode_impl, vi_hash);
   1754 		mutex_exit(&vcache_lock);
   1755 	}
   1756 	if (temp_key != temp_buf)
   1757 		kmem_free(temp_key, temp_key_len);
   1758 
   1759 	/* Done with purge, notify sleepers of the grim news. */
   1760 	mutex_enter(vp->v_interlock);
   1761 	vp->v_op = dead_vnodeop_p;
   1762 	vp->v_vflag |= VV_LOCKSWORK;
   1763 	VSTATE_CHANGE(vp, VS_RECLAIMING, VS_RECLAIMED);
   1764 	vp->v_tag = VT_NON;
   1765 	KNOTE(&vp->v_klist, NOTE_REVOKE);
   1766 	mutex_exit(vp->v_interlock);
   1767 
   1768 	/*
   1769 	 * Move to dead mount.  Must be after changing the operations
   1770 	 * vector as vnode operations enter the mount before using the
   1771 	 * operations vector.  See sys/kern/vnode_if.c.
   1772 	 */
   1773 	vp->v_vflag &= ~VV_ROOT;
   1774 	vfs_ref(dead_rootmount);
   1775 	vfs_insmntque(vp, dead_rootmount);
   1776 
   1777 #ifdef PAX_SEGVGUARD
   1778 	pax_segvguard_cleanup(vp);
   1779 #endif /* PAX_SEGVGUARD */
   1780 
   1781 	mutex_enter(vp->v_interlock);
   1782 	fstrans_done(mp);
   1783 	KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
   1784 }
   1785 
   1786 /*
   1787  * Disassociate the underlying file system from an open device vnode
   1788  * and make it anonymous.
   1789  *
   1790  * Vnode unlocked on entry, drops a reference to the vnode.
   1791  */
   1792 void
   1793 vcache_make_anon(vnode_t *vp)
   1794 {
   1795 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
   1796 	uint32_t hash;
   1797 	bool recycle;
   1798 
   1799 	KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
   1800 	KASSERT(vp->v_mount == dead_rootmount || fstrans_is_owner(vp->v_mount));
   1801 	VSTATE_ASSERT_UNLOCKED(vp, VS_ACTIVE);
   1802 
   1803 	/* Remove from vnode cache. */
   1804 	hash = vcache_hash(&vip->vi_key);
   1805 	mutex_enter(&vcache_lock);
   1806 	KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash));
   1807 	SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
   1808 	    vip, vnode_impl, vi_hash);
   1809 	vip->vi_key.vk_mount = dead_rootmount;
   1810 	vip->vi_key.vk_key_len = 0;
   1811 	vip->vi_key.vk_key = NULL;
   1812 	mutex_exit(&vcache_lock);
   1813 
   1814 	/*
   1815 	 * Disassociate the underlying file system from the vnode.
   1816 	 * VOP_INACTIVE leaves the vnode locked; VOP_RECLAIM unlocks
   1817 	 * the vnode, and may destroy the vnode so that VOP_UNLOCK
   1818 	 * would no longer function.
   1819 	 */
   1820 	if (vn_lock(vp, LK_EXCLUSIVE)) {
   1821 		vnpanic(vp, "%s: cannot lock", __func__);
   1822 	}
   1823 	VOP_INACTIVE(vp, &recycle);
   1824 	KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
   1825 	    VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1826 	if (VOP_RECLAIM(vp)) {
   1827 		vnpanic(vp, "%s: cannot reclaim", __func__);
   1828 	}
   1829 
   1830 	/* Purge name cache. */
   1831 	cache_purge(vp);
   1832 
   1833 	/* Done with purge, change operations vector. */
   1834 	mutex_enter(vp->v_interlock);
   1835 	vp->v_op = spec_vnodeop_p;
   1836 	vp->v_vflag |= VV_MPSAFE;
   1837 	vp->v_vflag &= ~VV_LOCKSWORK;
   1838 	mutex_exit(vp->v_interlock);
   1839 
   1840 	/*
   1841 	 * Move to dead mount.  Must be after changing the operations
   1842 	 * vector as vnode operations enter the mount before using the
   1843 	 * operations vector.  See sys/kern/vnode_if.c.
   1844 	 */
   1845 	vfs_ref(dead_rootmount);
   1846 	vfs_insmntque(vp, dead_rootmount);
   1847 
   1848 	vrele(vp);
   1849 }
   1850 
   1851 /*
   1852  * Update outstanding I/O count and do wakeup if requested.
   1853  */
   1854 void
   1855 vwakeup(struct buf *bp)
   1856 {
   1857 	vnode_t *vp;
   1858 
   1859 	if ((vp = bp->b_vp) == NULL)
   1860 		return;
   1861 
   1862 	KASSERT(bp->b_objlock == vp->v_interlock);
   1863 	KASSERT(mutex_owned(bp->b_objlock));
   1864 
   1865 	if (--vp->v_numoutput < 0)
   1866 		vnpanic(vp, "%s: neg numoutput, vp %p", __func__, vp);
   1867 	if (vp->v_numoutput == 0)
   1868 		cv_broadcast(&vp->v_cv);
   1869 }
   1870 
   1871 /*
   1872  * Test a vnode for being or becoming dead.  Returns one of:
   1873  * EBUSY:  vnode is becoming dead, with "flags == VDEAD_NOWAIT" only.
   1874  * ENOENT: vnode is dead.
   1875  * 0:      otherwise.
   1876  *
   1877  * Whenever this function returns a non-zero value all future
   1878  * calls will also return a non-zero value.
   1879  */
   1880 int
   1881 vdead_check(struct vnode *vp, int flags)
   1882 {
   1883 
   1884 	KASSERT(mutex_owned(vp->v_interlock));
   1885 
   1886 	if (! ISSET(flags, VDEAD_NOWAIT))
   1887 		VSTATE_WAIT_STABLE(vp);
   1888 
   1889 	if (VSTATE_GET(vp) == VS_RECLAIMING) {
   1890 		KASSERT(ISSET(flags, VDEAD_NOWAIT));
   1891 		return EBUSY;
   1892 	} else if (VSTATE_GET(vp) == VS_RECLAIMED) {
   1893 		return ENOENT;
   1894 	}
   1895 
   1896 	return 0;
   1897 }
   1898 
   1899 int
   1900 vfs_drainvnodes(void)
   1901 {
   1902 	int i, gen;
   1903 
   1904 	mutex_enter(&vdrain_lock);
   1905 	for (i = 0; i < 2; i++) {
   1906 		gen = vdrain_gen;
   1907 		while (gen == vdrain_gen) {
   1908 			cv_broadcast(&vdrain_cv);
   1909 			cv_wait(&vdrain_gen_cv, &vdrain_lock);
   1910 		}
   1911 	}
   1912 	mutex_exit(&vdrain_lock);
   1913 
   1914 	if (numvnodes >= desiredvnodes)
   1915 		return EBUSY;
   1916 
   1917 	if (vcache_hashsize != desiredvnodes)
   1918 		vcache_reinit();
   1919 
   1920 	return 0;
   1921 }
   1922 
   1923 void
   1924 vnpanic(vnode_t *vp, const char *fmt, ...)
   1925 {
   1926 	va_list ap;
   1927 
   1928 #ifdef DIAGNOSTIC
   1929 	vprint(NULL, vp);
   1930 #endif
   1931 	va_start(ap, fmt);
   1932 	vpanic(fmt, ap);
   1933 	va_end(ap);
   1934 }
   1935