xref: /titanic_51/usr/src/uts/common/fs/dnlc.c (revision b5fca8f855054d167d04d3b4de5210c83ed2083c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5dbd19a09Sah89892  * Common Development and Distribution License (the "License").
6dbd19a09Sah89892  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b5fca8f8Stomee  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
457c478bd9Sstevel@tonic-gate #include <sys/systm.h>
467c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
477c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
487c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
517c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
527c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
537c478bd9Sstevel@tonic-gate #include <sys/var.h>
547c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
557c478bd9Sstevel@tonic-gate #include <sys/kstat.h>
567c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
577c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Directory name lookup cache.
617c478bd9Sstevel@tonic-gate  * Based on code originally done by Robert Elz at Melbourne.
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  * Names found by directory scans are retained in a cache
647c478bd9Sstevel@tonic-gate  * for future reference.  Each hash chain is ordered by LRU
657c478bd9Sstevel@tonic-gate  * Cache is indexed by hash value obtained from (vp, name)
667c478bd9Sstevel@tonic-gate  * where the vp refers to the directory containing the name.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
70*b5fca8f8Stomee  * We want to be able to identify files that are referenced only by the DNLC.
71*b5fca8f8Stomee  * When adding a reference from the DNLC, call VN_HOLD_DNLC instead of VN_HOLD,
72*b5fca8f8Stomee  * since multiple DNLC references should only be counted once in v_count. This
73*b5fca8f8Stomee  * file contains only two(2) calls to VN_HOLD, renamed VN_HOLD_CALLER in the
74*b5fca8f8Stomee  * hope that no one will mistakenly add a VN_HOLD to this file. (Unfortunately
75*b5fca8f8Stomee  * it is not possible to #undef VN_HOLD and retain VN_HOLD_CALLER. Ideally a
76*b5fca8f8Stomee  * Makefile rule would grep uncommented C tokens to check that VN_HOLD is
77*b5fca8f8Stomee  * referenced only once in this file, to define VN_HOLD_CALLER.)
78*b5fca8f8Stomee  */
79*b5fca8f8Stomee #define	VN_HOLD_CALLER	VN_HOLD
80*b5fca8f8Stomee #define	VN_HOLD_DNLC(vp)	{	\
81*b5fca8f8Stomee 	mutex_enter(&(vp)->v_lock);	\
82*b5fca8f8Stomee 	if ((vp)->v_count_dnlc == 0)	\
83*b5fca8f8Stomee 		(vp)->v_count++;	\
84*b5fca8f8Stomee 	(vp)->v_count_dnlc++;		\
85*b5fca8f8Stomee 	mutex_exit(&(vp)->v_lock);	\
86*b5fca8f8Stomee }
87*b5fca8f8Stomee #define	VN_RELE_DNLC(vp)	{	\
88*b5fca8f8Stomee 	vn_rele_dnlc(vp);		\
89*b5fca8f8Stomee }
90*b5fca8f8Stomee 
91*b5fca8f8Stomee /*
927c478bd9Sstevel@tonic-gate  * Tunable nc_hashavelen is the average length desired for this chain, from
937c478bd9Sstevel@tonic-gate  * which the size of the nc_hash table is derived at create time.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate #define	NC_HASHAVELEN_DEFAULT	4
967c478bd9Sstevel@tonic-gate int nc_hashavelen = NC_HASHAVELEN_DEFAULT;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * NC_MOVETOFRONT is the move-to-front threshold: if the hash lookup
1007c478bd9Sstevel@tonic-gate  * depth exceeds this value, we move the looked-up entry to the front of
1017c478bd9Sstevel@tonic-gate  * its hash chain.  The idea is to make sure that the most frequently
1027c478bd9Sstevel@tonic-gate  * accessed entries are found most quickly (by keeping them near the
1037c478bd9Sstevel@tonic-gate  * front of their hash chains).
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate #define	NC_MOVETOFRONT	2
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * DNLC_MAX_RELE is used to size an array on the stack when releasing
1107c478bd9Sstevel@tonic-gate  * vnodes. This array is used rather than calling VN_RELE() inline because
1117c478bd9Sstevel@tonic-gate  * all dnlc locks must be dropped by that time in order to avoid a
1127c478bd9Sstevel@tonic-gate  * possible deadlock. This deadlock occurs when the dnlc holds the last
1137c478bd9Sstevel@tonic-gate  * reference to the vnode and so the VOP_INACTIVE vector is called which
1147c478bd9Sstevel@tonic-gate  * can in turn call back into the dnlc. A global array was used but had
1157c478bd9Sstevel@tonic-gate  * many problems:
1167c478bd9Sstevel@tonic-gate  *	1) Actually doesn't have an upper bound on the array size as
1177c478bd9Sstevel@tonic-gate  *	   entries can be added after starting the purge.
1187c478bd9Sstevel@tonic-gate  *	2) The locking scheme causes a hang.
1197c478bd9Sstevel@tonic-gate  *	3) Caused serialisation on the global lock.
1207c478bd9Sstevel@tonic-gate  *	4) The array was often unnecessarily huge.
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  * Note the current value 8 allows up to 4 cache entries (to be purged
1237c478bd9Sstevel@tonic-gate  * from each hash chain), before having to cycle around and retry.
1247c478bd9Sstevel@tonic-gate  * This ought to be ample given that nc_hashavelen is typically very small.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate #define	DNLC_MAX_RELE	8 /* must be even */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Hash table of name cache entries for fast lookup, dynamically
1307c478bd9Sstevel@tonic-gate  * allocated at startup.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate nc_hash_t *nc_hash;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Rotors. Used to select entries on a round-robin basis.
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate static nc_hash_t *dnlc_purge_fs1_rotor;
1387c478bd9Sstevel@tonic-gate static nc_hash_t *dnlc_free_rotor;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * # of dnlc entries (uninitialized)
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * the initial value was chosen as being
1447c478bd9Sstevel@tonic-gate  * a random string of bits, probably not
1457c478bd9Sstevel@tonic-gate  * normally chosen by a systems administrator
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate int ncsize = -1;
148104e2ed7Sperrin volatile uint32_t dnlc_nentries = 0;	/* current num of name cache entries */
1497c478bd9Sstevel@tonic-gate static int nc_hashsz;			/* size of hash table */
1507c478bd9Sstevel@tonic-gate static int nc_hashmask;			/* size of hash table minus 1 */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * The dnlc_reduce_cache() taskq queue is activated when there are
154033f9833Sek110237  * ncsize name cache entries and if no parameter is provided, it reduces
155033f9833Sek110237  * the size down to dnlc_nentries_low_water, which is by default one
156033f9833Sek110237  * hundreth less (or 99%) of ncsize.
157033f9833Sek110237  *
158033f9833Sek110237  * If a parameter is provided to dnlc_reduce_cache(), then we reduce
159033f9833Sek110237  * the size down based on ncsize_onepercent - where ncsize_onepercent
160104e2ed7Sperrin  * is 1% of ncsize; however, we never let dnlc_reduce_cache() reduce
161104e2ed7Sperrin  * the size below 3% of ncsize (ncsize_min_percent).
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate #define	DNLC_LOW_WATER_DIVISOR_DEFAULT 100
1647c478bd9Sstevel@tonic-gate uint_t dnlc_low_water_divisor = DNLC_LOW_WATER_DIVISOR_DEFAULT;
1657c478bd9Sstevel@tonic-gate uint_t dnlc_nentries_low_water;
1667c478bd9Sstevel@tonic-gate int dnlc_reduce_idle = 1; /* no locking needed */
167033f9833Sek110237 uint_t ncsize_onepercent;
168104e2ed7Sperrin uint_t ncsize_min_percent;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * If dnlc_nentries hits dnlc_max_nentries (twice ncsize)
1727c478bd9Sstevel@tonic-gate  * then this means the dnlc_reduce_cache() taskq is failing to
1737c478bd9Sstevel@tonic-gate  * keep up. In this case we refuse to add new entries to the dnlc
1747c478bd9Sstevel@tonic-gate  * until the taskq catches up.
1757c478bd9Sstevel@tonic-gate  */
1767c478bd9Sstevel@tonic-gate uint_t dnlc_max_nentries; /* twice ncsize */
1777c478bd9Sstevel@tonic-gate uint64_t dnlc_max_nentries_cnt = 0; /* statistic on times we failed */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /*
1807c478bd9Sstevel@tonic-gate  * Tunable to define when we should just remove items from
1817c478bd9Sstevel@tonic-gate  * the end of the chain.
1827c478bd9Sstevel@tonic-gate  */
1837c478bd9Sstevel@tonic-gate #define	DNLC_LONG_CHAIN 8
1847c478bd9Sstevel@tonic-gate uint_t dnlc_long_chain = DNLC_LONG_CHAIN;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * ncstats has been deprecated, due to the integer size of the counters
1887c478bd9Sstevel@tonic-gate  * which can easily overflow in the dnlc.
1897c478bd9Sstevel@tonic-gate  * It is maintained (at some expense) for compatability.
1907c478bd9Sstevel@tonic-gate  * The preferred interface is the kstat accessible nc_stats below.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate struct ncstats ncstats;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate struct nc_stats ncs = {
1957c478bd9Sstevel@tonic-gate 	{ "hits",			KSTAT_DATA_UINT64 },
1967c478bd9Sstevel@tonic-gate 	{ "misses",			KSTAT_DATA_UINT64 },
1977c478bd9Sstevel@tonic-gate 	{ "negative_cache_hits",	KSTAT_DATA_UINT64 },
1987c478bd9Sstevel@tonic-gate 	{ "enters",			KSTAT_DATA_UINT64 },
1997c478bd9Sstevel@tonic-gate 	{ "double_enters",		KSTAT_DATA_UINT64 },
2007c478bd9Sstevel@tonic-gate 	{ "purge_total_entries",	KSTAT_DATA_UINT64 },
2017c478bd9Sstevel@tonic-gate 	{ "purge_all",			KSTAT_DATA_UINT64 },
2027c478bd9Sstevel@tonic-gate 	{ "purge_vp",			KSTAT_DATA_UINT64 },
2037c478bd9Sstevel@tonic-gate 	{ "purge_vfs",			KSTAT_DATA_UINT64 },
2047c478bd9Sstevel@tonic-gate 	{ "purge_fs1",			KSTAT_DATA_UINT64 },
2057c478bd9Sstevel@tonic-gate 	{ "pick_free",			KSTAT_DATA_UINT64 },
2067c478bd9Sstevel@tonic-gate 	{ "pick_heuristic",		KSTAT_DATA_UINT64 },
2077c478bd9Sstevel@tonic-gate 	{ "pick_last",			KSTAT_DATA_UINT64 },
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	/* directory caching stats */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	{ "dir_hits",			KSTAT_DATA_UINT64 },
2127c478bd9Sstevel@tonic-gate 	{ "dir_misses",			KSTAT_DATA_UINT64 },
2137c478bd9Sstevel@tonic-gate 	{ "dir_cached_current",		KSTAT_DATA_UINT64 },
2147c478bd9Sstevel@tonic-gate 	{ "dir_entries_cached_current",	KSTAT_DATA_UINT64 },
2157c478bd9Sstevel@tonic-gate 	{ "dir_cached_total",		KSTAT_DATA_UINT64 },
2167c478bd9Sstevel@tonic-gate 	{ "dir_start_no_memory",	KSTAT_DATA_UINT64 },
2177c478bd9Sstevel@tonic-gate 	{ "dir_add_no_memory",		KSTAT_DATA_UINT64 },
2187c478bd9Sstevel@tonic-gate 	{ "dir_add_abort",		KSTAT_DATA_UINT64 },
2197c478bd9Sstevel@tonic-gate 	{ "dir_add_max",		KSTAT_DATA_UINT64 },
2207c478bd9Sstevel@tonic-gate 	{ "dir_remove_entry_fail",	KSTAT_DATA_UINT64 },
2217c478bd9Sstevel@tonic-gate 	{ "dir_remove_space_fail",	KSTAT_DATA_UINT64 },
2227c478bd9Sstevel@tonic-gate 	{ "dir_update_fail",		KSTAT_DATA_UINT64 },
2237c478bd9Sstevel@tonic-gate 	{ "dir_fini_purge",		KSTAT_DATA_UINT64 },
2247c478bd9Sstevel@tonic-gate 	{ "dir_reclaim_last",		KSTAT_DATA_UINT64 },
2257c478bd9Sstevel@tonic-gate 	{ "dir_reclaim_any",		KSTAT_DATA_UINT64 },
2267c478bd9Sstevel@tonic-gate };
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate static int doingcache = 1;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate vnode_t negative_cache_vnode;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate  * Insert entry at the front of the queue
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate #define	nc_inshash(ncp, hp) \
2367c478bd9Sstevel@tonic-gate { \
2377c478bd9Sstevel@tonic-gate 	(ncp)->hash_next = (hp)->hash_next; \
2387c478bd9Sstevel@tonic-gate 	(ncp)->hash_prev = (ncache_t *)(hp); \
2397c478bd9Sstevel@tonic-gate 	(hp)->hash_next->hash_prev = (ncp); \
2407c478bd9Sstevel@tonic-gate 	(hp)->hash_next = (ncp); \
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate /*
2447c478bd9Sstevel@tonic-gate  * Remove entry from hash queue
2457c478bd9Sstevel@tonic-gate  */
2467c478bd9Sstevel@tonic-gate #define	nc_rmhash(ncp) \
2477c478bd9Sstevel@tonic-gate { \
2487c478bd9Sstevel@tonic-gate 	(ncp)->hash_prev->hash_next = (ncp)->hash_next; \
2497c478bd9Sstevel@tonic-gate 	(ncp)->hash_next->hash_prev = (ncp)->hash_prev; \
2507c478bd9Sstevel@tonic-gate 	(ncp)->hash_prev = NULL; \
2517c478bd9Sstevel@tonic-gate 	(ncp)->hash_next = NULL; \
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * Free an entry.
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate #define	dnlc_free(ncp) \
2587c478bd9Sstevel@tonic-gate { \
2597c478bd9Sstevel@tonic-gate 	kmem_free((ncp), sizeof (ncache_t) + (ncp)->namlen); \
2607c478bd9Sstevel@tonic-gate 	atomic_add_32(&dnlc_nentries, -1); \
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate  * Cached directory info.
2667c478bd9Sstevel@tonic-gate  * ======================
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * Cached directory free space hash function.
2717c478bd9Sstevel@tonic-gate  * Needs the free space handle and the dcp to get the hash table size
2727c478bd9Sstevel@tonic-gate  * Returns the hash index.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate #define	DDFHASH(handle, dcp) ((handle >> 2) & (dcp)->dc_fhash_mask)
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * Cached directory name entry hash function.
2787c478bd9Sstevel@tonic-gate  * Uses the name and returns in the input arguments the hash and the name
2797c478bd9Sstevel@tonic-gate  * length.
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate #define	DNLC_DIR_HASH(name, hash, namelen)			\
2827c478bd9Sstevel@tonic-gate 	{							\
2837c478bd9Sstevel@tonic-gate 		char Xc, *Xcp;					\
2847c478bd9Sstevel@tonic-gate 		hash = *name;					\
2857c478bd9Sstevel@tonic-gate 		for (Xcp = (name + 1); (Xc = *Xcp) != 0; Xcp++)	\
2867c478bd9Sstevel@tonic-gate 			hash = (hash << 4) + hash + Xc;		\
2877c478bd9Sstevel@tonic-gate 		ASSERT((Xcp - (name)) <= ((1 << NBBY) - 1));	\
2887c478bd9Sstevel@tonic-gate 		namelen = Xcp - (name);				\
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /* special dircache_t pointer to indicate error should be returned */
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * The anchor directory cache pointer can contain 3 types of values,
2947c478bd9Sstevel@tonic-gate  * 1) NULL: No directory cache
2957c478bd9Sstevel@tonic-gate  * 2) DC_RET_LOW_MEM (-1): There was a directory cache that found to be
2967c478bd9Sstevel@tonic-gate  *    too big or a memory shortage occurred. This value remains in the
2977c478bd9Sstevel@tonic-gate  *    pointer until a dnlc_dir_start() which returns the a DNOMEM error.
2987c478bd9Sstevel@tonic-gate  *    This is kludgy but efficient and only visible in this source file.
2997c478bd9Sstevel@tonic-gate  * 3) A valid cache pointer.
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate #define	DC_RET_LOW_MEM (dircache_t *)1
3027c478bd9Sstevel@tonic-gate #define	VALID_DIR_CACHE(dcp) ((dircache_t *)(dcp) > DC_RET_LOW_MEM)
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /* Tunables */
3057c478bd9Sstevel@tonic-gate uint_t dnlc_dir_enable = 1; /* disable caching directories by setting to 0 */
3067c478bd9Sstevel@tonic-gate uint_t dnlc_dir_min_size = 40; /* min no of directory entries before caching */
3077c478bd9Sstevel@tonic-gate uint_t dnlc_dir_max_size = UINT_MAX; /* ditto maximum */
3087c478bd9Sstevel@tonic-gate uint_t dnlc_dir_hash_size_shift = 3; /* 8 entries per hash bucket */
3097c478bd9Sstevel@tonic-gate uint_t dnlc_dir_min_reclaim =  350000; /* approx 1MB of dcentrys */
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate  * dnlc_dir_hash_resize_shift determines when the hash tables
3127c478bd9Sstevel@tonic-gate  * get re-adjusted due to growth or shrinkage
3137c478bd9Sstevel@tonic-gate  * - currently 2 indicating that there can be at most 4
3147c478bd9Sstevel@tonic-gate  * times or at least one quarter the number of entries
3157c478bd9Sstevel@tonic-gate  * before hash table readjustment. Note that with
3167c478bd9Sstevel@tonic-gate  * dnlc_dir_hash_size_shift above set at 3 this would
3177c478bd9Sstevel@tonic-gate  * mean readjustment would occur if the average number
3187c478bd9Sstevel@tonic-gate  * of entries went above 32 or below 2
3197c478bd9Sstevel@tonic-gate  */
3207c478bd9Sstevel@tonic-gate uint_t dnlc_dir_hash_resize_shift = 2; /* readjust rate */
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate static kmem_cache_t *dnlc_dir_space_cache; /* free space entry cache */
3237c478bd9Sstevel@tonic-gate static dchead_t dc_head; /* anchor of cached directories */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /* Prototypes */
3267c478bd9Sstevel@tonic-gate static ncache_t *dnlc_get(uchar_t namlen);
3277c478bd9Sstevel@tonic-gate static ncache_t *dnlc_search(vnode_t *dp, char *name, uchar_t namlen, int hash);
3287c478bd9Sstevel@tonic-gate static void dnlc_dir_reclaim(void *unused);
3297c478bd9Sstevel@tonic-gate static void dnlc_dir_abort(dircache_t *dcp);
3307c478bd9Sstevel@tonic-gate static void dnlc_dir_adjust_fhash(dircache_t *dcp);
3317c478bd9Sstevel@tonic-gate static void dnlc_dir_adjust_nhash(dircache_t *dcp);
332104e2ed7Sperrin static void do_dnlc_reduce_cache(void *);
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * Initialize the directory cache.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate void
3397c478bd9Sstevel@tonic-gate dnlc_init()
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	nc_hash_t *hp;
3427c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
3437c478bd9Sstevel@tonic-gate 	int i;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/*
3467c478bd9Sstevel@tonic-gate 	 * Set up the size of the dnlc (ncsize) and its low water mark.
3477c478bd9Sstevel@tonic-gate 	 */
3487c478bd9Sstevel@tonic-gate 	if (ncsize == -1) {
3497c478bd9Sstevel@tonic-gate 		/* calculate a reasonable size for the low water */
3507c478bd9Sstevel@tonic-gate 		dnlc_nentries_low_water = 4 * (v.v_proc + maxusers) + 320;
3517c478bd9Sstevel@tonic-gate 		ncsize = dnlc_nentries_low_water +
3527c478bd9Sstevel@tonic-gate 		    (dnlc_nentries_low_water / dnlc_low_water_divisor);
3537c478bd9Sstevel@tonic-gate 	} else {
3547c478bd9Sstevel@tonic-gate 		/* don't change the user specified ncsize */
3557c478bd9Sstevel@tonic-gate 		dnlc_nentries_low_water =
3567c478bd9Sstevel@tonic-gate 		    ncsize - (ncsize / dnlc_low_water_divisor);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 	if (ncsize <= 0) {
3597c478bd9Sstevel@tonic-gate 		doingcache = 0;
3607c478bd9Sstevel@tonic-gate 		dnlc_dir_enable = 0; /* also disable directory caching */
3617c478bd9Sstevel@tonic-gate 		ncsize = 0;
3627c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "name cache (dnlc) disabled");
3637c478bd9Sstevel@tonic-gate 		return;
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 	dnlc_max_nentries = ncsize * 2;
366033f9833Sek110237 	ncsize_onepercent = ncsize / 100;
367104e2ed7Sperrin 	ncsize_min_percent = ncsize_onepercent * 3;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	/*
3707c478bd9Sstevel@tonic-gate 	 * Initialise the hash table.
3717c478bd9Sstevel@tonic-gate 	 * Compute hash size rounding to the next power of two.
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	nc_hashsz = ncsize / nc_hashavelen;
3747c478bd9Sstevel@tonic-gate 	nc_hashsz = 1 << highbit(nc_hashsz);
3757c478bd9Sstevel@tonic-gate 	nc_hashmask = nc_hashsz - 1;
3767c478bd9Sstevel@tonic-gate 	nc_hash = kmem_zalloc(nc_hashsz * sizeof (*nc_hash), KM_SLEEP);
3777c478bd9Sstevel@tonic-gate 	for (i = 0; i < nc_hashsz; i++) {
3787c478bd9Sstevel@tonic-gate 		hp = (nc_hash_t *)&nc_hash[i];
3797c478bd9Sstevel@tonic-gate 		mutex_init(&hp->hash_lock, NULL, MUTEX_DEFAULT, NULL);
3807c478bd9Sstevel@tonic-gate 		hp->hash_next = (ncache_t *)hp;
3817c478bd9Sstevel@tonic-gate 		hp->hash_prev = (ncache_t *)hp;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	/*
3857c478bd9Sstevel@tonic-gate 	 * Initialize rotors
3867c478bd9Sstevel@tonic-gate 	 */
3877c478bd9Sstevel@tonic-gate 	dnlc_free_rotor = dnlc_purge_fs1_rotor = &nc_hash[0];
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	/*
3907c478bd9Sstevel@tonic-gate 	 * Set up the directory caching to use kmem_cache_alloc
3917c478bd9Sstevel@tonic-gate 	 * for its free space entries so that we can get a callback
3927c478bd9Sstevel@tonic-gate 	 * when the system is short on memory, to allow us to free
3937c478bd9Sstevel@tonic-gate 	 * up some memory. we don't use the constructor/deconstructor
3947c478bd9Sstevel@tonic-gate 	 * functions.
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 	dnlc_dir_space_cache = kmem_cache_create("dnlc_space_cache",
3977c478bd9Sstevel@tonic-gate 	    sizeof (dcfree_t), 0, NULL, NULL, dnlc_dir_reclaim, NULL,
3987c478bd9Sstevel@tonic-gate 	    NULL, 0);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/*
4017c478bd9Sstevel@tonic-gate 	 * Initialise the head of the cached directory structures
4027c478bd9Sstevel@tonic-gate 	 */
4037c478bd9Sstevel@tonic-gate 	mutex_init(&dc_head.dch_lock, NULL, MUTEX_DEFAULT, NULL);
4047c478bd9Sstevel@tonic-gate 	dc_head.dch_next = (dircache_t *)&dc_head;
4057c478bd9Sstevel@tonic-gate 	dc_head.dch_prev = (dircache_t *)&dc_head;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/*
4087c478bd9Sstevel@tonic-gate 	 * Initialise the reference count of the negative cache vnode to 1
4097c478bd9Sstevel@tonic-gate 	 * so that it never goes away (VOP_INACTIVE isn't called on it).
4107c478bd9Sstevel@tonic-gate 	 */
4117c478bd9Sstevel@tonic-gate 	negative_cache_vnode.v_count = 1;
412*b5fca8f8Stomee 	negative_cache_vnode.v_count_dnlc = 0;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * Initialise kstats - both the old compatability raw kind and
4167c478bd9Sstevel@tonic-gate 	 * the more extensive named stats.
4177c478bd9Sstevel@tonic-gate 	 */
4187c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "ncstats", "misc", KSTAT_TYPE_RAW,
4197c478bd9Sstevel@tonic-gate 	    sizeof (struct ncstats), KSTAT_FLAG_VIRTUAL);
4207c478bd9Sstevel@tonic-gate 	if (ksp) {
4217c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &ncstats;
4227c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	ksp = kstat_create("unix", 0, "dnlcstats", "misc", KSTAT_TYPE_NAMED,
4257c478bd9Sstevel@tonic-gate 	    sizeof (ncs) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
4267c478bd9Sstevel@tonic-gate 	if (ksp) {
4277c478bd9Sstevel@tonic-gate 		ksp->ks_data = (void *) &ncs;
4287c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  * Add a name to the directory cache.
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate void
4367c478bd9Sstevel@tonic-gate dnlc_enter(vnode_t *dp, char *name, vnode_t *vp)
4377c478bd9Sstevel@tonic-gate {
4387c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
4397c478bd9Sstevel@tonic-gate 	nc_hash_t *hp;
4407c478bd9Sstevel@tonic-gate 	uchar_t namlen;
4417c478bd9Sstevel@tonic-gate 	int hash;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_enter_start:");
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	if (!doingcache) {
4467c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
4477c478bd9Sstevel@tonic-gate 		    "dnlc_enter_end:(%S) %d", "not caching", 0);
4487c478bd9Sstevel@tonic-gate 		return;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * Get a new dnlc entry. Assume the entry won't be in the cache
4537c478bd9Sstevel@tonic-gate 	 * and initialize it now
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 	DNLCHASH(name, dp, hash, namlen);
4567c478bd9Sstevel@tonic-gate 	if ((ncp = dnlc_get(namlen)) == NULL)
4577c478bd9Sstevel@tonic-gate 		return;
4587c478bd9Sstevel@tonic-gate 	ncp->dp = dp;
459*b5fca8f8Stomee 	VN_HOLD_DNLC(dp);
4607c478bd9Sstevel@tonic-gate 	ncp->vp = vp;
461*b5fca8f8Stomee 	VN_HOLD_DNLC(vp);
4627c478bd9Sstevel@tonic-gate 	bcopy(name, ncp->name, namlen + 1); /* name and null */
4637c478bd9Sstevel@tonic-gate 	ncp->hash = hash;
4647c478bd9Sstevel@tonic-gate 	hp = &nc_hash[hash & nc_hashmask];
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->hash_lock);
4677c478bd9Sstevel@tonic-gate 	if (dnlc_search(dp, name, namlen, hash) != NULL) {
4687c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->hash_lock);
4697c478bd9Sstevel@tonic-gate 		ncstats.dbl_enters++;
4707c478bd9Sstevel@tonic-gate 		ncs.ncs_dbl_enters.value.ui64++;
471*b5fca8f8Stomee 		VN_RELE_DNLC(dp);
472*b5fca8f8Stomee 		VN_RELE_DNLC(vp);
4737c478bd9Sstevel@tonic-gate 		dnlc_free(ncp);		/* crfree done here */
4747c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
475*b5fca8f8Stomee 		    "dnlc_enter_end:(%S) %d", "dbl enter", ncstats.dbl_enters);
4767c478bd9Sstevel@tonic-gate 		return;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 	/*
4797c478bd9Sstevel@tonic-gate 	 * Insert back into the hash chain.
4807c478bd9Sstevel@tonic-gate 	 */
4817c478bd9Sstevel@tonic-gate 	nc_inshash(ncp, hp);
4827c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->hash_lock);
4837c478bd9Sstevel@tonic-gate 	ncstats.enters++;
4847c478bd9Sstevel@tonic-gate 	ncs.ncs_enters.value.ui64++;
4857c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
4867c478bd9Sstevel@tonic-gate 	    "dnlc_enter_end:(%S) %d", "done", ncstats.enters);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * Add a name to the directory cache.
4917c478bd9Sstevel@tonic-gate  *
4927c478bd9Sstevel@tonic-gate  * This function is basically identical with
4937c478bd9Sstevel@tonic-gate  * dnlc_enter().  The difference is that when the
4947c478bd9Sstevel@tonic-gate  * desired dnlc entry is found, the vnode in the
4957c478bd9Sstevel@tonic-gate  * ncache is compared with the vnode passed in.
4967c478bd9Sstevel@tonic-gate  *
4977c478bd9Sstevel@tonic-gate  * If they are not equal then the ncache is
4987c478bd9Sstevel@tonic-gate  * updated with the passed in vnode.  Otherwise
4997c478bd9Sstevel@tonic-gate  * it just frees up the newly allocated dnlc entry.
5007c478bd9Sstevel@tonic-gate  */
5017c478bd9Sstevel@tonic-gate void
5027c478bd9Sstevel@tonic-gate dnlc_update(vnode_t *dp, char *name, vnode_t *vp)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
5057c478bd9Sstevel@tonic-gate 	ncache_t *tcp;
5067c478bd9Sstevel@tonic-gate 	vnode_t *tvp;
5077c478bd9Sstevel@tonic-gate 	nc_hash_t *hp;
5087c478bd9Sstevel@tonic-gate 	int hash;
5097c478bd9Sstevel@tonic-gate 	uchar_t namlen;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_update_start:");
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if (!doingcache) {
5147c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
5157c478bd9Sstevel@tonic-gate 		    "dnlc_update_end:(%S) %d", "not caching", 0);
5167c478bd9Sstevel@tonic-gate 		return;
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * Get a new dnlc entry and initialize it now.
5217c478bd9Sstevel@tonic-gate 	 * If we fail to get a new entry, call dnlc_remove() to purge
5227c478bd9Sstevel@tonic-gate 	 * any existing dnlc entry including negative cache (DNLC_NO_VNODE)
5237c478bd9Sstevel@tonic-gate 	 * entry.
5247c478bd9Sstevel@tonic-gate 	 * Failure to clear an existing entry could result in false dnlc
5257c478bd9Sstevel@tonic-gate 	 * lookup (negative/stale entry).
5267c478bd9Sstevel@tonic-gate 	 */
5277c478bd9Sstevel@tonic-gate 	DNLCHASH(name, dp, hash, namlen);
5287c478bd9Sstevel@tonic-gate 	if ((ncp = dnlc_get(namlen)) == NULL) {
5297c478bd9Sstevel@tonic-gate 		dnlc_remove(dp, name);
5307c478bd9Sstevel@tonic-gate 		return;
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 	ncp->dp = dp;
533*b5fca8f8Stomee 	VN_HOLD_DNLC(dp);
5347c478bd9Sstevel@tonic-gate 	ncp->vp = vp;
535*b5fca8f8Stomee 	VN_HOLD_DNLC(vp);
5367c478bd9Sstevel@tonic-gate 	bcopy(name, ncp->name, namlen + 1); /* name and null */
5377c478bd9Sstevel@tonic-gate 	ncp->hash = hash;
5387c478bd9Sstevel@tonic-gate 	hp = &nc_hash[hash & nc_hashmask];
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->hash_lock);
5417c478bd9Sstevel@tonic-gate 	if ((tcp = dnlc_search(dp, name, namlen, hash)) != NULL) {
5427c478bd9Sstevel@tonic-gate 		if (tcp->vp != vp) {
5437c478bd9Sstevel@tonic-gate 			tvp = tcp->vp;
5447c478bd9Sstevel@tonic-gate 			tcp->vp = vp;
5457c478bd9Sstevel@tonic-gate 			mutex_exit(&hp->hash_lock);
546*b5fca8f8Stomee 			VN_RELE_DNLC(tvp);
5477c478bd9Sstevel@tonic-gate 			ncstats.enters++;
5487c478bd9Sstevel@tonic-gate 			ncs.ncs_enters.value.ui64++;
5497c478bd9Sstevel@tonic-gate 			TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
5507c478bd9Sstevel@tonic-gate 			    "dnlc_update_end:(%S) %d", "done", ncstats.enters);
5517c478bd9Sstevel@tonic-gate 		} else {
5527c478bd9Sstevel@tonic-gate 			mutex_exit(&hp->hash_lock);
553*b5fca8f8Stomee 			VN_RELE_DNLC(vp);
5547c478bd9Sstevel@tonic-gate 			ncstats.dbl_enters++;
5557c478bd9Sstevel@tonic-gate 			ncs.ncs_dbl_enters.value.ui64++;
5567c478bd9Sstevel@tonic-gate 			TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
5577c478bd9Sstevel@tonic-gate 			    "dnlc_update_end:(%S) %d",
5587c478bd9Sstevel@tonic-gate 			    "dbl enter", ncstats.dbl_enters);
5597c478bd9Sstevel@tonic-gate 		}
560*b5fca8f8Stomee 		VN_RELE_DNLC(dp);
5617c478bd9Sstevel@tonic-gate 		dnlc_free(ncp);		/* crfree done here */
5627c478bd9Sstevel@tonic-gate 		return;
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 	/*
5657c478bd9Sstevel@tonic-gate 	 * insert the new entry, since it is not in dnlc yet
5667c478bd9Sstevel@tonic-gate 	 */
5677c478bd9Sstevel@tonic-gate 	nc_inshash(ncp, hp);
5687c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->hash_lock);
5697c478bd9Sstevel@tonic-gate 	ncstats.enters++;
5707c478bd9Sstevel@tonic-gate 	ncs.ncs_enters.value.ui64++;
5717c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
5727c478bd9Sstevel@tonic-gate 	    "dnlc_update_end:(%S) %d", "done", ncstats.enters);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate /*
5767c478bd9Sstevel@tonic-gate  * Look up a name in the directory name cache.
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  * Return a doubly-held vnode if found: one hold so that it may
5797c478bd9Sstevel@tonic-gate  * remain in the cache for other users, the other hold so that
5807c478bd9Sstevel@tonic-gate  * the cache is not re-cycled and the identity of the vnode is
5817c478bd9Sstevel@tonic-gate  * lost before the caller can use the vnode.
5827c478bd9Sstevel@tonic-gate  */
5837c478bd9Sstevel@tonic-gate vnode_t *
5847c478bd9Sstevel@tonic-gate dnlc_lookup(vnode_t *dp, char *name)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
5877c478bd9Sstevel@tonic-gate 	nc_hash_t *hp;
5887c478bd9Sstevel@tonic-gate 	vnode_t *vp;
5897c478bd9Sstevel@tonic-gate 	int hash, depth;
5907c478bd9Sstevel@tonic-gate 	uchar_t namlen;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_NFS, TR_DNLC_LOOKUP_START,
5937c478bd9Sstevel@tonic-gate 	    "dnlc_lookup_start:dp %x name %s", dp, name);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	if (!doingcache) {
5967c478bd9Sstevel@tonic-gate 		TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
5977c478bd9Sstevel@tonic-gate 		    "dnlc_lookup_end:%S %d vp %x name %s",
5987c478bd9Sstevel@tonic-gate 		    "not_caching", 0, NULL, name);
5997c478bd9Sstevel@tonic-gate 		return (NULL);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	DNLCHASH(name, dp, hash, namlen);
6037c478bd9Sstevel@tonic-gate 	depth = 1;
6047c478bd9Sstevel@tonic-gate 	hp = &nc_hash[hash & nc_hashmask];
6057c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->hash_lock);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	for (ncp = hp->hash_next; ncp != (ncache_t *)hp;
6087c478bd9Sstevel@tonic-gate 	    ncp = ncp->hash_next) {
6097c478bd9Sstevel@tonic-gate 		if (ncp->hash == hash &&	/* fast signature check */
6107c478bd9Sstevel@tonic-gate 		    ncp->dp == dp &&
6117c478bd9Sstevel@tonic-gate 		    ncp->namlen == namlen &&
6127c478bd9Sstevel@tonic-gate 		    bcmp(ncp->name, name, namlen) == 0) {
6137c478bd9Sstevel@tonic-gate 			/*
6147c478bd9Sstevel@tonic-gate 			 * Move this entry to the head of its hash chain
6157c478bd9Sstevel@tonic-gate 			 * if it's not already close.
6167c478bd9Sstevel@tonic-gate 			 */
6177c478bd9Sstevel@tonic-gate 			if (depth > NC_MOVETOFRONT) {
6187c478bd9Sstevel@tonic-gate 				ncache_t *next = ncp->hash_next;
6197c478bd9Sstevel@tonic-gate 				ncache_t *prev = ncp->hash_prev;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 				prev->hash_next = next;
6227c478bd9Sstevel@tonic-gate 				next->hash_prev = prev;
6237c478bd9Sstevel@tonic-gate 				ncp->hash_next = next = hp->hash_next;
6247c478bd9Sstevel@tonic-gate 				ncp->hash_prev = (ncache_t *)hp;
6257c478bd9Sstevel@tonic-gate 				next->hash_prev = ncp;
6267c478bd9Sstevel@tonic-gate 				hp->hash_next = ncp;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 				ncstats.move_to_front++;
6297c478bd9Sstevel@tonic-gate 			}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 			/*
6327c478bd9Sstevel@tonic-gate 			 * Put a hold on the vnode now so its identity
6337c478bd9Sstevel@tonic-gate 			 * can't change before the caller has a chance to
6347c478bd9Sstevel@tonic-gate 			 * put a hold on it.
6357c478bd9Sstevel@tonic-gate 			 */
6367c478bd9Sstevel@tonic-gate 			vp = ncp->vp;
637*b5fca8f8Stomee 			VN_HOLD_CALLER(vp); /* VN_HOLD 1 of 2 in this file */
6387c478bd9Sstevel@tonic-gate 			mutex_exit(&hp->hash_lock);
6397c478bd9Sstevel@tonic-gate 			ncstats.hits++;
6407c478bd9Sstevel@tonic-gate 			ncs.ncs_hits.value.ui64++;
6417c478bd9Sstevel@tonic-gate 			if (vp == DNLC_NO_VNODE) {
6427c478bd9Sstevel@tonic-gate 				ncs.ncs_neg_hits.value.ui64++;
6437c478bd9Sstevel@tonic-gate 			}
6447c478bd9Sstevel@tonic-gate 			TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
645*b5fca8f8Stomee 			    "dnlc_lookup_end:%S %d vp %x name %s", "hit",
646*b5fca8f8Stomee 			    ncstats.hits, vp, name);
6477c478bd9Sstevel@tonic-gate 			return (vp);
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 		depth++;
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->hash_lock);
6537c478bd9Sstevel@tonic-gate 	ncstats.misses++;
6547c478bd9Sstevel@tonic-gate 	ncs.ncs_misses.value.ui64++;
6557c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
6567c478bd9Sstevel@tonic-gate 	    "dnlc_lookup_end:%S %d vp %x name %s", "miss", ncstats.misses,
6577c478bd9Sstevel@tonic-gate 	    NULL, name);
6587c478bd9Sstevel@tonic-gate 	return (NULL);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate  * Remove an entry in the directory name cache.
6637c478bd9Sstevel@tonic-gate  */
6647c478bd9Sstevel@tonic-gate void
6657c478bd9Sstevel@tonic-gate dnlc_remove(vnode_t *dp, char *name)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
6687c478bd9Sstevel@tonic-gate 	nc_hash_t *hp;
6697c478bd9Sstevel@tonic-gate 	uchar_t namlen;
6707c478bd9Sstevel@tonic-gate 	int hash;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if (!doingcache)
6737c478bd9Sstevel@tonic-gate 		return;
6747c478bd9Sstevel@tonic-gate 	DNLCHASH(name, dp, hash, namlen);
6757c478bd9Sstevel@tonic-gate 	hp = &nc_hash[hash & nc_hashmask];
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	mutex_enter(&hp->hash_lock);
6787c478bd9Sstevel@tonic-gate 	if (ncp = dnlc_search(dp, name, namlen, hash)) {
6797c478bd9Sstevel@tonic-gate 		/*
6807c478bd9Sstevel@tonic-gate 		 * Free up the entry
6817c478bd9Sstevel@tonic-gate 		 */
6827c478bd9Sstevel@tonic-gate 		nc_rmhash(ncp);
6837c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->hash_lock);
684*b5fca8f8Stomee 		VN_RELE_DNLC(ncp->vp);
685*b5fca8f8Stomee 		VN_RELE_DNLC(ncp->dp);
6867c478bd9Sstevel@tonic-gate 		dnlc_free(ncp);
6877c478bd9Sstevel@tonic-gate 		return;
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 	mutex_exit(&hp->hash_lock);
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * Purge the entire cache.
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate void
6967c478bd9Sstevel@tonic-gate dnlc_purge()
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	nc_hash_t *nch;
6997c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
7007c478bd9Sstevel@tonic-gate 	int index;
7017c478bd9Sstevel@tonic-gate 	int i;
7027c478bd9Sstevel@tonic-gate 	vnode_t *nc_rele[DNLC_MAX_RELE];
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if (!doingcache)
7057c478bd9Sstevel@tonic-gate 		return;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	ncstats.purges++;
7087c478bd9Sstevel@tonic-gate 	ncs.ncs_purge_all.value.ui64++;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
7117c478bd9Sstevel@tonic-gate 		index = 0;
7127c478bd9Sstevel@tonic-gate 		mutex_enter(&nch->hash_lock);
7137c478bd9Sstevel@tonic-gate 		ncp = nch->hash_next;
7147c478bd9Sstevel@tonic-gate 		while (ncp != (ncache_t *)nch) {
7157c478bd9Sstevel@tonic-gate 			ncache_t *np;
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 			np = ncp->hash_next;
7187c478bd9Sstevel@tonic-gate 			nc_rele[index++] = ncp->vp;
7197c478bd9Sstevel@tonic-gate 			nc_rele[index++] = ncp->dp;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 			nc_rmhash(ncp);
7227c478bd9Sstevel@tonic-gate 			dnlc_free(ncp);
7237c478bd9Sstevel@tonic-gate 			ncp = np;
7247c478bd9Sstevel@tonic-gate 			ncs.ncs_purge_total.value.ui64++;
7257c478bd9Sstevel@tonic-gate 			if (index == DNLC_MAX_RELE)
7267c478bd9Sstevel@tonic-gate 				break;
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		mutex_exit(&nch->hash_lock);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		/* Release holds on all the vnodes now that we have no locks */
7317c478bd9Sstevel@tonic-gate 		for (i = 0; i < index; i++) {
732*b5fca8f8Stomee 			VN_RELE_DNLC(nc_rele[i]);
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 		if (ncp != (ncache_t *)nch) {
7357c478bd9Sstevel@tonic-gate 			nch--; /* Do current hash chain again */
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate /*
741*b5fca8f8Stomee  * Purge any cache entries referencing a vnode. Exit as soon as the dnlc
742*b5fca8f8Stomee  * reference count goes to zero (the caller still holds a reference).
7437c478bd9Sstevel@tonic-gate  */
7447c478bd9Sstevel@tonic-gate void
7457c478bd9Sstevel@tonic-gate dnlc_purge_vp(vnode_t *vp)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	nc_hash_t *nch;
7487c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
7497c478bd9Sstevel@tonic-gate 	int index;
7507c478bd9Sstevel@tonic-gate 	vnode_t *nc_rele[DNLC_MAX_RELE];
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
753*b5fca8f8Stomee 	if (vp->v_count_dnlc == 0) {
7547c478bd9Sstevel@tonic-gate 		return;
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	if (!doingcache)
7587c478bd9Sstevel@tonic-gate 		return;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	ncstats.purges++;
7617c478bd9Sstevel@tonic-gate 	ncs.ncs_purge_vp.value.ui64++;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
7647c478bd9Sstevel@tonic-gate 		index = 0;
7657c478bd9Sstevel@tonic-gate 		mutex_enter(&nch->hash_lock);
7667c478bd9Sstevel@tonic-gate 		ncp = nch->hash_next;
7677c478bd9Sstevel@tonic-gate 		while (ncp != (ncache_t *)nch) {
7687c478bd9Sstevel@tonic-gate 			ncache_t *np;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 			np = ncp->hash_next;
7717c478bd9Sstevel@tonic-gate 			if (ncp->dp == vp || ncp->vp == vp) {
7727c478bd9Sstevel@tonic-gate 				nc_rele[index++] = ncp->vp;
7737c478bd9Sstevel@tonic-gate 				nc_rele[index++] = ncp->dp;
7747c478bd9Sstevel@tonic-gate 				nc_rmhash(ncp);
7757c478bd9Sstevel@tonic-gate 				dnlc_free(ncp);
7767c478bd9Sstevel@tonic-gate 				ncs.ncs_purge_total.value.ui64++;
7777c478bd9Sstevel@tonic-gate 				if (index == DNLC_MAX_RELE) {
7787c478bd9Sstevel@tonic-gate 					ncp = np;
7797c478bd9Sstevel@tonic-gate 					break;
7807c478bd9Sstevel@tonic-gate 				}
7817c478bd9Sstevel@tonic-gate 			}
7827c478bd9Sstevel@tonic-gate 			ncp = np;
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 		mutex_exit(&nch->hash_lock);
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		/* Release holds on all the vnodes now that we have no locks */
787dbd19a09Sah89892 		while (index) {
788*b5fca8f8Stomee 			VN_RELE_DNLC(nc_rele[--index]);
7897c478bd9Sstevel@tonic-gate 		}
7907c478bd9Sstevel@tonic-gate 
791*b5fca8f8Stomee 		if (vp->v_count_dnlc == 0) {
792*b5fca8f8Stomee 			return;
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		if (ncp != (ncache_t *)nch) {
7967c478bd9Sstevel@tonic-gate 			nch--; /* Do current hash chain again */
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate /*
8027c478bd9Sstevel@tonic-gate  * Purge cache entries referencing a vfsp.  Caller supplies a count
8037c478bd9Sstevel@tonic-gate  * of entries to purge; up to that many will be freed.  A count of
8047c478bd9Sstevel@tonic-gate  * zero indicates that all such entries should be purged.  Returns
8057c478bd9Sstevel@tonic-gate  * the number of entries that were purged.
8067c478bd9Sstevel@tonic-gate  */
8077c478bd9Sstevel@tonic-gate int
8087c478bd9Sstevel@tonic-gate dnlc_purge_vfsp(vfs_t *vfsp, int count)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	nc_hash_t *nch;
8117c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
8127c478bd9Sstevel@tonic-gate 	int n = 0;
8137c478bd9Sstevel@tonic-gate 	int index;
8147c478bd9Sstevel@tonic-gate 	int i;
8157c478bd9Sstevel@tonic-gate 	vnode_t *nc_rele[DNLC_MAX_RELE];
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (!doingcache)
8187c478bd9Sstevel@tonic-gate 		return (0);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	ncstats.purges++;
8217c478bd9Sstevel@tonic-gate 	ncs.ncs_purge_vfs.value.ui64++;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
8247c478bd9Sstevel@tonic-gate 		index = 0;
8257c478bd9Sstevel@tonic-gate 		mutex_enter(&nch->hash_lock);
8267c478bd9Sstevel@tonic-gate 		ncp = nch->hash_next;
8277c478bd9Sstevel@tonic-gate 		while (ncp != (ncache_t *)nch) {
8287c478bd9Sstevel@tonic-gate 			ncache_t *np;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 			np = ncp->hash_next;
8317c478bd9Sstevel@tonic-gate 			ASSERT(ncp->dp != NULL);
8327c478bd9Sstevel@tonic-gate 			ASSERT(ncp->vp != NULL);
8337c478bd9Sstevel@tonic-gate 			if ((ncp->dp->v_vfsp == vfsp) ||
8347c478bd9Sstevel@tonic-gate 			    (ncp->vp->v_vfsp == vfsp)) {
8357c478bd9Sstevel@tonic-gate 				n++;
8367c478bd9Sstevel@tonic-gate 				nc_rele[index++] = ncp->vp;
8377c478bd9Sstevel@tonic-gate 				nc_rele[index++] = ncp->dp;
8387c478bd9Sstevel@tonic-gate 				nc_rmhash(ncp);
8397c478bd9Sstevel@tonic-gate 				dnlc_free(ncp);
8407c478bd9Sstevel@tonic-gate 				ncs.ncs_purge_total.value.ui64++;
8417c478bd9Sstevel@tonic-gate 				if (index == DNLC_MAX_RELE) {
8427c478bd9Sstevel@tonic-gate 					ncp = np;
8437c478bd9Sstevel@tonic-gate 					break;
8447c478bd9Sstevel@tonic-gate 				}
8457c478bd9Sstevel@tonic-gate 				if (count != 0 && n >= count) {
8467c478bd9Sstevel@tonic-gate 					break;
8477c478bd9Sstevel@tonic-gate 				}
8487c478bd9Sstevel@tonic-gate 			}
8497c478bd9Sstevel@tonic-gate 			ncp = np;
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 		mutex_exit(&nch->hash_lock);
8527c478bd9Sstevel@tonic-gate 		/* Release holds on all the vnodes now that we have no locks */
8537c478bd9Sstevel@tonic-gate 		for (i = 0; i < index; i++) {
854*b5fca8f8Stomee 			VN_RELE_DNLC(nc_rele[i]);
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 		if (count != 0 && n >= count) {
8577c478bd9Sstevel@tonic-gate 			return (n);
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 		if (ncp != (ncache_t *)nch) {
8607c478bd9Sstevel@tonic-gate 			nch--; /* Do current hash chain again */
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 	return (n);
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate /*
8677c478bd9Sstevel@tonic-gate  * Purge 1 entry from the dnlc that is part of the filesystem(s)
8687c478bd9Sstevel@tonic-gate  * represented by 'vop'. The purpose of this routine is to allow
8697c478bd9Sstevel@tonic-gate  * users of the dnlc to free a vnode that is being held by the dnlc.
8707c478bd9Sstevel@tonic-gate  *
8717c478bd9Sstevel@tonic-gate  * If we find a vnode that we release which will result in
8727c478bd9Sstevel@tonic-gate  * freeing the underlying vnode (count was 1), return 1, 0
8737c478bd9Sstevel@tonic-gate  * if no appropriate vnodes found.
8747c478bd9Sstevel@tonic-gate  *
8757c478bd9Sstevel@tonic-gate  * Note, vop is not the 'right' identifier for a filesystem.
8767c478bd9Sstevel@tonic-gate  */
8777c478bd9Sstevel@tonic-gate int
8787c478bd9Sstevel@tonic-gate dnlc_fs_purge1(vnodeops_t *vop)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	nc_hash_t *end;
8817c478bd9Sstevel@tonic-gate 	nc_hash_t *hp;
8827c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
8837c478bd9Sstevel@tonic-gate 	vnode_t *vp;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	if (!doingcache)
8867c478bd9Sstevel@tonic-gate 		return (0);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	ncs.ncs_purge_fs1.value.ui64++;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	/*
8917c478bd9Sstevel@tonic-gate 	 * Scan the dnlc entries looking for a likely candidate.
8927c478bd9Sstevel@tonic-gate 	 */
8937c478bd9Sstevel@tonic-gate 	hp = end = dnlc_purge_fs1_rotor;
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate 	do {
8967c478bd9Sstevel@tonic-gate 		if (++hp == &nc_hash[nc_hashsz])
8977c478bd9Sstevel@tonic-gate 			hp = nc_hash;
8987c478bd9Sstevel@tonic-gate 		dnlc_purge_fs1_rotor = hp;
8997c478bd9Sstevel@tonic-gate 		if (hp->hash_next == (ncache_t *)hp)
9007c478bd9Sstevel@tonic-gate 			continue;
9017c478bd9Sstevel@tonic-gate 		mutex_enter(&hp->hash_lock);
9027c478bd9Sstevel@tonic-gate 		for (ncp = hp->hash_prev;
9037c478bd9Sstevel@tonic-gate 		    ncp != (ncache_t *)hp;
9047c478bd9Sstevel@tonic-gate 		    ncp = ncp->hash_prev) {
9057c478bd9Sstevel@tonic-gate 			vp = ncp->vp;
9067c478bd9Sstevel@tonic-gate 			if (!vn_has_cached_data(vp) && (vp->v_count == 1) &&
9077c478bd9Sstevel@tonic-gate 			    vn_matchops(vp, vop))
9087c478bd9Sstevel@tonic-gate 				break;
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 		if (ncp != (ncache_t *)hp) {
9117c478bd9Sstevel@tonic-gate 			nc_rmhash(ncp);
9127c478bd9Sstevel@tonic-gate 			mutex_exit(&hp->hash_lock);
913*b5fca8f8Stomee 			VN_RELE_DNLC(ncp->dp);
914*b5fca8f8Stomee 			VN_RELE_DNLC(vp)
9157c478bd9Sstevel@tonic-gate 			dnlc_free(ncp);
9167c478bd9Sstevel@tonic-gate 			ncs.ncs_purge_total.value.ui64++;
9177c478bd9Sstevel@tonic-gate 			return (1);
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->hash_lock);
9207c478bd9Sstevel@tonic-gate 	} while (hp != end);
9217c478bd9Sstevel@tonic-gate 	return (0);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate /*
9257c478bd9Sstevel@tonic-gate  * Perform a reverse lookup in the DNLC.  This will find the first occurrence of
9267c478bd9Sstevel@tonic-gate  * the vnode.  If successful, it will return the vnode of the parent, and the
9277c478bd9Sstevel@tonic-gate  * name of the entry in the given buffer.  If it cannot be found, or the buffer
9287c478bd9Sstevel@tonic-gate  * is too small, then it will return NULL.  Note that this is a highly
9297c478bd9Sstevel@tonic-gate  * inefficient function, since the DNLC is constructed solely for forward
9307c478bd9Sstevel@tonic-gate  * lookups.
9317c478bd9Sstevel@tonic-gate  */
9327c478bd9Sstevel@tonic-gate vnode_t *
9337c478bd9Sstevel@tonic-gate dnlc_reverse_lookup(vnode_t *vp, char *buf, size_t buflen)
9347c478bd9Sstevel@tonic-gate {
9357c478bd9Sstevel@tonic-gate 	nc_hash_t *nch;
9367c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
9377c478bd9Sstevel@tonic-gate 	vnode_t *pvp;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	if (!doingcache)
9407c478bd9Sstevel@tonic-gate 		return (NULL);
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
9437c478bd9Sstevel@tonic-gate 		mutex_enter(&nch->hash_lock);
9447c478bd9Sstevel@tonic-gate 		ncp = nch->hash_next;
9457c478bd9Sstevel@tonic-gate 		while (ncp != (ncache_t *)nch) {
9467c478bd9Sstevel@tonic-gate 			/*
9477c478bd9Sstevel@tonic-gate 			 * We ignore '..' entries since it can create
9487c478bd9Sstevel@tonic-gate 			 * confusion and infinite loops.
9497c478bd9Sstevel@tonic-gate 			 */
9507c478bd9Sstevel@tonic-gate 			if (ncp->vp == vp && !(ncp->namlen == 2 &&
9517c478bd9Sstevel@tonic-gate 			    0 == bcmp(ncp->name, "..", 2)) &&
9527c478bd9Sstevel@tonic-gate 			    ncp->namlen < buflen) {
9537c478bd9Sstevel@tonic-gate 				bcopy(ncp->name, buf, ncp->namlen);
9547c478bd9Sstevel@tonic-gate 				buf[ncp->namlen] = '\0';
9557c478bd9Sstevel@tonic-gate 				pvp = ncp->dp;
956*b5fca8f8Stomee 				/* VN_HOLD 2 of 2 in this file */
957*b5fca8f8Stomee 				VN_HOLD_CALLER(pvp);
9587c478bd9Sstevel@tonic-gate 				mutex_exit(&nch->hash_lock);
9597c478bd9Sstevel@tonic-gate 				return (pvp);
9607c478bd9Sstevel@tonic-gate 			}
9617c478bd9Sstevel@tonic-gate 			ncp = ncp->hash_next;
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 		mutex_exit(&nch->hash_lock);
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	return (NULL);
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate /*
9697c478bd9Sstevel@tonic-gate  * Utility routine to search for a cache entry. Return the
9707c478bd9Sstevel@tonic-gate  * ncache entry if found, NULL otherwise.
9717c478bd9Sstevel@tonic-gate  */
9727c478bd9Sstevel@tonic-gate static ncache_t *
9737c478bd9Sstevel@tonic-gate dnlc_search(vnode_t *dp, char *name, uchar_t namlen, int hash)
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate 	nc_hash_t *hp;
9767c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	hp = &nc_hash[hash & nc_hashmask];
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	for (ncp = hp->hash_next; ncp != (ncache_t *)hp; ncp = ncp->hash_next) {
9817c478bd9Sstevel@tonic-gate 		if (ncp->hash == hash &&
9827c478bd9Sstevel@tonic-gate 		    ncp->dp == dp &&
9837c478bd9Sstevel@tonic-gate 		    ncp->namlen == namlen &&
9847c478bd9Sstevel@tonic-gate 		    bcmp(ncp->name, name, namlen) == 0)
9857c478bd9Sstevel@tonic-gate 			return (ncp);
9867c478bd9Sstevel@tonic-gate 	}
9877c478bd9Sstevel@tonic-gate 	return (NULL);
9887c478bd9Sstevel@tonic-gate }
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate #if ((1 << NBBY) - 1) < (MAXNAMELEN - 1)
9917c478bd9Sstevel@tonic-gate #error ncache_t name length representation is too small
9927c478bd9Sstevel@tonic-gate #endif
9937c478bd9Sstevel@tonic-gate 
994104e2ed7Sperrin void
995104e2ed7Sperrin dnlc_reduce_cache(void *reduce_percent)
996104e2ed7Sperrin {
997104e2ed7Sperrin 	if (dnlc_reduce_idle && (dnlc_nentries >= ncsize || reduce_percent)) {
998104e2ed7Sperrin 		dnlc_reduce_idle = 0;
999104e2ed7Sperrin 		if ((taskq_dispatch(system_taskq, do_dnlc_reduce_cache,
1000104e2ed7Sperrin 		    reduce_percent, TQ_NOSLEEP)) == NULL)
1001104e2ed7Sperrin 			dnlc_reduce_idle = 1;
1002104e2ed7Sperrin 	}
1003104e2ed7Sperrin }
1004104e2ed7Sperrin 
10057c478bd9Sstevel@tonic-gate /*
10067c478bd9Sstevel@tonic-gate  * Get a new name cache entry.
10077c478bd9Sstevel@tonic-gate  * If the dnlc_reduce_cache() taskq isn't keeping up with demand, or memory
10087c478bd9Sstevel@tonic-gate  * is short then just return NULL. If we're over ncsize then kick off a
10097c478bd9Sstevel@tonic-gate  * thread to free some in use entries down to dnlc_nentries_low_water.
10107c478bd9Sstevel@tonic-gate  * Caller must initialise all fields except namlen.
10117c478bd9Sstevel@tonic-gate  * Component names are defined to be less than MAXNAMELEN
10127c478bd9Sstevel@tonic-gate  * which includes a null.
10137c478bd9Sstevel@tonic-gate  */
10147c478bd9Sstevel@tonic-gate static ncache_t *
10157c478bd9Sstevel@tonic-gate dnlc_get(uchar_t namlen)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (dnlc_nentries > dnlc_max_nentries) {
10207c478bd9Sstevel@tonic-gate 		dnlc_max_nentries_cnt++; /* keep a statistic */
10217c478bd9Sstevel@tonic-gate 		return (NULL);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	ncp = kmem_alloc(sizeof (ncache_t) + namlen, KM_NOSLEEP);
10247c478bd9Sstevel@tonic-gate 	if (ncp == NULL) {
10257c478bd9Sstevel@tonic-gate 		return (NULL);
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate 	ncp->namlen = namlen;
10287c478bd9Sstevel@tonic-gate 	atomic_add_32(&dnlc_nentries, 1);
1029104e2ed7Sperrin 	dnlc_reduce_cache(NULL);
10307c478bd9Sstevel@tonic-gate 	return (ncp);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*
10347c478bd9Sstevel@tonic-gate  * Taskq routine to free up name cache entries to reduce the
1035033f9833Sek110237  * cache size to the low water mark if "reduce_percent" is not provided.
1036033f9833Sek110237  * If "reduce_percent" is provided, reduce cache size by
1037033f9833Sek110237  * (ncsize_onepercent * reduce_percent).
10387c478bd9Sstevel@tonic-gate  */
10397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1040104e2ed7Sperrin static void
1041104e2ed7Sperrin do_dnlc_reduce_cache(void *reduce_percent)
10427c478bd9Sstevel@tonic-gate {
1043104e2ed7Sperrin 	nc_hash_t *hp = dnlc_free_rotor, *start_hp = hp;
10447c478bd9Sstevel@tonic-gate 	vnode_t *vp;
10457c478bd9Sstevel@tonic-gate 	ncache_t *ncp;
10467c478bd9Sstevel@tonic-gate 	int cnt;
1047033f9833Sek110237 	uint_t low_water = dnlc_nentries_low_water;
1048033f9833Sek110237 
1049033f9833Sek110237 	if (reduce_percent) {
1050033f9833Sek110237 		uint_t reduce_cnt;
1051033f9833Sek110237 
1052104e2ed7Sperrin 		/*
1053104e2ed7Sperrin 		 * Never try to reduce the current number
1054104e2ed7Sperrin 		 * of cache entries below 3% of ncsize.
1055104e2ed7Sperrin 		 */
1056104e2ed7Sperrin 		if (dnlc_nentries <= ncsize_min_percent) {
1057104e2ed7Sperrin 			dnlc_reduce_idle = 1;
1058104e2ed7Sperrin 			return;
1059104e2ed7Sperrin 		}
1060cee972f8Sek110237 		reduce_cnt = ncsize_onepercent *
1061cee972f8Sek110237 		    (uint_t)(uintptr_t)reduce_percent;
1062104e2ed7Sperrin 
1063104e2ed7Sperrin 		if (reduce_cnt > dnlc_nentries ||
1064104e2ed7Sperrin 		    dnlc_nentries - reduce_cnt < ncsize_min_percent)
1065104e2ed7Sperrin 			low_water = ncsize_min_percent;
1066033f9833Sek110237 		else
1067033f9833Sek110237 			low_water = dnlc_nentries - reduce_cnt;
1068033f9833Sek110237 	}
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	do {
10717c478bd9Sstevel@tonic-gate 		/*
1072104e2ed7Sperrin 		 * Find the first non empty hash queue without locking.
1073104e2ed7Sperrin 		 * Only look at each hash queue once to avoid an infinite loop.
10747c478bd9Sstevel@tonic-gate 		 */
10757c478bd9Sstevel@tonic-gate 		do {
1076104e2ed7Sperrin 			if (++hp == &nc_hash[nc_hashsz])
10777c478bd9Sstevel@tonic-gate 				hp = nc_hash;
1078104e2ed7Sperrin 		} while (hp->hash_next == (ncache_t *)hp && hp != start_hp);
1079104e2ed7Sperrin 
1080104e2ed7Sperrin 		/* return if all hash queues are empty. */
1081104e2ed7Sperrin 		if (hp->hash_next == (ncache_t *)hp) {
10827c478bd9Sstevel@tonic-gate 			dnlc_reduce_idle = 1;
10837c478bd9Sstevel@tonic-gate 			return;
10847c478bd9Sstevel@tonic-gate 		}
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 		mutex_enter(&hp->hash_lock);
10877c478bd9Sstevel@tonic-gate 		for (cnt = 0, ncp = hp->hash_prev; ncp != (ncache_t *)hp;
10887c478bd9Sstevel@tonic-gate 		    ncp = ncp->hash_prev, cnt++) {
10897c478bd9Sstevel@tonic-gate 			vp = ncp->vp;
10907c478bd9Sstevel@tonic-gate 			/*
10917c478bd9Sstevel@tonic-gate 			 * A name cache entry with a reference count
10927c478bd9Sstevel@tonic-gate 			 * of one is only referenced by the dnlc.
10937c478bd9Sstevel@tonic-gate 			 * Also negative cache entries are purged first.
10947c478bd9Sstevel@tonic-gate 			 */
10957c478bd9Sstevel@tonic-gate 			if (!vn_has_cached_data(vp) &&
10967c478bd9Sstevel@tonic-gate 			    ((vp->v_count == 1) || (vp == DNLC_NO_VNODE))) {
10977c478bd9Sstevel@tonic-gate 				ncs.ncs_pick_heur.value.ui64++;
10987c478bd9Sstevel@tonic-gate 				goto found;
10997c478bd9Sstevel@tonic-gate 			}
11007c478bd9Sstevel@tonic-gate 			/*
11017c478bd9Sstevel@tonic-gate 			 * Remove from the end of the chain if the
11027c478bd9Sstevel@tonic-gate 			 * chain is too long
11037c478bd9Sstevel@tonic-gate 			 */
11047c478bd9Sstevel@tonic-gate 			if (cnt > dnlc_long_chain) {
11057c478bd9Sstevel@tonic-gate 				ncp = hp->hash_prev;
11067c478bd9Sstevel@tonic-gate 				ncs.ncs_pick_last.value.ui64++;
11077c478bd9Sstevel@tonic-gate 				vp = ncp->vp;
11087c478bd9Sstevel@tonic-gate 				goto found;
11097c478bd9Sstevel@tonic-gate 			}
11107c478bd9Sstevel@tonic-gate 		}
11117c478bd9Sstevel@tonic-gate 		/* check for race and continue */
11127c478bd9Sstevel@tonic-gate 		if (hp->hash_next == (ncache_t *)hp) {
11137c478bd9Sstevel@tonic-gate 			mutex_exit(&hp->hash_lock);
11147c478bd9Sstevel@tonic-gate 			continue;
11157c478bd9Sstevel@tonic-gate 		}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 		ncp = hp->hash_prev; /* pick the last one in the hash queue */
11187c478bd9Sstevel@tonic-gate 		ncs.ncs_pick_last.value.ui64++;
11197c478bd9Sstevel@tonic-gate 		vp = ncp->vp;
11207c478bd9Sstevel@tonic-gate found:
11217c478bd9Sstevel@tonic-gate 		/*
11227c478bd9Sstevel@tonic-gate 		 * Remove from hash chain.
11237c478bd9Sstevel@tonic-gate 		 */
11247c478bd9Sstevel@tonic-gate 		nc_rmhash(ncp);
11257c478bd9Sstevel@tonic-gate 		mutex_exit(&hp->hash_lock);
1126*b5fca8f8Stomee 		VN_RELE_DNLC(vp);
1127*b5fca8f8Stomee 		VN_RELE_DNLC(ncp->dp);
11287c478bd9Sstevel@tonic-gate 		dnlc_free(ncp);
1129033f9833Sek110237 	} while (dnlc_nentries > low_water);
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	dnlc_free_rotor = hp;
11327c478bd9Sstevel@tonic-gate 	dnlc_reduce_idle = 1;
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate /*
11367c478bd9Sstevel@tonic-gate  * Directory caching routines
11377c478bd9Sstevel@tonic-gate  * ==========================
11387c478bd9Sstevel@tonic-gate  *
11397c478bd9Sstevel@tonic-gate  * See dnlc.h for details of the interfaces below.
11407c478bd9Sstevel@tonic-gate  */
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate /*
11437c478bd9Sstevel@tonic-gate  * Lookup up an entry in a complete or partial directory cache.
11447c478bd9Sstevel@tonic-gate  */
11457c478bd9Sstevel@tonic-gate dcret_t
11467c478bd9Sstevel@tonic-gate dnlc_dir_lookup(dcanchor_t *dcap, char *name, uint64_t *handle)
11477c478bd9Sstevel@tonic-gate {
11487c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
11497c478bd9Sstevel@tonic-gate 	dcentry_t *dep;
11507c478bd9Sstevel@tonic-gate 	int hash;
11517c478bd9Sstevel@tonic-gate 	int ret;
11527c478bd9Sstevel@tonic-gate 	uchar_t namlen;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	/*
11557c478bd9Sstevel@tonic-gate 	 * can test without lock as we are only a cache
11567c478bd9Sstevel@tonic-gate 	 */
11577c478bd9Sstevel@tonic-gate 	if (!VALID_DIR_CACHE(dcap->dca_dircache)) {
11587c478bd9Sstevel@tonic-gate 		ncs.ncs_dir_misses.value.ui64++;
11597c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	if (!dnlc_dir_enable) {
11637c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
11677c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
11687c478bd9Sstevel@tonic-gate 	if (VALID_DIR_CACHE(dcp)) {
11697c478bd9Sstevel@tonic-gate 		dcp->dc_actime = lbolt64;
11707c478bd9Sstevel@tonic-gate 		DNLC_DIR_HASH(name, hash, namlen);
11717c478bd9Sstevel@tonic-gate 		dep = dcp->dc_namehash[hash & dcp->dc_nhash_mask];
11727c478bd9Sstevel@tonic-gate 		while (dep != NULL) {
11737c478bd9Sstevel@tonic-gate 			if ((dep->de_hash == hash) &&
11747c478bd9Sstevel@tonic-gate 			    (namlen == dep->de_namelen) &&
11757c478bd9Sstevel@tonic-gate 			    bcmp(dep->de_name, name, namlen) == 0) {
11767c478bd9Sstevel@tonic-gate 				*handle = dep->de_handle;
11777c478bd9Sstevel@tonic-gate 				mutex_exit(&dcap->dca_lock);
11787c478bd9Sstevel@tonic-gate 				ncs.ncs_dir_hits.value.ui64++;
11797c478bd9Sstevel@tonic-gate 				return (DFOUND);
11807c478bd9Sstevel@tonic-gate 			}
11817c478bd9Sstevel@tonic-gate 			dep = dep->de_next;
11827c478bd9Sstevel@tonic-gate 		}
11837c478bd9Sstevel@tonic-gate 		if (dcp->dc_complete) {
11847c478bd9Sstevel@tonic-gate 			ret = DNOENT;
11857c478bd9Sstevel@tonic-gate 		} else {
11867c478bd9Sstevel@tonic-gate 			ret = DNOCACHE;
11877c478bd9Sstevel@tonic-gate 		}
11887c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
11897c478bd9Sstevel@tonic-gate 		return (ret);
11907c478bd9Sstevel@tonic-gate 	} else {
11917c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
11927c478bd9Sstevel@tonic-gate 		ncs.ncs_dir_misses.value.ui64++;
11937c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate /*
11987c478bd9Sstevel@tonic-gate  * Start a new directory cache. An estimate of the number of
11997c478bd9Sstevel@tonic-gate  * entries is provided to as a quick check to ensure the directory
12007c478bd9Sstevel@tonic-gate  * is cacheable.
12017c478bd9Sstevel@tonic-gate  */
12027c478bd9Sstevel@tonic-gate dcret_t
12037c478bd9Sstevel@tonic-gate dnlc_dir_start(dcanchor_t *dcap, uint_t num_entries)
12047c478bd9Sstevel@tonic-gate {
12057c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	if (!dnlc_dir_enable ||
12087c478bd9Sstevel@tonic-gate 	    (num_entries < dnlc_dir_min_size)) {
12097c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
12107c478bd9Sstevel@tonic-gate 	}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	if (num_entries > dnlc_dir_max_size) {
12137c478bd9Sstevel@tonic-gate 		return (DTOOBIG);
12147c478bd9Sstevel@tonic-gate 	}
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	mutex_enter(&dc_head.dch_lock);
12177c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	if (dcap->dca_dircache == DC_RET_LOW_MEM) {
12207c478bd9Sstevel@tonic-gate 		dcap->dca_dircache = NULL;
12217c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
12227c478bd9Sstevel@tonic-gate 		mutex_exit(&dc_head.dch_lock);
12237c478bd9Sstevel@tonic-gate 		return (DNOMEM);
12247c478bd9Sstevel@tonic-gate 	}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	/*
12277c478bd9Sstevel@tonic-gate 	 * Check if there's currently a cache.
12287c478bd9Sstevel@tonic-gate 	 * This probably only occurs on a race.
12297c478bd9Sstevel@tonic-gate 	 */
12307c478bd9Sstevel@tonic-gate 	if (dcap->dca_dircache != NULL) {
12317c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
12327c478bd9Sstevel@tonic-gate 		mutex_exit(&dc_head.dch_lock);
12337c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/*
12377c478bd9Sstevel@tonic-gate 	 * Allocate the dircache struct, entry and free space hash tables.
12387c478bd9Sstevel@tonic-gate 	 * These tables are initially just one entry but dynamically resize
12397c478bd9Sstevel@tonic-gate 	 * when entries and free space are added or removed.
12407c478bd9Sstevel@tonic-gate 	 */
12417c478bd9Sstevel@tonic-gate 	if ((dcp = kmem_zalloc(sizeof (dircache_t), KM_NOSLEEP)) == NULL) {
12427c478bd9Sstevel@tonic-gate 		goto error;
12437c478bd9Sstevel@tonic-gate 	}
12447c478bd9Sstevel@tonic-gate 	if ((dcp->dc_namehash = kmem_zalloc(sizeof (dcentry_t *),
12457c478bd9Sstevel@tonic-gate 	    KM_NOSLEEP)) == NULL) {
12467c478bd9Sstevel@tonic-gate 		goto error;
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 	if ((dcp->dc_freehash = kmem_zalloc(sizeof (dcfree_t *),
12497c478bd9Sstevel@tonic-gate 	    KM_NOSLEEP)) == NULL) {
12507c478bd9Sstevel@tonic-gate 		goto error;
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	dcp->dc_anchor = dcap; /* set back pointer to anchor */
12547c478bd9Sstevel@tonic-gate 	dcap->dca_dircache = dcp;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	/* add into head of global chain */
12577c478bd9Sstevel@tonic-gate 	dcp->dc_next = dc_head.dch_next;
12587c478bd9Sstevel@tonic-gate 	dcp->dc_prev = (dircache_t *)&dc_head;
12597c478bd9Sstevel@tonic-gate 	dcp->dc_next->dc_prev = dcp;
12607c478bd9Sstevel@tonic-gate 	dc_head.dch_next = dcp;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	mutex_exit(&dcap->dca_lock);
12637c478bd9Sstevel@tonic-gate 	mutex_exit(&dc_head.dch_lock);
12647c478bd9Sstevel@tonic-gate 	ncs.ncs_cur_dirs.value.ui64++;
12657c478bd9Sstevel@tonic-gate 	ncs.ncs_dirs_cached.value.ui64++;
12667c478bd9Sstevel@tonic-gate 	return (DOK);
12677c478bd9Sstevel@tonic-gate error:
12687c478bd9Sstevel@tonic-gate 	if (dcp != NULL) {
12697c478bd9Sstevel@tonic-gate 		if (dcp->dc_namehash) {
12707c478bd9Sstevel@tonic-gate 			kmem_free(dcp->dc_namehash, sizeof (dcentry_t *));
12717c478bd9Sstevel@tonic-gate 		}
12727c478bd9Sstevel@tonic-gate 		kmem_free(dcp, sizeof (dircache_t));
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 	/*
12757c478bd9Sstevel@tonic-gate 	 * Must also kmem_free dcp->dc_freehash if more error cases are added
12767c478bd9Sstevel@tonic-gate 	 */
12777c478bd9Sstevel@tonic-gate 	mutex_exit(&dcap->dca_lock);
12787c478bd9Sstevel@tonic-gate 	mutex_exit(&dc_head.dch_lock);
12797c478bd9Sstevel@tonic-gate 	ncs.ncs_dir_start_nm.value.ui64++;
12807c478bd9Sstevel@tonic-gate 	return (DNOCACHE);
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate /*
12847c478bd9Sstevel@tonic-gate  * Add a directopry entry to a partial or complete directory cache.
12857c478bd9Sstevel@tonic-gate  */
12867c478bd9Sstevel@tonic-gate dcret_t
12877c478bd9Sstevel@tonic-gate dnlc_dir_add_entry(dcanchor_t *dcap, char *name, uint64_t handle)
12887c478bd9Sstevel@tonic-gate {
12897c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
12907c478bd9Sstevel@tonic-gate 	dcentry_t **hp, *dep;
12917c478bd9Sstevel@tonic-gate 	int hash;
12927c478bd9Sstevel@tonic-gate 	uint_t capacity;
12937c478bd9Sstevel@tonic-gate 	uchar_t namlen;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	/*
12967c478bd9Sstevel@tonic-gate 	 * Allocate the dcentry struct, including the variable
12977c478bd9Sstevel@tonic-gate 	 * size name. Note, the null terminator is not copied.
12987c478bd9Sstevel@tonic-gate 	 *
12997c478bd9Sstevel@tonic-gate 	 * We do this outside the lock to avoid possible deadlock if
13007c478bd9Sstevel@tonic-gate 	 * dnlc_dir_reclaim() is called as a result of memory shortage.
13017c478bd9Sstevel@tonic-gate 	 */
13027c478bd9Sstevel@tonic-gate 	DNLC_DIR_HASH(name, hash, namlen);
13037c478bd9Sstevel@tonic-gate 	dep = kmem_alloc(sizeof (dcentry_t) - 1 + namlen, KM_NOSLEEP);
13047c478bd9Sstevel@tonic-gate 	if (dep == NULL) {
13057c478bd9Sstevel@tonic-gate #ifdef DEBUG
13067c478bd9Sstevel@tonic-gate 		/*
13077c478bd9Sstevel@tonic-gate 		 * The kmem allocator generates random failures for
13087c478bd9Sstevel@tonic-gate 		 * KM_NOSLEEP calls (see KMEM_RANDOM_ALLOCATION_FAILURE)
13097c478bd9Sstevel@tonic-gate 		 * So try again before we blow away a perfectly good cache.
13107c478bd9Sstevel@tonic-gate 		 * This is done not to cover an error but purely for
13117c478bd9Sstevel@tonic-gate 		 * performance running a debug kernel.
13127c478bd9Sstevel@tonic-gate 		 * This random error only occurs in debug mode.
13137c478bd9Sstevel@tonic-gate 		 */
13147c478bd9Sstevel@tonic-gate 		dep = kmem_alloc(sizeof (dcentry_t) - 1 + namlen, KM_NOSLEEP);
13157c478bd9Sstevel@tonic-gate 		if (dep != NULL)
13167c478bd9Sstevel@tonic-gate 			goto ok;
13177c478bd9Sstevel@tonic-gate #endif
13187c478bd9Sstevel@tonic-gate 		ncs.ncs_dir_add_nm.value.ui64++;
13197c478bd9Sstevel@tonic-gate 		/*
13207c478bd9Sstevel@tonic-gate 		 * Free a directory cache. This may be the one we are
13217c478bd9Sstevel@tonic-gate 		 * called with.
13227c478bd9Sstevel@tonic-gate 		 */
13237c478bd9Sstevel@tonic-gate 		dnlc_dir_reclaim(NULL);
13247c478bd9Sstevel@tonic-gate 		dep = kmem_alloc(sizeof (dcentry_t) - 1 + namlen, KM_NOSLEEP);
13257c478bd9Sstevel@tonic-gate 		if (dep == NULL) {
13267c478bd9Sstevel@tonic-gate 			/*
13277c478bd9Sstevel@tonic-gate 			 * still no memory, better delete this cache
13287c478bd9Sstevel@tonic-gate 			 */
13297c478bd9Sstevel@tonic-gate 			mutex_enter(&dcap->dca_lock);
13307c478bd9Sstevel@tonic-gate 			dcp = (dircache_t *)dcap->dca_dircache;
13317c478bd9Sstevel@tonic-gate 			if (VALID_DIR_CACHE(dcp)) {
13327c478bd9Sstevel@tonic-gate 				dnlc_dir_abort(dcp);
13337c478bd9Sstevel@tonic-gate 				dcap->dca_dircache = DC_RET_LOW_MEM;
13347c478bd9Sstevel@tonic-gate 			}
13357c478bd9Sstevel@tonic-gate 			mutex_exit(&dcap->dca_lock);
13367c478bd9Sstevel@tonic-gate 			ncs.ncs_dir_addabort.value.ui64++;
13377c478bd9Sstevel@tonic-gate 			return (DNOCACHE);
13387c478bd9Sstevel@tonic-gate 		}
13397c478bd9Sstevel@tonic-gate 		/*
13407c478bd9Sstevel@tonic-gate 		 * fall through as if the 1st kmem_alloc had worked
13417c478bd9Sstevel@tonic-gate 		 */
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate #ifdef DEBUG
13447c478bd9Sstevel@tonic-gate ok:
13457c478bd9Sstevel@tonic-gate #endif
13467c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
13477c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
13487c478bd9Sstevel@tonic-gate 	if (VALID_DIR_CACHE(dcp)) {
13497c478bd9Sstevel@tonic-gate 		/*
13507c478bd9Sstevel@tonic-gate 		 * If the total number of entries goes above the max
13517c478bd9Sstevel@tonic-gate 		 * then free this cache
13527c478bd9Sstevel@tonic-gate 		 */
13537c478bd9Sstevel@tonic-gate 		if ((dcp->dc_num_entries + dcp->dc_num_free) >
13547c478bd9Sstevel@tonic-gate 		    dnlc_dir_max_size) {
13557c478bd9Sstevel@tonic-gate 			mutex_exit(&dcap->dca_lock);
13567c478bd9Sstevel@tonic-gate 			dnlc_dir_purge(dcap);
13577c478bd9Sstevel@tonic-gate 			kmem_free(dep, sizeof (dcentry_t) - 1 + namlen);
13587c478bd9Sstevel@tonic-gate 			ncs.ncs_dir_add_max.value.ui64++;
13597c478bd9Sstevel@tonic-gate 			return (DTOOBIG);
13607c478bd9Sstevel@tonic-gate 		}
13617c478bd9Sstevel@tonic-gate 		dcp->dc_num_entries++;
13627c478bd9Sstevel@tonic-gate 		capacity = (dcp->dc_nhash_mask + 1) << dnlc_dir_hash_size_shift;
13637c478bd9Sstevel@tonic-gate 		if (dcp->dc_num_entries >=
13647c478bd9Sstevel@tonic-gate 		    (capacity << dnlc_dir_hash_resize_shift)) {
13657c478bd9Sstevel@tonic-gate 			dnlc_dir_adjust_nhash(dcp);
13667c478bd9Sstevel@tonic-gate 		}
13677c478bd9Sstevel@tonic-gate 		hp = &dcp->dc_namehash[hash & dcp->dc_nhash_mask];
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 		/*
13707c478bd9Sstevel@tonic-gate 		 * Initialise and chain in new entry
13717c478bd9Sstevel@tonic-gate 		 */
13727c478bd9Sstevel@tonic-gate 		dep->de_handle = handle;
13737c478bd9Sstevel@tonic-gate 		dep->de_hash = hash;
13747c478bd9Sstevel@tonic-gate 		/*
13757c478bd9Sstevel@tonic-gate 		 * Note de_namelen is a uchar_t to conserve space
13767c478bd9Sstevel@tonic-gate 		 * and alignment padding. The max length of any
13777c478bd9Sstevel@tonic-gate 		 * pathname component is defined as MAXNAMELEN
13787c478bd9Sstevel@tonic-gate 		 * which is 256 (including the terminating null).
13797c478bd9Sstevel@tonic-gate 		 * So provided this doesn't change, we don't include the null,
13807c478bd9Sstevel@tonic-gate 		 * we always use bcmp to compare strings, and we don't
13817c478bd9Sstevel@tonic-gate 		 * start storing full names, then we are ok.
13827c478bd9Sstevel@tonic-gate 		 * The space savings is worth it.
13837c478bd9Sstevel@tonic-gate 		 */
13847c478bd9Sstevel@tonic-gate 		dep->de_namelen = namlen;
13857c478bd9Sstevel@tonic-gate 		bcopy(name, dep->de_name, namlen);
13867c478bd9Sstevel@tonic-gate 		dep->de_next = *hp;
13877c478bd9Sstevel@tonic-gate 		*hp = dep;
13887c478bd9Sstevel@tonic-gate 		dcp->dc_actime = lbolt64;
13897c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
13907c478bd9Sstevel@tonic-gate 		ncs.ncs_dir_num_ents.value.ui64++;
13917c478bd9Sstevel@tonic-gate 		return (DOK);
13927c478bd9Sstevel@tonic-gate 	} else {
13937c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
13947c478bd9Sstevel@tonic-gate 		kmem_free(dep, sizeof (dcentry_t) - 1 + namlen);
13957c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate /*
14007c478bd9Sstevel@tonic-gate  * Add free space to a partial or complete directory cache.
14017c478bd9Sstevel@tonic-gate  */
14027c478bd9Sstevel@tonic-gate dcret_t
14037c478bd9Sstevel@tonic-gate dnlc_dir_add_space(dcanchor_t *dcap, uint_t len, uint64_t handle)
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
14067c478bd9Sstevel@tonic-gate 	dcfree_t *dfp, **hp;
14077c478bd9Sstevel@tonic-gate 	uint_t capacity;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	/*
14107c478bd9Sstevel@tonic-gate 	 * We kmem_alloc outside the lock to avoid possible deadlock if
14117c478bd9Sstevel@tonic-gate 	 * dnlc_dir_reclaim() is called as a result of memory shortage.
14127c478bd9Sstevel@tonic-gate 	 */
14137c478bd9Sstevel@tonic-gate 	dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
14147c478bd9Sstevel@tonic-gate 	if (dfp == NULL) {
14157c478bd9Sstevel@tonic-gate #ifdef DEBUG
14167c478bd9Sstevel@tonic-gate 		/*
14177c478bd9Sstevel@tonic-gate 		 * The kmem allocator generates random failures for
14187c478bd9Sstevel@tonic-gate 		 * KM_NOSLEEP calls (see KMEM_RANDOM_ALLOCATION_FAILURE)
14197c478bd9Sstevel@tonic-gate 		 * So try again before we blow away a perfectly good cache.
14207c478bd9Sstevel@tonic-gate 		 * This random error only occurs in debug mode
14217c478bd9Sstevel@tonic-gate 		 */
14227c478bd9Sstevel@tonic-gate 		dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
14237c478bd9Sstevel@tonic-gate 		if (dfp != NULL)
14247c478bd9Sstevel@tonic-gate 			goto ok;
14257c478bd9Sstevel@tonic-gate #endif
14267c478bd9Sstevel@tonic-gate 		ncs.ncs_dir_add_nm.value.ui64++;
14277c478bd9Sstevel@tonic-gate 		/*
14287c478bd9Sstevel@tonic-gate 		 * Free a directory cache. This may be the one we are
14297c478bd9Sstevel@tonic-gate 		 * called with.
14307c478bd9Sstevel@tonic-gate 		 */
14317c478bd9Sstevel@tonic-gate 		dnlc_dir_reclaim(NULL);
14327c478bd9Sstevel@tonic-gate 		dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
14337c478bd9Sstevel@tonic-gate 		if (dfp == NULL) {
14347c478bd9Sstevel@tonic-gate 			/*
14357c478bd9Sstevel@tonic-gate 			 * still no memory, better delete this cache
14367c478bd9Sstevel@tonic-gate 			 */
14377c478bd9Sstevel@tonic-gate 			mutex_enter(&dcap->dca_lock);
14387c478bd9Sstevel@tonic-gate 			dcp = (dircache_t *)dcap->dca_dircache;
14397c478bd9Sstevel@tonic-gate 			if (VALID_DIR_CACHE(dcp)) {
14407c478bd9Sstevel@tonic-gate 				dnlc_dir_abort(dcp);
14417c478bd9Sstevel@tonic-gate 				dcap->dca_dircache = DC_RET_LOW_MEM;
14427c478bd9Sstevel@tonic-gate 			}
14437c478bd9Sstevel@tonic-gate 			mutex_exit(&dcap->dca_lock);
14447c478bd9Sstevel@tonic-gate 			ncs.ncs_dir_addabort.value.ui64++;
14457c478bd9Sstevel@tonic-gate 			return (DNOCACHE);
14467c478bd9Sstevel@tonic-gate 		}
14477c478bd9Sstevel@tonic-gate 		/*
14487c478bd9Sstevel@tonic-gate 		 * fall through as if the 1st kmem_alloc had worked
14497c478bd9Sstevel@tonic-gate 		 */
14507c478bd9Sstevel@tonic-gate 	}
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate #ifdef DEBUG
14537c478bd9Sstevel@tonic-gate ok:
14547c478bd9Sstevel@tonic-gate #endif
14557c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
14567c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
14577c478bd9Sstevel@tonic-gate 	if (VALID_DIR_CACHE(dcp)) {
14587c478bd9Sstevel@tonic-gate 		if ((dcp->dc_num_entries + dcp->dc_num_free) >
14597c478bd9Sstevel@tonic-gate 		    dnlc_dir_max_size) {
14607c478bd9Sstevel@tonic-gate 			mutex_exit(&dcap->dca_lock);
14617c478bd9Sstevel@tonic-gate 			dnlc_dir_purge(dcap);
14627c478bd9Sstevel@tonic-gate 			kmem_cache_free(dnlc_dir_space_cache, dfp);
14637c478bd9Sstevel@tonic-gate 			ncs.ncs_dir_add_max.value.ui64++;
14647c478bd9Sstevel@tonic-gate 			return (DTOOBIG);
14657c478bd9Sstevel@tonic-gate 		}
14667c478bd9Sstevel@tonic-gate 		dcp->dc_num_free++;
14677c478bd9Sstevel@tonic-gate 		capacity = (dcp->dc_fhash_mask + 1) << dnlc_dir_hash_size_shift;
14687c478bd9Sstevel@tonic-gate 		if (dcp->dc_num_free >=
14697c478bd9Sstevel@tonic-gate 		    (capacity << dnlc_dir_hash_resize_shift)) {
14707c478bd9Sstevel@tonic-gate 			dnlc_dir_adjust_fhash(dcp);
14717c478bd9Sstevel@tonic-gate 		}
14727c478bd9Sstevel@tonic-gate 		/*
14737c478bd9Sstevel@tonic-gate 		 * Initialise and chain a new entry
14747c478bd9Sstevel@tonic-gate 		 */
14757c478bd9Sstevel@tonic-gate 		dfp->df_handle = handle;
14767c478bd9Sstevel@tonic-gate 		dfp->df_len = len;
14777c478bd9Sstevel@tonic-gate 		dcp->dc_actime = lbolt64;
14787c478bd9Sstevel@tonic-gate 		hp = &(dcp->dc_freehash[DDFHASH(handle, dcp)]);
14797c478bd9Sstevel@tonic-gate 		dfp->df_next = *hp;
14807c478bd9Sstevel@tonic-gate 		*hp = dfp;
14817c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
14827c478bd9Sstevel@tonic-gate 		ncs.ncs_dir_num_ents.value.ui64++;
14837c478bd9Sstevel@tonic-gate 		return (DOK);
14847c478bd9Sstevel@tonic-gate 	} else {
14857c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
14867c478bd9Sstevel@tonic-gate 		kmem_cache_free(dnlc_dir_space_cache, dfp);
14877c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate /*
14927c478bd9Sstevel@tonic-gate  * Mark a directory cache as complete.
14937c478bd9Sstevel@tonic-gate  */
14947c478bd9Sstevel@tonic-gate void
14957c478bd9Sstevel@tonic-gate dnlc_dir_complete(dcanchor_t *dcap)
14967c478bd9Sstevel@tonic-gate {
14977c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
15007c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
15017c478bd9Sstevel@tonic-gate 	if (VALID_DIR_CACHE(dcp)) {
15027c478bd9Sstevel@tonic-gate 		dcp->dc_complete = B_TRUE;
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 	mutex_exit(&dcap->dca_lock);
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate /*
15087c478bd9Sstevel@tonic-gate  * Internal routine to delete a partial or full directory cache.
15097c478bd9Sstevel@tonic-gate  * No additional locking needed.
15107c478bd9Sstevel@tonic-gate  */
15117c478bd9Sstevel@tonic-gate static void
15127c478bd9Sstevel@tonic-gate dnlc_dir_abort(dircache_t *dcp)
15137c478bd9Sstevel@tonic-gate {
15147c478bd9Sstevel@tonic-gate 	dcentry_t *dep, *nhp;
15157c478bd9Sstevel@tonic-gate 	dcfree_t *fep, *fhp;
15167c478bd9Sstevel@tonic-gate 	uint_t nhtsize = dcp->dc_nhash_mask + 1; /* name hash table size */
15177c478bd9Sstevel@tonic-gate 	uint_t fhtsize = dcp->dc_fhash_mask + 1; /* free hash table size */
15187c478bd9Sstevel@tonic-gate 	uint_t i;
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	/*
15217c478bd9Sstevel@tonic-gate 	 * Free up the cached name entries and hash table
15227c478bd9Sstevel@tonic-gate 	 */
15237c478bd9Sstevel@tonic-gate 	for (i = 0; i < nhtsize; i++) { /* for each hash bucket */
15247c478bd9Sstevel@tonic-gate 		nhp = dcp->dc_namehash[i];
15257c478bd9Sstevel@tonic-gate 		while (nhp != NULL) { /* for each chained entry */
15267c478bd9Sstevel@tonic-gate 			dep = nhp->de_next;
15277c478bd9Sstevel@tonic-gate 			kmem_free(nhp, sizeof (dcentry_t) - 1 +
15287c478bd9Sstevel@tonic-gate 			    nhp->de_namelen);
15297c478bd9Sstevel@tonic-gate 			nhp = dep;
15307c478bd9Sstevel@tonic-gate 		}
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 	kmem_free(dcp->dc_namehash, sizeof (dcentry_t *) * nhtsize);
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	/*
15357c478bd9Sstevel@tonic-gate 	 * Free up the free space entries and hash table
15367c478bd9Sstevel@tonic-gate 	 */
15377c478bd9Sstevel@tonic-gate 	for (i = 0; i < fhtsize; i++) { /* for each hash bucket */
15387c478bd9Sstevel@tonic-gate 		fhp = dcp->dc_freehash[i];
15397c478bd9Sstevel@tonic-gate 		while (fhp != NULL) { /* for each chained entry */
15407c478bd9Sstevel@tonic-gate 			fep = fhp->df_next;
15417c478bd9Sstevel@tonic-gate 			kmem_cache_free(dnlc_dir_space_cache, fhp);
15427c478bd9Sstevel@tonic-gate 			fhp = fep;
15437c478bd9Sstevel@tonic-gate 		}
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 	kmem_free(dcp->dc_freehash, sizeof (dcfree_t *) * fhtsize);
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	/*
15487c478bd9Sstevel@tonic-gate 	 * Finally free the directory cache structure itself
15497c478bd9Sstevel@tonic-gate 	 */
15507c478bd9Sstevel@tonic-gate 	ncs.ncs_dir_num_ents.value.ui64 -= (dcp->dc_num_entries +
15517c478bd9Sstevel@tonic-gate 	    dcp->dc_num_free);
15527c478bd9Sstevel@tonic-gate 	kmem_free(dcp, sizeof (dircache_t));
15537c478bd9Sstevel@tonic-gate 	ncs.ncs_cur_dirs.value.ui64--;
15547c478bd9Sstevel@tonic-gate }
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate /*
15577c478bd9Sstevel@tonic-gate  * Remove a partial or complete directory cache
15587c478bd9Sstevel@tonic-gate  */
15597c478bd9Sstevel@tonic-gate void
15607c478bd9Sstevel@tonic-gate dnlc_dir_purge(dcanchor_t *dcap)
15617c478bd9Sstevel@tonic-gate {
15627c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	mutex_enter(&dc_head.dch_lock);
15657c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
15667c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
15677c478bd9Sstevel@tonic-gate 	if (!VALID_DIR_CACHE(dcp)) {
15687c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
15697c478bd9Sstevel@tonic-gate 		mutex_exit(&dc_head.dch_lock);
15707c478bd9Sstevel@tonic-gate 		return;
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 	dcap->dca_dircache = NULL;
15737c478bd9Sstevel@tonic-gate 	/*
15747c478bd9Sstevel@tonic-gate 	 * Unchain from global list
15757c478bd9Sstevel@tonic-gate 	 */
15767c478bd9Sstevel@tonic-gate 	dcp->dc_prev->dc_next = dcp->dc_next;
15777c478bd9Sstevel@tonic-gate 	dcp->dc_next->dc_prev = dcp->dc_prev;
15787c478bd9Sstevel@tonic-gate 	mutex_exit(&dcap->dca_lock);
15797c478bd9Sstevel@tonic-gate 	mutex_exit(&dc_head.dch_lock);
15807c478bd9Sstevel@tonic-gate 	dnlc_dir_abort(dcp);
15817c478bd9Sstevel@tonic-gate }
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate /*
15847c478bd9Sstevel@tonic-gate  * Remove an entry from a complete or partial directory cache.
15857c478bd9Sstevel@tonic-gate  * Return the handle if it's non null.
15867c478bd9Sstevel@tonic-gate  */
15877c478bd9Sstevel@tonic-gate dcret_t
15887c478bd9Sstevel@tonic-gate dnlc_dir_rem_entry(dcanchor_t *dcap, char *name, uint64_t *handlep)
15897c478bd9Sstevel@tonic-gate {
15907c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
15917c478bd9Sstevel@tonic-gate 	dcentry_t **prevpp, *te;
15927c478bd9Sstevel@tonic-gate 	uint_t capacity;
15937c478bd9Sstevel@tonic-gate 	int hash;
15947c478bd9Sstevel@tonic-gate 	int ret;
15957c478bd9Sstevel@tonic-gate 	uchar_t namlen;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	if (!dnlc_dir_enable) {
15987c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
15997c478bd9Sstevel@tonic-gate 	}
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
16027c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
16037c478bd9Sstevel@tonic-gate 	if (VALID_DIR_CACHE(dcp)) {
16047c478bd9Sstevel@tonic-gate 		dcp->dc_actime = lbolt64;
16057c478bd9Sstevel@tonic-gate 		if (dcp->dc_nhash_mask > 0) { /* ie not minimum */
16067c478bd9Sstevel@tonic-gate 			capacity = (dcp->dc_nhash_mask + 1) <<
16077c478bd9Sstevel@tonic-gate 			    dnlc_dir_hash_size_shift;
16087c478bd9Sstevel@tonic-gate 			if (dcp->dc_num_entries <=
16097c478bd9Sstevel@tonic-gate 			    (capacity >> dnlc_dir_hash_resize_shift)) {
16107c478bd9Sstevel@tonic-gate 				dnlc_dir_adjust_nhash(dcp);
16117c478bd9Sstevel@tonic-gate 			}
16127c478bd9Sstevel@tonic-gate 		}
16137c478bd9Sstevel@tonic-gate 		DNLC_DIR_HASH(name, hash, namlen);
16147c478bd9Sstevel@tonic-gate 		prevpp = &dcp->dc_namehash[hash & dcp->dc_nhash_mask];
16157c478bd9Sstevel@tonic-gate 		while (*prevpp != NULL) {
16167c478bd9Sstevel@tonic-gate 			if (((*prevpp)->de_hash == hash) &&
16177c478bd9Sstevel@tonic-gate 			    (namlen == (*prevpp)->de_namelen) &&
16187c478bd9Sstevel@tonic-gate 			    bcmp((*prevpp)->de_name, name, namlen) == 0) {
16197c478bd9Sstevel@tonic-gate 				if (handlep != NULL) {
16207c478bd9Sstevel@tonic-gate 					*handlep = (*prevpp)->de_handle;
16217c478bd9Sstevel@tonic-gate 				}
16227c478bd9Sstevel@tonic-gate 				te = *prevpp;
16237c478bd9Sstevel@tonic-gate 				*prevpp = (*prevpp)->de_next;
16247c478bd9Sstevel@tonic-gate 				kmem_free(te, sizeof (dcentry_t) - 1 +
16257c478bd9Sstevel@tonic-gate 				    te->de_namelen);
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 				/*
16287c478bd9Sstevel@tonic-gate 				 * If the total number of entries
16297c478bd9Sstevel@tonic-gate 				 * falls below half the minimum number
16307c478bd9Sstevel@tonic-gate 				 * of entries then free this cache.
16317c478bd9Sstevel@tonic-gate 				 */
16327c478bd9Sstevel@tonic-gate 				if (--dcp->dc_num_entries <
16337c478bd9Sstevel@tonic-gate 				    (dnlc_dir_min_size >> 1)) {
16347c478bd9Sstevel@tonic-gate 					mutex_exit(&dcap->dca_lock);
16357c478bd9Sstevel@tonic-gate 					dnlc_dir_purge(dcap);
16367c478bd9Sstevel@tonic-gate 				} else {
16377c478bd9Sstevel@tonic-gate 					mutex_exit(&dcap->dca_lock);
16387c478bd9Sstevel@tonic-gate 				}
16397c478bd9Sstevel@tonic-gate 				ncs.ncs_dir_num_ents.value.ui64--;
16407c478bd9Sstevel@tonic-gate 				return (DFOUND);
16417c478bd9Sstevel@tonic-gate 			}
16427c478bd9Sstevel@tonic-gate 			prevpp = &((*prevpp)->de_next);
16437c478bd9Sstevel@tonic-gate 		}
16447c478bd9Sstevel@tonic-gate 		if (dcp->dc_complete) {
16457c478bd9Sstevel@tonic-gate 			ncs.ncs_dir_reme_fai.value.ui64++;
16467c478bd9Sstevel@tonic-gate 			ret = DNOENT;
16477c478bd9Sstevel@tonic-gate 		} else {
16487c478bd9Sstevel@tonic-gate 			ret = DNOCACHE;
16497c478bd9Sstevel@tonic-gate 		}
16507c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
16517c478bd9Sstevel@tonic-gate 		return (ret);
16527c478bd9Sstevel@tonic-gate 	} else {
16537c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
16547c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
16557c478bd9Sstevel@tonic-gate 	}
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate /*
16607c478bd9Sstevel@tonic-gate  * Remove free space of at least the given length from a complete
16617c478bd9Sstevel@tonic-gate  * or partial directory cache.
16627c478bd9Sstevel@tonic-gate  */
16637c478bd9Sstevel@tonic-gate dcret_t
16647c478bd9Sstevel@tonic-gate dnlc_dir_rem_space_by_len(dcanchor_t *dcap, uint_t len, uint64_t *handlep)
16657c478bd9Sstevel@tonic-gate {
16667c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
16677c478bd9Sstevel@tonic-gate 	dcfree_t **prevpp, *tfp;
16687c478bd9Sstevel@tonic-gate 	uint_t fhtsize; /* free hash table size */
16697c478bd9Sstevel@tonic-gate 	uint_t i;
16707c478bd9Sstevel@tonic-gate 	uint_t capacity;
16717c478bd9Sstevel@tonic-gate 	int ret;
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	if (!dnlc_dir_enable) {
16747c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
16757c478bd9Sstevel@tonic-gate 	}
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
16787c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
16797c478bd9Sstevel@tonic-gate 	if (VALID_DIR_CACHE(dcp)) {
16807c478bd9Sstevel@tonic-gate 		dcp->dc_actime = lbolt64;
16817c478bd9Sstevel@tonic-gate 		if (dcp->dc_fhash_mask > 0) { /* ie not minimum */
16827c478bd9Sstevel@tonic-gate 			capacity = (dcp->dc_fhash_mask + 1) <<
16837c478bd9Sstevel@tonic-gate 			    dnlc_dir_hash_size_shift;
16847c478bd9Sstevel@tonic-gate 			if (dcp->dc_num_free <=
16857c478bd9Sstevel@tonic-gate 			    (capacity >> dnlc_dir_hash_resize_shift)) {
16867c478bd9Sstevel@tonic-gate 				dnlc_dir_adjust_fhash(dcp);
16877c478bd9Sstevel@tonic-gate 			}
16887c478bd9Sstevel@tonic-gate 		}
16897c478bd9Sstevel@tonic-gate 		/*
16907c478bd9Sstevel@tonic-gate 		 * Search for an entry of the appropriate size
16917c478bd9Sstevel@tonic-gate 		 * on a first fit basis.
16927c478bd9Sstevel@tonic-gate 		 */
16937c478bd9Sstevel@tonic-gate 		fhtsize = dcp->dc_fhash_mask + 1;
16947c478bd9Sstevel@tonic-gate 		for (i = 0; i < fhtsize; i++) { /* for each hash bucket */
16957c478bd9Sstevel@tonic-gate 			prevpp = &(dcp->dc_freehash[i]);
16967c478bd9Sstevel@tonic-gate 			while (*prevpp != NULL) {
16977c478bd9Sstevel@tonic-gate 				if ((*prevpp)->df_len >= len) {
16987c478bd9Sstevel@tonic-gate 					*handlep = (*prevpp)->df_handle;
16997c478bd9Sstevel@tonic-gate 					tfp = *prevpp;
17007c478bd9Sstevel@tonic-gate 					*prevpp = (*prevpp)->df_next;
17017c478bd9Sstevel@tonic-gate 					dcp->dc_num_free--;
17027c478bd9Sstevel@tonic-gate 					mutex_exit(&dcap->dca_lock);
17037c478bd9Sstevel@tonic-gate 					kmem_cache_free(dnlc_dir_space_cache,
17047c478bd9Sstevel@tonic-gate 					    tfp);
17057c478bd9Sstevel@tonic-gate 					ncs.ncs_dir_num_ents.value.ui64--;
17067c478bd9Sstevel@tonic-gate 					return (DFOUND);
17077c478bd9Sstevel@tonic-gate 				}
17087c478bd9Sstevel@tonic-gate 				prevpp = &((*prevpp)->df_next);
17097c478bd9Sstevel@tonic-gate 			}
17107c478bd9Sstevel@tonic-gate 		}
17117c478bd9Sstevel@tonic-gate 		if (dcp->dc_complete) {
17127c478bd9Sstevel@tonic-gate 			ret = DNOENT;
17137c478bd9Sstevel@tonic-gate 		} else {
17147c478bd9Sstevel@tonic-gate 			ret = DNOCACHE;
17157c478bd9Sstevel@tonic-gate 		}
17167c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
17177c478bd9Sstevel@tonic-gate 		return (ret);
17187c478bd9Sstevel@tonic-gate 	} else {
17197c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
17207c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate }
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate /*
17257c478bd9Sstevel@tonic-gate  * Remove free space with the given handle from a complete or partial
17267c478bd9Sstevel@tonic-gate  * directory cache.
17277c478bd9Sstevel@tonic-gate  */
17287c478bd9Sstevel@tonic-gate dcret_t
17297c478bd9Sstevel@tonic-gate dnlc_dir_rem_space_by_handle(dcanchor_t *dcap, uint64_t handle)
17307c478bd9Sstevel@tonic-gate {
17317c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
17327c478bd9Sstevel@tonic-gate 	dcfree_t **prevpp, *tfp;
17337c478bd9Sstevel@tonic-gate 	uint_t capacity;
17347c478bd9Sstevel@tonic-gate 	int ret;
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	if (!dnlc_dir_enable) {
17377c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
17417c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
17427c478bd9Sstevel@tonic-gate 	if (VALID_DIR_CACHE(dcp)) {
17437c478bd9Sstevel@tonic-gate 		dcp->dc_actime = lbolt64;
17447c478bd9Sstevel@tonic-gate 		if (dcp->dc_fhash_mask > 0) { /* ie not minimum */
17457c478bd9Sstevel@tonic-gate 			capacity = (dcp->dc_fhash_mask + 1) <<
17467c478bd9Sstevel@tonic-gate 			    dnlc_dir_hash_size_shift;
17477c478bd9Sstevel@tonic-gate 			if (dcp->dc_num_free <=
17487c478bd9Sstevel@tonic-gate 			    (capacity >> dnlc_dir_hash_resize_shift)) {
17497c478bd9Sstevel@tonic-gate 				dnlc_dir_adjust_fhash(dcp);
17507c478bd9Sstevel@tonic-gate 			}
17517c478bd9Sstevel@tonic-gate 		}
17527c478bd9Sstevel@tonic-gate 
17537c478bd9Sstevel@tonic-gate 		/*
17547c478bd9Sstevel@tonic-gate 		 * search for the exact entry
17557c478bd9Sstevel@tonic-gate 		 */
17567c478bd9Sstevel@tonic-gate 		prevpp = &(dcp->dc_freehash[DDFHASH(handle, dcp)]);
17577c478bd9Sstevel@tonic-gate 		while (*prevpp != NULL) {
17587c478bd9Sstevel@tonic-gate 			if ((*prevpp)->df_handle == handle) {
17597c478bd9Sstevel@tonic-gate 				tfp = *prevpp;
17607c478bd9Sstevel@tonic-gate 				*prevpp = (*prevpp)->df_next;
17617c478bd9Sstevel@tonic-gate 				dcp->dc_num_free--;
17627c478bd9Sstevel@tonic-gate 				mutex_exit(&dcap->dca_lock);
17637c478bd9Sstevel@tonic-gate 				kmem_cache_free(dnlc_dir_space_cache, tfp);
17647c478bd9Sstevel@tonic-gate 				ncs.ncs_dir_num_ents.value.ui64--;
17657c478bd9Sstevel@tonic-gate 				return (DFOUND);
17667c478bd9Sstevel@tonic-gate 			}
17677c478bd9Sstevel@tonic-gate 			prevpp = &((*prevpp)->df_next);
17687c478bd9Sstevel@tonic-gate 		}
17697c478bd9Sstevel@tonic-gate 		if (dcp->dc_complete) {
17707c478bd9Sstevel@tonic-gate 			ncs.ncs_dir_rems_fai.value.ui64++;
17717c478bd9Sstevel@tonic-gate 			ret = DNOENT;
17727c478bd9Sstevel@tonic-gate 		} else {
17737c478bd9Sstevel@tonic-gate 			ret = DNOCACHE;
17747c478bd9Sstevel@tonic-gate 		}
17757c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
17767c478bd9Sstevel@tonic-gate 		return (ret);
17777c478bd9Sstevel@tonic-gate 	} else {
17787c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
17797c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate }
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate /*
17847c478bd9Sstevel@tonic-gate  * Update the handle of an directory cache entry.
17857c478bd9Sstevel@tonic-gate  */
17867c478bd9Sstevel@tonic-gate dcret_t
17877c478bd9Sstevel@tonic-gate dnlc_dir_update(dcanchor_t *dcap, char *name, uint64_t handle)
17887c478bd9Sstevel@tonic-gate {
17897c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
17907c478bd9Sstevel@tonic-gate 	dcentry_t *dep;
17917c478bd9Sstevel@tonic-gate 	int hash;
17927c478bd9Sstevel@tonic-gate 	int ret;
17937c478bd9Sstevel@tonic-gate 	uchar_t namlen;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	if (!dnlc_dir_enable) {
17967c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
17977c478bd9Sstevel@tonic-gate 	}
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
18007c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
18017c478bd9Sstevel@tonic-gate 	if (VALID_DIR_CACHE(dcp)) {
18027c478bd9Sstevel@tonic-gate 		dcp->dc_actime = lbolt64;
18037c478bd9Sstevel@tonic-gate 		DNLC_DIR_HASH(name, hash, namlen);
18047c478bd9Sstevel@tonic-gate 		dep = dcp->dc_namehash[hash & dcp->dc_nhash_mask];
18057c478bd9Sstevel@tonic-gate 		while (dep != NULL) {
18067c478bd9Sstevel@tonic-gate 			if ((dep->de_hash == hash) &&
18077c478bd9Sstevel@tonic-gate 			    (namlen == dep->de_namelen) &&
18087c478bd9Sstevel@tonic-gate 			    bcmp(dep->de_name, name, namlen) == 0) {
18097c478bd9Sstevel@tonic-gate 				dep->de_handle = handle;
18107c478bd9Sstevel@tonic-gate 				mutex_exit(&dcap->dca_lock);
18117c478bd9Sstevel@tonic-gate 				return (DFOUND);
18127c478bd9Sstevel@tonic-gate 			}
18137c478bd9Sstevel@tonic-gate 			dep = dep->de_next;
18147c478bd9Sstevel@tonic-gate 		}
18157c478bd9Sstevel@tonic-gate 		if (dcp->dc_complete) {
18167c478bd9Sstevel@tonic-gate 			ncs.ncs_dir_upd_fail.value.ui64++;
18177c478bd9Sstevel@tonic-gate 			ret = DNOENT;
18187c478bd9Sstevel@tonic-gate 		} else {
18197c478bd9Sstevel@tonic-gate 			ret = DNOCACHE;
18207c478bd9Sstevel@tonic-gate 		}
18217c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
18227c478bd9Sstevel@tonic-gate 		return (ret);
18237c478bd9Sstevel@tonic-gate 	} else {
18247c478bd9Sstevel@tonic-gate 		mutex_exit(&dcap->dca_lock);
18257c478bd9Sstevel@tonic-gate 		return (DNOCACHE);
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate }
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate void
18307c478bd9Sstevel@tonic-gate dnlc_dir_fini(dcanchor_t *dcap)
18317c478bd9Sstevel@tonic-gate {
18327c478bd9Sstevel@tonic-gate 	dircache_t *dcp;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	mutex_enter(&dc_head.dch_lock);
18357c478bd9Sstevel@tonic-gate 	mutex_enter(&dcap->dca_lock);
18367c478bd9Sstevel@tonic-gate 	dcp = (dircache_t *)dcap->dca_dircache;
18377c478bd9Sstevel@tonic-gate 	if (VALID_DIR_CACHE(dcp)) {
18387c478bd9Sstevel@tonic-gate 		/*
18397c478bd9Sstevel@tonic-gate 		 * Unchain from global list
18407c478bd9Sstevel@tonic-gate 		 */
18417c478bd9Sstevel@tonic-gate 		ncs.ncs_dir_finipurg.value.ui64++;
18427c478bd9Sstevel@tonic-gate 		dcp->dc_prev->dc_next = dcp->dc_next;
18437c478bd9Sstevel@tonic-gate 		dcp->dc_next->dc_prev = dcp->dc_prev;
18447c478bd9Sstevel@tonic-gate 	} else {
18457c478bd9Sstevel@tonic-gate 		dcp = NULL;
18467c478bd9Sstevel@tonic-gate 	}
18477c478bd9Sstevel@tonic-gate 	dcap->dca_dircache = NULL;
18487c478bd9Sstevel@tonic-gate 	mutex_exit(&dcap->dca_lock);
18497c478bd9Sstevel@tonic-gate 	mutex_exit(&dc_head.dch_lock);
18507c478bd9Sstevel@tonic-gate 	mutex_destroy(&dcap->dca_lock);
18517c478bd9Sstevel@tonic-gate 	if (dcp) {
18527c478bd9Sstevel@tonic-gate 		dnlc_dir_abort(dcp);
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate }
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate /*
18577c478bd9Sstevel@tonic-gate  * Reclaim callback for dnlc directory caching.
18587c478bd9Sstevel@tonic-gate  * Invoked by the kernel memory allocator when memory gets tight.
18597c478bd9Sstevel@tonic-gate  * This is a pretty serious condition and can lead easily lead to system
18607c478bd9Sstevel@tonic-gate  * hangs if not enough space is returned.
18617c478bd9Sstevel@tonic-gate  *
18627c478bd9Sstevel@tonic-gate  * Deciding which directory (or directories) to purge is tricky.
18637c478bd9Sstevel@tonic-gate  * Purging everything is an overkill, but purging just the oldest used
18647c478bd9Sstevel@tonic-gate  * was found to lead to hangs. The largest cached directories use the
18657c478bd9Sstevel@tonic-gate  * most memory, but take the most effort to rebuild, whereas the smaller
18667c478bd9Sstevel@tonic-gate  * ones have little value and give back little space. So what to do?
18677c478bd9Sstevel@tonic-gate  *
18687c478bd9Sstevel@tonic-gate  * The current policy is to continue purging the oldest used directories
18697c478bd9Sstevel@tonic-gate  * until at least dnlc_dir_min_reclaim directory entries have been purged.
18707c478bd9Sstevel@tonic-gate  */
18717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18727c478bd9Sstevel@tonic-gate static void
18737c478bd9Sstevel@tonic-gate dnlc_dir_reclaim(void *unused)
18747c478bd9Sstevel@tonic-gate {
18757c478bd9Sstevel@tonic-gate 	dircache_t *dcp, *oldest;
18767c478bd9Sstevel@tonic-gate 	uint_t dirent_cnt = 0;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	mutex_enter(&dc_head.dch_lock);
18797c478bd9Sstevel@tonic-gate 	while (dirent_cnt < dnlc_dir_min_reclaim) {
18807c478bd9Sstevel@tonic-gate 		dcp = dc_head.dch_next;
18817c478bd9Sstevel@tonic-gate 		oldest = NULL;
18827c478bd9Sstevel@tonic-gate 		while (dcp != (dircache_t *)&dc_head) {
18837c478bd9Sstevel@tonic-gate 			if (oldest == NULL) {
18847c478bd9Sstevel@tonic-gate 				oldest = dcp;
18857c478bd9Sstevel@tonic-gate 			} else {
18867c478bd9Sstevel@tonic-gate 				if (dcp->dc_actime < oldest->dc_actime) {
18877c478bd9Sstevel@tonic-gate 					oldest = dcp;
18887c478bd9Sstevel@tonic-gate 				}
18897c478bd9Sstevel@tonic-gate 			}
18907c478bd9Sstevel@tonic-gate 			dcp = dcp->dc_next;
18917c478bd9Sstevel@tonic-gate 		}
18927c478bd9Sstevel@tonic-gate 		if (oldest == NULL) {
18937c478bd9Sstevel@tonic-gate 			/* nothing to delete */
18947c478bd9Sstevel@tonic-gate 			mutex_exit(&dc_head.dch_lock);
18957c478bd9Sstevel@tonic-gate 			return;
18967c478bd9Sstevel@tonic-gate 		}
18977c478bd9Sstevel@tonic-gate 		/*
18987c478bd9Sstevel@tonic-gate 		 * remove from directory chain and purge
18997c478bd9Sstevel@tonic-gate 		 */
19007c478bd9Sstevel@tonic-gate 		oldest->dc_prev->dc_next = oldest->dc_next;
19017c478bd9Sstevel@tonic-gate 		oldest->dc_next->dc_prev = oldest->dc_prev;
19027c478bd9Sstevel@tonic-gate 		mutex_enter(&oldest->dc_anchor->dca_lock);
19037c478bd9Sstevel@tonic-gate 		/*
19047c478bd9Sstevel@tonic-gate 		 * If this was the last entry then it must be too large.
19057c478bd9Sstevel@tonic-gate 		 * Mark it as such by saving a special dircache_t
19067c478bd9Sstevel@tonic-gate 		 * pointer (DC_RET_LOW_MEM) in the anchor. The error DNOMEM
19077c478bd9Sstevel@tonic-gate 		 * will be presented to the caller of dnlc_dir_start()
19087c478bd9Sstevel@tonic-gate 		 */
19097c478bd9Sstevel@tonic-gate 		if (oldest->dc_next == oldest->dc_prev) {
19107c478bd9Sstevel@tonic-gate 			oldest->dc_anchor->dca_dircache = DC_RET_LOW_MEM;
19117c478bd9Sstevel@tonic-gate 			ncs.ncs_dir_rec_last.value.ui64++;
19127c478bd9Sstevel@tonic-gate 		} else {
19137c478bd9Sstevel@tonic-gate 			oldest->dc_anchor->dca_dircache = NULL;
19147c478bd9Sstevel@tonic-gate 			ncs.ncs_dir_recl_any.value.ui64++;
19157c478bd9Sstevel@tonic-gate 		}
19167c478bd9Sstevel@tonic-gate 		mutex_exit(&oldest->dc_anchor->dca_lock);
19177c478bd9Sstevel@tonic-gate 		dirent_cnt += oldest->dc_num_entries;
19187c478bd9Sstevel@tonic-gate 		dnlc_dir_abort(oldest);
19197c478bd9Sstevel@tonic-gate 	}
19207c478bd9Sstevel@tonic-gate 	mutex_exit(&dc_head.dch_lock);
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate /*
19247c478bd9Sstevel@tonic-gate  * Dynamically grow or shrink the size of the name hash table
19257c478bd9Sstevel@tonic-gate  */
19267c478bd9Sstevel@tonic-gate static void
19277c478bd9Sstevel@tonic-gate dnlc_dir_adjust_nhash(dircache_t *dcp)
19287c478bd9Sstevel@tonic-gate {
19297c478bd9Sstevel@tonic-gate 	dcentry_t **newhash, *dep, **nhp, *tep;
19307c478bd9Sstevel@tonic-gate 	uint_t newsize;
19317c478bd9Sstevel@tonic-gate 	uint_t oldsize;
19327c478bd9Sstevel@tonic-gate 	uint_t newsizemask;
19337c478bd9Sstevel@tonic-gate 	int i;
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	/*
19367c478bd9Sstevel@tonic-gate 	 * Allocate new hash table
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	newsize = dcp->dc_num_entries >> dnlc_dir_hash_size_shift;
19397c478bd9Sstevel@tonic-gate 	newhash = kmem_zalloc(sizeof (dcentry_t *) * newsize, KM_NOSLEEP);
19407c478bd9Sstevel@tonic-gate 	if (newhash == NULL) {
19417c478bd9Sstevel@tonic-gate 		/*
19427c478bd9Sstevel@tonic-gate 		 * System is short on memory just return
19437c478bd9Sstevel@tonic-gate 		 * Note, the old hash table is still usable.
19447c478bd9Sstevel@tonic-gate 		 * This return is unlikely to repeatedy occur, because
19457c478bd9Sstevel@tonic-gate 		 * either some other directory caches will be reclaimed
19467c478bd9Sstevel@tonic-gate 		 * due to memory shortage, thus freeing memory, or this
19477c478bd9Sstevel@tonic-gate 		 * directory cahe will be reclaimed.
19487c478bd9Sstevel@tonic-gate 		 */
19497c478bd9Sstevel@tonic-gate 		return;
19507c478bd9Sstevel@tonic-gate 	}
19517c478bd9Sstevel@tonic-gate 	oldsize = dcp->dc_nhash_mask + 1;
19527c478bd9Sstevel@tonic-gate 	dcp->dc_nhash_mask = newsizemask = newsize - 1;
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	/*
19557c478bd9Sstevel@tonic-gate 	 * Move entries from the old table to the new
19567c478bd9Sstevel@tonic-gate 	 */
19577c478bd9Sstevel@tonic-gate 	for (i = 0; i < oldsize; i++) { /* for each hash bucket */
19587c478bd9Sstevel@tonic-gate 		dep = dcp->dc_namehash[i];
19597c478bd9Sstevel@tonic-gate 		while (dep != NULL) { /* for each chained entry */
19607c478bd9Sstevel@tonic-gate 			tep = dep;
19617c478bd9Sstevel@tonic-gate 			dep = dep->de_next;
19627c478bd9Sstevel@tonic-gate 			nhp = &newhash[tep->de_hash & newsizemask];
19637c478bd9Sstevel@tonic-gate 			tep->de_next = *nhp;
19647c478bd9Sstevel@tonic-gate 			*nhp = tep;
19657c478bd9Sstevel@tonic-gate 		}
19667c478bd9Sstevel@tonic-gate 	}
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	/*
19697c478bd9Sstevel@tonic-gate 	 * delete old hash table and set new one in place
19707c478bd9Sstevel@tonic-gate 	 */
19717c478bd9Sstevel@tonic-gate 	kmem_free(dcp->dc_namehash, sizeof (dcentry_t *) * oldsize);
19727c478bd9Sstevel@tonic-gate 	dcp->dc_namehash = newhash;
19737c478bd9Sstevel@tonic-gate }
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate /*
19767c478bd9Sstevel@tonic-gate  * Dynamically grow or shrink the size of the free space hash table
19777c478bd9Sstevel@tonic-gate  */
19787c478bd9Sstevel@tonic-gate static void
19797c478bd9Sstevel@tonic-gate dnlc_dir_adjust_fhash(dircache_t *dcp)
19807c478bd9Sstevel@tonic-gate {
19817c478bd9Sstevel@tonic-gate 	dcfree_t **newhash, *dfp, **nhp, *tfp;
19827c478bd9Sstevel@tonic-gate 	uint_t newsize;
19837c478bd9Sstevel@tonic-gate 	uint_t oldsize;
19847c478bd9Sstevel@tonic-gate 	int i;
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	/*
19877c478bd9Sstevel@tonic-gate 	 * Allocate new hash table
19887c478bd9Sstevel@tonic-gate 	 */
19897c478bd9Sstevel@tonic-gate 	newsize = dcp->dc_num_free >> dnlc_dir_hash_size_shift;
19907c478bd9Sstevel@tonic-gate 	newhash = kmem_zalloc(sizeof (dcfree_t *) * newsize, KM_NOSLEEP);
19917c478bd9Sstevel@tonic-gate 	if (newhash == NULL) {
19927c478bd9Sstevel@tonic-gate 		/*
19937c478bd9Sstevel@tonic-gate 		 * System is short on memory just return
19947c478bd9Sstevel@tonic-gate 		 * Note, the old hash table is still usable.
19957c478bd9Sstevel@tonic-gate 		 * This return is unlikely to repeatedy occur, because
19967c478bd9Sstevel@tonic-gate 		 * either some other directory caches will be reclaimed
19977c478bd9Sstevel@tonic-gate 		 * due to memory shortage, thus freeing memory, or this
19987c478bd9Sstevel@tonic-gate 		 * directory cahe will be reclaimed.
19997c478bd9Sstevel@tonic-gate 		 */
20007c478bd9Sstevel@tonic-gate 		return;
20017c478bd9Sstevel@tonic-gate 	}
20027c478bd9Sstevel@tonic-gate 	oldsize = dcp->dc_fhash_mask + 1;
20037c478bd9Sstevel@tonic-gate 	dcp->dc_fhash_mask = newsize - 1;
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 	/*
20067c478bd9Sstevel@tonic-gate 	 * Move entries from the old table to the new
20077c478bd9Sstevel@tonic-gate 	 */
20087c478bd9Sstevel@tonic-gate 	for (i = 0; i < oldsize; i++) { /* for each hash bucket */
20097c478bd9Sstevel@tonic-gate 		dfp = dcp->dc_freehash[i];
20107c478bd9Sstevel@tonic-gate 		while (dfp != NULL) { /* for each chained entry */
20117c478bd9Sstevel@tonic-gate 			tfp = dfp;
20127c478bd9Sstevel@tonic-gate 			dfp = dfp->df_next;
20137c478bd9Sstevel@tonic-gate 			nhp = &newhash[DDFHASH(tfp->df_handle, dcp)];
20147c478bd9Sstevel@tonic-gate 			tfp->df_next = *nhp;
20157c478bd9Sstevel@tonic-gate 			*nhp = tfp;
20167c478bd9Sstevel@tonic-gate 		}
20177c478bd9Sstevel@tonic-gate 	}
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	/*
20207c478bd9Sstevel@tonic-gate 	 * delete old hash table and set new one in place
20217c478bd9Sstevel@tonic-gate 	 */
20227c478bd9Sstevel@tonic-gate 	kmem_free(dcp->dc_freehash, sizeof (dcfree_t *) * oldsize);
20237c478bd9Sstevel@tonic-gate 	dcp->dc_freehash = newhash;
20247c478bd9Sstevel@tonic-gate }
2025