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