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