Home | History | Annotate | Line # | Download | only in kern
vfs_vnode.c revision 1.106
      1 /*	$NetBSD: vfs_vnode.c,v 1.106 2020/01/08 12:04:56 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.106 2020/01/08 12:04:56 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 			cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages);
    796 			cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages);
    797 		}
    798 		vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
    799 		vp->v_vflag &= ~VV_MAPPED;
    800 
    801 		/*
    802 		 * Recycle the vnode if the file is now unused (unlinked),
    803 		 * otherwise just free it.
    804 		 */
    805 		if (recycle) {
    806 			VSTATE_ASSERT(vp, VS_LOADED);
    807 			/* vcache_reclaim drops the lock. */
    808 			vcache_reclaim(vp);
    809 		}
    810 		KASSERT(vp->v_usecount > 0);
    811 	}
    812 
    813 	vp->v_usecount--;
    814 	if (vp->v_usecount != 0) {
    815 		/* Gained another reference while being reclaimed. */
    816 		mutex_exit(vp->v_interlock);
    817 		return;
    818 	}
    819 
    820 	if (VSTATE_GET(vp) == VS_RECLAIMED && vp->v_holdcnt == 0) {
    821 		/*
    822 		 * It's clean so destroy it.  It isn't referenced
    823 		 * anywhere since it has been reclaimed.
    824 		 */
    825 		vcache_free(VNODE_TO_VIMPL(vp));
    826 	} else {
    827 		/*
    828 		 * Otherwise, put it back onto the freelist.  It
    829 		 * can't be destroyed while still associated with
    830 		 * a file system.
    831 		 */
    832 		lru_requeue(vp, lru_which(vp));
    833 		mutex_exit(vp->v_interlock);
    834 	}
    835 }
    836 
    837 void
    838 vrele(vnode_t *vp)
    839 {
    840 
    841 	mutex_enter(vp->v_interlock);
    842 	vrelel(vp, 0);
    843 }
    844 
    845 /*
    846  * Asynchronous vnode release, vnode is released in different context.
    847  */
    848 void
    849 vrele_async(vnode_t *vp)
    850 {
    851 
    852 	mutex_enter(vp->v_interlock);
    853 	vrelel(vp, VRELEL_ASYNC);
    854 }
    855 
    856 /*
    857  * Vnode reference, where a reference is already held by some other
    858  * object (for example, a file structure).
    859  */
    860 void
    861 vref(vnode_t *vp)
    862 {
    863 
    864 	KASSERT(vp->v_usecount != 0);
    865 
    866 	mutex_enter(vp->v_interlock);
    867 	vp->v_usecount++;
    868 	mutex_exit(vp->v_interlock);
    869 }
    870 
    871 /*
    872  * Page or buffer structure gets a reference.
    873  * Called with v_interlock held.
    874  */
    875 void
    876 vholdl(vnode_t *vp)
    877 {
    878 
    879 	KASSERT(mutex_owned(vp->v_interlock));
    880 
    881 	if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0)
    882 		lru_requeue(vp, lru_which(vp));
    883 }
    884 
    885 /*
    886  * Page or buffer structure frees a reference.
    887  * Called with v_interlock held.
    888  */
    889 void
    890 holdrelel(vnode_t *vp)
    891 {
    892 
    893 	KASSERT(mutex_owned(vp->v_interlock));
    894 
    895 	if (vp->v_holdcnt <= 0) {
    896 		vnpanic(vp, "%s: holdcnt vp %p", __func__, vp);
    897 	}
    898 
    899 	vp->v_holdcnt--;
    900 	if (vp->v_holdcnt == 0 && vp->v_usecount == 0)
    901 		lru_requeue(vp, lru_which(vp));
    902 }
    903 
    904 /*
    905  * Recycle an unused vnode if caller holds the last reference.
    906  */
    907 bool
    908 vrecycle(vnode_t *vp)
    909 {
    910 	int error __diagused;
    911 
    912 	mutex_enter(vp->v_interlock);
    913 
    914 	/* Make sure we hold the last reference. */
    915 	VSTATE_WAIT_STABLE(vp);
    916 	if (vp->v_usecount != 1) {
    917 		mutex_exit(vp->v_interlock);
    918 		return false;
    919 	}
    920 
    921 	/* If the vnode is already clean we're done. */
    922 	if (VSTATE_GET(vp) != VS_LOADED) {
    923 		VSTATE_ASSERT(vp, VS_RECLAIMED);
    924 		vrelel(vp, 0);
    925 		return true;
    926 	}
    927 
    928 	/* Prevent further references until the vnode is locked. */
    929 	VSTATE_CHANGE(vp, VS_LOADED, VS_BLOCKED);
    930 	mutex_exit(vp->v_interlock);
    931 
    932 	/*
    933 	 * On a leaf file system this lock will always succeed as we hold
    934 	 * the last reference and prevent further references.
    935 	 * On layered file systems waiting for the lock would open a can of
    936 	 * deadlocks as the lower vnodes may have other active references.
    937 	 */
    938 	error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_NOWAIT);
    939 
    940 	mutex_enter(vp->v_interlock);
    941 	VSTATE_CHANGE(vp, VS_BLOCKED, VS_LOADED);
    942 
    943 	if (error) {
    944 		mutex_exit(vp->v_interlock);
    945 		return false;
    946 	}
    947 
    948 	KASSERT(vp->v_usecount == 1);
    949 	vcache_reclaim(vp);
    950 	vrelel(vp, 0);
    951 
    952 	return true;
    953 }
    954 
    955 /*
    956  * Helper for vrevoke() to propagate suspension from lastmp
    957  * to thismp.  Both args may be NULL.
    958  * Returns the currently suspended file system or NULL.
    959  */
    960 static struct mount *
    961 vrevoke_suspend_next(struct mount *lastmp, struct mount *thismp)
    962 {
    963 	int error;
    964 
    965 	if (lastmp == thismp)
    966 		return thismp;
    967 
    968 	if (lastmp != NULL)
    969 		vfs_resume(lastmp);
    970 
    971 	if (thismp == NULL)
    972 		return NULL;
    973 
    974 	do {
    975 		error = vfs_suspend(thismp, 0);
    976 	} while (error == EINTR || error == ERESTART);
    977 
    978 	if (error == 0)
    979 		return thismp;
    980 
    981 	KASSERT(error == EOPNOTSUPP);
    982 	return NULL;
    983 }
    984 
    985 /*
    986  * Eliminate all activity associated with the requested vnode
    987  * and with all vnodes aliased to the requested vnode.
    988  */
    989 void
    990 vrevoke(vnode_t *vp)
    991 {
    992 	struct mount *mp;
    993 	vnode_t *vq;
    994 	enum vtype type;
    995 	dev_t dev;
    996 
    997 	KASSERT(vp->v_usecount > 0);
    998 
    999 	mp = vrevoke_suspend_next(NULL, vp->v_mount);
   1000 
   1001 	mutex_enter(vp->v_interlock);
   1002 	VSTATE_WAIT_STABLE(vp);
   1003 	if (VSTATE_GET(vp) == VS_RECLAIMED) {
   1004 		mutex_exit(vp->v_interlock);
   1005 	} else if (vp->v_type != VBLK && vp->v_type != VCHR) {
   1006 		vp->v_usecount++;
   1007 		mutex_exit(vp->v_interlock);
   1008 		vgone(vp);
   1009 	} else {
   1010 		dev = vp->v_rdev;
   1011 		type = vp->v_type;
   1012 		mutex_exit(vp->v_interlock);
   1013 
   1014 		while (spec_node_lookup_by_dev(type, dev, &vq) == 0) {
   1015 			mp = vrevoke_suspend_next(mp, vq->v_mount);
   1016 			vgone(vq);
   1017 		}
   1018 	}
   1019 	vrevoke_suspend_next(mp, NULL);
   1020 }
   1021 
   1022 /*
   1023  * Eliminate all activity associated with a vnode in preparation for
   1024  * reuse.  Drops a reference from the vnode.
   1025  */
   1026 void
   1027 vgone(vnode_t *vp)
   1028 {
   1029 
   1030 	KASSERT(vp->v_mount == dead_rootmount || fstrans_is_owner(vp->v_mount));
   1031 
   1032 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   1033 	mutex_enter(vp->v_interlock);
   1034 	VSTATE_WAIT_STABLE(vp);
   1035 	if (VSTATE_GET(vp) == VS_LOADED)
   1036 		vcache_reclaim(vp);
   1037 	VSTATE_ASSERT(vp, VS_RECLAIMED);
   1038 	vrelel(vp, 0);
   1039 }
   1040 
   1041 static inline uint32_t
   1042 vcache_hash(const struct vcache_key *key)
   1043 {
   1044 	uint32_t hash = HASH32_BUF_INIT;
   1045 
   1046 	KASSERT(key->vk_key_len > 0);
   1047 
   1048 	hash = hash32_buf(&key->vk_mount, sizeof(struct mount *), hash);
   1049 	hash = hash32_buf(key->vk_key, key->vk_key_len, hash);
   1050 	return hash;
   1051 }
   1052 
   1053 static void
   1054 vcache_init(void)
   1055 {
   1056 
   1057 	vcache_pool = pool_cache_init(sizeof(vnode_impl_t), 0, 0, 0,
   1058 	    "vcachepl", NULL, IPL_NONE, NULL, NULL, NULL);
   1059 	KASSERT(vcache_pool != NULL);
   1060 	mutex_init(&vcache_lock, MUTEX_DEFAULT, IPL_NONE);
   1061 	cv_init(&vcache_cv, "vcache");
   1062 	vcache_hashsize = desiredvnodes;
   1063 	vcache_hashtab = hashinit(desiredvnodes, HASH_SLIST, true,
   1064 	    &vcache_hashmask);
   1065 }
   1066 
   1067 static void
   1068 vcache_reinit(void)
   1069 {
   1070 	int i;
   1071 	uint32_t hash;
   1072 	u_long oldmask, newmask;
   1073 	struct hashhead *oldtab, *newtab;
   1074 	vnode_impl_t *vip;
   1075 
   1076 	newtab = hashinit(desiredvnodes, HASH_SLIST, true, &newmask);
   1077 	mutex_enter(&vcache_lock);
   1078 	oldtab = vcache_hashtab;
   1079 	oldmask = vcache_hashmask;
   1080 	vcache_hashsize = desiredvnodes;
   1081 	vcache_hashtab = newtab;
   1082 	vcache_hashmask = newmask;
   1083 	for (i = 0; i <= oldmask; i++) {
   1084 		while ((vip = SLIST_FIRST(&oldtab[i])) != NULL) {
   1085 			SLIST_REMOVE(&oldtab[i], vip, vnode_impl, vi_hash);
   1086 			hash = vcache_hash(&vip->vi_key);
   1087 			SLIST_INSERT_HEAD(&newtab[hash & vcache_hashmask],
   1088 			    vip, vi_hash);
   1089 		}
   1090 	}
   1091 	mutex_exit(&vcache_lock);
   1092 	hashdone(oldtab, HASH_SLIST, oldmask);
   1093 }
   1094 
   1095 static inline vnode_impl_t *
   1096 vcache_hash_lookup(const struct vcache_key *key, uint32_t hash)
   1097 {
   1098 	struct hashhead *hashp;
   1099 	vnode_impl_t *vip;
   1100 
   1101 	KASSERT(mutex_owned(&vcache_lock));
   1102 
   1103 	hashp = &vcache_hashtab[hash & vcache_hashmask];
   1104 	SLIST_FOREACH(vip, hashp, vi_hash) {
   1105 		if (key->vk_mount != vip->vi_key.vk_mount)
   1106 			continue;
   1107 		if (key->vk_key_len != vip->vi_key.vk_key_len)
   1108 			continue;
   1109 		if (memcmp(key->vk_key, vip->vi_key.vk_key, key->vk_key_len))
   1110 			continue;
   1111 		return vip;
   1112 	}
   1113 	return NULL;
   1114 }
   1115 
   1116 /*
   1117  * Allocate a new, uninitialized vcache node.
   1118  */
   1119 static vnode_impl_t *
   1120 vcache_alloc(void)
   1121 {
   1122 	vnode_impl_t *vip;
   1123 	vnode_t *vp;
   1124 
   1125 	vip = pool_cache_get(vcache_pool, PR_WAITOK);
   1126 	memset(vip, 0, sizeof(*vip));
   1127 
   1128 	vip->vi_lock = rw_obj_alloc();
   1129 	/* SLIST_INIT(&vip->vi_hash); */
   1130 	TAILQ_INIT(&vip->vi_nclist);
   1131 	/* LIST_INIT(&vip->vi_dnclist); */
   1132 
   1133 	vp = VIMPL_TO_VNODE(vip);
   1134 	uvm_obj_init(&vp->v_uobj, &uvm_vnodeops, true, 0);
   1135 	cv_init(&vp->v_cv, "vnode");
   1136 
   1137 	vp->v_usecount = 1;
   1138 	vp->v_type = VNON;
   1139 	vp->v_size = vp->v_writesize = VSIZENOTSET;
   1140 
   1141 	vip->vi_state = VS_LOADING;
   1142 
   1143 	lru_requeue(vp, &lru_list[LRU_FREE]);
   1144 
   1145 	return vip;
   1146 }
   1147 
   1148 /*
   1149  * Deallocate a vcache node in state VS_LOADING.
   1150  *
   1151  * vcache_lock held on entry and released on return.
   1152  */
   1153 static void
   1154 vcache_dealloc(vnode_impl_t *vip)
   1155 {
   1156 	vnode_t *vp;
   1157 
   1158 	KASSERT(mutex_owned(&vcache_lock));
   1159 
   1160 	vp = VIMPL_TO_VNODE(vip);
   1161 	vfs_ref(dead_rootmount);
   1162 	vfs_insmntque(vp, dead_rootmount);
   1163 	mutex_enter(vp->v_interlock);
   1164 	vp->v_op = dead_vnodeop_p;
   1165 	VSTATE_CHANGE(vp, VS_LOADING, VS_RECLAIMED);
   1166 	mutex_exit(&vcache_lock);
   1167 	vrelel(vp, 0);
   1168 }
   1169 
   1170 /*
   1171  * Free an unused, unreferenced vcache node.
   1172  * v_interlock locked on entry.
   1173  */
   1174 static void
   1175 vcache_free(vnode_impl_t *vip)
   1176 {
   1177 	vnode_t *vp;
   1178 
   1179 	vp = VIMPL_TO_VNODE(vip);
   1180 	KASSERT(mutex_owned(vp->v_interlock));
   1181 
   1182 	KASSERT(vp->v_usecount == 0);
   1183 	KASSERT(vp->v_holdcnt == 0);
   1184 	KASSERT(vp->v_writecount == 0);
   1185 	lru_requeue(vp, NULL);
   1186 	mutex_exit(vp->v_interlock);
   1187 
   1188 	vfs_insmntque(vp, NULL);
   1189 	if (vp->v_type == VBLK || vp->v_type == VCHR)
   1190 		spec_node_destroy(vp);
   1191 
   1192 	rw_obj_free(vip->vi_lock);
   1193 	uvm_obj_destroy(&vp->v_uobj, true);
   1194 	cv_destroy(&vp->v_cv);
   1195 	pool_cache_put(vcache_pool, vip);
   1196 }
   1197 
   1198 /*
   1199  * Try to get an initial reference on this cached vnode.
   1200  * Returns zero on success,  ENOENT if the vnode has been reclaimed and
   1201  * EBUSY if the vnode state is unstable.
   1202  *
   1203  * v_interlock locked on entry and unlocked on exit.
   1204  */
   1205 int
   1206 vcache_tryvget(vnode_t *vp)
   1207 {
   1208 	int error = 0;
   1209 
   1210 	KASSERT(mutex_owned(vp->v_interlock));
   1211 
   1212 	if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED))
   1213 		error = ENOENT;
   1214 	else if (__predict_false(VSTATE_GET(vp) != VS_LOADED))
   1215 		error = EBUSY;
   1216 	else
   1217 		vp->v_usecount++;
   1218 
   1219 	mutex_exit(vp->v_interlock);
   1220 
   1221 	return error;
   1222 }
   1223 
   1224 /*
   1225  * Try to get an initial reference on this cached vnode.
   1226  * Returns zero on success and  ENOENT if the vnode has been reclaimed.
   1227  * Will wait for the vnode state to be stable.
   1228  *
   1229  * v_interlock locked on entry and unlocked on exit.
   1230  */
   1231 int
   1232 vcache_vget(vnode_t *vp)
   1233 {
   1234 
   1235 	KASSERT(mutex_owned(vp->v_interlock));
   1236 
   1237 	/* Increment hold count to prevent vnode from disappearing. */
   1238 	vp->v_holdcnt++;
   1239 	VSTATE_WAIT_STABLE(vp);
   1240 	vp->v_holdcnt--;
   1241 
   1242 	/* If this was the last reference to a reclaimed vnode free it now. */
   1243 	if (__predict_false(VSTATE_GET(vp) == VS_RECLAIMED)) {
   1244 		if (vp->v_holdcnt == 0 && vp->v_usecount == 0)
   1245 			vcache_free(VNODE_TO_VIMPL(vp));
   1246 		else
   1247 			mutex_exit(vp->v_interlock);
   1248 		return ENOENT;
   1249 	}
   1250 	VSTATE_ASSERT(vp, VS_LOADED);
   1251 	vp->v_usecount++;
   1252 	mutex_exit(vp->v_interlock);
   1253 
   1254 	return 0;
   1255 }
   1256 
   1257 /*
   1258  * Get a vnode / fs node pair by key and return it referenced through vpp.
   1259  */
   1260 int
   1261 vcache_get(struct mount *mp, const void *key, size_t key_len,
   1262     struct vnode **vpp)
   1263 {
   1264 	int error;
   1265 	uint32_t hash;
   1266 	const void *new_key;
   1267 	struct vnode *vp;
   1268 	struct vcache_key vcache_key;
   1269 	vnode_impl_t *vip, *new_vip;
   1270 
   1271 	new_key = NULL;
   1272 	*vpp = NULL;
   1273 
   1274 	vcache_key.vk_mount = mp;
   1275 	vcache_key.vk_key = key;
   1276 	vcache_key.vk_key_len = key_len;
   1277 	hash = vcache_hash(&vcache_key);
   1278 
   1279 again:
   1280 	mutex_enter(&vcache_lock);
   1281 	vip = vcache_hash_lookup(&vcache_key, hash);
   1282 
   1283 	/* If found, take a reference or retry. */
   1284 	if (__predict_true(vip != NULL)) {
   1285 		/*
   1286 		 * If the vnode is loading we cannot take the v_interlock
   1287 		 * here as it might change during load (see uvm_obj_setlock()).
   1288 		 * As changing state from VS_LOADING requires both vcache_lock
   1289 		 * and v_interlock it is safe to test with vcache_lock held.
   1290 		 *
   1291 		 * Wait for vnodes changing state from VS_LOADING and retry.
   1292 		 */
   1293 		if (__predict_false(vip->vi_state == VS_LOADING)) {
   1294 			cv_wait(&vcache_cv, &vcache_lock);
   1295 			mutex_exit(&vcache_lock);
   1296 			goto again;
   1297 		}
   1298 		vp = VIMPL_TO_VNODE(vip);
   1299 		mutex_enter(vp->v_interlock);
   1300 		mutex_exit(&vcache_lock);
   1301 		error = vcache_vget(vp);
   1302 		if (error == ENOENT)
   1303 			goto again;
   1304 		if (error == 0)
   1305 			*vpp = vp;
   1306 		KASSERT((error != 0) == (*vpp == NULL));
   1307 		return error;
   1308 	}
   1309 	mutex_exit(&vcache_lock);
   1310 
   1311 	/* Allocate and initialize a new vcache / vnode pair. */
   1312 	error = vfs_busy(mp);
   1313 	if (error)
   1314 		return error;
   1315 	new_vip = vcache_alloc();
   1316 	new_vip->vi_key = vcache_key;
   1317 	vp = VIMPL_TO_VNODE(new_vip);
   1318 	mutex_enter(&vcache_lock);
   1319 	vip = vcache_hash_lookup(&vcache_key, hash);
   1320 	if (vip == NULL) {
   1321 		SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
   1322 		    new_vip, vi_hash);
   1323 		vip = new_vip;
   1324 	}
   1325 
   1326 	/* If another thread beat us inserting this node, retry. */
   1327 	if (vip != new_vip) {
   1328 		vcache_dealloc(new_vip);
   1329 		vfs_unbusy(mp);
   1330 		goto again;
   1331 	}
   1332 	mutex_exit(&vcache_lock);
   1333 
   1334 	/* Load the fs node.  Exclusive as new_node is VS_LOADING. */
   1335 	error = VFS_LOADVNODE(mp, vp, key, key_len, &new_key);
   1336 	if (error) {
   1337 		mutex_enter(&vcache_lock);
   1338 		SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
   1339 		    new_vip, vnode_impl, vi_hash);
   1340 		vcache_dealloc(new_vip);
   1341 		vfs_unbusy(mp);
   1342 		KASSERT(*vpp == NULL);
   1343 		return error;
   1344 	}
   1345 	KASSERT(new_key != NULL);
   1346 	KASSERT(memcmp(key, new_key, key_len) == 0);
   1347 	KASSERT(vp->v_op != NULL);
   1348 	vfs_insmntque(vp, mp);
   1349 	if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
   1350 		vp->v_vflag |= VV_MPSAFE;
   1351 	vfs_ref(mp);
   1352 	vfs_unbusy(mp);
   1353 
   1354 	/* Finished loading, finalize node. */
   1355 	mutex_enter(&vcache_lock);
   1356 	new_vip->vi_key.vk_key = new_key;
   1357 	mutex_enter(vp->v_interlock);
   1358 	VSTATE_CHANGE(vp, VS_LOADING, VS_LOADED);
   1359 	mutex_exit(vp->v_interlock);
   1360 	mutex_exit(&vcache_lock);
   1361 	*vpp = vp;
   1362 	return 0;
   1363 }
   1364 
   1365 /*
   1366  * Create a new vnode / fs node pair and return it referenced through vpp.
   1367  */
   1368 int
   1369 vcache_new(struct mount *mp, struct vnode *dvp, struct vattr *vap,
   1370     kauth_cred_t cred, void *extra, struct vnode **vpp)
   1371 {
   1372 	int error;
   1373 	uint32_t hash;
   1374 	struct vnode *vp, *ovp;
   1375 	vnode_impl_t *vip, *ovip;
   1376 
   1377 	*vpp = NULL;
   1378 
   1379 	/* Allocate and initialize a new vcache / vnode pair. */
   1380 	error = vfs_busy(mp);
   1381 	if (error)
   1382 		return error;
   1383 	vip = vcache_alloc();
   1384 	vip->vi_key.vk_mount = mp;
   1385 	vp = VIMPL_TO_VNODE(vip);
   1386 
   1387 	/* Create and load the fs node. */
   1388 	error = VFS_NEWVNODE(mp, dvp, vp, vap, cred, extra,
   1389 	    &vip->vi_key.vk_key_len, &vip->vi_key.vk_key);
   1390 	if (error) {
   1391 		mutex_enter(&vcache_lock);
   1392 		vcache_dealloc(vip);
   1393 		vfs_unbusy(mp);
   1394 		KASSERT(*vpp == NULL);
   1395 		return error;
   1396 	}
   1397 	KASSERT(vp->v_op != NULL);
   1398 	KASSERT((vip->vi_key.vk_key_len == 0) == (mp == dead_rootmount));
   1399 	if (vip->vi_key.vk_key_len > 0) {
   1400 		KASSERT(vip->vi_key.vk_key != NULL);
   1401 		hash = vcache_hash(&vip->vi_key);
   1402 
   1403 		/*
   1404 		 * Wait for previous instance to be reclaimed,
   1405 		 * then insert new node.
   1406 		 */
   1407 		mutex_enter(&vcache_lock);
   1408 		while ((ovip = vcache_hash_lookup(&vip->vi_key, hash))) {
   1409 			ovp = VIMPL_TO_VNODE(ovip);
   1410 			mutex_enter(ovp->v_interlock);
   1411 			mutex_exit(&vcache_lock);
   1412 			error = vcache_vget(ovp);
   1413 			KASSERT(error == ENOENT);
   1414 			mutex_enter(&vcache_lock);
   1415 		}
   1416 		SLIST_INSERT_HEAD(&vcache_hashtab[hash & vcache_hashmask],
   1417 		    vip, vi_hash);
   1418 		mutex_exit(&vcache_lock);
   1419 	}
   1420 	vfs_insmntque(vp, mp);
   1421 	if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
   1422 		vp->v_vflag |= VV_MPSAFE;
   1423 	vfs_ref(mp);
   1424 	vfs_unbusy(mp);
   1425 
   1426 	/* Finished loading, finalize node. */
   1427 	mutex_enter(&vcache_lock);
   1428 	mutex_enter(vp->v_interlock);
   1429 	VSTATE_CHANGE(vp, VS_LOADING, VS_LOADED);
   1430 	mutex_exit(&vcache_lock);
   1431 	mutex_exit(vp->v_interlock);
   1432 	*vpp = vp;
   1433 	return 0;
   1434 }
   1435 
   1436 /*
   1437  * Prepare key change: update old cache nodes key and lock new cache node.
   1438  * Return an error if the new node already exists.
   1439  */
   1440 int
   1441 vcache_rekey_enter(struct mount *mp, struct vnode *vp,
   1442     const void *old_key, size_t old_key_len,
   1443     const void *new_key, size_t new_key_len)
   1444 {
   1445 	uint32_t old_hash, new_hash;
   1446 	struct vcache_key old_vcache_key, new_vcache_key;
   1447 	vnode_impl_t *vip, *new_vip;
   1448 
   1449 	old_vcache_key.vk_mount = mp;
   1450 	old_vcache_key.vk_key = old_key;
   1451 	old_vcache_key.vk_key_len = old_key_len;
   1452 	old_hash = vcache_hash(&old_vcache_key);
   1453 
   1454 	new_vcache_key.vk_mount = mp;
   1455 	new_vcache_key.vk_key = new_key;
   1456 	new_vcache_key.vk_key_len = new_key_len;
   1457 	new_hash = vcache_hash(&new_vcache_key);
   1458 
   1459 	new_vip = vcache_alloc();
   1460 	new_vip->vi_key = new_vcache_key;
   1461 
   1462 	/* Insert locked new node used as placeholder. */
   1463 	mutex_enter(&vcache_lock);
   1464 	vip = vcache_hash_lookup(&new_vcache_key, new_hash);
   1465 	if (vip != NULL) {
   1466 		vcache_dealloc(new_vip);
   1467 		return EEXIST;
   1468 	}
   1469 	SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
   1470 	    new_vip, vi_hash);
   1471 
   1472 	/* Replace old nodes key with the temporary copy. */
   1473 	vip = vcache_hash_lookup(&old_vcache_key, old_hash);
   1474 	KASSERT(vip != NULL);
   1475 	KASSERT(VIMPL_TO_VNODE(vip) == vp);
   1476 	KASSERT(vip->vi_key.vk_key != old_vcache_key.vk_key);
   1477 	vip->vi_key = old_vcache_key;
   1478 	mutex_exit(&vcache_lock);
   1479 	return 0;
   1480 }
   1481 
   1482 /*
   1483  * Key change complete: update old node and remove placeholder.
   1484  */
   1485 void
   1486 vcache_rekey_exit(struct mount *mp, struct vnode *vp,
   1487     const void *old_key, size_t old_key_len,
   1488     const void *new_key, size_t new_key_len)
   1489 {
   1490 	uint32_t old_hash, new_hash;
   1491 	struct vcache_key old_vcache_key, new_vcache_key;
   1492 	vnode_impl_t *vip, *new_vip;
   1493 	struct vnode *new_vp;
   1494 
   1495 	old_vcache_key.vk_mount = mp;
   1496 	old_vcache_key.vk_key = old_key;
   1497 	old_vcache_key.vk_key_len = old_key_len;
   1498 	old_hash = vcache_hash(&old_vcache_key);
   1499 
   1500 	new_vcache_key.vk_mount = mp;
   1501 	new_vcache_key.vk_key = new_key;
   1502 	new_vcache_key.vk_key_len = new_key_len;
   1503 	new_hash = vcache_hash(&new_vcache_key);
   1504 
   1505 	mutex_enter(&vcache_lock);
   1506 
   1507 	/* Lookup old and new node. */
   1508 	vip = vcache_hash_lookup(&old_vcache_key, old_hash);
   1509 	KASSERT(vip != NULL);
   1510 	KASSERT(VIMPL_TO_VNODE(vip) == vp);
   1511 
   1512 	new_vip = vcache_hash_lookup(&new_vcache_key, new_hash);
   1513 	KASSERT(new_vip != NULL);
   1514 	KASSERT(new_vip->vi_key.vk_key_len == new_key_len);
   1515 	new_vp = VIMPL_TO_VNODE(new_vip);
   1516 	mutex_enter(new_vp->v_interlock);
   1517 	VSTATE_ASSERT(VIMPL_TO_VNODE(new_vip), VS_LOADING);
   1518 	mutex_exit(new_vp->v_interlock);
   1519 
   1520 	/* Rekey old node and put it onto its new hashlist. */
   1521 	vip->vi_key = new_vcache_key;
   1522 	if (old_hash != new_hash) {
   1523 		SLIST_REMOVE(&vcache_hashtab[old_hash & vcache_hashmask],
   1524 		    vip, vnode_impl, vi_hash);
   1525 		SLIST_INSERT_HEAD(&vcache_hashtab[new_hash & vcache_hashmask],
   1526 		    vip, vi_hash);
   1527 	}
   1528 
   1529 	/* Remove new node used as placeholder. */
   1530 	SLIST_REMOVE(&vcache_hashtab[new_hash & vcache_hashmask],
   1531 	    new_vip, vnode_impl, vi_hash);
   1532 	vcache_dealloc(new_vip);
   1533 }
   1534 
   1535 /*
   1536  * Disassociate the underlying file system from a vnode.
   1537  *
   1538  * Must be called with vnode locked and will return unlocked.
   1539  * Must be called with the interlock held, and will return with it held.
   1540  */
   1541 static void
   1542 vcache_reclaim(vnode_t *vp)
   1543 {
   1544 	lwp_t *l = curlwp;
   1545 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
   1546 	struct mount *mp = vp->v_mount;
   1547 	uint32_t hash;
   1548 	uint8_t temp_buf[64], *temp_key;
   1549 	size_t temp_key_len;
   1550 	bool recycle, active;
   1551 	int error;
   1552 
   1553 	KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
   1554 	    VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1555 	KASSERT(mutex_owned(vp->v_interlock));
   1556 	KASSERT(vp->v_usecount != 0);
   1557 
   1558 	active = (vp->v_usecount > 1);
   1559 	temp_key_len = vip->vi_key.vk_key_len;
   1560 	/*
   1561 	 * Prevent the vnode from being recycled or brought into use
   1562 	 * while we clean it out.
   1563 	 */
   1564 	VSTATE_CHANGE(vp, VS_LOADED, VS_RECLAIMING);
   1565 	if ((vp->v_iflag & VI_EXECMAP) != 0 && vp->v_uobj.uo_npages != 0) {
   1566 		cpu_count(CPU_COUNT_EXECPAGES, -vp->v_uobj.uo_npages);
   1567 		cpu_count(CPU_COUNT_FILEPAGES, vp->v_uobj.uo_npages);
   1568 	}
   1569 	vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
   1570 	mutex_exit(vp->v_interlock);
   1571 
   1572 	/* Replace the vnode key with a temporary copy. */
   1573 	if (vip->vi_key.vk_key_len > sizeof(temp_buf)) {
   1574 		temp_key = kmem_alloc(temp_key_len, KM_SLEEP);
   1575 	} else {
   1576 		temp_key = temp_buf;
   1577 	}
   1578 	if (vip->vi_key.vk_key_len > 0) {
   1579 		mutex_enter(&vcache_lock);
   1580 		memcpy(temp_key, vip->vi_key.vk_key, temp_key_len);
   1581 		vip->vi_key.vk_key = temp_key;
   1582 		mutex_exit(&vcache_lock);
   1583 	}
   1584 
   1585 	fstrans_start(mp);
   1586 
   1587 	/*
   1588 	 * Clean out any cached data associated with the vnode.
   1589 	 * If purging an active vnode, it must be closed and
   1590 	 * deactivated before being reclaimed.
   1591 	 */
   1592 	error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
   1593 	if (error != 0) {
   1594 		if (wapbl_vphaswapbl(vp))
   1595 			WAPBL_DISCARD(wapbl_vptomp(vp));
   1596 		error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
   1597 	}
   1598 	KASSERTMSG((error == 0), "vinvalbuf failed: %d", error);
   1599 	KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
   1600 	if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
   1601 		 spec_node_revoke(vp);
   1602 	}
   1603 
   1604 	/*
   1605 	 * Disassociate the underlying file system from the vnode.
   1606 	 * VOP_INACTIVE leaves the vnode locked; VOP_RECLAIM unlocks
   1607 	 * the vnode, and may destroy the vnode so that VOP_UNLOCK
   1608 	 * would no longer function.
   1609 	 */
   1610 	VOP_INACTIVE(vp, &recycle);
   1611 	KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
   1612 	    VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1613 	if (VOP_RECLAIM(vp)) {
   1614 		vnpanic(vp, "%s: cannot reclaim", __func__);
   1615 	}
   1616 
   1617 	KASSERT(vp->v_data == NULL);
   1618 	KASSERT(vp->v_uobj.uo_npages == 0);
   1619 
   1620 	if (vp->v_type == VREG && vp->v_ractx != NULL) {
   1621 		uvm_ra_freectx(vp->v_ractx);
   1622 		vp->v_ractx = NULL;
   1623 	}
   1624 
   1625 	/* Purge name cache. */
   1626 	cache_purge(vp);
   1627 
   1628 	if (vip->vi_key.vk_key_len > 0) {
   1629 	/* Remove from vnode cache. */
   1630 		hash = vcache_hash(&vip->vi_key);
   1631 		mutex_enter(&vcache_lock);
   1632 		KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash));
   1633 		SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
   1634 		    vip, vnode_impl, vi_hash);
   1635 		mutex_exit(&vcache_lock);
   1636 	}
   1637 	if (temp_key != temp_buf)
   1638 		kmem_free(temp_key, temp_key_len);
   1639 
   1640 	/* Done with purge, notify sleepers of the grim news. */
   1641 	mutex_enter(vp->v_interlock);
   1642 	vp->v_op = dead_vnodeop_p;
   1643 	vp->v_vflag |= VV_LOCKSWORK;
   1644 	VSTATE_CHANGE(vp, VS_RECLAIMING, VS_RECLAIMED);
   1645 	vp->v_tag = VT_NON;
   1646 	KNOTE(&vp->v_klist, NOTE_REVOKE);
   1647 	mutex_exit(vp->v_interlock);
   1648 
   1649 	/*
   1650 	 * Move to dead mount.  Must be after changing the operations
   1651 	 * vector as vnode operations enter the mount before using the
   1652 	 * operations vector.  See sys/kern/vnode_if.c.
   1653 	 */
   1654 	vp->v_vflag &= ~VV_ROOT;
   1655 	vfs_ref(dead_rootmount);
   1656 	vfs_insmntque(vp, dead_rootmount);
   1657 
   1658 	mutex_enter(vp->v_interlock);
   1659 	fstrans_done(mp);
   1660 	KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
   1661 }
   1662 
   1663 /*
   1664  * Disassociate the underlying file system from an open device vnode
   1665  * and make it anonymous.
   1666  *
   1667  * Vnode unlocked on entry, drops a reference to the vnode.
   1668  */
   1669 void
   1670 vcache_make_anon(vnode_t *vp)
   1671 {
   1672 	vnode_impl_t *vip = VNODE_TO_VIMPL(vp);
   1673 	uint32_t hash;
   1674 	bool recycle;
   1675 
   1676 	KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
   1677 	KASSERT(vp->v_mount == dead_rootmount || fstrans_is_owner(vp->v_mount));
   1678 	VSTATE_ASSERT_UNLOCKED(vp, VS_ACTIVE);
   1679 
   1680 	/* Remove from vnode cache. */
   1681 	hash = vcache_hash(&vip->vi_key);
   1682 	mutex_enter(&vcache_lock);
   1683 	KASSERT(vip == vcache_hash_lookup(&vip->vi_key, hash));
   1684 	SLIST_REMOVE(&vcache_hashtab[hash & vcache_hashmask],
   1685 	    vip, vnode_impl, vi_hash);
   1686 	vip->vi_key.vk_mount = dead_rootmount;
   1687 	vip->vi_key.vk_key_len = 0;
   1688 	vip->vi_key.vk_key = NULL;
   1689 	mutex_exit(&vcache_lock);
   1690 
   1691 	/*
   1692 	 * Disassociate the underlying file system from the vnode.
   1693 	 * VOP_INACTIVE leaves the vnode locked; VOP_RECLAIM unlocks
   1694 	 * the vnode, and may destroy the vnode so that VOP_UNLOCK
   1695 	 * would no longer function.
   1696 	 */
   1697 	if (vn_lock(vp, LK_EXCLUSIVE)) {
   1698 		vnpanic(vp, "%s: cannot lock", __func__);
   1699 	}
   1700 	VOP_INACTIVE(vp, &recycle);
   1701 	KASSERT((vp->v_vflag & VV_LOCKSWORK) == 0 ||
   1702 	    VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
   1703 	if (VOP_RECLAIM(vp)) {
   1704 		vnpanic(vp, "%s: cannot reclaim", __func__);
   1705 	}
   1706 
   1707 	/* Purge name cache. */
   1708 	cache_purge(vp);
   1709 
   1710 	/* Done with purge, change operations vector. */
   1711 	mutex_enter(vp->v_interlock);
   1712 	vp->v_op = spec_vnodeop_p;
   1713 	vp->v_vflag |= VV_MPSAFE;
   1714 	vp->v_vflag &= ~VV_LOCKSWORK;
   1715 	mutex_exit(vp->v_interlock);
   1716 
   1717 	/*
   1718 	 * Move to dead mount.  Must be after changing the operations
   1719 	 * vector as vnode operations enter the mount before using the
   1720 	 * operations vector.  See sys/kern/vnode_if.c.
   1721 	 */
   1722 	vfs_ref(dead_rootmount);
   1723 	vfs_insmntque(vp, dead_rootmount);
   1724 
   1725 	vrele(vp);
   1726 }
   1727 
   1728 /*
   1729  * Update outstanding I/O count and do wakeup if requested.
   1730  */
   1731 void
   1732 vwakeup(struct buf *bp)
   1733 {
   1734 	vnode_t *vp;
   1735 
   1736 	if ((vp = bp->b_vp) == NULL)
   1737 		return;
   1738 
   1739 	KASSERT(bp->b_objlock == vp->v_interlock);
   1740 	KASSERT(mutex_owned(bp->b_objlock));
   1741 
   1742 	if (--vp->v_numoutput < 0)
   1743 		vnpanic(vp, "%s: neg numoutput, vp %p", __func__, vp);
   1744 	if (vp->v_numoutput == 0)
   1745 		cv_broadcast(&vp->v_cv);
   1746 }
   1747 
   1748 /*
   1749  * Test a vnode for being or becoming dead.  Returns one of:
   1750  * EBUSY:  vnode is becoming dead, with "flags == VDEAD_NOWAIT" only.
   1751  * ENOENT: vnode is dead.
   1752  * 0:      otherwise.
   1753  *
   1754  * Whenever this function returns a non-zero value all future
   1755  * calls will also return a non-zero value.
   1756  */
   1757 int
   1758 vdead_check(struct vnode *vp, int flags)
   1759 {
   1760 
   1761 	KASSERT(mutex_owned(vp->v_interlock));
   1762 
   1763 	if (! ISSET(flags, VDEAD_NOWAIT))
   1764 		VSTATE_WAIT_STABLE(vp);
   1765 
   1766 	if (VSTATE_GET(vp) == VS_RECLAIMING) {
   1767 		KASSERT(ISSET(flags, VDEAD_NOWAIT));
   1768 		return EBUSY;
   1769 	} else if (VSTATE_GET(vp) == VS_RECLAIMED) {
   1770 		return ENOENT;
   1771 	}
   1772 
   1773 	return 0;
   1774 }
   1775 
   1776 int
   1777 vfs_drainvnodes(void)
   1778 {
   1779 	int i, gen;
   1780 
   1781 	mutex_enter(&vdrain_lock);
   1782 	for (i = 0; i < 2; i++) {
   1783 		gen = vdrain_gen;
   1784 		while (gen == vdrain_gen) {
   1785 			cv_broadcast(&vdrain_cv);
   1786 			cv_wait(&vdrain_gen_cv, &vdrain_lock);
   1787 		}
   1788 	}
   1789 	mutex_exit(&vdrain_lock);
   1790 
   1791 	if (numvnodes >= desiredvnodes)
   1792 		return EBUSY;
   1793 
   1794 	if (vcache_hashsize != desiredvnodes)
   1795 		vcache_reinit();
   1796 
   1797 	return 0;
   1798 }
   1799 
   1800 void
   1801 vnpanic(vnode_t *vp, const char *fmt, ...)
   1802 {
   1803 	va_list ap;
   1804 
   1805 #ifdef DIAGNOSTIC
   1806 	vprint(NULL, vp);
   1807 #endif
   1808 	va_start(ap, fmt);
   1809 	vpanic(fmt, ap);
   1810 	va_end(ap);
   1811 }
   1812