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