Home | History | Annotate | Download | only in fs
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
     27 /*	  All Rights Reserved  	*/
     28 
     29 /*
     30  * University Copyright- Copyright (c) 1982, 1986, 1988
     31  * The Regents of the University of California
     32  * All Rights Reserved
     33  *
     34  * University Acknowledgment- Portions of this document are derived from
     35  * software developed by the University of California, Berkeley, and its
     36  * contributors.
     37  */
     38 
     39 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     40 
     41 #include <sys/types.h>
     42 #include <sys/systm.h>
     43 #include <sys/param.h>
     44 #include <sys/t_lock.h>
     45 #include <sys/systm.h>
     46 #include <sys/vfs.h>
     47 #include <sys/vnode.h>
     48 #include <sys/dnlc.h>
     49 #include <sys/kmem.h>
     50 #include <sys/cmn_err.h>
     51 #include <sys/vtrace.h>
     52 #include <sys/bitmap.h>
     53 #include <sys/var.h>
     54 #include <sys/sysmacros.h>
     55 #include <sys/kstat.h>
     56 #include <sys/atomic.h>
     57 #include <sys/taskq.h>
     58 
     59 /*
     60  * Directory name lookup cache.
     61  * Based on code originally done by Robert Elz at Melbourne.
     62  *
     63  * Names found by directory scans are retained in a cache
     64  * for future reference.  Each hash chain is ordered by LRU
     65  * Cache is indexed by hash value obtained from (vp, name)
     66  * where the vp refers to the directory containing the name.
     67  */
     68 
     69 /*
     70  * We want to be able to identify files that are referenced only by the DNLC.
     71  * When adding a reference from the DNLC, call VN_HOLD_DNLC instead of VN_HOLD,
     72  * since multiple DNLC references should only be counted once in v_count. This
     73  * file contains only two(2) calls to VN_HOLD, renamed VN_HOLD_CALLER in the
     74  * hope that no one will mistakenly add a VN_HOLD to this file. (Unfortunately
     75  * it is not possible to #undef VN_HOLD and retain VN_HOLD_CALLER. Ideally a
     76  * Makefile rule would grep uncommented C tokens to check that VN_HOLD is
     77  * referenced only once in this file, to define VN_HOLD_CALLER.)
     78  */
     79 #define	VN_HOLD_CALLER	VN_HOLD
     80 #define	VN_HOLD_DNLC(vp)	{	\
     81 	mutex_enter(&(vp)->v_lock);	\
     82 	if ((vp)->v_count_dnlc == 0)	\
     83 		(vp)->v_count++;	\
     84 	(vp)->v_count_dnlc++;		\
     85 	mutex_exit(&(vp)->v_lock);	\
     86 }
     87 #define	VN_RELE_DNLC(vp)	{	\
     88 	vn_rele_dnlc(vp);		\
     89 }
     90 
     91 /*
     92  * Tunable nc_hashavelen is the average length desired for this chain, from
     93  * which the size of the nc_hash table is derived at create time.
     94  */
     95 #define	NC_HASHAVELEN_DEFAULT	4
     96 int nc_hashavelen = NC_HASHAVELEN_DEFAULT;
     97 
     98 /*
     99  * NC_MOVETOFRONT is the move-to-front threshold: if the hash lookup
    100  * depth exceeds this value, we move the looked-up entry to the front of
    101  * its hash chain.  The idea is to make sure that the most frequently
    102  * accessed entries are found most quickly (by keeping them near the
    103  * front of their hash chains).
    104  */
    105 #define	NC_MOVETOFRONT	2
    106 
    107 /*
    108  *
    109  * DNLC_MAX_RELE is used to size an array on the stack when releasing
    110  * vnodes. This array is used rather than calling VN_RELE() inline because
    111  * all dnlc locks must be dropped by that time in order to avoid a
    112  * possible deadlock. This deadlock occurs when the dnlc holds the last
    113  * reference to the vnode and so the VOP_INACTIVE vector is called which
    114  * can in turn call back into the dnlc. A global array was used but had
    115  * many problems:
    116  *	1) Actually doesn't have an upper bound on the array size as
    117  *	   entries can be added after starting the purge.
    118  *	2) The locking scheme causes a hang.
    119  *	3) Caused serialisation on the global lock.
    120  *	4) The array was often unnecessarily huge.
    121  *
    122  * Note the current value 8 allows up to 4 cache entries (to be purged
    123  * from each hash chain), before having to cycle around and retry.
    124  * This ought to be ample given that nc_hashavelen is typically very small.
    125  */
    126 #define	DNLC_MAX_RELE	8 /* must be even */
    127 
    128 /*
    129  * Hash table of name cache entries for fast lookup, dynamically
    130  * allocated at startup.
    131  */
    132 nc_hash_t *nc_hash;
    133 
    134 /*
    135  * Rotors. Used to select entries on a round-robin basis.
    136  */
    137 static nc_hash_t *dnlc_purge_fs1_rotor;
    138 static nc_hash_t *dnlc_free_rotor;
    139 
    140 /*
    141  * # of dnlc entries (uninitialized)
    142  *
    143  * the initial value was chosen as being
    144  * a random string of bits, probably not
    145  * normally chosen by a systems administrator
    146  */
    147 int ncsize = -1;
    148 volatile uint32_t dnlc_nentries = 0;	/* current num of name cache entries */
    149 static int nc_hashsz;			/* size of hash table */
    150 static int nc_hashmask;			/* size of hash table minus 1 */
    151 
    152 /*
    153  * The dnlc_reduce_cache() taskq queue is activated when there are
    154  * ncsize name cache entries and if no parameter is provided, it reduces
    155  * the size down to dnlc_nentries_low_water, which is by default one
    156  * hundreth less (or 99%) of ncsize.
    157  *
    158  * If a parameter is provided to dnlc_reduce_cache(), then we reduce
    159  * the size down based on ncsize_onepercent - where ncsize_onepercent
    160  * is 1% of ncsize; however, we never let dnlc_reduce_cache() reduce
    161  * the size below 3% of ncsize (ncsize_min_percent).
    162  */
    163 #define	DNLC_LOW_WATER_DIVISOR_DEFAULT 100
    164 uint_t dnlc_low_water_divisor = DNLC_LOW_WATER_DIVISOR_DEFAULT;
    165 uint_t dnlc_nentries_low_water;
    166 int dnlc_reduce_idle = 1; /* no locking needed */
    167 uint_t ncsize_onepercent;
    168 uint_t ncsize_min_percent;
    169 
    170 /*
    171  * If dnlc_nentries hits dnlc_max_nentries (twice ncsize)
    172  * then this means the dnlc_reduce_cache() taskq is failing to
    173  * keep up. In this case we refuse to add new entries to the dnlc
    174  * until the taskq catches up.
    175  */
    176 uint_t dnlc_max_nentries; /* twice ncsize */
    177 uint64_t dnlc_max_nentries_cnt = 0; /* statistic on times we failed */
    178 
    179 /*
    180  * Tunable to define when we should just remove items from
    181  * the end of the chain.
    182  */
    183 #define	DNLC_LONG_CHAIN 8
    184 uint_t dnlc_long_chain = DNLC_LONG_CHAIN;
    185 
    186 /*
    187  * ncstats has been deprecated, due to the integer size of the counters
    188  * which can easily overflow in the dnlc.
    189  * It is maintained (at some expense) for compatability.
    190  * The preferred interface is the kstat accessible nc_stats below.
    191  */
    192 struct ncstats ncstats;
    193 
    194 struct nc_stats ncs = {
    195 	{ "hits",			KSTAT_DATA_UINT64 },
    196 	{ "misses",			KSTAT_DATA_UINT64 },
    197 	{ "negative_cache_hits",	KSTAT_DATA_UINT64 },
    198 	{ "enters",			KSTAT_DATA_UINT64 },
    199 	{ "double_enters",		KSTAT_DATA_UINT64 },
    200 	{ "purge_total_entries",	KSTAT_DATA_UINT64 },
    201 	{ "purge_all",			KSTAT_DATA_UINT64 },
    202 	{ "purge_vp",			KSTAT_DATA_UINT64 },
    203 	{ "purge_vfs",			KSTAT_DATA_UINT64 },
    204 	{ "purge_fs1",			KSTAT_DATA_UINT64 },
    205 	{ "pick_free",			KSTAT_DATA_UINT64 },
    206 	{ "pick_heuristic",		KSTAT_DATA_UINT64 },
    207 	{ "pick_last",			KSTAT_DATA_UINT64 },
    208 
    209 	/* directory caching stats */
    210 
    211 	{ "dir_hits",			KSTAT_DATA_UINT64 },
    212 	{ "dir_misses",			KSTAT_DATA_UINT64 },
    213 	{ "dir_cached_current",		KSTAT_DATA_UINT64 },
    214 	{ "dir_entries_cached_current",	KSTAT_DATA_UINT64 },
    215 	{ "dir_cached_total",		KSTAT_DATA_UINT64 },
    216 	{ "dir_start_no_memory",	KSTAT_DATA_UINT64 },
    217 	{ "dir_add_no_memory",		KSTAT_DATA_UINT64 },
    218 	{ "dir_add_abort",		KSTAT_DATA_UINT64 },
    219 	{ "dir_add_max",		KSTAT_DATA_UINT64 },
    220 	{ "dir_remove_entry_fail",	KSTAT_DATA_UINT64 },
    221 	{ "dir_remove_space_fail",	KSTAT_DATA_UINT64 },
    222 	{ "dir_update_fail",		KSTAT_DATA_UINT64 },
    223 	{ "dir_fini_purge",		KSTAT_DATA_UINT64 },
    224 	{ "dir_reclaim_last",		KSTAT_DATA_UINT64 },
    225 	{ "dir_reclaim_any",		KSTAT_DATA_UINT64 },
    226 };
    227 
    228 static int doingcache = 1;
    229 
    230 vnode_t negative_cache_vnode;
    231 
    232 /*
    233  * Insert entry at the front of the queue
    234  */
    235 #define	nc_inshash(ncp, hp) \
    236 { \
    237 	(ncp)->hash_next = (hp)->hash_next; \
    238 	(ncp)->hash_prev = (ncache_t *)(hp); \
    239 	(hp)->hash_next->hash_prev = (ncp); \
    240 	(hp)->hash_next = (ncp); \
    241 }
    242 
    243 /*
    244  * Remove entry from hash queue
    245  */
    246 #define	nc_rmhash(ncp) \
    247 { \
    248 	(ncp)->hash_prev->hash_next = (ncp)->hash_next; \
    249 	(ncp)->hash_next->hash_prev = (ncp)->hash_prev; \
    250 	(ncp)->hash_prev = NULL; \
    251 	(ncp)->hash_next = NULL; \
    252 }
    253 
    254 /*
    255  * Free an entry.
    256  */
    257 #define	dnlc_free(ncp) \
    258 { \
    259 	kmem_free((ncp), sizeof (ncache_t) + (ncp)->namlen); \
    260 	atomic_add_32(&dnlc_nentries, -1); \
    261 }
    262 
    263 
    264 /*
    265  * Cached directory info.
    266  * ======================
    267  */
    268 
    269 /*
    270  * Cached directory free space hash function.
    271  * Needs the free space handle and the dcp to get the hash table size
    272  * Returns the hash index.
    273  */
    274 #define	DDFHASH(handle, dcp) ((handle >> 2) & (dcp)->dc_fhash_mask)
    275 
    276 /*
    277  * Cached directory name entry hash function.
    278  * Uses the name and returns in the input arguments the hash and the name
    279  * length.
    280  */
    281 #define	DNLC_DIR_HASH(name, hash, namelen)			\
    282 	{							\
    283 		char Xc, *Xcp;					\
    284 		hash = *name;					\
    285 		for (Xcp = (name + 1); (Xc = *Xcp) != 0; Xcp++)	\
    286 			hash = (hash << 4) + hash + Xc;		\
    287 		ASSERT((Xcp - (name)) <= ((1 << NBBY) - 1));	\
    288 		namelen = Xcp - (name);				\
    289 	}
    290 
    291 /* special dircache_t pointer to indicate error should be returned */
    292 /*
    293  * The anchor directory cache pointer can contain 3 types of values,
    294  * 1) NULL: No directory cache
    295  * 2) DC_RET_LOW_MEM (-1): There was a directory cache that found to be
    296  *    too big or a memory shortage occurred. This value remains in the
    297  *    pointer until a dnlc_dir_start() which returns the a DNOMEM error.
    298  *    This is kludgy but efficient and only visible in this source file.
    299  * 3) A valid cache pointer.
    300  */
    301 #define	DC_RET_LOW_MEM (dircache_t *)1
    302 #define	VALID_DIR_CACHE(dcp) ((dircache_t *)(dcp) > DC_RET_LOW_MEM)
    303 
    304 /* Tunables */
    305 uint_t dnlc_dir_enable = 1; /* disable caching directories by setting to 0 */
    306 uint_t dnlc_dir_min_size = 40; /* min no of directory entries before caching */
    307 uint_t dnlc_dir_max_size = UINT_MAX; /* ditto maximum */
    308 uint_t dnlc_dir_hash_size_shift = 3; /* 8 entries per hash bucket */
    309 uint_t dnlc_dir_min_reclaim =  350000; /* approx 1MB of dcentrys */
    310 /*
    311  * dnlc_dir_hash_resize_shift determines when the hash tables
    312  * get re-adjusted due to growth or shrinkage
    313  * - currently 2 indicating that there can be at most 4
    314  * times or at least one quarter the number of entries
    315  * before hash table readjustment. Note that with
    316  * dnlc_dir_hash_size_shift above set at 3 this would
    317  * mean readjustment would occur if the average number
    318  * of entries went above 32 or below 2
    319  */
    320 uint_t dnlc_dir_hash_resize_shift = 2; /* readjust rate */
    321 
    322 static kmem_cache_t *dnlc_dir_space_cache; /* free space entry cache */
    323 static dchead_t dc_head; /* anchor of cached directories */
    324 
    325 /* Prototypes */
    326 static ncache_t *dnlc_get(uchar_t namlen);
    327 static ncache_t *dnlc_search(vnode_t *dp, char *name, uchar_t namlen, int hash);
    328 static void dnlc_dir_reclaim(void *unused);
    329 static void dnlc_dir_abort(dircache_t *dcp);
    330 static void dnlc_dir_adjust_fhash(dircache_t *dcp);
    331 static void dnlc_dir_adjust_nhash(dircache_t *dcp);
    332 static void do_dnlc_reduce_cache(void *);
    333 
    334 
    335 /*
    336  * Initialize the directory cache.
    337  */
    338 void
    339 dnlc_init()
    340 {
    341 	nc_hash_t *hp;
    342 	kstat_t *ksp;
    343 	int i;
    344 
    345 	/*
    346 	 * Set up the size of the dnlc (ncsize) and its low water mark.
    347 	 */
    348 	if (ncsize == -1) {
    349 		/* calculate a reasonable size for the low water */
    350 		dnlc_nentries_low_water = 4 * (v.v_proc + maxusers) + 320;
    351 		ncsize = dnlc_nentries_low_water +
    352 		    (dnlc_nentries_low_water / dnlc_low_water_divisor);
    353 	} else {
    354 		/* don't change the user specified ncsize */
    355 		dnlc_nentries_low_water =
    356 		    ncsize - (ncsize / dnlc_low_water_divisor);
    357 	}
    358 	if (ncsize <= 0) {
    359 		doingcache = 0;
    360 		dnlc_dir_enable = 0; /* also disable directory caching */
    361 		ncsize = 0;
    362 		cmn_err(CE_NOTE, "name cache (dnlc) disabled");
    363 		return;
    364 	}
    365 	dnlc_max_nentries = ncsize * 2;
    366 	ncsize_onepercent = ncsize / 100;
    367 	ncsize_min_percent = ncsize_onepercent * 3;
    368 
    369 	/*
    370 	 * Initialise the hash table.
    371 	 * Compute hash size rounding to the next power of two.
    372 	 */
    373 	nc_hashsz = ncsize / nc_hashavelen;
    374 	nc_hashsz = 1 << highbit(nc_hashsz);
    375 	nc_hashmask = nc_hashsz - 1;
    376 	nc_hash = kmem_zalloc(nc_hashsz * sizeof (*nc_hash), KM_SLEEP);
    377 	for (i = 0; i < nc_hashsz; i++) {
    378 		hp = (nc_hash_t *)&nc_hash[i];
    379 		mutex_init(&hp->hash_lock, NULL, MUTEX_DEFAULT, NULL);
    380 		hp->hash_next = (ncache_t *)hp;
    381 		hp->hash_prev = (ncache_t *)hp;
    382 	}
    383 
    384 	/*
    385 	 * Initialize rotors
    386 	 */
    387 	dnlc_free_rotor = dnlc_purge_fs1_rotor = &nc_hash[0];
    388 
    389 	/*
    390 	 * Set up the directory caching to use kmem_cache_alloc
    391 	 * for its free space entries so that we can get a callback
    392 	 * when the system is short on memory, to allow us to free
    393 	 * up some memory. we don't use the constructor/deconstructor
    394 	 * functions.
    395 	 */
    396 	dnlc_dir_space_cache = kmem_cache_create("dnlc_space_cache",
    397 	    sizeof (dcfree_t), 0, NULL, NULL, dnlc_dir_reclaim, NULL,
    398 	    NULL, 0);
    399 
    400 	/*
    401 	 * Initialise the head of the cached directory structures
    402 	 */
    403 	mutex_init(&dc_head.dch_lock, NULL, MUTEX_DEFAULT, NULL);
    404 	dc_head.dch_next = (dircache_t *)&dc_head;
    405 	dc_head.dch_prev = (dircache_t *)&dc_head;
    406 
    407 	/*
    408 	 * Initialise the reference count of the negative cache vnode to 1
    409 	 * so that it never goes away (VOP_INACTIVE isn't called on it).
    410 	 */
    411 	negative_cache_vnode.v_count = 1;
    412 	negative_cache_vnode.v_count_dnlc = 0;
    413 
    414 	/*
    415 	 * Initialise kstats - both the old compatability raw kind and
    416 	 * the more extensive named stats.
    417 	 */
    418 	ksp = kstat_create("unix", 0, "ncstats", "misc", KSTAT_TYPE_RAW,
    419 	    sizeof (struct ncstats), KSTAT_FLAG_VIRTUAL);
    420 	if (ksp) {
    421 		ksp->ks_data = (void *) &ncstats;
    422 		kstat_install(ksp);
    423 	}
    424 	ksp = kstat_create("unix", 0, "dnlcstats", "misc", KSTAT_TYPE_NAMED,
    425 	    sizeof (ncs) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
    426 	if (ksp) {
    427 		ksp->ks_data = (void *) &ncs;
    428 		kstat_install(ksp);
    429 	}
    430 }
    431 
    432 /*
    433  * Add a name to the directory cache.
    434  */
    435 void
    436 dnlc_enter(vnode_t *dp, char *name, vnode_t *vp)
    437 {
    438 	ncache_t *ncp;
    439 	nc_hash_t *hp;
    440 	uchar_t namlen;
    441 	int hash;
    442 
    443 	TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_enter_start:");
    444 
    445 	if (!doingcache) {
    446 		TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
    447 		    "dnlc_enter_end:(%S) %d", "not caching", 0);
    448 		return;
    449 	}
    450 
    451 	/*
    452 	 * Get a new dnlc entry. Assume the entry won't be in the cache
    453 	 * and initialize it now
    454 	 */
    455 	DNLCHASH(name, dp, hash, namlen);
    456 	if ((ncp = dnlc_get(namlen)) == NULL)
    457 		return;
    458 	ncp->dp = dp;
    459 	VN_HOLD_DNLC(dp);
    460 	ncp->vp = vp;
    461 	VN_HOLD_DNLC(vp);
    462 	bcopy(name, ncp->name, namlen + 1); /* name and null */
    463 	ncp->hash = hash;
    464 	hp = &nc_hash[hash & nc_hashmask];
    465 
    466 	mutex_enter(&hp->hash_lock);
    467 	if (dnlc_search(dp, name, namlen, hash) != NULL) {
    468 		mutex_exit(&hp->hash_lock);
    469 		ncstats.dbl_enters++;
    470 		ncs.ncs_dbl_enters.value.ui64++;
    471 		VN_RELE_DNLC(dp);
    472 		VN_RELE_DNLC(vp);
    473 		dnlc_free(ncp);		/* crfree done here */
    474 		TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
    475 		    "dnlc_enter_end:(%S) %d", "dbl enter", ncstats.dbl_enters);
    476 		return;
    477 	}
    478 	/*
    479 	 * Insert back into the hash chain.
    480 	 */
    481 	nc_inshash(ncp, hp);
    482 	mutex_exit(&hp->hash_lock);
    483 	ncstats.enters++;
    484 	ncs.ncs_enters.value.ui64++;
    485 	TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
    486 	    "dnlc_enter_end:(%S) %d", "done", ncstats.enters);
    487 }
    488 
    489 /*
    490  * Add a name to the directory cache.
    491  *
    492  * This function is basically identical with
    493  * dnlc_enter().  The difference is that when the
    494  * desired dnlc entry is found, the vnode in the
    495  * ncache is compared with the vnode passed in.
    496  *
    497  * If they are not equal then the ncache is
    498  * updated with the passed in vnode.  Otherwise
    499  * it just frees up the newly allocated dnlc entry.
    500  */
    501 void
    502 dnlc_update(vnode_t *dp, char *name, vnode_t *vp)
    503 {
    504 	ncache_t *ncp;
    505 	ncache_t *tcp;
    506 	vnode_t *tvp;
    507 	nc_hash_t *hp;
    508 	int hash;
    509 	uchar_t namlen;
    510 
    511 	TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_update_start:");
    512 
    513 	if (!doingcache) {
    514 		TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
    515 		    "dnlc_update_end:(%S) %d", "not caching", 0);
    516 		return;
    517 	}
    518 
    519 	/*
    520 	 * Get a new dnlc entry and initialize it now.
    521 	 * If we fail to get a new entry, call dnlc_remove() to purge
    522 	 * any existing dnlc entry including negative cache (DNLC_NO_VNODE)
    523 	 * entry.
    524 	 * Failure to clear an existing entry could result in false dnlc
    525 	 * lookup (negative/stale entry).
    526 	 */
    527 	DNLCHASH(name, dp, hash, namlen);
    528 	if ((ncp = dnlc_get(namlen)) == NULL) {
    529 		dnlc_remove(dp, name);
    530 		return;
    531 	}
    532 	ncp->dp = dp;
    533 	VN_HOLD_DNLC(dp);
    534 	ncp->vp = vp;
    535 	VN_HOLD_DNLC(vp);
    536 	bcopy(name, ncp->name, namlen + 1); /* name and null */
    537 	ncp->hash = hash;
    538 	hp = &nc_hash[hash & nc_hashmask];
    539 
    540 	mutex_enter(&hp->hash_lock);
    541 	if ((tcp = dnlc_search(dp, name, namlen, hash)) != NULL) {
    542 		if (tcp->vp != vp) {
    543 			tvp = tcp->vp;
    544 			tcp->vp = vp;
    545 			mutex_exit(&hp->hash_lock);
    546 			VN_RELE_DNLC(tvp);
    547 			ncstats.enters++;
    548 			ncs.ncs_enters.value.ui64++;
    549 			TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
    550 			    "dnlc_update_end:(%S) %d", "done", ncstats.enters);
    551 		} else {
    552 			mutex_exit(&hp->hash_lock);
    553 			VN_RELE_DNLC(vp);
    554 			ncstats.dbl_enters++;
    555 			ncs.ncs_dbl_enters.value.ui64++;
    556 			TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
    557 			    "dnlc_update_end:(%S) %d",
    558 			    "dbl enter", ncstats.dbl_enters);
    559 		}
    560 		VN_RELE_DNLC(dp);
    561 		dnlc_free(ncp);		/* crfree done here */
    562 		return;
    563 	}
    564 	/*
    565 	 * insert the new entry, since it is not in dnlc yet
    566 	 */
    567 	nc_inshash(ncp, hp);
    568 	mutex_exit(&hp->hash_lock);
    569 	ncstats.enters++;
    570 	ncs.ncs_enters.value.ui64++;
    571 	TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
    572 	    "dnlc_update_end:(%S) %d", "done", ncstats.enters);
    573 }
    574 
    575 /*
    576  * Look up a name in the directory name cache.
    577  *
    578  * Return a doubly-held vnode if found: one hold so that it may
    579  * remain in the cache for other users, the other hold so that
    580  * the cache is not re-cycled and the identity of the vnode is
    581  * lost before the caller can use the vnode.
    582  */
    583 vnode_t *
    584 dnlc_lookup(vnode_t *dp, char *name)
    585 {
    586 	ncache_t *ncp;
    587 	nc_hash_t *hp;
    588 	vnode_t *vp;
    589 	int hash, depth;
    590 	uchar_t namlen;
    591 
    592 	TRACE_2(TR_FAC_NFS, TR_DNLC_LOOKUP_START,
    593 	    "dnlc_lookup_start:dp %x name %s", dp, name);
    594 
    595 	if (!doingcache) {
    596 		TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
    597 		    "dnlc_lookup_end:%S %d vp %x name %s",
    598 		    "not_caching", 0, NULL, name);
    599 		return (NULL);
    600 	}
    601 
    602 	DNLCHASH(name, dp, hash, namlen);
    603 	depth = 1;
    604 	hp = &nc_hash[hash & nc_hashmask];
    605 	mutex_enter(&hp->hash_lock);
    606 
    607 	for (ncp = hp->hash_next; ncp != (ncache_t *)hp;
    608 	    ncp = ncp->hash_next) {
    609 		if (ncp->hash == hash &&	/* fast signature check */
    610 		    ncp->dp == dp &&
    611 		    ncp->namlen == namlen &&
    612 		    bcmp(ncp->name, name, namlen) == 0) {
    613 			/*
    614 			 * Move this entry to the head of its hash chain
    615 			 * if it's not already close.
    616 			 */
    617 			if (depth > NC_MOVETOFRONT) {
    618 				ncache_t *next = ncp->hash_next;
    619 				ncache_t *prev = ncp->hash_prev;
    620 
    621 				prev->hash_next = next;
    622 				next->hash_prev = prev;
    623 				ncp->hash_next = next = hp->hash_next;
    624 				ncp->hash_prev = (ncache_t *)hp;
    625 				next->hash_prev = ncp;
    626 				hp->hash_next = ncp;
    627 
    628 				ncstats.move_to_front++;
    629 			}
    630 
    631 			/*
    632 			 * Put a hold on the vnode now so its identity
    633 			 * can't change before the caller has a chance to
    634 			 * put a hold on it.
    635 			 */
    636 			vp = ncp->vp;
    637 			VN_HOLD_CALLER(vp); /* VN_HOLD 1 of 2 in this file */
    638 			mutex_exit(&hp->hash_lock);
    639 			ncstats.hits++;
    640 			ncs.ncs_hits.value.ui64++;
    641 			if (vp == DNLC_NO_VNODE) {
    642 				ncs.ncs_neg_hits.value.ui64++;
    643 			}
    644 			TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
    645 			    "dnlc_lookup_end:%S %d vp %x name %s", "hit",
    646 			    ncstats.hits, vp, name);
    647 			return (vp);
    648 		}
    649 		depth++;
    650 	}
    651 
    652 	mutex_exit(&hp->hash_lock);
    653 	ncstats.misses++;
    654 	ncs.ncs_misses.value.ui64++;
    655 	TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
    656 	    "dnlc_lookup_end:%S %d vp %x name %s", "miss", ncstats.misses,
    657 	    NULL, name);
    658 	return (NULL);
    659 }
    660 
    661 /*
    662  * Remove an entry in the directory name cache.
    663  */
    664 void
    665 dnlc_remove(vnode_t *dp, char *name)
    666 {
    667 	ncache_t *ncp;
    668 	nc_hash_t *hp;
    669 	uchar_t namlen;
    670 	int hash;
    671 
    672 	if (!doingcache)
    673 		return;
    674 	DNLCHASH(name, dp, hash, namlen);
    675 	hp = &nc_hash[hash & nc_hashmask];
    676 
    677 	mutex_enter(&hp->hash_lock);
    678 	if (ncp = dnlc_search(dp, name, namlen, hash)) {
    679 		/*
    680 		 * Free up the entry
    681 		 */
    682 		nc_rmhash(ncp);
    683 		mutex_exit(&hp->hash_lock);
    684 		VN_RELE_DNLC(ncp->vp);
    685 		VN_RELE_DNLC(ncp->dp);
    686 		dnlc_free(ncp);
    687 		return;
    688 	}
    689 	mutex_exit(&hp->hash_lock);
    690 }
    691 
    692 /*
    693  * Purge the entire cache.
    694  */
    695 void
    696 dnlc_purge()
    697 {
    698 	nc_hash_t *nch;
    699 	ncache_t *ncp;
    700 	int index;
    701 	int i;
    702 	vnode_t *nc_rele[DNLC_MAX_RELE];
    703 
    704 	if (!doingcache)
    705 		return;
    706 
    707 	ncstats.purges++;
    708 	ncs.ncs_purge_all.value.ui64++;
    709 
    710 	for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
    711 		index = 0;
    712 		mutex_enter(&nch->hash_lock);
    713 		ncp = nch->hash_next;
    714 		while (ncp != (ncache_t *)nch) {
    715 			ncache_t *np;
    716 
    717 			np = ncp->hash_next;
    718 			nc_rele[index++] = ncp->vp;
    719 			nc_rele[index++] = ncp->dp;
    720 
    721 			nc_rmhash(ncp);
    722 			dnlc_free(ncp);
    723 			ncp = np;
    724 			ncs.ncs_purge_total.value.ui64++;
    725 			if (index == DNLC_MAX_RELE)
    726 				break;
    727 		}
    728 		mutex_exit(&nch->hash_lock);
    729 
    730 		/* Release holds on all the vnodes now that we have no locks */
    731 		for (i = 0; i < index; i++) {
    732 			VN_RELE_DNLC(nc_rele[i]);
    733 		}
    734 		if (ncp != (ncache_t *)nch) {
    735 			nch--; /* Do current hash chain again */
    736 		}
    737 	}
    738 }
    739 
    740 /*
    741  * Purge any cache entries referencing a vnode. Exit as soon as the dnlc
    742  * reference count goes to zero (the caller still holds a reference).
    743  */
    744 void
    745 dnlc_purge_vp(vnode_t *vp)
    746 {
    747 	nc_hash_t *nch;
    748 	ncache_t *ncp;
    749 	int index;
    750 	vnode_t *nc_rele[DNLC_MAX_RELE];
    751 
    752 	ASSERT(vp->v_count > 0);
    753 	if (vp->v_count_dnlc == 0) {
    754 		return;
    755 	}
    756 
    757 	if (!doingcache)
    758 		return;
    759 
    760 	ncstats.purges++;
    761 	ncs.ncs_purge_vp.value.ui64++;
    762 
    763 	for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
    764 		index = 0;
    765 		mutex_enter(&nch->hash_lock);
    766 		ncp = nch->hash_next;
    767 		while (ncp != (ncache_t *)nch) {
    768 			ncache_t *np;
    769 
    770 			np = ncp->hash_next;
    771 			if (ncp->dp == vp || ncp->vp == vp) {
    772 				nc_rele[index++] = ncp->vp;
    773 				nc_rele[index++] = ncp->dp;
    774 				nc_rmhash(ncp);
    775 				dnlc_free(ncp);
    776 				ncs.ncs_purge_total.value.ui64++;
    777 				if (index == DNLC_MAX_RELE) {
    778 					ncp = np;
    779 					break;
    780 				}
    781 			}
    782 			ncp = np;
    783 		}
    784 		mutex_exit(&nch->hash_lock);
    785 
    786 		/* Release holds on all the vnodes now that we have no locks */
    787 		while (index) {
    788 			VN_RELE_DNLC(nc_rele[--index]);
    789 		}
    790 
    791 		if (vp->v_count_dnlc == 0) {
    792 			return;
    793 		}
    794 
    795 		if (ncp != (ncache_t *)nch) {
    796 			nch--; /* Do current hash chain again */
    797 		}
    798 	}
    799 }
    800 
    801 /*
    802  * Purge cache entries referencing a vfsp.  Caller supplies a count
    803  * of entries to purge; up to that many will be freed.  A count of
    804  * zero indicates that all such entries should be purged.  Returns
    805  * the number of entries that were purged.
    806  */
    807 int
    808 dnlc_purge_vfsp(vfs_t *vfsp, int count)
    809 {
    810 	nc_hash_t *nch;
    811 	ncache_t *ncp;
    812 	int n = 0;
    813 	int index;
    814 	int i;
    815 	vnode_t *nc_rele[DNLC_MAX_RELE];
    816 
    817 	if (!doingcache)
    818 		return (0);
    819 
    820 	ncstats.purges++;
    821 	ncs.ncs_purge_vfs.value.ui64++;
    822 
    823 	for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
    824 		index = 0;
    825 		mutex_enter(&nch->hash_lock);
    826 		ncp = nch->hash_next;
    827 		while (ncp != (ncache_t *)nch) {
    828 			ncache_t *np;
    829 
    830 			np = ncp->hash_next;
    831 			ASSERT(ncp->dp != NULL);
    832 			ASSERT(ncp->vp != NULL);
    833 			if ((ncp->dp->v_vfsp == vfsp) ||
    834 			    (ncp->vp->v_vfsp == vfsp)) {
    835 				n++;
    836 				nc_rele[index++] = ncp->vp;
    837 				nc_rele[index++] = ncp->dp;
    838 				nc_rmhash(ncp);
    839 				dnlc_free(ncp);
    840 				ncs.ncs_purge_total.value.ui64++;
    841 				if (index == DNLC_MAX_RELE) {
    842 					ncp = np;
    843 					break;
    844 				}
    845 				if (count != 0 && n >= count) {
    846 					break;
    847 				}
    848 			}
    849 			ncp = np;
    850 		}
    851 		mutex_exit(&nch->hash_lock);
    852 		/* Release holds on all the vnodes now that we have no locks */
    853 		for (i = 0; i < index; i++) {
    854 			VN_RELE_DNLC(nc_rele[i]);
    855 		}
    856 		if (count != 0 && n >= count) {
    857 			return (n);
    858 		}
    859 		if (ncp != (ncache_t *)nch) {
    860 			nch--; /* Do current hash chain again */
    861 		}
    862 	}
    863 	return (n);
    864 }
    865 
    866 /*
    867  * Purge 1 entry from the dnlc that is part of the filesystem(s)
    868  * represented by 'vop'. The purpose of this routine is to allow
    869  * users of the dnlc to free a vnode that is being held by the dnlc.
    870  *
    871  * If we find a vnode that we release which will result in
    872  * freeing the underlying vnode (count was 1), return 1, 0
    873  * if no appropriate vnodes found.
    874  *
    875  * Note, vop is not the 'right' identifier for a filesystem.
    876  */
    877 int
    878 dnlc_fs_purge1(vnodeops_t *vop)
    879 {
    880 	nc_hash_t *end;
    881 	nc_hash_t *hp;
    882 	ncache_t *ncp;
    883 	vnode_t *vp;
    884 
    885 	if (!doingcache)
    886 		return (0);
    887 
    888 	ncs.ncs_purge_fs1.value.ui64++;
    889 
    890 	/*
    891 	 * Scan the dnlc entries looking for a likely candidate.
    892 	 */
    893 	hp = end = dnlc_purge_fs1_rotor;
    894 
    895 	do {
    896 		if (++hp == &nc_hash[nc_hashsz])
    897 			hp = nc_hash;
    898 		dnlc_purge_fs1_rotor = hp;
    899 		if (hp->hash_next == (ncache_t *)hp)
    900 			continue;
    901 		mutex_enter(&hp->hash_lock);
    902 		for (ncp = hp->hash_prev;
    903 		    ncp != (ncache_t *)hp;
    904 		    ncp = ncp->hash_prev) {
    905 			vp = ncp->vp;
    906 			if (!vn_has_cached_data(vp) && (vp->v_count == 1) &&
    907 			    vn_matchops(vp, vop))
    908 				break;
    909 		}
    910 		if (ncp != (ncache_t *)hp) {
    911 			nc_rmhash(ncp);
    912 			mutex_exit(&hp->hash_lock);
    913 			VN_RELE_DNLC(ncp->dp);
    914 			VN_RELE_DNLC(vp)
    915 			dnlc_free(ncp);
    916 			ncs.ncs_purge_total.value.ui64++;
    917 			return (1);
    918 		}
    919 		mutex_exit(&hp->hash_lock);
    920 	} while (hp != end);
    921 	return (0);
    922 }
    923 
    924 /*
    925  * Perform a reverse lookup in the DNLC.  This will find the first occurrence of
    926  * the vnode.  If successful, it will return the vnode of the parent, and the
    927  * name of the entry in the given buffer.  If it cannot be found, or the buffer
    928  * is too small, then it will return NULL.  Note that this is a highly
    929  * inefficient function, since the DNLC is constructed solely for forward
    930  * lookups.
    931  */
    932 vnode_t *
    933 dnlc_reverse_lookup(vnode_t *vp, char *buf, size_t buflen)
    934 {
    935 	nc_hash_t *nch;
    936 	ncache_t *ncp;
    937 	vnode_t *pvp;
    938 
    939 	if (!doingcache)
    940 		return (NULL);
    941 
    942 	for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
    943 		mutex_enter(&nch->hash_lock);
    944 		ncp = nch->hash_next;
    945 		while (ncp != (ncache_t *)nch) {
    946 			/*
    947 			 * We ignore '..' entries since it can create
    948 			 * confusion and infinite loops.
    949 			 */
    950 			if (ncp->vp == vp && !(ncp->namlen == 2 &&
    951 			    0 == bcmp(ncp->name, "..", 2)) &&
    952 			    ncp->namlen < buflen) {
    953 				bcopy(ncp->name, buf, ncp->namlen);
    954 				buf[ncp->namlen] = '\0';
    955 				pvp = ncp->dp;
    956 				/* VN_HOLD 2 of 2 in this file */
    957 				VN_HOLD_CALLER(pvp);
    958 				mutex_exit(&nch->hash_lock);
    959 				return (pvp);
    960 			}
    961 			ncp = ncp->hash_next;
    962 		}
    963 		mutex_exit(&nch->hash_lock);
    964 	}
    965 
    966 	return (NULL);
    967 }
    968 /*
    969  * Utility routine to search for a cache entry. Return the
    970  * ncache entry if found, NULL otherwise.
    971  */
    972 static ncache_t *
    973 dnlc_search(vnode_t *dp, char *name, uchar_t namlen, int hash)
    974 {
    975 	nc_hash_t *hp;
    976 	ncache_t *ncp;
    977 
    978 	hp = &nc_hash[hash & nc_hashmask];
    979 
    980 	for (ncp = hp->hash_next; ncp != (ncache_t *)hp; ncp = ncp->hash_next) {
    981 		if (ncp->hash == hash &&
    982 		    ncp->dp == dp &&
    983 		    ncp->namlen == namlen &&
    984 		    bcmp(ncp->name, name, namlen) == 0)
    985 			return (ncp);
    986 	}
    987 	return (NULL);
    988 }
    989 
    990 #if ((1 << NBBY) - 1) < (MAXNAMELEN - 1)
    991 #error ncache_t name length representation is too small
    992 #endif
    993 
    994 void
    995 dnlc_reduce_cache(void *reduce_percent)
    996 {
    997 	if (dnlc_reduce_idle && (dnlc_nentries >= ncsize || reduce_percent)) {
    998 		dnlc_reduce_idle = 0;
    999 		if ((taskq_dispatch(system_taskq, do_dnlc_reduce_cache,
   1000 		    reduce_percent, TQ_NOSLEEP)) == NULL)
   1001 			dnlc_reduce_idle = 1;
   1002 	}
   1003 }
   1004 
   1005 /*
   1006  * Get a new name cache entry.
   1007  * If the dnlc_reduce_cache() taskq isn't keeping up with demand, or memory
   1008  * is short then just return NULL. If we're over ncsize then kick off a
   1009  * thread to free some in use entries down to dnlc_nentries_low_water.
   1010  * Caller must initialise all fields except namlen.
   1011  * Component names are defined to be less than MAXNAMELEN
   1012  * which includes a null.
   1013  */
   1014 static ncache_t *
   1015 dnlc_get(uchar_t namlen)
   1016 {
   1017 	ncache_t *ncp;
   1018 
   1019 	if (dnlc_nentries > dnlc_max_nentries) {
   1020 		dnlc_max_nentries_cnt++; /* keep a statistic */
   1021 		return (NULL);
   1022 	}
   1023 	ncp = kmem_alloc(sizeof (ncache_t) + namlen, KM_NOSLEEP);
   1024 	if (ncp == NULL) {
   1025 		return (NULL);
   1026 	}
   1027 	ncp->namlen = namlen;
   1028 	atomic_add_32(&dnlc_nentries, 1);
   1029 	dnlc_reduce_cache(NULL);
   1030 	return (ncp);
   1031 }
   1032 
   1033 /*
   1034  * Taskq routine to free up name cache entries to reduce the
   1035  * cache size to the low water mark if "reduce_percent" is not provided.
   1036  * If "reduce_percent" is provided, reduce cache size by
   1037  * (ncsize_onepercent * reduce_percent).
   1038  */
   1039 /*ARGSUSED*/
   1040 static void
   1041 do_dnlc_reduce_cache(void *reduce_percent)
   1042 {
   1043 	nc_hash_t *hp = dnlc_free_rotor, *start_hp = hp;
   1044 	vnode_t *vp;
   1045 	ncache_t *ncp;
   1046 	int cnt;
   1047 	uint_t low_water = dnlc_nentries_low_water;
   1048 
   1049 	if (reduce_percent) {
   1050 		uint_t reduce_cnt;
   1051 
   1052 		/*
   1053 		 * Never try to reduce the current number
   1054 		 * of cache entries below 3% of ncsize.
   1055 		 */
   1056 		if (dnlc_nentries <= ncsize_min_percent) {
   1057 			dnlc_reduce_idle = 1;
   1058 			return;
   1059 		}
   1060 		reduce_cnt = ncsize_onepercent *
   1061 		    (uint_t)(uintptr_t)reduce_percent;
   1062 
   1063 		if (reduce_cnt > dnlc_nentries ||
   1064 		    dnlc_nentries - reduce_cnt < ncsize_min_percent)
   1065 			low_water = ncsize_min_percent;
   1066 		else
   1067 			low_water = dnlc_nentries - reduce_cnt;
   1068 	}
   1069 
   1070 	do {
   1071 		/*
   1072 		 * Find the first non empty hash queue without locking.
   1073 		 * Only look at each hash queue once to avoid an infinite loop.
   1074 		 */
   1075 		do {
   1076 			if (++hp == &nc_hash[nc_hashsz])
   1077 				hp = nc_hash;
   1078 		} while (hp->hash_next == (ncache_t *)hp && hp != start_hp);
   1079 
   1080 		/* return if all hash queues are empty. */
   1081 		if (hp->hash_next == (ncache_t *)hp) {
   1082 			dnlc_reduce_idle = 1;
   1083 			return;
   1084 		}
   1085 
   1086 		mutex_enter(&hp->hash_lock);
   1087 		for (cnt = 0, ncp = hp->hash_prev; ncp != (ncache_t *)hp;
   1088 		    ncp = ncp->hash_prev, cnt++) {
   1089 			vp = ncp->vp;
   1090 			/*
   1091 			 * A name cache entry with a reference count
   1092 			 * of one is only referenced by the dnlc.
   1093 			 * Also negative cache entries are purged first.
   1094 			 */
   1095 			if (!vn_has_cached_data(vp) &&
   1096 			    ((vp->v_count == 1) || (vp == DNLC_NO_VNODE))) {
   1097 				ncs.ncs_pick_heur.value.ui64++;
   1098 				goto found;
   1099 			}
   1100 			/*
   1101 			 * Remove from the end of the chain if the
   1102 			 * chain is too long
   1103 			 */
   1104 			if (cnt > dnlc_long_chain) {
   1