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