Home | History | Annotate | Line # | Download | only in kern
vfs_vnode.c revision 1.8.4.2
      1  1.8.4.2  jruoho /*	$NetBSD: vfs_vnode.c,v 1.8.4.2 2011/06/06 09:09:41 jruoho Exp $	*/
      2  1.8.4.2  jruoho 
      3  1.8.4.2  jruoho /*-
      4  1.8.4.2  jruoho  * Copyright (c) 1997-2011 The NetBSD Foundation, Inc.
      5  1.8.4.2  jruoho  * All rights reserved.
      6  1.8.4.2  jruoho  *
      7  1.8.4.2  jruoho  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8.4.2  jruoho  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.8.4.2  jruoho  * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
     10  1.8.4.2  jruoho  *
     11  1.8.4.2  jruoho  * Redistribution and use in source and binary forms, with or without
     12  1.8.4.2  jruoho  * modification, are permitted provided that the following conditions
     13  1.8.4.2  jruoho  * are met:
     14  1.8.4.2  jruoho  * 1. Redistributions of source code must retain the above copyright
     15  1.8.4.2  jruoho  *    notice, this list of conditions and the following disclaimer.
     16  1.8.4.2  jruoho  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.8.4.2  jruoho  *    notice, this list of conditions and the following disclaimer in the
     18  1.8.4.2  jruoho  *    documentation and/or other materials provided with the distribution.
     19  1.8.4.2  jruoho  *
     20  1.8.4.2  jruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.8.4.2  jruoho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.8.4.2  jruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.8.4.2  jruoho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.8.4.2  jruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.8.4.2  jruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.8.4.2  jruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.8.4.2  jruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.8.4.2  jruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.8.4.2  jruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.8.4.2  jruoho  * POSSIBILITY OF SUCH DAMAGE.
     31  1.8.4.2  jruoho  */
     32  1.8.4.2  jruoho 
     33  1.8.4.2  jruoho /*
     34  1.8.4.2  jruoho  * Copyright (c) 1989, 1993
     35  1.8.4.2  jruoho  *	The Regents of the University of California.  All rights reserved.
     36  1.8.4.2  jruoho  * (c) UNIX System Laboratories, Inc.
     37  1.8.4.2  jruoho  * All or some portions of this file are derived from material licensed
     38  1.8.4.2  jruoho  * to the University of California by American Telephone and Telegraph
     39  1.8.4.2  jruoho  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  1.8.4.2  jruoho  * the permission of UNIX System Laboratories, Inc.
     41  1.8.4.2  jruoho  *
     42  1.8.4.2  jruoho  * Redistribution and use in source and binary forms, with or without
     43  1.8.4.2  jruoho  * modification, are permitted provided that the following conditions
     44  1.8.4.2  jruoho  * are met:
     45  1.8.4.2  jruoho  * 1. Redistributions of source code must retain the above copyright
     46  1.8.4.2  jruoho  *    notice, this list of conditions and the following disclaimer.
     47  1.8.4.2  jruoho  * 2. Redistributions in binary form must reproduce the above copyright
     48  1.8.4.2  jruoho  *    notice, this list of conditions and the following disclaimer in the
     49  1.8.4.2  jruoho  *    documentation and/or other materials provided with the distribution.
     50  1.8.4.2  jruoho  * 3. Neither the name of the University nor the names of its contributors
     51  1.8.4.2  jruoho  *    may be used to endorse or promote products derived from this software
     52  1.8.4.2  jruoho  *    without specific prior written permission.
     53  1.8.4.2  jruoho  *
     54  1.8.4.2  jruoho  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  1.8.4.2  jruoho  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  1.8.4.2  jruoho  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  1.8.4.2  jruoho  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  1.8.4.2  jruoho  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  1.8.4.2  jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  1.8.4.2  jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  1.8.4.2  jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  1.8.4.2  jruoho  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  1.8.4.2  jruoho  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  1.8.4.2  jruoho  * SUCH DAMAGE.
     65  1.8.4.2  jruoho  *
     66  1.8.4.2  jruoho  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
     67  1.8.4.2  jruoho  */
     68  1.8.4.2  jruoho 
     69  1.8.4.2  jruoho /*
     70  1.8.4.2  jruoho  * The vnode cache subsystem.
     71  1.8.4.2  jruoho  *
     72  1.8.4.2  jruoho  * Life-cycle
     73  1.8.4.2  jruoho  *
     74  1.8.4.2  jruoho  *	Normally, there are two points where new vnodes are created:
     75  1.8.4.2  jruoho  *	VOP_CREATE(9) and VOP_LOOKUP(9).  The life-cycle of a vnode
     76  1.8.4.2  jruoho  *	starts in one of the following ways:
     77  1.8.4.2  jruoho  *
     78  1.8.4.2  jruoho  *	- Allocation, via getnewvnode(9) and/or vnalloc(9).
     79  1.8.4.2  jruoho  *	- Recycle from a free list, via getnewvnode(9) -> getcleanvnode(9).
     80  1.8.4.2  jruoho  *	- Reclamation of inactive vnode, via vget(9).
     81  1.8.4.2  jruoho  *
     82  1.8.4.2  jruoho  *	The life-cycle ends when the last reference is dropped, usually
     83  1.8.4.2  jruoho  *	in VOP_REMOVE(9).  In such case, VOP_INACTIVE(9) is called to inform
     84  1.8.4.2  jruoho  *	the file system that vnode is inactive.  Via this call, file system
     85  1.8.4.2  jruoho  *	indicates whether vnode should be recycled (usually, count of links
     86  1.8.4.2  jruoho  *	is checked i.e. whether file was removed).
     87  1.8.4.2  jruoho  *
     88  1.8.4.2  jruoho  *	Depending on indication, vnode can be put into a free list (cache),
     89  1.8.4.2  jruoho  *	or cleaned via vclean(9), which calls VOP_RECLAIM(9) to disassociate
     90  1.8.4.2  jruoho  *	underlying file system from the vnode, and finally destroyed.
     91  1.8.4.2  jruoho  *
     92  1.8.4.2  jruoho  * Reference counting
     93  1.8.4.2  jruoho  *
     94  1.8.4.2  jruoho  *	Vnode is considered active, if reference count (vnode_t::v_usecount)
     95  1.8.4.2  jruoho  *	is non-zero.  It is maintained using: vref(9) and vrele(9), as well
     96  1.8.4.2  jruoho  *	as vput(9), routines.  Common points holding references are e.g.
     97  1.8.4.2  jruoho  *	file openings, current working directory, mount points, etc.
     98  1.8.4.2  jruoho  *
     99  1.8.4.2  jruoho  * Note on v_usecount and its locking
    100  1.8.4.2  jruoho  *
    101  1.8.4.2  jruoho  *	At nearly all points it is known that v_usecount could be zero,
    102  1.8.4.2  jruoho  *	the vnode_t::v_interlock will be held.  To change v_usecount away
    103  1.8.4.2  jruoho  *	from zero, the interlock must be held.  To change from a non-zero
    104  1.8.4.2  jruoho  *	value to zero, again the interlock must be held.
    105  1.8.4.2  jruoho  *
    106  1.8.4.2  jruoho  *	There is a flag bit, VC_XLOCK, embedded in v_usecount.  To raise
    107  1.8.4.2  jruoho  *	v_usecount, if the VC_XLOCK bit is set in it, the interlock must
    108  1.8.4.2  jruoho  *	be held.  To modify the VC_XLOCK bit, the interlock must be held.
    109  1.8.4.2  jruoho  *	We always keep the usecount (v_usecount & VC_MASK) non-zero while
    110  1.8.4.2  jruoho  *	the VC_XLOCK bit is set.
    111  1.8.4.2  jruoho  *
    112  1.8.4.2  jruoho  *	Unless the VC_XLOCK bit is set, changing the usecount from a non-zero
    113  1.8.4.2  jruoho  *	value to a non-zero value can safely be done using atomic operations,
    114  1.8.4.2  jruoho  *	without the interlock held.
    115  1.8.4.2  jruoho  *
    116  1.8.4.2  jruoho  *	Even if the VC_XLOCK bit is set, decreasing the usecount to a non-zero
    117  1.8.4.2  jruoho  *	value can be done using atomic operations, without the interlock held.
    118  1.8.4.2  jruoho  *
    119  1.8.4.2  jruoho  *	Note: if VI_CLEAN is set, vnode_t::v_interlock will be released while
    120  1.8.4.2  jruoho  *	mntvnode_lock is still held.
    121  1.8.4.2  jruoho  */
    122  1.8.4.2  jruoho 
    123  1.8.4.2  jruoho #include <sys/cdefs.h>
    124  1.8.4.2  jruoho __KERNEL_RCSID(0, "$NetBSD: vfs_vnode.c,v 1.8.4.2 2011/06/06 09:09:41 jruoho Exp $");
    125  1.8.4.2  jruoho 
    126  1.8.4.2  jruoho #include <sys/param.h>
    127  1.8.4.2  jruoho #include <sys/kernel.h>
    128  1.8.4.2  jruoho 
    129  1.8.4.2  jruoho #include <sys/atomic.h>
    130  1.8.4.2  jruoho #include <sys/buf.h>
    131  1.8.4.2  jruoho #include <sys/conf.h>
    132  1.8.4.2  jruoho #include <sys/device.h>
    133  1.8.4.2  jruoho #include <sys/kauth.h>
    134  1.8.4.2  jruoho #include <sys/kmem.h>
    135  1.8.4.2  jruoho #include <sys/kthread.h>
    136  1.8.4.2  jruoho #include <sys/module.h>
    137  1.8.4.2  jruoho #include <sys/mount.h>
    138  1.8.4.2  jruoho #include <sys/namei.h>
    139  1.8.4.2  jruoho #include <sys/syscallargs.h>
    140  1.8.4.2  jruoho #include <sys/sysctl.h>
    141  1.8.4.2  jruoho #include <sys/systm.h>
    142  1.8.4.2  jruoho #include <sys/vnode.h>
    143  1.8.4.2  jruoho #include <sys/wapbl.h>
    144  1.8.4.2  jruoho 
    145  1.8.4.2  jruoho #include <uvm/uvm.h>
    146  1.8.4.2  jruoho #include <uvm/uvm_readahead.h>
    147  1.8.4.2  jruoho 
    148  1.8.4.2  jruoho u_int			numvnodes		__cacheline_aligned;
    149  1.8.4.2  jruoho 
    150  1.8.4.2  jruoho static pool_cache_t	vnode_cache		__read_mostly;
    151  1.8.4.2  jruoho static kmutex_t		vnode_free_list_lock	__cacheline_aligned;
    152  1.8.4.2  jruoho 
    153  1.8.4.2  jruoho static vnodelst_t	vnode_free_list		__cacheline_aligned;
    154  1.8.4.2  jruoho static vnodelst_t	vnode_hold_list		__cacheline_aligned;
    155  1.8.4.2  jruoho static vnodelst_t	vrele_list		__cacheline_aligned;
    156  1.8.4.2  jruoho 
    157  1.8.4.2  jruoho static kmutex_t		vrele_lock		__cacheline_aligned;
    158  1.8.4.2  jruoho static kcondvar_t	vrele_cv		__cacheline_aligned;
    159  1.8.4.2  jruoho static lwp_t *		vrele_lwp		__cacheline_aligned;
    160  1.8.4.2  jruoho static int		vrele_pending		__cacheline_aligned;
    161  1.8.4.2  jruoho static int		vrele_gen		__cacheline_aligned;
    162  1.8.4.2  jruoho 
    163  1.8.4.2  jruoho static vnode_t *	getcleanvnode(void);
    164  1.8.4.2  jruoho static void		vrele_thread(void *);
    165  1.8.4.2  jruoho static void		vpanic(vnode_t *, const char *);
    166  1.8.4.2  jruoho 
    167  1.8.4.2  jruoho /* Routines having to do with the management of the vnode table. */
    168  1.8.4.2  jruoho extern int		(**dead_vnodeop_p)(void *);
    169  1.8.4.2  jruoho 
    170  1.8.4.2  jruoho void
    171  1.8.4.2  jruoho vfs_vnode_sysinit(void)
    172  1.8.4.2  jruoho {
    173  1.8.4.2  jruoho 	int error;
    174  1.8.4.2  jruoho 
    175  1.8.4.2  jruoho 	vnode_cache = pool_cache_init(sizeof(vnode_t), 0, 0, 0, "vnodepl",
    176  1.8.4.2  jruoho 	    NULL, IPL_NONE, NULL, NULL, NULL);
    177  1.8.4.2  jruoho 	KASSERT(vnode_cache != NULL);
    178  1.8.4.2  jruoho 
    179  1.8.4.2  jruoho 	mutex_init(&vnode_free_list_lock, MUTEX_DEFAULT, IPL_NONE);
    180  1.8.4.2  jruoho 	TAILQ_INIT(&vnode_free_list);
    181  1.8.4.2  jruoho 	TAILQ_INIT(&vnode_hold_list);
    182  1.8.4.2  jruoho 	TAILQ_INIT(&vrele_list);
    183  1.8.4.2  jruoho 
    184  1.8.4.2  jruoho 	mutex_init(&vrele_lock, MUTEX_DEFAULT, IPL_NONE);
    185  1.8.4.2  jruoho 	cv_init(&vrele_cv, "vrele");
    186  1.8.4.2  jruoho 	error = kthread_create(PRI_VM, KTHREAD_MPSAFE, NULL, vrele_thread,
    187  1.8.4.2  jruoho 	    NULL, &vrele_lwp, "vrele");
    188  1.8.4.2  jruoho 	KASSERT(error == 0);
    189  1.8.4.2  jruoho }
    190  1.8.4.2  jruoho 
    191  1.8.4.2  jruoho /*
    192  1.8.4.2  jruoho  * Allocate a new, uninitialized vnode.  If 'mp' is non-NULL, this is a
    193  1.8.4.2  jruoho  * marker vnode and we are prepared to wait for the allocation.
    194  1.8.4.2  jruoho  */
    195  1.8.4.2  jruoho vnode_t *
    196  1.8.4.2  jruoho vnalloc(struct mount *mp)
    197  1.8.4.2  jruoho {
    198  1.8.4.2  jruoho 	vnode_t *vp;
    199  1.8.4.2  jruoho 
    200  1.8.4.2  jruoho 	vp = pool_cache_get(vnode_cache, (mp != NULL ? PR_WAITOK : PR_NOWAIT));
    201  1.8.4.2  jruoho 	if (vp == NULL) {
    202  1.8.4.2  jruoho 		return NULL;
    203  1.8.4.2  jruoho 	}
    204  1.8.4.2  jruoho 
    205  1.8.4.2  jruoho 	memset(vp, 0, sizeof(*vp));
    206  1.8.4.2  jruoho 	UVM_OBJ_INIT(&vp->v_uobj, &uvm_vnodeops, 0);
    207  1.8.4.2  jruoho 	cv_init(&vp->v_cv, "vnode");
    208  1.8.4.2  jruoho 	/*
    209  1.8.4.2  jruoho 	 * Done by memset() above.
    210  1.8.4.2  jruoho 	 *	LIST_INIT(&vp->v_nclist);
    211  1.8.4.2  jruoho 	 *	LIST_INIT(&vp->v_dnclist);
    212  1.8.4.2  jruoho 	 */
    213  1.8.4.2  jruoho 
    214  1.8.4.2  jruoho 	if (mp != NULL) {
    215  1.8.4.2  jruoho 		vp->v_mount = mp;
    216  1.8.4.2  jruoho 		vp->v_type = VBAD;
    217  1.8.4.2  jruoho 		vp->v_iflag = VI_MARKER;
    218  1.8.4.2  jruoho 	} else {
    219  1.8.4.2  jruoho 		rw_init(&vp->v_lock);
    220  1.8.4.2  jruoho 	}
    221  1.8.4.2  jruoho 
    222  1.8.4.2  jruoho 	return vp;
    223  1.8.4.2  jruoho }
    224  1.8.4.2  jruoho 
    225  1.8.4.2  jruoho /*
    226  1.8.4.2  jruoho  * Free an unused, unreferenced vnode.
    227  1.8.4.2  jruoho  */
    228  1.8.4.2  jruoho void
    229  1.8.4.2  jruoho vnfree(vnode_t *vp)
    230  1.8.4.2  jruoho {
    231  1.8.4.2  jruoho 
    232  1.8.4.2  jruoho 	KASSERT(vp->v_usecount == 0);
    233  1.8.4.2  jruoho 
    234  1.8.4.2  jruoho 	if ((vp->v_iflag & VI_MARKER) == 0) {
    235  1.8.4.2  jruoho 		rw_destroy(&vp->v_lock);
    236  1.8.4.2  jruoho 		mutex_enter(&vnode_free_list_lock);
    237  1.8.4.2  jruoho 		numvnodes--;
    238  1.8.4.2  jruoho 		mutex_exit(&vnode_free_list_lock);
    239  1.8.4.2  jruoho 	}
    240  1.8.4.2  jruoho 
    241  1.8.4.2  jruoho 	UVM_OBJ_DESTROY(&vp->v_uobj);
    242  1.8.4.2  jruoho 	cv_destroy(&vp->v_cv);
    243  1.8.4.2  jruoho 	pool_cache_put(vnode_cache, vp);
    244  1.8.4.2  jruoho }
    245  1.8.4.2  jruoho 
    246  1.8.4.2  jruoho /*
    247  1.8.4.2  jruoho  * getcleanvnode: grab a vnode from freelist and clean it.
    248  1.8.4.2  jruoho  *
    249  1.8.4.2  jruoho  * => Releases vnode_free_list_lock.
    250  1.8.4.2  jruoho  * => Returns referenced vnode on success.
    251  1.8.4.2  jruoho  */
    252  1.8.4.2  jruoho static vnode_t *
    253  1.8.4.2  jruoho getcleanvnode(void)
    254  1.8.4.2  jruoho {
    255  1.8.4.2  jruoho 	vnode_t *vp;
    256  1.8.4.2  jruoho 	vnodelst_t *listhd;
    257  1.8.4.2  jruoho 
    258  1.8.4.2  jruoho 	KASSERT(mutex_owned(&vnode_free_list_lock));
    259  1.8.4.2  jruoho retry:
    260  1.8.4.2  jruoho 	listhd = &vnode_free_list;
    261  1.8.4.2  jruoho try_nextlist:
    262  1.8.4.2  jruoho 	TAILQ_FOREACH(vp, listhd, v_freelist) {
    263  1.8.4.2  jruoho 		/*
    264  1.8.4.2  jruoho 		 * It's safe to test v_usecount and v_iflag
    265  1.8.4.2  jruoho 		 * without holding the interlock here, since
    266  1.8.4.2  jruoho 		 * these vnodes should never appear on the
    267  1.8.4.2  jruoho 		 * lists.
    268  1.8.4.2  jruoho 		 */
    269  1.8.4.2  jruoho 		KASSERT(vp->v_usecount == 0);
    270  1.8.4.2  jruoho 		KASSERT((vp->v_iflag & VI_CLEAN) == 0);
    271  1.8.4.2  jruoho 		KASSERT(vp->v_freelisthd == listhd);
    272  1.8.4.2  jruoho 
    273  1.8.4.2  jruoho 		if (!mutex_tryenter(&vp->v_interlock))
    274  1.8.4.2  jruoho 			continue;
    275  1.8.4.2  jruoho 		if ((vp->v_iflag & VI_XLOCK) == 0)
    276  1.8.4.2  jruoho 			break;
    277  1.8.4.2  jruoho 		mutex_exit(&vp->v_interlock);
    278  1.8.4.2  jruoho 	}
    279  1.8.4.2  jruoho 
    280  1.8.4.2  jruoho 	if (vp == NULL) {
    281  1.8.4.2  jruoho 		if (listhd == &vnode_free_list) {
    282  1.8.4.2  jruoho 			listhd = &vnode_hold_list;
    283  1.8.4.2  jruoho 			goto try_nextlist;
    284  1.8.4.2  jruoho 		}
    285  1.8.4.2  jruoho 		mutex_exit(&vnode_free_list_lock);
    286  1.8.4.2  jruoho 		return NULL;
    287  1.8.4.2  jruoho 	}
    288  1.8.4.2  jruoho 
    289  1.8.4.2  jruoho 	/* Remove it from the freelist. */
    290  1.8.4.2  jruoho 	TAILQ_REMOVE(listhd, vp, v_freelist);
    291  1.8.4.2  jruoho 	vp->v_freelisthd = NULL;
    292  1.8.4.2  jruoho 	mutex_exit(&vnode_free_list_lock);
    293  1.8.4.2  jruoho 
    294  1.8.4.2  jruoho 	KASSERT(vp->v_usecount == 0);
    295  1.8.4.2  jruoho 
    296  1.8.4.2  jruoho 	/*
    297  1.8.4.2  jruoho 	 * The vnode is still associated with a file system, so we must
    298  1.8.4.2  jruoho 	 * clean it out before reusing it.  We need to add a reference
    299  1.8.4.2  jruoho 	 * before doing this.  If the vnode gains another reference while
    300  1.8.4.2  jruoho 	 * being cleaned out then we lose - retry.
    301  1.8.4.2  jruoho 	 */
    302  1.8.4.2  jruoho 	atomic_add_int(&vp->v_usecount, 1 + VC_XLOCK);
    303  1.8.4.2  jruoho 	vclean(vp, DOCLOSE);
    304  1.8.4.2  jruoho 	KASSERT(vp->v_usecount >= 1 + VC_XLOCK);
    305  1.8.4.2  jruoho 	atomic_add_int(&vp->v_usecount, -VC_XLOCK);
    306  1.8.4.2  jruoho 	if (vp->v_usecount == 1) {
    307  1.8.4.2  jruoho 		/* We're about to dirty it. */
    308  1.8.4.2  jruoho 		vp->v_iflag &= ~VI_CLEAN;
    309  1.8.4.2  jruoho 		mutex_exit(&vp->v_interlock);
    310  1.8.4.2  jruoho 		if (vp->v_type == VBLK || vp->v_type == VCHR) {
    311  1.8.4.2  jruoho 			spec_node_destroy(vp);
    312  1.8.4.2  jruoho 		}
    313  1.8.4.2  jruoho 		vp->v_type = VNON;
    314  1.8.4.2  jruoho 	} else {
    315  1.8.4.2  jruoho 		/*
    316  1.8.4.2  jruoho 		 * Don't return to freelist - the holder of the last
    317  1.8.4.2  jruoho 		 * reference will destroy it.
    318  1.8.4.2  jruoho 		 */
    319  1.8.4.2  jruoho 		vrelel(vp, 0); /* releases vp->v_interlock */
    320  1.8.4.2  jruoho 		mutex_enter(&vnode_free_list_lock);
    321  1.8.4.2  jruoho 		goto retry;
    322  1.8.4.2  jruoho 	}
    323  1.8.4.2  jruoho 
    324  1.8.4.2  jruoho 	KASSERT(vp->v_data == NULL);
    325  1.8.4.2  jruoho 	KASSERT(vp->v_uobj.uo_npages == 0);
    326  1.8.4.2  jruoho 	KASSERT(TAILQ_EMPTY(&vp->v_uobj.memq));
    327  1.8.4.2  jruoho 	KASSERT(vp->v_numoutput == 0);
    328  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
    329  1.8.4.2  jruoho 
    330  1.8.4.2  jruoho 	return vp;
    331  1.8.4.2  jruoho }
    332  1.8.4.2  jruoho 
    333  1.8.4.2  jruoho /*
    334  1.8.4.2  jruoho  * getnewvnode: return the next vnode from the free list.
    335  1.8.4.2  jruoho  *
    336  1.8.4.2  jruoho  * => Returns referenced vnode, moved into the mount queue.
    337  1.8.4.2  jruoho  */
    338  1.8.4.2  jruoho int
    339  1.8.4.2  jruoho getnewvnode(enum vtagtype tag, struct mount *mp, int (**vops)(void *),
    340  1.8.4.2  jruoho 	    vnode_t **vpp)
    341  1.8.4.2  jruoho {
    342  1.8.4.2  jruoho 	struct uvm_object *uobj;
    343  1.8.4.2  jruoho 	static int toggle;
    344  1.8.4.2  jruoho 	vnode_t *vp;
    345  1.8.4.2  jruoho 	int error = 0, tryalloc;
    346  1.8.4.2  jruoho 
    347  1.8.4.2  jruoho try_again:
    348  1.8.4.2  jruoho 	if (mp != NULL) {
    349  1.8.4.2  jruoho 		/*
    350  1.8.4.2  jruoho 		 * Mark filesystem busy while we are creating a vnode.
    351  1.8.4.2  jruoho 		 * If unmount is in progress, this will fail.
    352  1.8.4.2  jruoho 		 */
    353  1.8.4.2  jruoho 		error = vfs_busy(mp, NULL);
    354  1.8.4.2  jruoho 		if (error)
    355  1.8.4.2  jruoho 			return error;
    356  1.8.4.2  jruoho 	}
    357  1.8.4.2  jruoho 
    358  1.8.4.2  jruoho 	/*
    359  1.8.4.2  jruoho 	 * We must choose whether to allocate a new vnode or recycle an
    360  1.8.4.2  jruoho 	 * existing one. The criterion for allocating a new one is that
    361  1.8.4.2  jruoho 	 * the total number of vnodes is less than the number desired or
    362  1.8.4.2  jruoho 	 * there are no vnodes on either free list. Generally we only
    363  1.8.4.2  jruoho 	 * want to recycle vnodes that have no buffers associated with
    364  1.8.4.2  jruoho 	 * them, so we look first on the vnode_free_list. If it is empty,
    365  1.8.4.2  jruoho 	 * we next consider vnodes with referencing buffers on the
    366  1.8.4.2  jruoho 	 * vnode_hold_list. The toggle ensures that half the time we
    367  1.8.4.2  jruoho 	 * will use a buffer from the vnode_hold_list, and half the time
    368  1.8.4.2  jruoho 	 * we will allocate a new one unless the list has grown to twice
    369  1.8.4.2  jruoho 	 * the desired size. We are reticent to recycle vnodes from the
    370  1.8.4.2  jruoho 	 * vnode_hold_list because we will lose the identity of all its
    371  1.8.4.2  jruoho 	 * referencing buffers.
    372  1.8.4.2  jruoho 	 */
    373  1.8.4.2  jruoho 
    374  1.8.4.2  jruoho 	vp = NULL;
    375  1.8.4.2  jruoho 
    376  1.8.4.2  jruoho 	mutex_enter(&vnode_free_list_lock);
    377  1.8.4.2  jruoho 
    378  1.8.4.2  jruoho 	toggle ^= 1;
    379  1.8.4.2  jruoho 	if (numvnodes > 2 * desiredvnodes)
    380  1.8.4.2  jruoho 		toggle = 0;
    381  1.8.4.2  jruoho 
    382  1.8.4.2  jruoho 	tryalloc = numvnodes < desiredvnodes ||
    383  1.8.4.2  jruoho 	    (TAILQ_FIRST(&vnode_free_list) == NULL &&
    384  1.8.4.2  jruoho 	    (TAILQ_FIRST(&vnode_hold_list) == NULL || toggle));
    385  1.8.4.2  jruoho 
    386  1.8.4.2  jruoho 	if (tryalloc) {
    387  1.8.4.2  jruoho 		/* Allocate a new vnode. */
    388  1.8.4.2  jruoho 		numvnodes++;
    389  1.8.4.2  jruoho 		mutex_exit(&vnode_free_list_lock);
    390  1.8.4.2  jruoho 		if ((vp = vnalloc(NULL)) == NULL) {
    391  1.8.4.2  jruoho 			mutex_enter(&vnode_free_list_lock);
    392  1.8.4.2  jruoho 			numvnodes--;
    393  1.8.4.2  jruoho 		} else
    394  1.8.4.2  jruoho 			vp->v_usecount = 1;
    395  1.8.4.2  jruoho 	}
    396  1.8.4.2  jruoho 
    397  1.8.4.2  jruoho 	if (vp == NULL) {
    398  1.8.4.2  jruoho 		/* Recycle and get vnode clean. */
    399  1.8.4.2  jruoho 		vp = getcleanvnode();
    400  1.8.4.2  jruoho 		if (vp == NULL) {
    401  1.8.4.2  jruoho 			if (mp != NULL) {
    402  1.8.4.2  jruoho 				vfs_unbusy(mp, false, NULL);
    403  1.8.4.2  jruoho 			}
    404  1.8.4.2  jruoho 			if (tryalloc) {
    405  1.8.4.2  jruoho 				printf("WARNING: unable to allocate new "
    406  1.8.4.2  jruoho 				    "vnode, retrying...\n");
    407  1.8.4.2  jruoho 				kpause("newvn", false, hz, NULL);
    408  1.8.4.2  jruoho 				goto try_again;
    409  1.8.4.2  jruoho 			}
    410  1.8.4.2  jruoho 			tablefull("vnode", "increase kern.maxvnodes or NVNODE");
    411  1.8.4.2  jruoho 			*vpp = 0;
    412  1.8.4.2  jruoho 			return ENFILE;
    413  1.8.4.2  jruoho 		}
    414  1.8.4.2  jruoho 		vp->v_iflag = 0;
    415  1.8.4.2  jruoho 		vp->v_vflag = 0;
    416  1.8.4.2  jruoho 		vp->v_uflag = 0;
    417  1.8.4.2  jruoho 		vp->v_socket = NULL;
    418  1.8.4.2  jruoho 	}
    419  1.8.4.2  jruoho 
    420  1.8.4.2  jruoho 	KASSERT(vp->v_usecount == 1);
    421  1.8.4.2  jruoho 	KASSERT(vp->v_freelisthd == NULL);
    422  1.8.4.2  jruoho 	KASSERT(LIST_EMPTY(&vp->v_nclist));
    423  1.8.4.2  jruoho 	KASSERT(LIST_EMPTY(&vp->v_dnclist));
    424  1.8.4.2  jruoho 
    425  1.8.4.2  jruoho 	/* Initialize vnode. */
    426  1.8.4.2  jruoho 	vp->v_type = VNON;
    427  1.8.4.2  jruoho 	vp->v_tag = tag;
    428  1.8.4.2  jruoho 	vp->v_op = vops;
    429  1.8.4.2  jruoho 	vp->v_data = NULL;
    430  1.8.4.2  jruoho 
    431  1.8.4.2  jruoho 	uobj = &vp->v_uobj;
    432  1.8.4.2  jruoho 	KASSERT(uobj->pgops == &uvm_vnodeops);
    433  1.8.4.2  jruoho 	KASSERT(uobj->uo_npages == 0);
    434  1.8.4.2  jruoho 	KASSERT(TAILQ_FIRST(&uobj->memq) == NULL);
    435  1.8.4.2  jruoho 	vp->v_size = vp->v_writesize = VSIZENOTSET;
    436  1.8.4.2  jruoho 
    437  1.8.4.2  jruoho 	/* Finally, move vnode into the mount queue. */
    438  1.8.4.2  jruoho 	vfs_insmntque(vp, mp);
    439  1.8.4.2  jruoho 
    440  1.8.4.2  jruoho 	if (mp != NULL) {
    441  1.8.4.2  jruoho 		if ((mp->mnt_iflag & IMNT_MPSAFE) != 0)
    442  1.8.4.2  jruoho 			vp->v_vflag |= VV_MPSAFE;
    443  1.8.4.2  jruoho 		vfs_unbusy(mp, true, NULL);
    444  1.8.4.2  jruoho 	}
    445  1.8.4.2  jruoho 
    446  1.8.4.2  jruoho 	*vpp = vp;
    447  1.8.4.2  jruoho 	return 0;
    448  1.8.4.2  jruoho }
    449  1.8.4.2  jruoho 
    450  1.8.4.2  jruoho /*
    451  1.8.4.2  jruoho  * This is really just the reverse of getnewvnode(). Needed for
    452  1.8.4.2  jruoho  * VFS_VGET functions who may need to push back a vnode in case
    453  1.8.4.2  jruoho  * of a locking race.
    454  1.8.4.2  jruoho  */
    455  1.8.4.2  jruoho void
    456  1.8.4.2  jruoho ungetnewvnode(vnode_t *vp)
    457  1.8.4.2  jruoho {
    458  1.8.4.2  jruoho 
    459  1.8.4.2  jruoho 	KASSERT(vp->v_usecount == 1);
    460  1.8.4.2  jruoho 	KASSERT(vp->v_data == NULL);
    461  1.8.4.2  jruoho 	KASSERT(vp->v_freelisthd == NULL);
    462  1.8.4.2  jruoho 
    463  1.8.4.2  jruoho 	mutex_enter(&vp->v_interlock);
    464  1.8.4.2  jruoho 	vp->v_iflag |= VI_CLEAN;
    465  1.8.4.2  jruoho 	vrelel(vp, 0);
    466  1.8.4.2  jruoho }
    467  1.8.4.2  jruoho 
    468  1.8.4.2  jruoho /*
    469  1.8.4.2  jruoho  * Remove a vnode from its freelist.
    470  1.8.4.2  jruoho  */
    471  1.8.4.2  jruoho void
    472  1.8.4.2  jruoho vremfree(vnode_t *vp)
    473  1.8.4.2  jruoho {
    474  1.8.4.2  jruoho 
    475  1.8.4.2  jruoho 	KASSERT(mutex_owned(&vp->v_interlock));
    476  1.8.4.2  jruoho 	KASSERT(vp->v_usecount == 0);
    477  1.8.4.2  jruoho 
    478  1.8.4.2  jruoho 	/*
    479  1.8.4.2  jruoho 	 * Note that the reference count must not change until
    480  1.8.4.2  jruoho 	 * the vnode is removed.
    481  1.8.4.2  jruoho 	 */
    482  1.8.4.2  jruoho 	mutex_enter(&vnode_free_list_lock);
    483  1.8.4.2  jruoho 	if (vp->v_holdcnt > 0) {
    484  1.8.4.2  jruoho 		KASSERT(vp->v_freelisthd == &vnode_hold_list);
    485  1.8.4.2  jruoho 	} else {
    486  1.8.4.2  jruoho 		KASSERT(vp->v_freelisthd == &vnode_free_list);
    487  1.8.4.2  jruoho 	}
    488  1.8.4.2  jruoho 	TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
    489  1.8.4.2  jruoho 	vp->v_freelisthd = NULL;
    490  1.8.4.2  jruoho 	mutex_exit(&vnode_free_list_lock);
    491  1.8.4.2  jruoho }
    492  1.8.4.2  jruoho 
    493  1.8.4.2  jruoho /*
    494  1.8.4.2  jruoho  * Try to gain a reference to a vnode, without acquiring its interlock.
    495  1.8.4.2  jruoho  * The caller must hold a lock that will prevent the vnode from being
    496  1.8.4.2  jruoho  * recycled or freed.
    497  1.8.4.2  jruoho  */
    498  1.8.4.2  jruoho bool
    499  1.8.4.2  jruoho vtryget(vnode_t *vp)
    500  1.8.4.2  jruoho {
    501  1.8.4.2  jruoho 	u_int use, next;
    502  1.8.4.2  jruoho 
    503  1.8.4.2  jruoho 	/*
    504  1.8.4.2  jruoho 	 * If the vnode is being freed, don't make life any harder
    505  1.8.4.2  jruoho 	 * for vclean() by adding another reference without waiting.
    506  1.8.4.2  jruoho 	 * This is not strictly necessary, but we'll do it anyway.
    507  1.8.4.2  jruoho 	 */
    508  1.8.4.2  jruoho 	if (__predict_false((vp->v_iflag & VI_XLOCK) != 0)) {
    509  1.8.4.2  jruoho 		return false;
    510  1.8.4.2  jruoho 	}
    511  1.8.4.2  jruoho 	for (use = vp->v_usecount;; use = next) {
    512  1.8.4.2  jruoho 		if (use == 0 || __predict_false((use & VC_XLOCK) != 0)) {
    513  1.8.4.2  jruoho 			/* Need interlock held if first reference. */
    514  1.8.4.2  jruoho 			return false;
    515  1.8.4.2  jruoho 		}
    516  1.8.4.2  jruoho 		next = atomic_cas_uint(&vp->v_usecount, use, use + 1);
    517  1.8.4.2  jruoho 		if (__predict_true(next == use)) {
    518  1.8.4.2  jruoho 			return true;
    519  1.8.4.2  jruoho 		}
    520  1.8.4.2  jruoho 	}
    521  1.8.4.2  jruoho }
    522  1.8.4.2  jruoho 
    523  1.8.4.2  jruoho /*
    524  1.8.4.2  jruoho  * vget: get a particular vnode from the free list, increment its reference
    525  1.8.4.2  jruoho  * count and lock it.
    526  1.8.4.2  jruoho  *
    527  1.8.4.2  jruoho  * => Should be called with v_interlock held.
    528  1.8.4.2  jruoho  *
    529  1.8.4.2  jruoho  * If VI_XLOCK is set, the vnode is being eliminated in vgone()/vclean().
    530  1.8.4.2  jruoho  * In that case, we cannot grab the vnode, so the process is awakened when
    531  1.8.4.2  jruoho  * the transition is completed, and an error returned to indicate that the
    532  1.8.4.2  jruoho  * vnode is no longer usable (e.g. changed to a new file system type).
    533  1.8.4.2  jruoho  */
    534  1.8.4.2  jruoho int
    535  1.8.4.2  jruoho vget(vnode_t *vp, int flags)
    536  1.8.4.2  jruoho {
    537  1.8.4.2  jruoho 	int error = 0;
    538  1.8.4.2  jruoho 
    539  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
    540  1.8.4.2  jruoho 	KASSERT(mutex_owned(&vp->v_interlock));
    541  1.8.4.2  jruoho 	KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT)) == 0);
    542  1.8.4.2  jruoho 
    543  1.8.4.2  jruoho 	/*
    544  1.8.4.2  jruoho 	 * Before adding a reference, we must remove the vnode
    545  1.8.4.2  jruoho 	 * from its freelist.
    546  1.8.4.2  jruoho 	 */
    547  1.8.4.2  jruoho 	if (vp->v_usecount == 0) {
    548  1.8.4.2  jruoho 		vremfree(vp);
    549  1.8.4.2  jruoho 		vp->v_usecount = 1;
    550  1.8.4.2  jruoho 	} else {
    551  1.8.4.2  jruoho 		atomic_inc_uint(&vp->v_usecount);
    552  1.8.4.2  jruoho 	}
    553  1.8.4.2  jruoho 
    554  1.8.4.2  jruoho 	/*
    555  1.8.4.2  jruoho 	 * If the vnode is in the process of being cleaned out for
    556  1.8.4.2  jruoho 	 * another use, we wait for the cleaning to finish and then
    557  1.8.4.2  jruoho 	 * return failure.  Cleaning is determined by checking if
    558  1.8.4.2  jruoho 	 * the VI_XLOCK flag is set.
    559  1.8.4.2  jruoho 	 */
    560  1.8.4.2  jruoho 	if ((vp->v_iflag & VI_XLOCK) != 0) {
    561  1.8.4.2  jruoho 		if ((flags & LK_NOWAIT) != 0) {
    562  1.8.4.2  jruoho 			vrelel(vp, 0);
    563  1.8.4.2  jruoho 			return EBUSY;
    564  1.8.4.2  jruoho 		}
    565  1.8.4.2  jruoho 		vwait(vp, VI_XLOCK);
    566  1.8.4.2  jruoho 		vrelel(vp, 0);
    567  1.8.4.2  jruoho 		return ENOENT;
    568  1.8.4.2  jruoho 	}
    569  1.8.4.2  jruoho 
    570  1.8.4.2  jruoho 	/*
    571  1.8.4.2  jruoho 	 * Ok, we got it in good shape.  Just locking left.
    572  1.8.4.2  jruoho 	 */
    573  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_CLEAN) == 0);
    574  1.8.4.2  jruoho 	mutex_exit(&vp->v_interlock);
    575  1.8.4.2  jruoho 	if (flags & (LK_EXCLUSIVE | LK_SHARED)) {
    576  1.8.4.2  jruoho 		error = vn_lock(vp, flags);
    577  1.8.4.2  jruoho 		if (error != 0) {
    578  1.8.4.2  jruoho 			vrele(vp);
    579  1.8.4.2  jruoho 		}
    580  1.8.4.2  jruoho 	}
    581  1.8.4.2  jruoho 	return error;
    582  1.8.4.2  jruoho }
    583  1.8.4.2  jruoho 
    584  1.8.4.2  jruoho /*
    585  1.8.4.2  jruoho  * vput: unlock and release the reference.
    586  1.8.4.2  jruoho  */
    587  1.8.4.2  jruoho void
    588  1.8.4.2  jruoho vput(vnode_t *vp)
    589  1.8.4.2  jruoho {
    590  1.8.4.2  jruoho 
    591  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
    592  1.8.4.2  jruoho 
    593  1.8.4.2  jruoho 	VOP_UNLOCK(vp);
    594  1.8.4.2  jruoho 	vrele(vp);
    595  1.8.4.2  jruoho }
    596  1.8.4.2  jruoho 
    597  1.8.4.2  jruoho /*
    598  1.8.4.2  jruoho  * Try to drop reference on a vnode.  Abort if we are releasing the
    599  1.8.4.2  jruoho  * last reference.  Note: this _must_ succeed if not the last reference.
    600  1.8.4.2  jruoho  */
    601  1.8.4.2  jruoho static inline bool
    602  1.8.4.2  jruoho vtryrele(vnode_t *vp)
    603  1.8.4.2  jruoho {
    604  1.8.4.2  jruoho 	u_int use, next;
    605  1.8.4.2  jruoho 
    606  1.8.4.2  jruoho 	for (use = vp->v_usecount;; use = next) {
    607  1.8.4.2  jruoho 		if (use == 1) {
    608  1.8.4.2  jruoho 			return false;
    609  1.8.4.2  jruoho 		}
    610  1.8.4.2  jruoho 		KASSERT((use & VC_MASK) > 1);
    611  1.8.4.2  jruoho 		next = atomic_cas_uint(&vp->v_usecount, use, use - 1);
    612  1.8.4.2  jruoho 		if (__predict_true(next == use)) {
    613  1.8.4.2  jruoho 			return true;
    614  1.8.4.2  jruoho 		}
    615  1.8.4.2  jruoho 	}
    616  1.8.4.2  jruoho }
    617  1.8.4.2  jruoho 
    618  1.8.4.2  jruoho /*
    619  1.8.4.2  jruoho  * Vnode release.  If reference count drops to zero, call inactive
    620  1.8.4.2  jruoho  * routine and either return to freelist or free to the pool.
    621  1.8.4.2  jruoho  */
    622  1.8.4.2  jruoho void
    623  1.8.4.2  jruoho vrelel(vnode_t *vp, int flags)
    624  1.8.4.2  jruoho {
    625  1.8.4.2  jruoho 	bool recycle, defer;
    626  1.8.4.2  jruoho 	int error;
    627  1.8.4.2  jruoho 
    628  1.8.4.2  jruoho 	KASSERT(mutex_owned(&vp->v_interlock));
    629  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
    630  1.8.4.2  jruoho 	KASSERT(vp->v_freelisthd == NULL);
    631  1.8.4.2  jruoho 
    632  1.8.4.2  jruoho 	if (__predict_false(vp->v_op == dead_vnodeop_p &&
    633  1.8.4.2  jruoho 	    (vp->v_iflag & (VI_CLEAN|VI_XLOCK)) == 0)) {
    634  1.8.4.2  jruoho 		vpanic(vp, "dead but not clean");
    635  1.8.4.2  jruoho 	}
    636  1.8.4.2  jruoho 
    637  1.8.4.2  jruoho 	/*
    638  1.8.4.2  jruoho 	 * If not the last reference, just drop the reference count
    639  1.8.4.2  jruoho 	 * and unlock.
    640  1.8.4.2  jruoho 	 */
    641  1.8.4.2  jruoho 	if (vtryrele(vp)) {
    642  1.8.4.2  jruoho 		vp->v_iflag |= VI_INACTREDO;
    643  1.8.4.2  jruoho 		mutex_exit(&vp->v_interlock);
    644  1.8.4.2  jruoho 		return;
    645  1.8.4.2  jruoho 	}
    646  1.8.4.2  jruoho 	if (vp->v_usecount <= 0 || vp->v_writecount != 0) {
    647  1.8.4.2  jruoho 		vpanic(vp, "vrelel: bad ref count");
    648  1.8.4.2  jruoho 	}
    649  1.8.4.2  jruoho 
    650  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_XLOCK) == 0);
    651  1.8.4.2  jruoho 
    652  1.8.4.2  jruoho 	/*
    653  1.8.4.2  jruoho 	 * If not clean, deactivate the vnode, but preserve
    654  1.8.4.2  jruoho 	 * our reference across the call to VOP_INACTIVE().
    655  1.8.4.2  jruoho 	 */
    656  1.8.4.2  jruoho retry:
    657  1.8.4.2  jruoho 	if ((vp->v_iflag & VI_CLEAN) == 0) {
    658  1.8.4.2  jruoho 		recycle = false;
    659  1.8.4.2  jruoho 		vp->v_iflag |= VI_INACTNOW;
    660  1.8.4.2  jruoho 
    661  1.8.4.2  jruoho 		/*
    662  1.8.4.2  jruoho 		 * XXX This ugly block can be largely eliminated if
    663  1.8.4.2  jruoho 		 * locking is pushed down into the file systems.
    664  1.8.4.2  jruoho 		 *
    665  1.8.4.2  jruoho 		 * Defer vnode release to vrele_thread if caller
    666  1.8.4.2  jruoho 		 * requests it explicitly.
    667  1.8.4.2  jruoho 		 */
    668  1.8.4.2  jruoho 		if ((curlwp == uvm.pagedaemon_lwp) ||
    669  1.8.4.2  jruoho 		    (flags & VRELEL_ASYNC_RELE) != 0) {
    670  1.8.4.2  jruoho 			/* The pagedaemon can't wait around; defer. */
    671  1.8.4.2  jruoho 			defer = true;
    672  1.8.4.2  jruoho 		} else if (curlwp == vrele_lwp) {
    673  1.8.4.2  jruoho 			/* We have to try harder. */
    674  1.8.4.2  jruoho 			vp->v_iflag &= ~VI_INACTREDO;
    675  1.8.4.2  jruoho 			mutex_exit(&vp->v_interlock);
    676  1.8.4.2  jruoho 			error = vn_lock(vp, LK_EXCLUSIVE);
    677  1.8.4.2  jruoho 			if (error != 0) {
    678  1.8.4.2  jruoho 				/* XXX */
    679  1.8.4.2  jruoho 				vpanic(vp, "vrele: unable to lock %p");
    680  1.8.4.2  jruoho 			}
    681  1.8.4.2  jruoho 			defer = false;
    682  1.8.4.2  jruoho 		} else if ((vp->v_iflag & VI_LAYER) != 0) {
    683  1.8.4.2  jruoho 			/*
    684  1.8.4.2  jruoho 			 * Acquiring the stack's lock in vclean() even
    685  1.8.4.2  jruoho 			 * for an honest vput/vrele is dangerous because
    686  1.8.4.2  jruoho 			 * our caller may hold other vnode locks; defer.
    687  1.8.4.2  jruoho 			 */
    688  1.8.4.2  jruoho 			defer = true;
    689  1.8.4.2  jruoho 		} else {
    690  1.8.4.2  jruoho 			/* If we can't acquire the lock, then defer. */
    691  1.8.4.2  jruoho 			vp->v_iflag &= ~VI_INACTREDO;
    692  1.8.4.2  jruoho 			mutex_exit(&vp->v_interlock);
    693  1.8.4.2  jruoho 			error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
    694  1.8.4.2  jruoho 			if (error != 0) {
    695  1.8.4.2  jruoho 				defer = true;
    696  1.8.4.2  jruoho 				mutex_enter(&vp->v_interlock);
    697  1.8.4.2  jruoho 			} else {
    698  1.8.4.2  jruoho 				defer = false;
    699  1.8.4.2  jruoho 			}
    700  1.8.4.2  jruoho 		}
    701  1.8.4.2  jruoho 
    702  1.8.4.2  jruoho 		if (defer) {
    703  1.8.4.2  jruoho 			/*
    704  1.8.4.2  jruoho 			 * Defer reclaim to the kthread; it's not safe to
    705  1.8.4.2  jruoho 			 * clean it here.  We donate it our last reference.
    706  1.8.4.2  jruoho 			 */
    707  1.8.4.2  jruoho 			KASSERT(mutex_owned(&vp->v_interlock));
    708  1.8.4.2  jruoho 			KASSERT((vp->v_iflag & VI_INACTPEND) == 0);
    709  1.8.4.2  jruoho 			vp->v_iflag &= ~VI_INACTNOW;
    710  1.8.4.2  jruoho 			vp->v_iflag |= VI_INACTPEND;
    711  1.8.4.2  jruoho 			mutex_enter(&vrele_lock);
    712  1.8.4.2  jruoho 			TAILQ_INSERT_TAIL(&vrele_list, vp, v_freelist);
    713  1.8.4.2  jruoho 			if (++vrele_pending > (desiredvnodes >> 8))
    714  1.8.4.2  jruoho 				cv_signal(&vrele_cv);
    715  1.8.4.2  jruoho 			mutex_exit(&vrele_lock);
    716  1.8.4.2  jruoho 			mutex_exit(&vp->v_interlock);
    717  1.8.4.2  jruoho 			return;
    718  1.8.4.2  jruoho 		}
    719  1.8.4.2  jruoho 
    720  1.8.4.2  jruoho #ifdef DIAGNOSTIC
    721  1.8.4.2  jruoho 		if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
    722  1.8.4.2  jruoho 		    vp->v_specnode != NULL && vp->v_specnode->sn_opencnt != 0) {
    723  1.8.4.2  jruoho 			vprint("vrelel: missing VOP_CLOSE()", vp);
    724  1.8.4.2  jruoho 		}
    725  1.8.4.2  jruoho #endif
    726  1.8.4.2  jruoho 
    727  1.8.4.2  jruoho 		/*
    728  1.8.4.2  jruoho 		 * The vnode can gain another reference while being
    729  1.8.4.2  jruoho 		 * deactivated.  If VOP_INACTIVE() indicates that
    730  1.8.4.2  jruoho 		 * the described file has been deleted, then recycle
    731  1.8.4.2  jruoho 		 * the vnode irrespective of additional references.
    732  1.8.4.2  jruoho 		 * Another thread may be waiting to re-use the on-disk
    733  1.8.4.2  jruoho 		 * inode.
    734  1.8.4.2  jruoho 		 *
    735  1.8.4.2  jruoho 		 * Note that VOP_INACTIVE() will drop the vnode lock.
    736  1.8.4.2  jruoho 		 */
    737  1.8.4.2  jruoho 		VOP_INACTIVE(vp, &recycle);
    738  1.8.4.2  jruoho 		mutex_enter(&vp->v_interlock);
    739  1.8.4.2  jruoho 		vp->v_iflag &= ~VI_INACTNOW;
    740  1.8.4.2  jruoho 		if (!recycle) {
    741  1.8.4.2  jruoho 			if (vtryrele(vp)) {
    742  1.8.4.2  jruoho 				mutex_exit(&vp->v_interlock);
    743  1.8.4.2  jruoho 				return;
    744  1.8.4.2  jruoho 			}
    745  1.8.4.2  jruoho 
    746  1.8.4.2  jruoho 			/*
    747  1.8.4.2  jruoho 			 * If we grew another reference while
    748  1.8.4.2  jruoho 			 * VOP_INACTIVE() was underway, retry.
    749  1.8.4.2  jruoho 			 */
    750  1.8.4.2  jruoho 			if ((vp->v_iflag & VI_INACTREDO) != 0) {
    751  1.8.4.2  jruoho 				goto retry;
    752  1.8.4.2  jruoho 			}
    753  1.8.4.2  jruoho 		}
    754  1.8.4.2  jruoho 
    755  1.8.4.2  jruoho 		/* Take care of space accounting. */
    756  1.8.4.2  jruoho 		if (vp->v_iflag & VI_EXECMAP) {
    757  1.8.4.2  jruoho 			atomic_add_int(&uvmexp.execpages,
    758  1.8.4.2  jruoho 			    -vp->v_uobj.uo_npages);
    759  1.8.4.2  jruoho 			atomic_add_int(&uvmexp.filepages,
    760  1.8.4.2  jruoho 			    vp->v_uobj.uo_npages);
    761  1.8.4.2  jruoho 		}
    762  1.8.4.2  jruoho 		vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP|VI_WRMAP);
    763  1.8.4.2  jruoho 		vp->v_vflag &= ~VV_MAPPED;
    764  1.8.4.2  jruoho 
    765  1.8.4.2  jruoho 		/*
    766  1.8.4.2  jruoho 		 * Recycle the vnode if the file is now unused (unlinked),
    767  1.8.4.2  jruoho 		 * otherwise just free it.
    768  1.8.4.2  jruoho 		 */
    769  1.8.4.2  jruoho 		if (recycle) {
    770  1.8.4.2  jruoho 			vclean(vp, DOCLOSE);
    771  1.8.4.2  jruoho 		}
    772  1.8.4.2  jruoho 		KASSERT(vp->v_usecount > 0);
    773  1.8.4.2  jruoho 	}
    774  1.8.4.2  jruoho 
    775  1.8.4.2  jruoho 	if (atomic_dec_uint_nv(&vp->v_usecount) != 0) {
    776  1.8.4.2  jruoho 		/* Gained another reference while being reclaimed. */
    777  1.8.4.2  jruoho 		mutex_exit(&vp->v_interlock);
    778  1.8.4.2  jruoho 		return;
    779  1.8.4.2  jruoho 	}
    780  1.8.4.2  jruoho 
    781  1.8.4.2  jruoho 	if ((vp->v_iflag & VI_CLEAN) != 0) {
    782  1.8.4.2  jruoho 		/*
    783  1.8.4.2  jruoho 		 * It's clean so destroy it.  It isn't referenced
    784  1.8.4.2  jruoho 		 * anywhere since it has been reclaimed.
    785  1.8.4.2  jruoho 		 */
    786  1.8.4.2  jruoho 		KASSERT(vp->v_holdcnt == 0);
    787  1.8.4.2  jruoho 		KASSERT(vp->v_writecount == 0);
    788  1.8.4.2  jruoho 		mutex_exit(&vp->v_interlock);
    789  1.8.4.2  jruoho 		vfs_insmntque(vp, NULL);
    790  1.8.4.2  jruoho 		if (vp->v_type == VBLK || vp->v_type == VCHR) {
    791  1.8.4.2  jruoho 			spec_node_destroy(vp);
    792  1.8.4.2  jruoho 		}
    793  1.8.4.2  jruoho 		vnfree(vp);
    794  1.8.4.2  jruoho 	} else {
    795  1.8.4.2  jruoho 		/*
    796  1.8.4.2  jruoho 		 * Otherwise, put it back onto the freelist.  It
    797  1.8.4.2  jruoho 		 * can't be destroyed while still associated with
    798  1.8.4.2  jruoho 		 * a file system.
    799  1.8.4.2  jruoho 		 */
    800  1.8.4.2  jruoho 		mutex_enter(&vnode_free_list_lock);
    801  1.8.4.2  jruoho 		if (vp->v_holdcnt > 0) {
    802  1.8.4.2  jruoho 			vp->v_freelisthd = &vnode_hold_list;
    803  1.8.4.2  jruoho 		} else {
    804  1.8.4.2  jruoho 			vp->v_freelisthd = &vnode_free_list;
    805  1.8.4.2  jruoho 		}
    806  1.8.4.2  jruoho 		TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
    807  1.8.4.2  jruoho 		mutex_exit(&vnode_free_list_lock);
    808  1.8.4.2  jruoho 		mutex_exit(&vp->v_interlock);
    809  1.8.4.2  jruoho 	}
    810  1.8.4.2  jruoho }
    811  1.8.4.2  jruoho 
    812  1.8.4.2  jruoho void
    813  1.8.4.2  jruoho vrele(vnode_t *vp)
    814  1.8.4.2  jruoho {
    815  1.8.4.2  jruoho 
    816  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
    817  1.8.4.2  jruoho 
    818  1.8.4.2  jruoho 	if ((vp->v_iflag & VI_INACTNOW) == 0 && vtryrele(vp)) {
    819  1.8.4.2  jruoho 		return;
    820  1.8.4.2  jruoho 	}
    821  1.8.4.2  jruoho 	mutex_enter(&vp->v_interlock);
    822  1.8.4.2  jruoho 	vrelel(vp, 0);
    823  1.8.4.2  jruoho }
    824  1.8.4.2  jruoho 
    825  1.8.4.2  jruoho /*
    826  1.8.4.2  jruoho  * Asynchronous vnode release, vnode is released in different context.
    827  1.8.4.2  jruoho  */
    828  1.8.4.2  jruoho void
    829  1.8.4.2  jruoho vrele_async(vnode_t *vp)
    830  1.8.4.2  jruoho {
    831  1.8.4.2  jruoho 
    832  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
    833  1.8.4.2  jruoho 
    834  1.8.4.2  jruoho 	if ((vp->v_iflag & VI_INACTNOW) == 0 && vtryrele(vp)) {
    835  1.8.4.2  jruoho 		return;
    836  1.8.4.2  jruoho 	}
    837  1.8.4.2  jruoho 	mutex_enter(&vp->v_interlock);
    838  1.8.4.2  jruoho 	vrelel(vp, VRELEL_ASYNC_RELE);
    839  1.8.4.2  jruoho }
    840  1.8.4.2  jruoho 
    841  1.8.4.2  jruoho static void
    842  1.8.4.2  jruoho vrele_thread(void *cookie)
    843  1.8.4.2  jruoho {
    844  1.8.4.2  jruoho 	vnode_t *vp;
    845  1.8.4.2  jruoho 
    846  1.8.4.2  jruoho 	for (;;) {
    847  1.8.4.2  jruoho 		mutex_enter(&vrele_lock);
    848  1.8.4.2  jruoho 		while (TAILQ_EMPTY(&vrele_list)) {
    849  1.8.4.2  jruoho 			vrele_gen++;
    850  1.8.4.2  jruoho 			cv_broadcast(&vrele_cv);
    851  1.8.4.2  jruoho 			cv_timedwait(&vrele_cv, &vrele_lock, hz);
    852  1.8.4.2  jruoho 		}
    853  1.8.4.2  jruoho 		vp = TAILQ_FIRST(&vrele_list);
    854  1.8.4.2  jruoho 		TAILQ_REMOVE(&vrele_list, vp, v_freelist);
    855  1.8.4.2  jruoho 		vrele_pending--;
    856  1.8.4.2  jruoho 		mutex_exit(&vrele_lock);
    857  1.8.4.2  jruoho 
    858  1.8.4.2  jruoho 		/*
    859  1.8.4.2  jruoho 		 * If not the last reference, then ignore the vnode
    860  1.8.4.2  jruoho 		 * and look for more work.
    861  1.8.4.2  jruoho 		 */
    862  1.8.4.2  jruoho 		mutex_enter(&vp->v_interlock);
    863  1.8.4.2  jruoho 		KASSERT((vp->v_iflag & VI_INACTPEND) != 0);
    864  1.8.4.2  jruoho 		vp->v_iflag &= ~VI_INACTPEND;
    865  1.8.4.2  jruoho 		vrelel(vp, 0);
    866  1.8.4.2  jruoho 	}
    867  1.8.4.2  jruoho }
    868  1.8.4.2  jruoho 
    869  1.8.4.2  jruoho void
    870  1.8.4.2  jruoho vrele_flush(void)
    871  1.8.4.2  jruoho {
    872  1.8.4.2  jruoho 	int gen;
    873  1.8.4.2  jruoho 
    874  1.8.4.2  jruoho 	mutex_enter(&vrele_lock);
    875  1.8.4.2  jruoho 	gen = vrele_gen;
    876  1.8.4.2  jruoho 	while (vrele_pending && gen == vrele_gen) {
    877  1.8.4.2  jruoho 		cv_broadcast(&vrele_cv);
    878  1.8.4.2  jruoho 		cv_wait(&vrele_cv, &vrele_lock);
    879  1.8.4.2  jruoho 	}
    880  1.8.4.2  jruoho 	mutex_exit(&vrele_lock);
    881  1.8.4.2  jruoho }
    882  1.8.4.2  jruoho 
    883  1.8.4.2  jruoho /*
    884  1.8.4.2  jruoho  * Vnode reference, where a reference is already held by some other
    885  1.8.4.2  jruoho  * object (for example, a file structure).
    886  1.8.4.2  jruoho  */
    887  1.8.4.2  jruoho void
    888  1.8.4.2  jruoho vref(vnode_t *vp)
    889  1.8.4.2  jruoho {
    890  1.8.4.2  jruoho 
    891  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
    892  1.8.4.2  jruoho 	KASSERT(vp->v_usecount != 0);
    893  1.8.4.2  jruoho 
    894  1.8.4.2  jruoho 	atomic_inc_uint(&vp->v_usecount);
    895  1.8.4.2  jruoho }
    896  1.8.4.2  jruoho 
    897  1.8.4.2  jruoho /*
    898  1.8.4.2  jruoho  * Page or buffer structure gets a reference.
    899  1.8.4.2  jruoho  * Called with v_interlock held.
    900  1.8.4.2  jruoho  */
    901  1.8.4.2  jruoho void
    902  1.8.4.2  jruoho vholdl(vnode_t *vp)
    903  1.8.4.2  jruoho {
    904  1.8.4.2  jruoho 
    905  1.8.4.2  jruoho 	KASSERT(mutex_owned(&vp->v_interlock));
    906  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
    907  1.8.4.2  jruoho 
    908  1.8.4.2  jruoho 	if (vp->v_holdcnt++ == 0 && vp->v_usecount == 0) {
    909  1.8.4.2  jruoho 		mutex_enter(&vnode_free_list_lock);
    910  1.8.4.2  jruoho 		KASSERT(vp->v_freelisthd == &vnode_free_list);
    911  1.8.4.2  jruoho 		TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
    912  1.8.4.2  jruoho 		vp->v_freelisthd = &vnode_hold_list;
    913  1.8.4.2  jruoho 		TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
    914  1.8.4.2  jruoho 		mutex_exit(&vnode_free_list_lock);
    915  1.8.4.2  jruoho 	}
    916  1.8.4.2  jruoho }
    917  1.8.4.2  jruoho 
    918  1.8.4.2  jruoho /*
    919  1.8.4.2  jruoho  * Page or buffer structure frees a reference.
    920  1.8.4.2  jruoho  * Called with v_interlock held.
    921  1.8.4.2  jruoho  */
    922  1.8.4.2  jruoho void
    923  1.8.4.2  jruoho holdrelel(vnode_t *vp)
    924  1.8.4.2  jruoho {
    925  1.8.4.2  jruoho 
    926  1.8.4.2  jruoho 	KASSERT(mutex_owned(&vp->v_interlock));
    927  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
    928  1.8.4.2  jruoho 
    929  1.8.4.2  jruoho 	if (vp->v_holdcnt <= 0) {
    930  1.8.4.2  jruoho 		vpanic(vp, "holdrelel: holdcnt vp %p");
    931  1.8.4.2  jruoho 	}
    932  1.8.4.2  jruoho 
    933  1.8.4.2  jruoho 	vp->v_holdcnt--;
    934  1.8.4.2  jruoho 	if (vp->v_holdcnt == 0 && vp->v_usecount == 0) {
    935  1.8.4.2  jruoho 		mutex_enter(&vnode_free_list_lock);
    936  1.8.4.2  jruoho 		KASSERT(vp->v_freelisthd == &vnode_hold_list);
    937  1.8.4.2  jruoho 		TAILQ_REMOVE(vp->v_freelisthd, vp, v_freelist);
    938  1.8.4.2  jruoho 		vp->v_freelisthd = &vnode_free_list;
    939  1.8.4.2  jruoho 		TAILQ_INSERT_TAIL(vp->v_freelisthd, vp, v_freelist);
    940  1.8.4.2  jruoho 		mutex_exit(&vnode_free_list_lock);
    941  1.8.4.2  jruoho 	}
    942  1.8.4.2  jruoho }
    943  1.8.4.2  jruoho 
    944  1.8.4.2  jruoho /*
    945  1.8.4.2  jruoho  * Disassociate the underlying file system from a vnode.
    946  1.8.4.2  jruoho  *
    947  1.8.4.2  jruoho  * Must be called with the interlock held, and will return with it held.
    948  1.8.4.2  jruoho  */
    949  1.8.4.2  jruoho void
    950  1.8.4.2  jruoho vclean(vnode_t *vp, int flags)
    951  1.8.4.2  jruoho {
    952  1.8.4.2  jruoho 	lwp_t *l = curlwp;
    953  1.8.4.2  jruoho 	bool recycle, active;
    954  1.8.4.2  jruoho 	int error;
    955  1.8.4.2  jruoho 
    956  1.8.4.2  jruoho 	KASSERT(mutex_owned(&vp->v_interlock));
    957  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
    958  1.8.4.2  jruoho 	KASSERT(vp->v_usecount != 0);
    959  1.8.4.2  jruoho 
    960  1.8.4.2  jruoho 	/* If cleaning is already in progress wait until done and return. */
    961  1.8.4.2  jruoho 	if (vp->v_iflag & VI_XLOCK) {
    962  1.8.4.2  jruoho 		vwait(vp, VI_XLOCK);
    963  1.8.4.2  jruoho 		return;
    964  1.8.4.2  jruoho 	}
    965  1.8.4.2  jruoho 
    966  1.8.4.2  jruoho 	/* If already clean, nothing to do. */
    967  1.8.4.2  jruoho 	if ((vp->v_iflag & VI_CLEAN) != 0) {
    968  1.8.4.2  jruoho 		return;
    969  1.8.4.2  jruoho 	}
    970  1.8.4.2  jruoho 
    971  1.8.4.2  jruoho 	/*
    972  1.8.4.2  jruoho 	 * Prevent the vnode from being recycled or brought into use
    973  1.8.4.2  jruoho 	 * while we clean it out.
    974  1.8.4.2  jruoho 	 */
    975  1.8.4.2  jruoho 	vp->v_iflag |= VI_XLOCK;
    976  1.8.4.2  jruoho 	if (vp->v_iflag & VI_EXECMAP) {
    977  1.8.4.2  jruoho 		atomic_add_int(&uvmexp.execpages, -vp->v_uobj.uo_npages);
    978  1.8.4.2  jruoho 		atomic_add_int(&uvmexp.filepages, vp->v_uobj.uo_npages);
    979  1.8.4.2  jruoho 	}
    980  1.8.4.2  jruoho 	vp->v_iflag &= ~(VI_TEXT|VI_EXECMAP);
    981  1.8.4.2  jruoho 	active = (vp->v_usecount & VC_MASK) > 1;
    982  1.8.4.2  jruoho 
    983  1.8.4.2  jruoho 	/* XXXAD should not lock vnode under layer */
    984  1.8.4.2  jruoho 	mutex_exit(&vp->v_interlock);
    985  1.8.4.2  jruoho 	VOP_LOCK(vp, LK_EXCLUSIVE);
    986  1.8.4.2  jruoho 
    987  1.8.4.2  jruoho 	/*
    988  1.8.4.2  jruoho 	 * Clean out any cached data associated with the vnode.
    989  1.8.4.2  jruoho 	 * If purging an active vnode, it must be closed and
    990  1.8.4.2  jruoho 	 * deactivated before being reclaimed. Note that the
    991  1.8.4.2  jruoho 	 * VOP_INACTIVE will unlock the vnode.
    992  1.8.4.2  jruoho 	 */
    993  1.8.4.2  jruoho 	if (flags & DOCLOSE) {
    994  1.8.4.2  jruoho 		error = vinvalbuf(vp, V_SAVE, NOCRED, l, 0, 0);
    995  1.8.4.2  jruoho 		if (error != 0) {
    996  1.8.4.2  jruoho 			/* XXX, fix vn_start_write's grab of mp and use that. */
    997  1.8.4.2  jruoho 
    998  1.8.4.2  jruoho 			if (wapbl_vphaswapbl(vp))
    999  1.8.4.2  jruoho 				WAPBL_DISCARD(wapbl_vptomp(vp));
   1000  1.8.4.2  jruoho 			error = vinvalbuf(vp, 0, NOCRED, l, 0, 0);
   1001  1.8.4.2  jruoho 		}
   1002  1.8.4.2  jruoho 		KASSERT(error == 0);
   1003  1.8.4.2  jruoho 		KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
   1004  1.8.4.2  jruoho 		if (active && (vp->v_type == VBLK || vp->v_type == VCHR)) {
   1005  1.8.4.2  jruoho 			 spec_node_revoke(vp);
   1006  1.8.4.2  jruoho 		}
   1007  1.8.4.2  jruoho 	}
   1008  1.8.4.2  jruoho 	if (active) {
   1009  1.8.4.2  jruoho 		VOP_INACTIVE(vp, &recycle);
   1010  1.8.4.2  jruoho 	} else {
   1011  1.8.4.2  jruoho 		/*
   1012  1.8.4.2  jruoho 		 * Any other processes trying to obtain this lock must first
   1013  1.8.4.2  jruoho 		 * wait for VI_XLOCK to clear, then call the new lock operation.
   1014  1.8.4.2  jruoho 		 */
   1015  1.8.4.2  jruoho 		VOP_UNLOCK(vp);
   1016  1.8.4.2  jruoho 	}
   1017  1.8.4.2  jruoho 
   1018  1.8.4.2  jruoho 	/* Disassociate the underlying file system from the vnode. */
   1019  1.8.4.2  jruoho 	if (VOP_RECLAIM(vp)) {
   1020  1.8.4.2  jruoho 		vpanic(vp, "vclean: cannot reclaim");
   1021  1.8.4.2  jruoho 	}
   1022  1.8.4.2  jruoho 
   1023  1.8.4.2  jruoho 	KASSERT(vp->v_data == NULL);
   1024  1.8.4.2  jruoho 	KASSERT(vp->v_uobj.uo_npages == 0);
   1025  1.8.4.2  jruoho 
   1026  1.8.4.2  jruoho 	if (vp->v_type == VREG && vp->v_ractx != NULL) {
   1027  1.8.4.2  jruoho 		uvm_ra_freectx(vp->v_ractx);
   1028  1.8.4.2  jruoho 		vp->v_ractx = NULL;
   1029  1.8.4.2  jruoho 	}
   1030  1.8.4.2  jruoho 
   1031  1.8.4.2  jruoho 	/* Purge name cache. */
   1032  1.8.4.2  jruoho 	cache_purge(vp);
   1033  1.8.4.2  jruoho 
   1034  1.8.4.2  jruoho 	/* Done with purge, notify sleepers of the grim news. */
   1035  1.8.4.2  jruoho 	mutex_enter(&vp->v_interlock);
   1036  1.8.4.2  jruoho 	vp->v_op = dead_vnodeop_p;
   1037  1.8.4.2  jruoho 	vp->v_tag = VT_NON;
   1038  1.8.4.2  jruoho 	KNOTE(&vp->v_klist, NOTE_REVOKE);
   1039  1.8.4.2  jruoho 	vp->v_iflag &= ~VI_XLOCK;
   1040  1.8.4.2  jruoho 	vp->v_vflag &= ~VV_LOCKSWORK;
   1041  1.8.4.2  jruoho 	if ((flags & DOCLOSE) != 0) {
   1042  1.8.4.2  jruoho 		vp->v_iflag |= VI_CLEAN;
   1043  1.8.4.2  jruoho 	}
   1044  1.8.4.2  jruoho 	cv_broadcast(&vp->v_cv);
   1045  1.8.4.2  jruoho 
   1046  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_ONWORKLST) == 0);
   1047  1.8.4.2  jruoho }
   1048  1.8.4.2  jruoho 
   1049  1.8.4.2  jruoho /*
   1050  1.8.4.2  jruoho  * Recycle an unused vnode to the front of the free list.
   1051  1.8.4.2  jruoho  * Release the passed interlock if the vnode will be recycled.
   1052  1.8.4.2  jruoho  */
   1053  1.8.4.2  jruoho int
   1054  1.8.4.2  jruoho vrecycle(vnode_t *vp, kmutex_t *inter_lkp, struct lwp *l)
   1055  1.8.4.2  jruoho {
   1056  1.8.4.2  jruoho 
   1057  1.8.4.2  jruoho 	KASSERT((vp->v_iflag & VI_MARKER) == 0);
   1058  1.8.4.2  jruoho 
   1059  1.8.4.2  jruoho 	mutex_enter(&vp->v_interlock);
   1060  1.8.4.2  jruoho 	if (vp->v_usecount != 0) {
   1061  1.8.4.2  jruoho 		mutex_exit(&vp->v_interlock);
   1062  1.8.4.2  jruoho 		return 0;
   1063  1.8.4.2  jruoho 	}
   1064  1.8.4.2  jruoho 	if (inter_lkp) {
   1065  1.8.4.2  jruoho 		mutex_exit(inter_lkp);
   1066  1.8.4.2  jruoho 	}
   1067  1.8.4.2  jruoho 	vremfree(vp);
   1068  1.8.4.2  jruoho 	vp->v_usecount = 1;
   1069  1.8.4.2  jruoho 	vclean(vp, DOCLOSE);
   1070  1.8.4.2  jruoho 	vrelel(vp, 0);
   1071  1.8.4.2  jruoho 	return 1;
   1072  1.8.4.2  jruoho }
   1073  1.8.4.2  jruoho 
   1074  1.8.4.2  jruoho /*
   1075  1.8.4.2  jruoho  * Eliminate all activity associated with the requested vnode
   1076  1.8.4.2  jruoho  * and with all vnodes aliased to the requested vnode.
   1077  1.8.4.2  jruoho  */
   1078  1.8.4.2  jruoho void
   1079  1.8.4.2  jruoho vrevoke(vnode_t *vp)
   1080  1.8.4.2  jruoho {
   1081  1.8.4.2  jruoho 	vnode_t *vq, **vpp;
   1082  1.8.4.2  jruoho 	enum vtype type;
   1083  1.8.4.2  jruoho 	dev_t dev;
   1084  1.8.4.2  jruoho 
   1085  1.8.4.2  jruoho 	KASSERT(vp->v_usecount > 0);
   1086  1.8.4.2  jruoho 
   1087  1.8.4.2  jruoho 	mutex_enter(&vp->v_interlock);
   1088  1.8.4.2  jruoho 	if ((vp->v_iflag & VI_CLEAN) != 0) {
   1089  1.8.4.2  jruoho 		mutex_exit(&vp->v_interlock);
   1090  1.8.4.2  jruoho 		return;
   1091  1.8.4.2  jruoho 	} else if (vp->v_type != VBLK && vp->v_type != VCHR) {
   1092  1.8.4.2  jruoho 		atomic_inc_uint(&vp->v_usecount);
   1093  1.8.4.2  jruoho 		vclean(vp, DOCLOSE);
   1094  1.8.4.2  jruoho 		vrelel(vp, 0);
   1095  1.8.4.2  jruoho 		return;
   1096  1.8.4.2  jruoho 	} else {
   1097  1.8.4.2  jruoho 		dev = vp->v_rdev;
   1098  1.8.4.2  jruoho 		type = vp->v_type;
   1099  1.8.4.2  jruoho 		mutex_exit(&vp->v_interlock);
   1100  1.8.4.2  jruoho 	}
   1101  1.8.4.2  jruoho 
   1102  1.8.4.2  jruoho 	vpp = &specfs_hash[SPECHASH(dev)];
   1103  1.8.4.2  jruoho 	mutex_enter(&device_lock);
   1104  1.8.4.2  jruoho 	for (vq = *vpp; vq != NULL;) {
   1105  1.8.4.2  jruoho 		/* If clean or being cleaned, then ignore it. */
   1106  1.8.4.2  jruoho 		mutex_enter(&vq->v_interlock);
   1107  1.8.4.2  jruoho 		if ((vq->v_iflag & (VI_CLEAN | VI_XLOCK)) != 0 ||
   1108  1.8.4.2  jruoho 		    vq->v_rdev != dev || vq->v_type != type) {
   1109  1.8.4.2  jruoho 			mutex_exit(&vq->v_interlock);
   1110  1.8.4.2  jruoho 			vq = vq->v_specnext;
   1111  1.8.4.2  jruoho 			continue;
   1112  1.8.4.2  jruoho 		}
   1113  1.8.4.2  jruoho 		mutex_exit(&device_lock);
   1114  1.8.4.2  jruoho 		if (vq->v_usecount == 0) {
   1115  1.8.4.2  jruoho 			vremfree(vq);
   1116  1.8.4.2  jruoho 			vq->v_usecount = 1;
   1117  1.8.4.2  jruoho 		} else {
   1118  1.8.4.2  jruoho 			atomic_inc_uint(&vq->v_usecount);
   1119  1.8.4.2  jruoho 		}
   1120  1.8.4.2  jruoho 		vclean(vq, DOCLOSE);
   1121  1.8.4.2  jruoho 		vrelel(vq, 0);
   1122  1.8.4.2  jruoho 		mutex_enter(&device_lock);
   1123  1.8.4.2  jruoho 		vq = *vpp;
   1124  1.8.4.2  jruoho 	}
   1125  1.8.4.2  jruoho 	mutex_exit(&device_lock);
   1126  1.8.4.2  jruoho }
   1127  1.8.4.2  jruoho 
   1128  1.8.4.2  jruoho /*
   1129  1.8.4.2  jruoho  * Eliminate all activity associated with a vnode in preparation for
   1130  1.8.4.2  jruoho  * reuse.  Drops a reference from the vnode.
   1131  1.8.4.2  jruoho  */
   1132  1.8.4.2  jruoho void
   1133  1.8.4.2  jruoho vgone(vnode_t *vp)
   1134  1.8.4.2  jruoho {
   1135  1.8.4.2  jruoho 
   1136  1.8.4.2  jruoho 	mutex_enter(&vp->v_interlock);
   1137  1.8.4.2  jruoho 	vclean(vp, DOCLOSE);
   1138  1.8.4.2  jruoho 	vrelel(vp, 0);
   1139  1.8.4.2  jruoho }
   1140  1.8.4.2  jruoho 
   1141  1.8.4.2  jruoho /*
   1142  1.8.4.2  jruoho  * Update outstanding I/O count and do wakeup if requested.
   1143  1.8.4.2  jruoho  */
   1144  1.8.4.2  jruoho void
   1145  1.8.4.2  jruoho vwakeup(struct buf *bp)
   1146  1.8.4.2  jruoho {
   1147  1.8.4.2  jruoho 	vnode_t *vp;
   1148  1.8.4.2  jruoho 
   1149  1.8.4.2  jruoho 	if ((vp = bp->b_vp) == NULL)
   1150  1.8.4.2  jruoho 		return;
   1151  1.8.4.2  jruoho 
   1152  1.8.4.2  jruoho 	KASSERT(bp->b_objlock == &vp->v_interlock);
   1153  1.8.4.2  jruoho 	KASSERT(mutex_owned(bp->b_objlock));
   1154  1.8.4.2  jruoho 
   1155  1.8.4.2  jruoho 	if (--vp->v_numoutput < 0)
   1156  1.8.4.2  jruoho 		panic("vwakeup: neg numoutput, vp %p", vp);
   1157  1.8.4.2  jruoho 	if (vp->v_numoutput == 0)
   1158  1.8.4.2  jruoho 		cv_broadcast(&vp->v_cv);
   1159  1.8.4.2  jruoho }
   1160  1.8.4.2  jruoho 
   1161  1.8.4.2  jruoho /*
   1162  1.8.4.2  jruoho  * Wait for a vnode (typically with VI_XLOCK set) to be cleaned or
   1163  1.8.4.2  jruoho  * recycled.
   1164  1.8.4.2  jruoho  */
   1165  1.8.4.2  jruoho void
   1166  1.8.4.2  jruoho vwait(vnode_t *vp, int flags)
   1167  1.8.4.2  jruoho {
   1168  1.8.4.2  jruoho 
   1169  1.8.4.2  jruoho 	KASSERT(mutex_owned(&vp->v_interlock));
   1170  1.8.4.2  jruoho 	KASSERT(vp->v_usecount != 0);
   1171  1.8.4.2  jruoho 
   1172  1.8.4.2  jruoho 	while ((vp->v_iflag & flags) != 0)
   1173  1.8.4.2  jruoho 		cv_wait(&vp->v_cv, &vp->v_interlock);
   1174  1.8.4.2  jruoho }
   1175  1.8.4.2  jruoho 
   1176  1.8.4.2  jruoho int
   1177  1.8.4.2  jruoho vfs_drainvnodes(long target)
   1178  1.8.4.2  jruoho {
   1179  1.8.4.2  jruoho 
   1180  1.8.4.2  jruoho 	while (numvnodes > target) {
   1181  1.8.4.2  jruoho 		vnode_t *vp;
   1182  1.8.4.2  jruoho 
   1183  1.8.4.2  jruoho 		mutex_enter(&vnode_free_list_lock);
   1184  1.8.4.2  jruoho 		vp = getcleanvnode();
   1185  1.8.4.2  jruoho 		if (vp == NULL) {
   1186  1.8.4.2  jruoho 			return EBUSY;
   1187  1.8.4.2  jruoho 		}
   1188  1.8.4.2  jruoho 		ungetnewvnode(vp);
   1189  1.8.4.2  jruoho 	}
   1190  1.8.4.2  jruoho 	return 0;
   1191  1.8.4.2  jruoho }
   1192  1.8.4.2  jruoho 
   1193  1.8.4.2  jruoho void
   1194  1.8.4.2  jruoho vpanic(vnode_t *vp, const char *msg)
   1195  1.8.4.2  jruoho {
   1196  1.8.4.2  jruoho #ifdef DIAGNOSTIC
   1197  1.8.4.2  jruoho 
   1198  1.8.4.2  jruoho 	vprint(NULL, vp);
   1199  1.8.4.2  jruoho 	panic("%s\n", msg);
   1200  1.8.4.2  jruoho #endif
   1201  1.8.4.2  jruoho }
   1202