10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51257Sah89892 * Common Development and Distribution License (the "License").
61257Sah89892 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12684STom.Erickson@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
260Sstevel@tonic-gate /* All Rights Reserved */
270Sstevel@tonic-gate
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
300Sstevel@tonic-gate * The Regents of the University of California
310Sstevel@tonic-gate * All Rights Reserved
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
340Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
350Sstevel@tonic-gate * contributors.
360Sstevel@tonic-gate */
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/param.h>
410Sstevel@tonic-gate #include <sys/t_lock.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/vfs.h>
440Sstevel@tonic-gate #include <sys/vnode.h>
450Sstevel@tonic-gate #include <sys/dnlc.h>
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate #include <sys/cmn_err.h>
480Sstevel@tonic-gate #include <sys/vtrace.h>
490Sstevel@tonic-gate #include <sys/bitmap.h>
500Sstevel@tonic-gate #include <sys/var.h>
510Sstevel@tonic-gate #include <sys/sysmacros.h>
520Sstevel@tonic-gate #include <sys/kstat.h>
530Sstevel@tonic-gate #include <sys/atomic.h>
540Sstevel@tonic-gate #include <sys/taskq.h>
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * Directory name lookup cache.
580Sstevel@tonic-gate * Based on code originally done by Robert Elz at Melbourne.
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * Names found by directory scans are retained in a cache
610Sstevel@tonic-gate * for future reference. Each hash chain is ordered by LRU
620Sstevel@tonic-gate * Cache is indexed by hash value obtained from (vp, name)
630Sstevel@tonic-gate * where the vp refers to the directory containing the name.
640Sstevel@tonic-gate */
650Sstevel@tonic-gate
660Sstevel@tonic-gate /*
676712Stomee * We want to be able to identify files that are referenced only by the DNLC.
686712Stomee * When adding a reference from the DNLC, call VN_HOLD_DNLC instead of VN_HOLD,
696712Stomee * since multiple DNLC references should only be counted once in v_count. This
706712Stomee * file contains only two(2) calls to VN_HOLD, renamed VN_HOLD_CALLER in the
716712Stomee * hope that no one will mistakenly add a VN_HOLD to this file. (Unfortunately
726712Stomee * it is not possible to #undef VN_HOLD and retain VN_HOLD_CALLER. Ideally a
736712Stomee * Makefile rule would grep uncommented C tokens to check that VN_HOLD is
746712Stomee * referenced only once in this file, to define VN_HOLD_CALLER.)
756712Stomee */
766712Stomee #define VN_HOLD_CALLER VN_HOLD
776712Stomee #define VN_HOLD_DNLC(vp) { \
786712Stomee mutex_enter(&(vp)->v_lock); \
796712Stomee if ((vp)->v_count_dnlc == 0) \
806712Stomee (vp)->v_count++; \
816712Stomee (vp)->v_count_dnlc++; \
826712Stomee mutex_exit(&(vp)->v_lock); \
836712Stomee }
846712Stomee #define VN_RELE_DNLC(vp) { \
856712Stomee vn_rele_dnlc(vp); \
866712Stomee }
876712Stomee
886712Stomee /*
890Sstevel@tonic-gate * Tunable nc_hashavelen is the average length desired for this chain, from
900Sstevel@tonic-gate * which the size of the nc_hash table is derived at create time.
910Sstevel@tonic-gate */
920Sstevel@tonic-gate #define NC_HASHAVELEN_DEFAULT 4
930Sstevel@tonic-gate int nc_hashavelen = NC_HASHAVELEN_DEFAULT;
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * NC_MOVETOFRONT is the move-to-front threshold: if the hash lookup
970Sstevel@tonic-gate * depth exceeds this value, we move the looked-up entry to the front of
980Sstevel@tonic-gate * its hash chain. The idea is to make sure that the most frequently
990Sstevel@tonic-gate * accessed entries are found most quickly (by keeping them near the
1000Sstevel@tonic-gate * front of their hash chains).
1010Sstevel@tonic-gate */
1020Sstevel@tonic-gate #define NC_MOVETOFRONT 2
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate *
1060Sstevel@tonic-gate * DNLC_MAX_RELE is used to size an array on the stack when releasing
1070Sstevel@tonic-gate * vnodes. This array is used rather than calling VN_RELE() inline because
1080Sstevel@tonic-gate * all dnlc locks must be dropped by that time in order to avoid a
1090Sstevel@tonic-gate * possible deadlock. This deadlock occurs when the dnlc holds the last
1100Sstevel@tonic-gate * reference to the vnode and so the VOP_INACTIVE vector is called which
1110Sstevel@tonic-gate * can in turn call back into the dnlc. A global array was used but had
1120Sstevel@tonic-gate * many problems:
1130Sstevel@tonic-gate * 1) Actually doesn't have an upper bound on the array size as
1140Sstevel@tonic-gate * entries can be added after starting the purge.
1150Sstevel@tonic-gate * 2) The locking scheme causes a hang.
1160Sstevel@tonic-gate * 3) Caused serialisation on the global lock.
1170Sstevel@tonic-gate * 4) The array was often unnecessarily huge.
1180Sstevel@tonic-gate *
1190Sstevel@tonic-gate * Note the current value 8 allows up to 4 cache entries (to be purged
1200Sstevel@tonic-gate * from each hash chain), before having to cycle around and retry.
1210Sstevel@tonic-gate * This ought to be ample given that nc_hashavelen is typically very small.
1220Sstevel@tonic-gate */
1230Sstevel@tonic-gate #define DNLC_MAX_RELE 8 /* must be even */
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate * Hash table of name cache entries for fast lookup, dynamically
1270Sstevel@tonic-gate * allocated at startup.
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate nc_hash_t *nc_hash;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate * Rotors. Used to select entries on a round-robin basis.
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate static nc_hash_t *dnlc_purge_fs1_rotor;
1350Sstevel@tonic-gate static nc_hash_t *dnlc_free_rotor;
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate * # of dnlc entries (uninitialized)
1390Sstevel@tonic-gate *
1400Sstevel@tonic-gate * the initial value was chosen as being
1410Sstevel@tonic-gate * a random string of bits, probably not
1420Sstevel@tonic-gate * normally chosen by a systems administrator
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate int ncsize = -1;
1451669Sperrin volatile uint32_t dnlc_nentries = 0; /* current num of name cache entries */
1461669Sperrin static int nc_hashsz; /* size of hash table */
1471669Sperrin static int nc_hashmask; /* size of hash table minus 1 */
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate * The dnlc_reduce_cache() taskq queue is activated when there are
1511484Sek110237 * ncsize name cache entries and if no parameter is provided, it reduces
1521484Sek110237 * the size down to dnlc_nentries_low_water, which is by default one
1531484Sek110237 * hundreth less (or 99%) of ncsize.
1541484Sek110237 *
1551484Sek110237 * If a parameter is provided to dnlc_reduce_cache(), then we reduce
1561484Sek110237 * the size down based on ncsize_onepercent - where ncsize_onepercent
1571669Sperrin * is 1% of ncsize; however, we never let dnlc_reduce_cache() reduce
1581669Sperrin * the size below 3% of ncsize (ncsize_min_percent).
1590Sstevel@tonic-gate */
1600Sstevel@tonic-gate #define DNLC_LOW_WATER_DIVISOR_DEFAULT 100
1610Sstevel@tonic-gate uint_t dnlc_low_water_divisor = DNLC_LOW_WATER_DIVISOR_DEFAULT;
1620Sstevel@tonic-gate uint_t dnlc_nentries_low_water;
1630Sstevel@tonic-gate int dnlc_reduce_idle = 1; /* no locking needed */
1641484Sek110237 uint_t ncsize_onepercent;
1651669Sperrin uint_t ncsize_min_percent;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate * If dnlc_nentries hits dnlc_max_nentries (twice ncsize)
1690Sstevel@tonic-gate * then this means the dnlc_reduce_cache() taskq is failing to
1700Sstevel@tonic-gate * keep up. In this case we refuse to add new entries to the dnlc
1710Sstevel@tonic-gate * until the taskq catches up.
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate uint_t dnlc_max_nentries; /* twice ncsize */
1740Sstevel@tonic-gate uint64_t dnlc_max_nentries_cnt = 0; /* statistic on times we failed */
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * Tunable to define when we should just remove items from
1780Sstevel@tonic-gate * the end of the chain.
1790Sstevel@tonic-gate */
1800Sstevel@tonic-gate #define DNLC_LONG_CHAIN 8
1810Sstevel@tonic-gate uint_t dnlc_long_chain = DNLC_LONG_CHAIN;
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate * ncstats has been deprecated, due to the integer size of the counters
1850Sstevel@tonic-gate * which can easily overflow in the dnlc.
1860Sstevel@tonic-gate * It is maintained (at some expense) for compatability.
1870Sstevel@tonic-gate * The preferred interface is the kstat accessible nc_stats below.
1880Sstevel@tonic-gate */
1890Sstevel@tonic-gate struct ncstats ncstats;
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate struct nc_stats ncs = {
1920Sstevel@tonic-gate { "hits", KSTAT_DATA_UINT64 },
1930Sstevel@tonic-gate { "misses", KSTAT_DATA_UINT64 },
1940Sstevel@tonic-gate { "negative_cache_hits", KSTAT_DATA_UINT64 },
1950Sstevel@tonic-gate { "enters", KSTAT_DATA_UINT64 },
1960Sstevel@tonic-gate { "double_enters", KSTAT_DATA_UINT64 },
1970Sstevel@tonic-gate { "purge_total_entries", KSTAT_DATA_UINT64 },
1980Sstevel@tonic-gate { "purge_all", KSTAT_DATA_UINT64 },
1990Sstevel@tonic-gate { "purge_vp", KSTAT_DATA_UINT64 },
2000Sstevel@tonic-gate { "purge_vfs", KSTAT_DATA_UINT64 },
2010Sstevel@tonic-gate { "purge_fs1", KSTAT_DATA_UINT64 },
2020Sstevel@tonic-gate { "pick_free", KSTAT_DATA_UINT64 },
2030Sstevel@tonic-gate { "pick_heuristic", KSTAT_DATA_UINT64 },
2040Sstevel@tonic-gate { "pick_last", KSTAT_DATA_UINT64 },
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate /* directory caching stats */
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate { "dir_hits", KSTAT_DATA_UINT64 },
2090Sstevel@tonic-gate { "dir_misses", KSTAT_DATA_UINT64 },
2100Sstevel@tonic-gate { "dir_cached_current", KSTAT_DATA_UINT64 },
2110Sstevel@tonic-gate { "dir_entries_cached_current", KSTAT_DATA_UINT64 },
2120Sstevel@tonic-gate { "dir_cached_total", KSTAT_DATA_UINT64 },
2130Sstevel@tonic-gate { "dir_start_no_memory", KSTAT_DATA_UINT64 },
2140Sstevel@tonic-gate { "dir_add_no_memory", KSTAT_DATA_UINT64 },
2150Sstevel@tonic-gate { "dir_add_abort", KSTAT_DATA_UINT64 },
2160Sstevel@tonic-gate { "dir_add_max", KSTAT_DATA_UINT64 },
2170Sstevel@tonic-gate { "dir_remove_entry_fail", KSTAT_DATA_UINT64 },
2180Sstevel@tonic-gate { "dir_remove_space_fail", KSTAT_DATA_UINT64 },
2190Sstevel@tonic-gate { "dir_update_fail", KSTAT_DATA_UINT64 },
2200Sstevel@tonic-gate { "dir_fini_purge", KSTAT_DATA_UINT64 },
2210Sstevel@tonic-gate { "dir_reclaim_last", KSTAT_DATA_UINT64 },
2220Sstevel@tonic-gate { "dir_reclaim_any", KSTAT_DATA_UINT64 },
2230Sstevel@tonic-gate };
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate static int doingcache = 1;
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate vnode_t negative_cache_vnode;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate * Insert entry at the front of the queue
2310Sstevel@tonic-gate */
2320Sstevel@tonic-gate #define nc_inshash(ncp, hp) \
2330Sstevel@tonic-gate { \
2340Sstevel@tonic-gate (ncp)->hash_next = (hp)->hash_next; \
2350Sstevel@tonic-gate (ncp)->hash_prev = (ncache_t *)(hp); \
2360Sstevel@tonic-gate (hp)->hash_next->hash_prev = (ncp); \
2370Sstevel@tonic-gate (hp)->hash_next = (ncp); \
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate * Remove entry from hash queue
2420Sstevel@tonic-gate */
2430Sstevel@tonic-gate #define nc_rmhash(ncp) \
2440Sstevel@tonic-gate { \
2450Sstevel@tonic-gate (ncp)->hash_prev->hash_next = (ncp)->hash_next; \
2460Sstevel@tonic-gate (ncp)->hash_next->hash_prev = (ncp)->hash_prev; \
2470Sstevel@tonic-gate (ncp)->hash_prev = NULL; \
2480Sstevel@tonic-gate (ncp)->hash_next = NULL; \
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate * Free an entry.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate #define dnlc_free(ncp) \
2550Sstevel@tonic-gate { \
2560Sstevel@tonic-gate kmem_free((ncp), sizeof (ncache_t) + (ncp)->namlen); \
2570Sstevel@tonic-gate atomic_add_32(&dnlc_nentries, -1); \
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate * Cached directory info.
2630Sstevel@tonic-gate * ======================
2640Sstevel@tonic-gate */
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate * Cached directory free space hash function.
2680Sstevel@tonic-gate * Needs the free space handle and the dcp to get the hash table size
2690Sstevel@tonic-gate * Returns the hash index.
2700Sstevel@tonic-gate */
2710Sstevel@tonic-gate #define DDFHASH(handle, dcp) ((handle >> 2) & (dcp)->dc_fhash_mask)
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate * Cached directory name entry hash function.
2750Sstevel@tonic-gate * Uses the name and returns in the input arguments the hash and the name
2760Sstevel@tonic-gate * length.
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate #define DNLC_DIR_HASH(name, hash, namelen) \
2790Sstevel@tonic-gate { \
280*12684STom.Erickson@Sun.COM char Xc; \
281*12684STom.Erickson@Sun.COM const char *Xcp; \
2820Sstevel@tonic-gate hash = *name; \
2830Sstevel@tonic-gate for (Xcp = (name + 1); (Xc = *Xcp) != 0; Xcp++) \
2840Sstevel@tonic-gate hash = (hash << 4) + hash + Xc; \
2850Sstevel@tonic-gate ASSERT((Xcp - (name)) <= ((1 << NBBY) - 1)); \
2860Sstevel@tonic-gate namelen = Xcp - (name); \
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate /* special dircache_t pointer to indicate error should be returned */
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * The anchor directory cache pointer can contain 3 types of values,
2920Sstevel@tonic-gate * 1) NULL: No directory cache
2930Sstevel@tonic-gate * 2) DC_RET_LOW_MEM (-1): There was a directory cache that found to be
2940Sstevel@tonic-gate * too big or a memory shortage occurred. This value remains in the
2950Sstevel@tonic-gate * pointer until a dnlc_dir_start() which returns the a DNOMEM error.
2960Sstevel@tonic-gate * This is kludgy but efficient and only visible in this source file.
2970Sstevel@tonic-gate * 3) A valid cache pointer.
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate #define DC_RET_LOW_MEM (dircache_t *)1
3000Sstevel@tonic-gate #define VALID_DIR_CACHE(dcp) ((dircache_t *)(dcp) > DC_RET_LOW_MEM)
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate /* Tunables */
3030Sstevel@tonic-gate uint_t dnlc_dir_enable = 1; /* disable caching directories by setting to 0 */
3040Sstevel@tonic-gate uint_t dnlc_dir_min_size = 40; /* min no of directory entries before caching */
3050Sstevel@tonic-gate uint_t dnlc_dir_max_size = UINT_MAX; /* ditto maximum */
3060Sstevel@tonic-gate uint_t dnlc_dir_hash_size_shift = 3; /* 8 entries per hash bucket */
3070Sstevel@tonic-gate uint_t dnlc_dir_min_reclaim = 350000; /* approx 1MB of dcentrys */
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate * dnlc_dir_hash_resize_shift determines when the hash tables
3100Sstevel@tonic-gate * get re-adjusted due to growth or shrinkage
3110Sstevel@tonic-gate * - currently 2 indicating that there can be at most 4
3120Sstevel@tonic-gate * times or at least one quarter the number of entries
3130Sstevel@tonic-gate * before hash table readjustment. Note that with
3140Sstevel@tonic-gate * dnlc_dir_hash_size_shift above set at 3 this would
3150Sstevel@tonic-gate * mean readjustment would occur if the average number
3160Sstevel@tonic-gate * of entries went above 32 or below 2
3170Sstevel@tonic-gate */
3180Sstevel@tonic-gate uint_t dnlc_dir_hash_resize_shift = 2; /* readjust rate */
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate static kmem_cache_t *dnlc_dir_space_cache; /* free space entry cache */
3210Sstevel@tonic-gate static dchead_t dc_head; /* anchor of cached directories */
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate /* Prototypes */
3240Sstevel@tonic-gate static ncache_t *dnlc_get(uchar_t namlen);
325*12684STom.Erickson@Sun.COM static ncache_t *dnlc_search(vnode_t *dp, const char *name, uchar_t namlen,
326*12684STom.Erickson@Sun.COM int hash);
3270Sstevel@tonic-gate static void dnlc_dir_reclaim(void *unused);
3280Sstevel@tonic-gate static void dnlc_dir_abort(dircache_t *dcp);
3290Sstevel@tonic-gate static void dnlc_dir_adjust_fhash(dircache_t *dcp);
3300Sstevel@tonic-gate static void dnlc_dir_adjust_nhash(dircache_t *dcp);
3311669Sperrin static void do_dnlc_reduce_cache(void *);
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate * Initialize the directory cache.
3360Sstevel@tonic-gate */
3370Sstevel@tonic-gate void
dnlc_init()3380Sstevel@tonic-gate dnlc_init()
3390Sstevel@tonic-gate {
3400Sstevel@tonic-gate nc_hash_t *hp;
3410Sstevel@tonic-gate kstat_t *ksp;
3420Sstevel@tonic-gate int i;
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate * Set up the size of the dnlc (ncsize) and its low water mark.
3460Sstevel@tonic-gate */
3470Sstevel@tonic-gate if (ncsize == -1) {
3480Sstevel@tonic-gate /* calculate a reasonable size for the low water */
3490Sstevel@tonic-gate dnlc_nentries_low_water = 4 * (v.v_proc + maxusers) + 320;
3500Sstevel@tonic-gate ncsize = dnlc_nentries_low_water +
3510Sstevel@tonic-gate (dnlc_nentries_low_water / dnlc_low_water_divisor);
3520Sstevel@tonic-gate } else {
3530Sstevel@tonic-gate /* don't change the user specified ncsize */
3540Sstevel@tonic-gate dnlc_nentries_low_water =
3550Sstevel@tonic-gate ncsize - (ncsize / dnlc_low_water_divisor);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate if (ncsize <= 0) {
3580Sstevel@tonic-gate doingcache = 0;
3590Sstevel@tonic-gate dnlc_dir_enable = 0; /* also disable directory caching */
3600Sstevel@tonic-gate ncsize = 0;
3610Sstevel@tonic-gate cmn_err(CE_NOTE, "name cache (dnlc) disabled");
3620Sstevel@tonic-gate return;
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate dnlc_max_nentries = ncsize * 2;
3651484Sek110237 ncsize_onepercent = ncsize / 100;
3661669Sperrin ncsize_min_percent = ncsize_onepercent * 3;
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate * Initialise the hash table.
3700Sstevel@tonic-gate * Compute hash size rounding to the next power of two.
3710Sstevel@tonic-gate */
3720Sstevel@tonic-gate nc_hashsz = ncsize / nc_hashavelen;
3730Sstevel@tonic-gate nc_hashsz = 1 << highbit(nc_hashsz);
3740Sstevel@tonic-gate nc_hashmask = nc_hashsz - 1;
3750Sstevel@tonic-gate nc_hash = kmem_zalloc(nc_hashsz * sizeof (*nc_hash), KM_SLEEP);
3760Sstevel@tonic-gate for (i = 0; i < nc_hashsz; i++) {
3770Sstevel@tonic-gate hp = (nc_hash_t *)&nc_hash[i];
3780Sstevel@tonic-gate mutex_init(&hp->hash_lock, NULL, MUTEX_DEFAULT, NULL);
3790Sstevel@tonic-gate hp->hash_next = (ncache_t *)hp;
3800Sstevel@tonic-gate hp->hash_prev = (ncache_t *)hp;
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate * Initialize rotors
3850Sstevel@tonic-gate */
3860Sstevel@tonic-gate dnlc_free_rotor = dnlc_purge_fs1_rotor = &nc_hash[0];
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate * Set up the directory caching to use kmem_cache_alloc
3900Sstevel@tonic-gate * for its free space entries so that we can get a callback
3910Sstevel@tonic-gate * when the system is short on memory, to allow us to free
3920Sstevel@tonic-gate * up some memory. we don't use the constructor/deconstructor
3930Sstevel@tonic-gate * functions.
3940Sstevel@tonic-gate */
3950Sstevel@tonic-gate dnlc_dir_space_cache = kmem_cache_create("dnlc_space_cache",
3960Sstevel@tonic-gate sizeof (dcfree_t), 0, NULL, NULL, dnlc_dir_reclaim, NULL,
3970Sstevel@tonic-gate NULL, 0);
3980Sstevel@tonic-gate
3990Sstevel@tonic-gate /*
4000Sstevel@tonic-gate * Initialise the head of the cached directory structures
4010Sstevel@tonic-gate */
4020Sstevel@tonic-gate mutex_init(&dc_head.dch_lock, NULL, MUTEX_DEFAULT, NULL);
4030Sstevel@tonic-gate dc_head.dch_next = (dircache_t *)&dc_head;
4040Sstevel@tonic-gate dc_head.dch_prev = (dircache_t *)&dc_head;
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate * Initialise the reference count of the negative cache vnode to 1
4080Sstevel@tonic-gate * so that it never goes away (VOP_INACTIVE isn't called on it).
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate negative_cache_vnode.v_count = 1;
4116712Stomee negative_cache_vnode.v_count_dnlc = 0;
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate * Initialise kstats - both the old compatability raw kind and
4150Sstevel@tonic-gate * the more extensive named stats.
4160Sstevel@tonic-gate */
4170Sstevel@tonic-gate ksp = kstat_create("unix", 0, "ncstats", "misc", KSTAT_TYPE_RAW,
4186712Stomee sizeof (struct ncstats), KSTAT_FLAG_VIRTUAL);
4190Sstevel@tonic-gate if (ksp) {
4200Sstevel@tonic-gate ksp->ks_data = (void *) &ncstats;
4210Sstevel@tonic-gate kstat_install(ksp);
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate ksp = kstat_create("unix", 0, "dnlcstats", "misc", KSTAT_TYPE_NAMED,
4240Sstevel@tonic-gate sizeof (ncs) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
4250Sstevel@tonic-gate if (ksp) {
4260Sstevel@tonic-gate ksp->ks_data = (void *) &ncs;
4270Sstevel@tonic-gate kstat_install(ksp);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate * Add a name to the directory cache.
4330Sstevel@tonic-gate */
4340Sstevel@tonic-gate void
dnlc_enter(vnode_t * dp,const char * name,vnode_t * vp)435*12684STom.Erickson@Sun.COM dnlc_enter(vnode_t *dp, const char *name, vnode_t *vp)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate ncache_t *ncp;
4380Sstevel@tonic-gate nc_hash_t *hp;
4390Sstevel@tonic-gate uchar_t namlen;
4400Sstevel@tonic-gate int hash;
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_enter_start:");
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate if (!doingcache) {
4450Sstevel@tonic-gate TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
4460Sstevel@tonic-gate "dnlc_enter_end:(%S) %d", "not caching", 0);
4470Sstevel@tonic-gate return;
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate * Get a new dnlc entry. Assume the entry won't be in the cache
4520Sstevel@tonic-gate * and initialize it now
4530Sstevel@tonic-gate */
4540Sstevel@tonic-gate DNLCHASH(name, dp, hash, namlen);
4550Sstevel@tonic-gate if ((ncp = dnlc_get(namlen)) == NULL)
4560Sstevel@tonic-gate return;
4570Sstevel@tonic-gate ncp->dp = dp;
4586712Stomee VN_HOLD_DNLC(dp);
4590Sstevel@tonic-gate ncp->vp = vp;
4606712Stomee VN_HOLD_DNLC(vp);
4610Sstevel@tonic-gate bcopy(name, ncp->name, namlen + 1); /* name and null */
4620Sstevel@tonic-gate ncp->hash = hash;
4630Sstevel@tonic-gate hp = &nc_hash[hash & nc_hashmask];
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate mutex_enter(&hp->hash_lock);
4660Sstevel@tonic-gate if (dnlc_search(dp, name, namlen, hash) != NULL) {
4670Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
4680Sstevel@tonic-gate ncstats.dbl_enters++;
4690Sstevel@tonic-gate ncs.ncs_dbl_enters.value.ui64++;
4706712Stomee VN_RELE_DNLC(dp);
4716712Stomee VN_RELE_DNLC(vp);
4720Sstevel@tonic-gate dnlc_free(ncp); /* crfree done here */
4730Sstevel@tonic-gate TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
4746712Stomee "dnlc_enter_end:(%S) %d", "dbl enter", ncstats.dbl_enters);
4750Sstevel@tonic-gate return;
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * Insert back into the hash chain.
4790Sstevel@tonic-gate */
4800Sstevel@tonic-gate nc_inshash(ncp, hp);
4810Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
4820Sstevel@tonic-gate ncstats.enters++;
4830Sstevel@tonic-gate ncs.ncs_enters.value.ui64++;
4840Sstevel@tonic-gate TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
4850Sstevel@tonic-gate "dnlc_enter_end:(%S) %d", "done", ncstats.enters);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate /*
4890Sstevel@tonic-gate * Add a name to the directory cache.
4900Sstevel@tonic-gate *
4910Sstevel@tonic-gate * This function is basically identical with
4920Sstevel@tonic-gate * dnlc_enter(). The difference is that when the
4930Sstevel@tonic-gate * desired dnlc entry is found, the vnode in the
4940Sstevel@tonic-gate * ncache is compared with the vnode passed in.
4950Sstevel@tonic-gate *
4960Sstevel@tonic-gate * If they are not equal then the ncache is
4970Sstevel@tonic-gate * updated with the passed in vnode. Otherwise
4980Sstevel@tonic-gate * it just frees up the newly allocated dnlc entry.
4990Sstevel@tonic-gate */
5000Sstevel@tonic-gate void
dnlc_update(vnode_t * dp,const char * name,vnode_t * vp)501*12684STom.Erickson@Sun.COM dnlc_update(vnode_t *dp, const char *name, vnode_t *vp)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate ncache_t *ncp;
5040Sstevel@tonic-gate ncache_t *tcp;
5050Sstevel@tonic-gate vnode_t *tvp;
5060Sstevel@tonic-gate nc_hash_t *hp;
5070Sstevel@tonic-gate int hash;
5080Sstevel@tonic-gate uchar_t namlen;
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_update_start:");
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate if (!doingcache) {
5130Sstevel@tonic-gate TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
5140Sstevel@tonic-gate "dnlc_update_end:(%S) %d", "not caching", 0);
5150Sstevel@tonic-gate return;
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate /*
5190Sstevel@tonic-gate * Get a new dnlc entry and initialize it now.
5200Sstevel@tonic-gate * If we fail to get a new entry, call dnlc_remove() to purge
5210Sstevel@tonic-gate * any existing dnlc entry including negative cache (DNLC_NO_VNODE)
5220Sstevel@tonic-gate * entry.
5230Sstevel@tonic-gate * Failure to clear an existing entry could result in false dnlc
5240Sstevel@tonic-gate * lookup (negative/stale entry).
5250Sstevel@tonic-gate */
5260Sstevel@tonic-gate DNLCHASH(name, dp, hash, namlen);
5270Sstevel@tonic-gate if ((ncp = dnlc_get(namlen)) == NULL) {
5280Sstevel@tonic-gate dnlc_remove(dp, name);
5290Sstevel@tonic-gate return;
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate ncp->dp = dp;
5326712Stomee VN_HOLD_DNLC(dp);
5330Sstevel@tonic-gate ncp->vp = vp;
5346712Stomee VN_HOLD_DNLC(vp);
5350Sstevel@tonic-gate bcopy(name, ncp->name, namlen + 1); /* name and null */
5360Sstevel@tonic-gate ncp->hash = hash;
5370Sstevel@tonic-gate hp = &nc_hash[hash & nc_hashmask];
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate mutex_enter(&hp->hash_lock);
5400Sstevel@tonic-gate if ((tcp = dnlc_search(dp, name, namlen, hash)) != NULL) {
5410Sstevel@tonic-gate if (tcp->vp != vp) {
5420Sstevel@tonic-gate tvp = tcp->vp;
5430Sstevel@tonic-gate tcp->vp = vp;
5440Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
5456712Stomee VN_RELE_DNLC(tvp);
5460Sstevel@tonic-gate ncstats.enters++;
5470Sstevel@tonic-gate ncs.ncs_enters.value.ui64++;
5480Sstevel@tonic-gate TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
5490Sstevel@tonic-gate "dnlc_update_end:(%S) %d", "done", ncstats.enters);
5500Sstevel@tonic-gate } else {
5510Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
5526712Stomee VN_RELE_DNLC(vp);
5530Sstevel@tonic-gate ncstats.dbl_enters++;
5540Sstevel@tonic-gate ncs.ncs_dbl_enters.value.ui64++;
5550Sstevel@tonic-gate TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
5560Sstevel@tonic-gate "dnlc_update_end:(%S) %d",
5570Sstevel@tonic-gate "dbl enter", ncstats.dbl_enters);
5580Sstevel@tonic-gate }
5596712Stomee VN_RELE_DNLC(dp);
5600Sstevel@tonic-gate dnlc_free(ncp); /* crfree done here */
5610Sstevel@tonic-gate return;
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate /*
5640Sstevel@tonic-gate * insert the new entry, since it is not in dnlc yet
5650Sstevel@tonic-gate */
5660Sstevel@tonic-gate nc_inshash(ncp, hp);
5670Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
5680Sstevel@tonic-gate ncstats.enters++;
5690Sstevel@tonic-gate ncs.ncs_enters.value.ui64++;
5700Sstevel@tonic-gate TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
5710Sstevel@tonic-gate "dnlc_update_end:(%S) %d", "done", ncstats.enters);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate /*
5750Sstevel@tonic-gate * Look up a name in the directory name cache.
5760Sstevel@tonic-gate *
5770Sstevel@tonic-gate * Return a doubly-held vnode if found: one hold so that it may
5780Sstevel@tonic-gate * remain in the cache for other users, the other hold so that
5790Sstevel@tonic-gate * the cache is not re-cycled and the identity of the vnode is
5800Sstevel@tonic-gate * lost before the caller can use the vnode.
5810Sstevel@tonic-gate */
5820Sstevel@tonic-gate vnode_t *
dnlc_lookup(vnode_t * dp,const char * name)583*12684STom.Erickson@Sun.COM dnlc_lookup(vnode_t *dp, const char *name)
5840Sstevel@tonic-gate {
5850Sstevel@tonic-gate ncache_t *ncp;
5860Sstevel@tonic-gate nc_hash_t *hp;
5870Sstevel@tonic-gate vnode_t *vp;
5880Sstevel@tonic-gate int hash, depth;
5890Sstevel@tonic-gate uchar_t namlen;
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate TRACE_2(TR_FAC_NFS, TR_DNLC_LOOKUP_START,
5920Sstevel@tonic-gate "dnlc_lookup_start:dp %x name %s", dp, name);
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate if (!doingcache) {
5950Sstevel@tonic-gate TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
5960Sstevel@tonic-gate "dnlc_lookup_end:%S %d vp %x name %s",
5970Sstevel@tonic-gate "not_caching", 0, NULL, name);
5980Sstevel@tonic-gate return (NULL);
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate DNLCHASH(name, dp, hash, namlen);
6020Sstevel@tonic-gate depth = 1;
6030Sstevel@tonic-gate hp = &nc_hash[hash & nc_hashmask];
6040Sstevel@tonic-gate mutex_enter(&hp->hash_lock);
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate for (ncp = hp->hash_next; ncp != (ncache_t *)hp;
6070Sstevel@tonic-gate ncp = ncp->hash_next) {
6080Sstevel@tonic-gate if (ncp->hash == hash && /* fast signature check */
6090Sstevel@tonic-gate ncp->dp == dp &&
6100Sstevel@tonic-gate ncp->namlen == namlen &&
6110Sstevel@tonic-gate bcmp(ncp->name, name, namlen) == 0) {
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate * Move this entry to the head of its hash chain
6140Sstevel@tonic-gate * if it's not already close.
6150Sstevel@tonic-gate */
6160Sstevel@tonic-gate if (depth > NC_MOVETOFRONT) {
6170Sstevel@tonic-gate ncache_t *next = ncp->hash_next;
6180Sstevel@tonic-gate ncache_t *prev = ncp->hash_prev;
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate prev->hash_next = next;
6210Sstevel@tonic-gate next->hash_prev = prev;
6220Sstevel@tonic-gate ncp->hash_next = next = hp->hash_next;
6230Sstevel@tonic-gate ncp->hash_prev = (ncache_t *)hp;
6240Sstevel@tonic-gate next->hash_prev = ncp;
6250Sstevel@tonic-gate hp->hash_next = ncp;
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate ncstats.move_to_front++;
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate * Put a hold on the vnode now so its identity
6320Sstevel@tonic-gate * can't change before the caller has a chance to
6330Sstevel@tonic-gate * put a hold on it.
6340Sstevel@tonic-gate */
6350Sstevel@tonic-gate vp = ncp->vp;
6366712Stomee VN_HOLD_CALLER(vp); /* VN_HOLD 1 of 2 in this file */
6370Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
6380Sstevel@tonic-gate ncstats.hits++;
6390Sstevel@tonic-gate ncs.ncs_hits.value.ui64++;
6400Sstevel@tonic-gate if (vp == DNLC_NO_VNODE) {
6410Sstevel@tonic-gate ncs.ncs_neg_hits.value.ui64++;
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
6446712Stomee "dnlc_lookup_end:%S %d vp %x name %s", "hit",
6456712Stomee ncstats.hits, vp, name);
6460Sstevel@tonic-gate return (vp);
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate depth++;
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
6520Sstevel@tonic-gate ncstats.misses++;
6530Sstevel@tonic-gate ncs.ncs_misses.value.ui64++;
6540Sstevel@tonic-gate TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
6556712Stomee "dnlc_lookup_end:%S %d vp %x name %s", "miss", ncstats.misses,
6560Sstevel@tonic-gate NULL, name);
6570Sstevel@tonic-gate return (NULL);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate * Remove an entry in the directory name cache.
6620Sstevel@tonic-gate */
6630Sstevel@tonic-gate void
dnlc_remove(vnode_t * dp,const char * name)664*12684STom.Erickson@Sun.COM dnlc_remove(vnode_t *dp, const char *name)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate ncache_t *ncp;
6670Sstevel@tonic-gate nc_hash_t *hp;
6680Sstevel@tonic-gate uchar_t namlen;
6690Sstevel@tonic-gate int hash;
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate if (!doingcache)
6720Sstevel@tonic-gate return;
6730Sstevel@tonic-gate DNLCHASH(name, dp, hash, namlen);
6740Sstevel@tonic-gate hp = &nc_hash[hash & nc_hashmask];
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate mutex_enter(&hp->hash_lock);
6770Sstevel@tonic-gate if (ncp = dnlc_search(dp, name, namlen, hash)) {
6780Sstevel@tonic-gate /*
6790Sstevel@tonic-gate * Free up the entry
6800Sstevel@tonic-gate */
6810Sstevel@tonic-gate nc_rmhash(ncp);
6820Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
6836712Stomee VN_RELE_DNLC(ncp->vp);
6846712Stomee VN_RELE_DNLC(ncp->dp);
6850Sstevel@tonic-gate dnlc_free(ncp);
6860Sstevel@tonic-gate return;
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate /*
6920Sstevel@tonic-gate * Purge the entire cache.
6930Sstevel@tonic-gate */
6940Sstevel@tonic-gate void
dnlc_purge()6950Sstevel@tonic-gate dnlc_purge()
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate nc_hash_t *nch;
6980Sstevel@tonic-gate ncache_t *ncp;
6990Sstevel@tonic-gate int index;
7000Sstevel@tonic-gate int i;
7010Sstevel@tonic-gate vnode_t *nc_rele[DNLC_MAX_RELE];
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate if (!doingcache)
7040Sstevel@tonic-gate return;
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate ncstats.purges++;
7070Sstevel@tonic-gate ncs.ncs_purge_all.value.ui64++;
7080Sstevel@tonic-gate
7090Sstevel@tonic-gate for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
7100Sstevel@tonic-gate index = 0;
7110Sstevel@tonic-gate mutex_enter(&nch->hash_lock);
7120Sstevel@tonic-gate ncp = nch->hash_next;
7130Sstevel@tonic-gate while (ncp != (ncache_t *)nch) {
7140Sstevel@tonic-gate ncache_t *np;
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate np = ncp->hash_next;
7170Sstevel@tonic-gate nc_rele[index++] = ncp->vp;
7180Sstevel@tonic-gate nc_rele[index++] = ncp->dp;
7190Sstevel@tonic-gate
7200Sstevel@tonic-gate nc_rmhash(ncp);
7210Sstevel@tonic-gate dnlc_free(ncp);
7220Sstevel@tonic-gate ncp = np;
7230Sstevel@tonic-gate ncs.ncs_purge_total.value.ui64++;
7240Sstevel@tonic-gate if (index == DNLC_MAX_RELE)
7250Sstevel@tonic-gate break;
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate mutex_exit(&nch->hash_lock);
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate /* Release holds on all the vnodes now that we have no locks */
7300Sstevel@tonic-gate for (i = 0; i < index; i++) {
7316712Stomee VN_RELE_DNLC(nc_rele[i]);
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate if (ncp != (ncache_t *)nch) {
7340Sstevel@tonic-gate nch--; /* Do current hash chain again */
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate }
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate /*
7406712Stomee * Purge any cache entries referencing a vnode. Exit as soon as the dnlc
7416712Stomee * reference count goes to zero (the caller still holds a reference).
7420Sstevel@tonic-gate */
7430Sstevel@tonic-gate void
dnlc_purge_vp(vnode_t * vp)7440Sstevel@tonic-gate dnlc_purge_vp(vnode_t *vp)
7450Sstevel@tonic-gate {
7460Sstevel@tonic-gate nc_hash_t *nch;
7470Sstevel@tonic-gate ncache_t *ncp;
7480Sstevel@tonic-gate int index;
7490Sstevel@tonic-gate vnode_t *nc_rele[DNLC_MAX_RELE];
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate ASSERT(vp->v_count > 0);
7526712Stomee if (vp->v_count_dnlc == 0) {
7530Sstevel@tonic-gate return;
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate if (!doingcache)
7570Sstevel@tonic-gate return;
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate ncstats.purges++;
7600Sstevel@tonic-gate ncs.ncs_purge_vp.value.ui64++;
7610Sstevel@tonic-gate
7620Sstevel@tonic-gate for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
7630Sstevel@tonic-gate index = 0;
7640Sstevel@tonic-gate mutex_enter(&nch->hash_lock);
7650Sstevel@tonic-gate ncp = nch->hash_next;
7660Sstevel@tonic-gate while (ncp != (ncache_t *)nch) {
7670Sstevel@tonic-gate ncache_t *np;
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate np = ncp->hash_next;
7700Sstevel@tonic-gate if (ncp->dp == vp || ncp->vp == vp) {
7710Sstevel@tonic-gate nc_rele[index++] = ncp->vp;
7720Sstevel@tonic-gate nc_rele[index++] = ncp->dp;
7730Sstevel@tonic-gate nc_rmhash(ncp);
7740Sstevel@tonic-gate dnlc_free(ncp);
7750Sstevel@tonic-gate ncs.ncs_purge_total.value.ui64++;
7760Sstevel@tonic-gate if (index == DNLC_MAX_RELE) {
7770Sstevel@tonic-gate ncp = np;
7780Sstevel@tonic-gate break;
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate ncp = np;
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate mutex_exit(&nch->hash_lock);
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate /* Release holds on all the vnodes now that we have no locks */
7861257Sah89892 while (index) {
7876712Stomee VN_RELE_DNLC(nc_rele[--index]);
7881257Sah89892 }
7890Sstevel@tonic-gate
7906712Stomee if (vp->v_count_dnlc == 0) {
7916712Stomee return;
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate if (ncp != (ncache_t *)nch) {
7950Sstevel@tonic-gate nch--; /* Do current hash chain again */
7960Sstevel@tonic-gate }
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate }
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate /*
8010Sstevel@tonic-gate * Purge cache entries referencing a vfsp. Caller supplies a count
8020Sstevel@tonic-gate * of entries to purge; up to that many will be freed. A count of
8030Sstevel@tonic-gate * zero indicates that all such entries should be purged. Returns
8040Sstevel@tonic-gate * the number of entries that were purged.
8050Sstevel@tonic-gate */
8060Sstevel@tonic-gate int
dnlc_purge_vfsp(vfs_t * vfsp,int count)8070Sstevel@tonic-gate dnlc_purge_vfsp(vfs_t *vfsp, int count)
8080Sstevel@tonic-gate {
8090Sstevel@tonic-gate nc_hash_t *nch;
8100Sstevel@tonic-gate ncache_t *ncp;
8110Sstevel@tonic-gate int n = 0;
8120Sstevel@tonic-gate int index;
8130Sstevel@tonic-gate int i;
8140Sstevel@tonic-gate vnode_t *nc_rele[DNLC_MAX_RELE];
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate if (!doingcache)
8170Sstevel@tonic-gate return (0);
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate ncstats.purges++;
8200Sstevel@tonic-gate ncs.ncs_purge_vfs.value.ui64++;
8210Sstevel@tonic-gate
8220Sstevel@tonic-gate for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
8230Sstevel@tonic-gate index = 0;
8240Sstevel@tonic-gate mutex_enter(&nch->hash_lock);
8250Sstevel@tonic-gate ncp = nch->hash_next;
8260Sstevel@tonic-gate while (ncp != (ncache_t *)nch) {
8270Sstevel@tonic-gate ncache_t *np;
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate np = ncp->hash_next;
8300Sstevel@tonic-gate ASSERT(ncp->dp != NULL);
8310Sstevel@tonic-gate ASSERT(ncp->vp != NULL);
8320Sstevel@tonic-gate if ((ncp->dp->v_vfsp == vfsp) ||
8330Sstevel@tonic-gate (ncp->vp->v_vfsp == vfsp)) {
8340Sstevel@tonic-gate n++;
8350Sstevel@tonic-gate nc_rele[index++] = ncp->vp;
8360Sstevel@tonic-gate nc_rele[index++] = ncp->dp;
8370Sstevel@tonic-gate nc_rmhash(ncp);
8380Sstevel@tonic-gate dnlc_free(ncp);
8390Sstevel@tonic-gate ncs.ncs_purge_total.value.ui64++;
8400Sstevel@tonic-gate if (index == DNLC_MAX_RELE) {
8410Sstevel@tonic-gate ncp = np;
8420Sstevel@tonic-gate break;
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate if (count != 0 && n >= count) {
8450Sstevel@tonic-gate break;
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate ncp = np;
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate mutex_exit(&nch->hash_lock);
8510Sstevel@tonic-gate /* Release holds on all the vnodes now that we have no locks */
8520Sstevel@tonic-gate for (i = 0; i < index; i++) {
8536712Stomee VN_RELE_DNLC(nc_rele[i]);
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate if (count != 0 && n >= count) {
8560Sstevel@tonic-gate return (n);
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate if (ncp != (ncache_t *)nch) {
8590Sstevel@tonic-gate nch--; /* Do current hash chain again */
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate return (n);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate * Purge 1 entry from the dnlc that is part of the filesystem(s)
8670Sstevel@tonic-gate * represented by 'vop'. The purpose of this routine is to allow
8680Sstevel@tonic-gate * users of the dnlc to free a vnode that is being held by the dnlc.
8690Sstevel@tonic-gate *
8700Sstevel@tonic-gate * If we find a vnode that we release which will result in
8710Sstevel@tonic-gate * freeing the underlying vnode (count was 1), return 1, 0
8720Sstevel@tonic-gate * if no appropriate vnodes found.
8730Sstevel@tonic-gate *
8740Sstevel@tonic-gate * Note, vop is not the 'right' identifier for a filesystem.
8750Sstevel@tonic-gate */
8760Sstevel@tonic-gate int
dnlc_fs_purge1(vnodeops_t * vop)8770Sstevel@tonic-gate dnlc_fs_purge1(vnodeops_t *vop)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate nc_hash_t *end;
8800Sstevel@tonic-gate nc_hash_t *hp;
8810Sstevel@tonic-gate ncache_t *ncp;
8820Sstevel@tonic-gate vnode_t *vp;
8830Sstevel@tonic-gate
8840Sstevel@tonic-gate if (!doingcache)
8850Sstevel@tonic-gate return (0);
8860Sstevel@tonic-gate
8870Sstevel@tonic-gate ncs.ncs_purge_fs1.value.ui64++;
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate /*
8900Sstevel@tonic-gate * Scan the dnlc entries looking for a likely candidate.
8910Sstevel@tonic-gate */
8920Sstevel@tonic-gate hp = end = dnlc_purge_fs1_rotor;
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate do {
8950Sstevel@tonic-gate if (++hp == &nc_hash[nc_hashsz])
8960Sstevel@tonic-gate hp = nc_hash;
8970Sstevel@tonic-gate dnlc_purge_fs1_rotor = hp;
8980Sstevel@tonic-gate if (hp->hash_next == (ncache_t *)hp)
8990Sstevel@tonic-gate continue;
9000Sstevel@tonic-gate mutex_enter(&hp->hash_lock);
9010Sstevel@tonic-gate for (ncp = hp->hash_prev;
9020Sstevel@tonic-gate ncp != (ncache_t *)hp;
9030Sstevel@tonic-gate ncp = ncp->hash_prev) {
9040Sstevel@tonic-gate vp = ncp->vp;
9050Sstevel@tonic-gate if (!vn_has_cached_data(vp) && (vp->v_count == 1) &&
9060Sstevel@tonic-gate vn_matchops(vp, vop))
9070Sstevel@tonic-gate break;
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate if (ncp != (ncache_t *)hp) {
9100Sstevel@tonic-gate nc_rmhash(ncp);
9110Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
9126712Stomee VN_RELE_DNLC(ncp->dp);
9136712Stomee VN_RELE_DNLC(vp)
9140Sstevel@tonic-gate dnlc_free(ncp);
9150Sstevel@tonic-gate ncs.ncs_purge_total.value.ui64++;
9160Sstevel@tonic-gate return (1);
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
9190Sstevel@tonic-gate } while (hp != end);
9200Sstevel@tonic-gate return (0);
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate /*
9240Sstevel@tonic-gate * Perform a reverse lookup in the DNLC. This will find the first occurrence of
9250Sstevel@tonic-gate * the vnode. If successful, it will return the vnode of the parent, and the
9260Sstevel@tonic-gate * name of the entry in the given buffer. If it cannot be found, or the buffer
9270Sstevel@tonic-gate * is too small, then it will return NULL. Note that this is a highly
9280Sstevel@tonic-gate * inefficient function, since the DNLC is constructed solely for forward
9290Sstevel@tonic-gate * lookups.
9300Sstevel@tonic-gate */
9310Sstevel@tonic-gate vnode_t *
dnlc_reverse_lookup(vnode_t * vp,char * buf,size_t buflen)9320Sstevel@tonic-gate dnlc_reverse_lookup(vnode_t *vp, char *buf, size_t buflen)
9330Sstevel@tonic-gate {
9340Sstevel@tonic-gate nc_hash_t *nch;
9350Sstevel@tonic-gate ncache_t *ncp;
9360Sstevel@tonic-gate vnode_t *pvp;
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate if (!doingcache)
9390Sstevel@tonic-gate return (NULL);
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
9420Sstevel@tonic-gate mutex_enter(&nch->hash_lock);
9430Sstevel@tonic-gate ncp = nch->hash_next;
9440Sstevel@tonic-gate while (ncp != (ncache_t *)nch) {
9450Sstevel@tonic-gate /*
9460Sstevel@tonic-gate * We ignore '..' entries since it can create
9470Sstevel@tonic-gate * confusion and infinite loops.
9480Sstevel@tonic-gate */
9490Sstevel@tonic-gate if (ncp->vp == vp && !(ncp->namlen == 2 &&
9500Sstevel@tonic-gate 0 == bcmp(ncp->name, "..", 2)) &&
9510Sstevel@tonic-gate ncp->namlen < buflen) {
9520Sstevel@tonic-gate bcopy(ncp->name, buf, ncp->namlen);
9530Sstevel@tonic-gate buf[ncp->namlen] = '\0';
9540Sstevel@tonic-gate pvp = ncp->dp;
9556712Stomee /* VN_HOLD 2 of 2 in this file */
9566712Stomee VN_HOLD_CALLER(pvp);
9570Sstevel@tonic-gate mutex_exit(&nch->hash_lock);
9580Sstevel@tonic-gate return (pvp);
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate ncp = ncp->hash_next;
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate mutex_exit(&nch->hash_lock);
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate
9650Sstevel@tonic-gate return (NULL);
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate /*
9680Sstevel@tonic-gate * Utility routine to search for a cache entry. Return the
9690Sstevel@tonic-gate * ncache entry if found, NULL otherwise.
9700Sstevel@tonic-gate */
9710Sstevel@tonic-gate static ncache_t *
dnlc_search(vnode_t * dp,const char * name,uchar_t namlen,int hash)972*12684STom.Erickson@Sun.COM dnlc_search(vnode_t *dp, const char *name, uchar_t namlen, int hash)
9730Sstevel@tonic-gate {
9740Sstevel@tonic-gate nc_hash_t *hp;
9750Sstevel@tonic-gate ncache_t *ncp;
9760Sstevel@tonic-gate
9770Sstevel@tonic-gate hp = &nc_hash[hash & nc_hashmask];
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate for (ncp = hp->hash_next; ncp != (ncache_t *)hp; ncp = ncp->hash_next) {
9800Sstevel@tonic-gate if (ncp->hash == hash &&
9810Sstevel@tonic-gate ncp->dp == dp &&
9820Sstevel@tonic-gate ncp->namlen == namlen &&
9830Sstevel@tonic-gate bcmp(ncp->name, name, namlen) == 0)
9840Sstevel@tonic-gate return (ncp);
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate return (NULL);
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate
9890Sstevel@tonic-gate #if ((1 << NBBY) - 1) < (MAXNAMELEN - 1)
9900Sstevel@tonic-gate #error ncache_t name length representation is too small
9910Sstevel@tonic-gate #endif
9920Sstevel@tonic-gate
9931669Sperrin void
dnlc_reduce_cache(void * reduce_percent)9941669Sperrin dnlc_reduce_cache(void *reduce_percent)
9951669Sperrin {
9961669Sperrin if (dnlc_reduce_idle && (dnlc_nentries >= ncsize || reduce_percent)) {
9971669Sperrin dnlc_reduce_idle = 0;
9981669Sperrin if ((taskq_dispatch(system_taskq, do_dnlc_reduce_cache,
9991669Sperrin reduce_percent, TQ_NOSLEEP)) == NULL)
10001669Sperrin dnlc_reduce_idle = 1;
10011669Sperrin }
10021669Sperrin }
10031669Sperrin
10040Sstevel@tonic-gate /*
10050Sstevel@tonic-gate * Get a new name cache entry.
10060Sstevel@tonic-gate * If the dnlc_reduce_cache() taskq isn't keeping up with demand, or memory
10070Sstevel@tonic-gate * is short then just return NULL. If we're over ncsize then kick off a
10080Sstevel@tonic-gate * thread to free some in use entries down to dnlc_nentries_low_water.
10090Sstevel@tonic-gate * Caller must initialise all fields except namlen.
10100Sstevel@tonic-gate * Component names are defined to be less than MAXNAMELEN
10110Sstevel@tonic-gate * which includes a null.
10120Sstevel@tonic-gate */
10130Sstevel@tonic-gate static ncache_t *
dnlc_get(uchar_t namlen)10140Sstevel@tonic-gate dnlc_get(uchar_t namlen)
10150Sstevel@tonic-gate {
10160Sstevel@tonic-gate ncache_t *ncp;
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate if (dnlc_nentries > dnlc_max_nentries) {
10190Sstevel@tonic-gate dnlc_max_nentries_cnt++; /* keep a statistic */
10200Sstevel@tonic-gate return (NULL);
10210Sstevel@tonic-gate }
10220Sstevel@tonic-gate ncp = kmem_alloc(sizeof (ncache_t) + namlen, KM_NOSLEEP);
10230Sstevel@tonic-gate if (ncp == NULL) {
10240Sstevel@tonic-gate return (NULL);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate ncp->namlen = namlen;
10270Sstevel@tonic-gate atomic_add_32(&dnlc_nentries, 1);
10281669Sperrin dnlc_reduce_cache(NULL);
10290Sstevel@tonic-gate return (ncp);
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate /*
10330Sstevel@tonic-gate * Taskq routine to free up name cache entries to reduce the
10341484Sek110237 * cache size to the low water mark if "reduce_percent" is not provided.
10351484Sek110237 * If "reduce_percent" is provided, reduce cache size by
10361484Sek110237 * (ncsize_onepercent * reduce_percent).
10370Sstevel@tonic-gate */
10380Sstevel@tonic-gate /*ARGSUSED*/
10391669Sperrin static void
do_dnlc_reduce_cache(void * reduce_percent)10401669Sperrin do_dnlc_reduce_cache(void *reduce_percent)
10410Sstevel@tonic-gate {
10421669Sperrin nc_hash_t *hp = dnlc_free_rotor, *start_hp = hp;
10430Sstevel@tonic-gate vnode_t *vp;
10440Sstevel@tonic-gate ncache_t *ncp;
10450Sstevel@tonic-gate int cnt;
10461484Sek110237 uint_t low_water = dnlc_nentries_low_water;
10471484Sek110237
10481484Sek110237 if (reduce_percent) {
10491484Sek110237 uint_t reduce_cnt;
10501484Sek110237
10511669Sperrin /*
10521669Sperrin * Never try to reduce the current number
10531669Sperrin * of cache entries below 3% of ncsize.
10541669Sperrin */
10551669Sperrin if (dnlc_nentries <= ncsize_min_percent) {
10561669Sperrin dnlc_reduce_idle = 1;
10571669Sperrin return;
10581669Sperrin }
10591505Sek110237 reduce_cnt = ncsize_onepercent *
10601505Sek110237 (uint_t)(uintptr_t)reduce_percent;
10611669Sperrin
10621669Sperrin if (reduce_cnt > dnlc_nentries ||
10631669Sperrin dnlc_nentries - reduce_cnt < ncsize_min_percent)
10641669Sperrin low_water = ncsize_min_percent;
10651484Sek110237 else
10661484Sek110237 low_water = dnlc_nentries - reduce_cnt;
10671484Sek110237 }
10680Sstevel@tonic-gate
10690Sstevel@tonic-gate do {
10700Sstevel@tonic-gate /*
10711669Sperrin * Find the first non empty hash queue without locking.
10721669Sperrin * Only look at each hash queue once to avoid an infinite loop.
10730Sstevel@tonic-gate */
10740Sstevel@tonic-gate do {
10751669Sperrin if (++hp == &nc_hash[nc_hashsz])
10760Sstevel@tonic-gate hp = nc_hash;
10771669Sperrin } while (hp->hash_next == (ncache_t *)hp && hp != start_hp);
10781669Sperrin
10791669Sperrin /* return if all hash queues are empty. */
10801669Sperrin if (hp->hash_next == (ncache_t *)hp) {
10811669Sperrin dnlc_reduce_idle = 1;
10821669Sperrin return;
10831669Sperrin }
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate mutex_enter(&hp->hash_lock);
10860Sstevel@tonic-gate for (cnt = 0, ncp = hp->hash_prev; ncp != (ncache_t *)hp;
10870Sstevel@tonic-gate ncp = ncp->hash_prev, cnt++) {
10880Sstevel@tonic-gate vp = ncp->vp;
10890Sstevel@tonic-gate /*
10900Sstevel@tonic-gate * A name cache entry with a reference count
10910Sstevel@tonic-gate * of one is only referenced by the dnlc.
10920Sstevel@tonic-gate * Also negative cache entries are purged first.
10930Sstevel@tonic-gate */
10940Sstevel@tonic-gate if (!vn_has_cached_data(vp) &&
10950Sstevel@tonic-gate ((vp->v_count == 1) || (vp == DNLC_NO_VNODE))) {
10960Sstevel@tonic-gate ncs.ncs_pick_heur.value.ui64++;
10970Sstevel@tonic-gate goto found;
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate /*
11000Sstevel@tonic-gate * Remove from the end of the chain if the
11010Sstevel@tonic-gate * chain is too long
11020Sstevel@tonic-gate */
11030Sstevel@tonic-gate if (cnt > dnlc_long_chain) {
11040Sstevel@tonic-gate ncp = hp->hash_prev;
11050Sstevel@tonic-gate ncs.ncs_pick_last.value.ui64++;
11060Sstevel@tonic-gate vp = ncp->vp;
11070Sstevel@tonic-gate goto found;
11080Sstevel@tonic-gate }
11090Sstevel@tonic-gate }
11100Sstevel@tonic-gate /* check for race and continue */
11110Sstevel@tonic-gate if (hp->hash_next == (ncache_t *)hp) {
11120Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
11130Sstevel@tonic-gate continue;
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate
11160Sstevel@tonic-gate ncp = hp->hash_prev; /* pick the last one in the hash queue */
11170Sstevel@tonic-gate ncs.ncs_pick_last.value.ui64++;
11180Sstevel@tonic-gate vp = ncp->vp;
11190Sstevel@tonic-gate found:
11200Sstevel@tonic-gate /*
11210Sstevel@tonic-gate * Remove from hash chain.
11220Sstevel@tonic-gate */
11230Sstevel@tonic-gate nc_rmhash(ncp);
11240Sstevel@tonic-gate mutex_exit(&hp->hash_lock);
11256712Stomee VN_RELE_DNLC(vp);
11266712Stomee VN_RELE_DNLC(ncp->dp);
11270Sstevel@tonic-gate dnlc_free(ncp);
11281484Sek110237 } while (dnlc_nentries > low_water);
11290Sstevel@tonic-gate
11300Sstevel@tonic-gate dnlc_free_rotor = hp;
11310Sstevel@tonic-gate dnlc_reduce_idle = 1;
11320Sstevel@tonic-gate }
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate /*
11350Sstevel@tonic-gate * Directory caching routines
11360Sstevel@tonic-gate * ==========================
11370Sstevel@tonic-gate *
11380Sstevel@tonic-gate * See dnlc.h for details of the interfaces below.
11390Sstevel@tonic-gate */
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate /*
11420Sstevel@tonic-gate * Lookup up an entry in a complete or partial directory cache.
11430Sstevel@tonic-gate */
11440Sstevel@tonic-gate dcret_t
dnlc_dir_lookup(dcanchor_t * dcap,const char * name,uint64_t * handle)1145*12684STom.Erickson@Sun.COM dnlc_dir_lookup(dcanchor_t *dcap, const char *name, uint64_t *handle)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate dircache_t *dcp;
11480Sstevel@tonic-gate dcentry_t *dep;
11490Sstevel@tonic-gate int hash;
11500Sstevel@tonic-gate int ret;
11510Sstevel@tonic-gate uchar_t namlen;
11520Sstevel@tonic-gate
11530Sstevel@tonic-gate /*
11540Sstevel@tonic-gate * can test without lock as we are only a cache
11550Sstevel@tonic-gate */
11560Sstevel@tonic-gate if (!VALID_DIR_CACHE(dcap->dca_dircache)) {
11570Sstevel@tonic-gate ncs.ncs_dir_misses.value.ui64++;
11580Sstevel@tonic-gate return (DNOCACHE);
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate
11610Sstevel@tonic-gate if (!dnlc_dir_enable) {
11620Sstevel@tonic-gate return (DNOCACHE);
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate
11650Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
11660Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
11670Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
116811066Srafael.vanoni@sun.com dcp->dc_actime = ddi_get_lbolt64();
11690Sstevel@tonic-gate DNLC_DIR_HASH(name, hash, namlen);
11700Sstevel@tonic-gate dep = dcp->dc_namehash[hash & dcp->dc_nhash_mask];
11710Sstevel@tonic-gate while (dep != NULL) {
11720Sstevel@tonic-gate if ((dep->de_hash == hash) &&
11730Sstevel@tonic-gate (namlen == dep->de_namelen) &&
11740Sstevel@tonic-gate bcmp(dep->de_name, name, namlen) == 0) {
11750Sstevel@tonic-gate *handle = dep->de_handle;
11760Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
11770Sstevel@tonic-gate ncs.ncs_dir_hits.value.ui64++;
11780Sstevel@tonic-gate return (DFOUND);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate dep = dep->de_next;
11810Sstevel@tonic-gate }
11820Sstevel@tonic-gate if (dcp->dc_complete) {
11830Sstevel@tonic-gate ret = DNOENT;
11840Sstevel@tonic-gate } else {
11850Sstevel@tonic-gate ret = DNOCACHE;
11860Sstevel@tonic-gate }
11870Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
11880Sstevel@tonic-gate return (ret);
11890Sstevel@tonic-gate } else {
11900Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
11910Sstevel@tonic-gate ncs.ncs_dir_misses.value.ui64++;
11920Sstevel@tonic-gate return (DNOCACHE);
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate }
11950Sstevel@tonic-gate
11960Sstevel@tonic-gate /*
11970Sstevel@tonic-gate * Start a new directory cache. An estimate of the number of
11980Sstevel@tonic-gate * entries is provided to as a quick check to ensure the directory
11990Sstevel@tonic-gate * is cacheable.
12000Sstevel@tonic-gate */
12010Sstevel@tonic-gate dcret_t
dnlc_dir_start(dcanchor_t * dcap,uint_t num_entries)12020Sstevel@tonic-gate dnlc_dir_start(dcanchor_t *dcap, uint_t num_entries)
12030Sstevel@tonic-gate {
12040Sstevel@tonic-gate dircache_t *dcp;
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate if (!dnlc_dir_enable ||
12070Sstevel@tonic-gate (num_entries < dnlc_dir_min_size)) {
12080Sstevel@tonic-gate return (DNOCACHE);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate
12110Sstevel@tonic-gate if (num_entries > dnlc_dir_max_size) {
12120Sstevel@tonic-gate return (DTOOBIG);
12130Sstevel@tonic-gate }
12140Sstevel@tonic-gate
12150Sstevel@tonic-gate mutex_enter(&dc_head.dch_lock);
12160Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate if (dcap->dca_dircache == DC_RET_LOW_MEM) {
12190Sstevel@tonic-gate dcap->dca_dircache = NULL;
12200Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
12210Sstevel@tonic-gate mutex_exit(&dc_head.dch_lock);
12220Sstevel@tonic-gate return (DNOMEM);
12230Sstevel@tonic-gate }
12240Sstevel@tonic-gate
12250Sstevel@tonic-gate /*
12260Sstevel@tonic-gate * Check if there's currently a cache.
12270Sstevel@tonic-gate * This probably only occurs on a race.
12280Sstevel@tonic-gate */
12290Sstevel@tonic-gate if (dcap->dca_dircache != NULL) {
12300Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
12310Sstevel@tonic-gate mutex_exit(&dc_head.dch_lock);
12320Sstevel@tonic-gate return (DNOCACHE);
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate
12350Sstevel@tonic-gate /*
12360Sstevel@tonic-gate * Allocate the dircache struct, entry and free space hash tables.
12370Sstevel@tonic-gate * These tables are initially just one entry but dynamically resize
12380Sstevel@tonic-gate * when entries and free space are added or removed.
12390Sstevel@tonic-gate */
12400Sstevel@tonic-gate if ((dcp = kmem_zalloc(sizeof (dircache_t), KM_NOSLEEP)) == NULL) {
12410Sstevel@tonic-gate goto error;
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate if ((dcp->dc_namehash = kmem_zalloc(sizeof (dcentry_t *),
12440Sstevel@tonic-gate KM_NOSLEEP)) == NULL) {
12450Sstevel@tonic-gate goto error;
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate if ((dcp->dc_freehash = kmem_zalloc(sizeof (dcfree_t *),
12480Sstevel@tonic-gate KM_NOSLEEP)) == NULL) {
12490Sstevel@tonic-gate goto error;
12500Sstevel@tonic-gate }
12510Sstevel@tonic-gate
12520Sstevel@tonic-gate dcp->dc_anchor = dcap; /* set back pointer to anchor */
12530Sstevel@tonic-gate dcap->dca_dircache = dcp;
12540Sstevel@tonic-gate
12550Sstevel@tonic-gate /* add into head of global chain */
12560Sstevel@tonic-gate dcp->dc_next = dc_head.dch_next;
12570Sstevel@tonic-gate dcp->dc_prev = (dircache_t *)&dc_head;
12580Sstevel@tonic-gate dcp->dc_next->dc_prev = dcp;
12590Sstevel@tonic-gate dc_head.dch_next = dcp;
12600Sstevel@tonic-gate
12610Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
12620Sstevel@tonic-gate mutex_exit(&dc_head.dch_lock);
12630Sstevel@tonic-gate ncs.ncs_cur_dirs.value.ui64++;
12640Sstevel@tonic-gate ncs.ncs_dirs_cached.value.ui64++;
12650Sstevel@tonic-gate return (DOK);
12660Sstevel@tonic-gate error:
12670Sstevel@tonic-gate if (dcp != NULL) {
12680Sstevel@tonic-gate if (dcp->dc_namehash) {
12690Sstevel@tonic-gate kmem_free(dcp->dc_namehash, sizeof (dcentry_t *));
12700Sstevel@tonic-gate }
12710Sstevel@tonic-gate kmem_free(dcp, sizeof (dircache_t));
12720Sstevel@tonic-gate }
12730Sstevel@tonic-gate /*
12740Sstevel@tonic-gate * Must also kmem_free dcp->dc_freehash if more error cases are added
12750Sstevel@tonic-gate */
12760Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
12770Sstevel@tonic-gate mutex_exit(&dc_head.dch_lock);
12780Sstevel@tonic-gate ncs.ncs_dir_start_nm.value.ui64++;
12790Sstevel@tonic-gate return (DNOCACHE);
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate /*
12830Sstevel@tonic-gate * Add a directopry entry to a partial or complete directory cache.
12840Sstevel@tonic-gate */
12850Sstevel@tonic-gate dcret_t
dnlc_dir_add_entry(dcanchor_t * dcap,const char * name,uint64_t handle)1286*12684STom.Erickson@Sun.COM dnlc_dir_add_entry(dcanchor_t *dcap, const char *name, uint64_t handle)
12870Sstevel@tonic-gate {
12880Sstevel@tonic-gate dircache_t *dcp;
12890Sstevel@tonic-gate dcentry_t **hp, *dep;
12900Sstevel@tonic-gate int hash;
12910Sstevel@tonic-gate uint_t capacity;
12920Sstevel@tonic-gate uchar_t namlen;
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate /*
12950Sstevel@tonic-gate * Allocate the dcentry struct, including the variable
12960Sstevel@tonic-gate * size name. Note, the null terminator is not copied.
12970Sstevel@tonic-gate *
12980Sstevel@tonic-gate * We do this outside the lock to avoid possible deadlock if
12990Sstevel@tonic-gate * dnlc_dir_reclaim() is called as a result of memory shortage.
13000Sstevel@tonic-gate */
13010Sstevel@tonic-gate DNLC_DIR_HASH(name, hash, namlen);
13020Sstevel@tonic-gate dep = kmem_alloc(sizeof (dcentry_t) - 1 + namlen, KM_NOSLEEP);
13030Sstevel@tonic-gate if (dep == NULL) {
13040Sstevel@tonic-gate #ifdef DEBUG
13050Sstevel@tonic-gate /*
13060Sstevel@tonic-gate * The kmem allocator generates random failures for
13070Sstevel@tonic-gate * KM_NOSLEEP calls (see KMEM_RANDOM_ALLOCATION_FAILURE)
13080Sstevel@tonic-gate * So try again before we blow away a perfectly good cache.
13090Sstevel@tonic-gate * This is done not to cover an error but purely for
13100Sstevel@tonic-gate * performance running a debug kernel.
13110Sstevel@tonic-gate * This random error only occurs in debug mode.
13120Sstevel@tonic-gate */
13130Sstevel@tonic-gate dep = kmem_alloc(sizeof (dcentry_t) - 1 + namlen, KM_NOSLEEP);
13140Sstevel@tonic-gate if (dep != NULL)
13150Sstevel@tonic-gate goto ok;
13160Sstevel@tonic-gate #endif
13170Sstevel@tonic-gate ncs.ncs_dir_add_nm.value.ui64++;
13180Sstevel@tonic-gate /*
13190Sstevel@tonic-gate * Free a directory cache. This may be the one we are
13200Sstevel@tonic-gate * called with.
13210Sstevel@tonic-gate */
13220Sstevel@tonic-gate dnlc_dir_reclaim(NULL);
13230Sstevel@tonic-gate dep = kmem_alloc(sizeof (dcentry_t) - 1 + namlen, KM_NOSLEEP);
13240Sstevel@tonic-gate if (dep == NULL) {
13250Sstevel@tonic-gate /*
13260Sstevel@tonic-gate * still no memory, better delete this cache
13270Sstevel@tonic-gate */
13280Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
13290Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
13300Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
13310Sstevel@tonic-gate dnlc_dir_abort(dcp);
13320Sstevel@tonic-gate dcap->dca_dircache = DC_RET_LOW_MEM;
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
13350Sstevel@tonic-gate ncs.ncs_dir_addabort.value.ui64++;
13360Sstevel@tonic-gate return (DNOCACHE);
13370Sstevel@tonic-gate }
13380Sstevel@tonic-gate /*
13390Sstevel@tonic-gate * fall through as if the 1st kmem_alloc had worked
13400Sstevel@tonic-gate */
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate #ifdef DEBUG
13430Sstevel@tonic-gate ok:
13440Sstevel@tonic-gate #endif
13450Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
13460Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
13470Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
13480Sstevel@tonic-gate /*
13490Sstevel@tonic-gate * If the total number of entries goes above the max
13500Sstevel@tonic-gate * then free this cache
13510Sstevel@tonic-gate */
13520Sstevel@tonic-gate if ((dcp->dc_num_entries + dcp->dc_num_free) >
13536712Stomee dnlc_dir_max_size) {
13540Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
13550Sstevel@tonic-gate dnlc_dir_purge(dcap);
13560Sstevel@tonic-gate kmem_free(dep, sizeof (dcentry_t) - 1 + namlen);
13570Sstevel@tonic-gate ncs.ncs_dir_add_max.value.ui64++;
13580Sstevel@tonic-gate return (DTOOBIG);
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate dcp->dc_num_entries++;
13610Sstevel@tonic-gate capacity = (dcp->dc_nhash_mask + 1) << dnlc_dir_hash_size_shift;
13620Sstevel@tonic-gate if (dcp->dc_num_entries >=
13630Sstevel@tonic-gate (capacity << dnlc_dir_hash_resize_shift)) {
13640Sstevel@tonic-gate dnlc_dir_adjust_nhash(dcp);
13650Sstevel@tonic-gate }
13660Sstevel@tonic-gate hp = &dcp->dc_namehash[hash & dcp->dc_nhash_mask];
13670Sstevel@tonic-gate
13680Sstevel@tonic-gate /*
13690Sstevel@tonic-gate * Initialise and chain in new entry
13700Sstevel@tonic-gate */
13710Sstevel@tonic-gate dep->de_handle = handle;
13720Sstevel@tonic-gate dep->de_hash = hash;
13730Sstevel@tonic-gate /*
13740Sstevel@tonic-gate * Note de_namelen is a uchar_t to conserve space
13750Sstevel@tonic-gate * and alignment padding. The max length of any
13760Sstevel@tonic-gate * pathname component is defined as MAXNAMELEN
13770Sstevel@tonic-gate * which is 256 (including the terminating null).
13780Sstevel@tonic-gate * So provided this doesn't change, we don't include the null,
13790Sstevel@tonic-gate * we always use bcmp to compare strings, and we don't
13800Sstevel@tonic-gate * start storing full names, then we are ok.
13810Sstevel@tonic-gate * The space savings is worth it.
13820Sstevel@tonic-gate */
13830Sstevel@tonic-gate dep->de_namelen = namlen;
13840Sstevel@tonic-gate bcopy(name, dep->de_name, namlen);
13850Sstevel@tonic-gate dep->de_next = *hp;
13860Sstevel@tonic-gate *hp = dep;
138711066Srafael.vanoni@sun.com dcp->dc_actime = ddi_get_lbolt64();
13880Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
13890Sstevel@tonic-gate ncs.ncs_dir_num_ents.value.ui64++;
13900Sstevel@tonic-gate return (DOK);
13910Sstevel@tonic-gate } else {
13920Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
13930Sstevel@tonic-gate kmem_free(dep, sizeof (dcentry_t) - 1 + namlen);
13940Sstevel@tonic-gate return (DNOCACHE);
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate }
13970Sstevel@tonic-gate
13980Sstevel@tonic-gate /*
13990Sstevel@tonic-gate * Add free space to a partial or complete directory cache.
14000Sstevel@tonic-gate */
14010Sstevel@tonic-gate dcret_t
dnlc_dir_add_space(dcanchor_t * dcap,uint_t len,uint64_t handle)14020Sstevel@tonic-gate dnlc_dir_add_space(dcanchor_t *dcap, uint_t len, uint64_t handle)
14030Sstevel@tonic-gate {
14040Sstevel@tonic-gate dircache_t *dcp;
14050Sstevel@tonic-gate dcfree_t *dfp, **hp;
14060Sstevel@tonic-gate uint_t capacity;
14070Sstevel@tonic-gate
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate * We kmem_alloc outside the lock to avoid possible deadlock if
14100Sstevel@tonic-gate * dnlc_dir_reclaim() is called as a result of memory shortage.
14110Sstevel@tonic-gate */
14120Sstevel@tonic-gate dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
14130Sstevel@tonic-gate if (dfp == NULL) {
14140Sstevel@tonic-gate #ifdef DEBUG
14150Sstevel@tonic-gate /*
14160Sstevel@tonic-gate * The kmem allocator generates random failures for
14170Sstevel@tonic-gate * KM_NOSLEEP calls (see KMEM_RANDOM_ALLOCATION_FAILURE)
14180Sstevel@tonic-gate * So try again before we blow away a perfectly good cache.
14190Sstevel@tonic-gate * This random error only occurs in debug mode
14200Sstevel@tonic-gate */
14210Sstevel@tonic-gate dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
14220Sstevel@tonic-gate if (dfp != NULL)
14230Sstevel@tonic-gate goto ok;
14240Sstevel@tonic-gate #endif
14250Sstevel@tonic-gate ncs.ncs_dir_add_nm.value.ui64++;
14260Sstevel@tonic-gate /*
14270Sstevel@tonic-gate * Free a directory cache. This may be the one we are
14280Sstevel@tonic-gate * called with.
14290Sstevel@tonic-gate */
14300Sstevel@tonic-gate dnlc_dir_reclaim(NULL);
14310Sstevel@tonic-gate dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
14320Sstevel@tonic-gate if (dfp == NULL) {
14330Sstevel@tonic-gate /*
14340Sstevel@tonic-gate * still no memory, better delete this cache
14350Sstevel@tonic-gate */
14360Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
14370Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
14380Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
14390Sstevel@tonic-gate dnlc_dir_abort(dcp);
14400Sstevel@tonic-gate dcap->dca_dircache = DC_RET_LOW_MEM;
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
14430Sstevel@tonic-gate ncs.ncs_dir_addabort.value.ui64++;
14440Sstevel@tonic-gate return (DNOCACHE);
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate /*
14470Sstevel@tonic-gate * fall through as if the 1st kmem_alloc had worked
14480Sstevel@tonic-gate */
14490Sstevel@tonic-gate }
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate #ifdef DEBUG
14520Sstevel@tonic-gate ok:
14530Sstevel@tonic-gate #endif
14540Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
14550Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
14560Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
14570Sstevel@tonic-gate if ((dcp->dc_num_entries + dcp->dc_num_free) >
14586712Stomee dnlc_dir_max_size) {
14590Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
14600Sstevel@tonic-gate dnlc_dir_purge(dcap);
14610Sstevel@tonic-gate kmem_cache_free(dnlc_dir_space_cache, dfp);
14620Sstevel@tonic-gate ncs.ncs_dir_add_max.value.ui64++;
14630Sstevel@tonic-gate return (DTOOBIG);
14640Sstevel@tonic-gate }
14650Sstevel@tonic-gate dcp->dc_num_free++;
14660Sstevel@tonic-gate capacity = (dcp->dc_fhash_mask + 1) << dnlc_dir_hash_size_shift;
14670Sstevel@tonic-gate if (dcp->dc_num_free >=
14680Sstevel@tonic-gate (capacity << dnlc_dir_hash_resize_shift)) {
14690Sstevel@tonic-gate dnlc_dir_adjust_fhash(dcp);
14700Sstevel@tonic-gate }
14710Sstevel@tonic-gate /*
14720Sstevel@tonic-gate * Initialise and chain a new entry
14730Sstevel@tonic-gate */
14740Sstevel@tonic-gate dfp->df_handle = handle;
14750Sstevel@tonic-gate dfp->df_len = len;
147611066Srafael.vanoni@sun.com dcp->dc_actime = ddi_get_lbolt64();
14770Sstevel@tonic-gate hp = &(dcp->dc_freehash[DDFHASH(handle, dcp)]);
14780Sstevel@tonic-gate dfp->df_next = *hp;
14790Sstevel@tonic-gate *hp = dfp;
14800Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
14810Sstevel@tonic-gate ncs.ncs_dir_num_ents.value.ui64++;
14820Sstevel@tonic-gate return (DOK);
14830Sstevel@tonic-gate } else {
14840Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
14850Sstevel@tonic-gate kmem_cache_free(dnlc_dir_space_cache, dfp);
14860Sstevel@tonic-gate return (DNOCACHE);
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate
14900Sstevel@tonic-gate /*
14910Sstevel@tonic-gate * Mark a directory cache as complete.
14920Sstevel@tonic-gate */
14930Sstevel@tonic-gate void
dnlc_dir_complete(dcanchor_t * dcap)14940Sstevel@tonic-gate dnlc_dir_complete(dcanchor_t *dcap)
14950Sstevel@tonic-gate {
14960Sstevel@tonic-gate dircache_t *dcp;
14970Sstevel@tonic-gate
14980Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
14990Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
15000Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
15010Sstevel@tonic-gate dcp->dc_complete = B_TRUE;
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate
15060Sstevel@tonic-gate /*
15070Sstevel@tonic-gate * Internal routine to delete a partial or full directory cache.
15080Sstevel@tonic-gate * No additional locking needed.
15090Sstevel@tonic-gate */
15100Sstevel@tonic-gate static void
dnlc_dir_abort(dircache_t * dcp)15110Sstevel@tonic-gate dnlc_dir_abort(dircache_t *dcp)
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate dcentry_t *dep, *nhp;
15140Sstevel@tonic-gate dcfree_t *fep, *fhp;
15150Sstevel@tonic-gate uint_t nhtsize = dcp->dc_nhash_mask + 1; /* name hash table size */
15160Sstevel@tonic-gate uint_t fhtsize = dcp->dc_fhash_mask + 1; /* free hash table size */
15170Sstevel@tonic-gate uint_t i;
15180Sstevel@tonic-gate
15190Sstevel@tonic-gate /*
15200Sstevel@tonic-gate * Free up the cached name entries and hash table
15210Sstevel@tonic-gate */
15220Sstevel@tonic-gate for (i = 0; i < nhtsize; i++) { /* for each hash bucket */
15230Sstevel@tonic-gate nhp = dcp->dc_namehash[i];
15240Sstevel@tonic-gate while (nhp != NULL) { /* for each chained entry */
15250Sstevel@tonic-gate dep = nhp->de_next;
15260Sstevel@tonic-gate kmem_free(nhp, sizeof (dcentry_t) - 1 +
15270Sstevel@tonic-gate nhp->de_namelen);
15280Sstevel@tonic-gate nhp = dep;
15290Sstevel@tonic-gate }
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate kmem_free(dcp->dc_namehash, sizeof (dcentry_t *) * nhtsize);
15320Sstevel@tonic-gate
15330Sstevel@tonic-gate /*
15340Sstevel@tonic-gate * Free up the free space entries and hash table
15350Sstevel@tonic-gate */
15360Sstevel@tonic-gate for (i = 0; i < fhtsize; i++) { /* for each hash bucket */
15370Sstevel@tonic-gate fhp = dcp->dc_freehash[i];
15380Sstevel@tonic-gate while (fhp != NULL) { /* for each chained entry */
15390Sstevel@tonic-gate fep = fhp->df_next;
15400Sstevel@tonic-gate kmem_cache_free(dnlc_dir_space_cache, fhp);
15410Sstevel@tonic-gate fhp = fep;
15420Sstevel@tonic-gate }
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate kmem_free(dcp->dc_freehash, sizeof (dcfree_t *) * fhtsize);
15450Sstevel@tonic-gate
15460Sstevel@tonic-gate /*
15470Sstevel@tonic-gate * Finally free the directory cache structure itself
15480Sstevel@tonic-gate */
15490Sstevel@tonic-gate ncs.ncs_dir_num_ents.value.ui64 -= (dcp->dc_num_entries +
15500Sstevel@tonic-gate dcp->dc_num_free);
15510Sstevel@tonic-gate kmem_free(dcp, sizeof (dircache_t));
15520Sstevel@tonic-gate ncs.ncs_cur_dirs.value.ui64--;
15530Sstevel@tonic-gate }
15540Sstevel@tonic-gate
15550Sstevel@tonic-gate /*
15560Sstevel@tonic-gate * Remove a partial or complete directory cache
15570Sstevel@tonic-gate */
15580Sstevel@tonic-gate void
dnlc_dir_purge(dcanchor_t * dcap)15590Sstevel@tonic-gate dnlc_dir_purge(dcanchor_t *dcap)
15600Sstevel@tonic-gate {
15610Sstevel@tonic-gate dircache_t *dcp;
15620Sstevel@tonic-gate
15630Sstevel@tonic-gate mutex_enter(&dc_head.dch_lock);
15640Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
15650Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
15660Sstevel@tonic-gate if (!VALID_DIR_CACHE(dcp)) {
15670Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
15680Sstevel@tonic-gate mutex_exit(&dc_head.dch_lock);
15690Sstevel@tonic-gate return;
15700Sstevel@tonic-gate }
15710Sstevel@tonic-gate dcap->dca_dircache = NULL;
15720Sstevel@tonic-gate /*
15730Sstevel@tonic-gate * Unchain from global list
15740Sstevel@tonic-gate */
15750Sstevel@tonic-gate dcp->dc_prev->dc_next = dcp->dc_next;
15760Sstevel@tonic-gate dcp->dc_next->dc_prev = dcp->dc_prev;
15770Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
15780Sstevel@tonic-gate mutex_exit(&dc_head.dch_lock);
15790Sstevel@tonic-gate dnlc_dir_abort(dcp);
15800Sstevel@tonic-gate }
15810Sstevel@tonic-gate
15820Sstevel@tonic-gate /*
15830Sstevel@tonic-gate * Remove an entry from a complete or partial directory cache.
15840Sstevel@tonic-gate * Return the handle if it's non null.
15850Sstevel@tonic-gate */
15860Sstevel@tonic-gate dcret_t
dnlc_dir_rem_entry(dcanchor_t * dcap,const char * name,uint64_t * handlep)1587*12684STom.Erickson@Sun.COM dnlc_dir_rem_entry(dcanchor_t *dcap, const char *name, uint64_t *handlep)
15880Sstevel@tonic-gate {
15890Sstevel@tonic-gate dircache_t *dcp;
15900Sstevel@tonic-gate dcentry_t **prevpp, *te;
15910Sstevel@tonic-gate uint_t capacity;
15920Sstevel@tonic-gate int hash;
15930Sstevel@tonic-gate int ret;
15940Sstevel@tonic-gate uchar_t namlen;
15950Sstevel@tonic-gate
15960Sstevel@tonic-gate if (!dnlc_dir_enable) {
15970Sstevel@tonic-gate return (DNOCACHE);
15980Sstevel@tonic-gate }
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
16010Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
16020Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
160311066Srafael.vanoni@sun.com dcp->dc_actime = ddi_get_lbolt64();
16040Sstevel@tonic-gate if (dcp->dc_nhash_mask > 0) { /* ie not minimum */
16050Sstevel@tonic-gate capacity = (dcp->dc_nhash_mask + 1) <<
16060Sstevel@tonic-gate dnlc_dir_hash_size_shift;
16070Sstevel@tonic-gate if (dcp->dc_num_entries <=
16080Sstevel@tonic-gate (capacity >> dnlc_dir_hash_resize_shift)) {
16090Sstevel@tonic-gate dnlc_dir_adjust_nhash(dcp);
16100Sstevel@tonic-gate }
16110Sstevel@tonic-gate }
16120Sstevel@tonic-gate DNLC_DIR_HASH(name, hash, namlen);
16130Sstevel@tonic-gate prevpp = &dcp->dc_namehash[hash & dcp->dc_nhash_mask];
16140Sstevel@tonic-gate while (*prevpp != NULL) {
16150Sstevel@tonic-gate if (((*prevpp)->de_hash == hash) &&
16160Sstevel@tonic-gate (namlen == (*prevpp)->de_namelen) &&
16170Sstevel@tonic-gate bcmp((*prevpp)->de_name, name, namlen) == 0) {
16180Sstevel@tonic-gate if (handlep != NULL) {
16190Sstevel@tonic-gate *handlep = (*prevpp)->de_handle;
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate te = *prevpp;
16220Sstevel@tonic-gate *prevpp = (*prevpp)->de_next;
16230Sstevel@tonic-gate kmem_free(te, sizeof (dcentry_t) - 1 +
16240Sstevel@tonic-gate te->de_namelen);
16250Sstevel@tonic-gate
16260Sstevel@tonic-gate /*
16270Sstevel@tonic-gate * If the total number of entries
16280Sstevel@tonic-gate * falls below half the minimum number
16290Sstevel@tonic-gate * of entries then free this cache.
16300Sstevel@tonic-gate */
16310Sstevel@tonic-gate if (--dcp->dc_num_entries <
16320Sstevel@tonic-gate (dnlc_dir_min_size >> 1)) {
16330Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
16340Sstevel@tonic-gate dnlc_dir_purge(dcap);
16350Sstevel@tonic-gate } else {
16360Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
16370Sstevel@tonic-gate }
16380Sstevel@tonic-gate ncs.ncs_dir_num_ents.value.ui64--;
16390Sstevel@tonic-gate return (DFOUND);
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate prevpp = &((*prevpp)->de_next);
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate if (dcp->dc_complete) {
16440Sstevel@tonic-gate ncs.ncs_dir_reme_fai.value.ui64++;
16450Sstevel@tonic-gate ret = DNOENT;
16460Sstevel@tonic-gate } else {
16470Sstevel@tonic-gate ret = DNOCACHE;
16480Sstevel@tonic-gate }
16490Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
16500Sstevel@tonic-gate return (ret);
16510Sstevel@tonic-gate } else {
16520Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
16530Sstevel@tonic-gate return (DNOCACHE);
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate }
16560Sstevel@tonic-gate
16570Sstevel@tonic-gate
16580Sstevel@tonic-gate /*
16590Sstevel@tonic-gate * Remove free space of at least the given length from a complete
16600Sstevel@tonic-gate * or partial directory cache.
16610Sstevel@tonic-gate */
16620Sstevel@tonic-gate dcret_t
dnlc_dir_rem_space_by_len(dcanchor_t * dcap,uint_t len,uint64_t * handlep)16630Sstevel@tonic-gate dnlc_dir_rem_space_by_len(dcanchor_t *dcap, uint_t len, uint64_t *handlep)
16640Sstevel@tonic-gate {
16650Sstevel@tonic-gate dircache_t *dcp;
16660Sstevel@tonic-gate dcfree_t **prevpp, *tfp;
16670Sstevel@tonic-gate uint_t fhtsize; /* free hash table size */
16680Sstevel@tonic-gate uint_t i;
16690Sstevel@tonic-gate uint_t capacity;
16700Sstevel@tonic-gate int ret;
16710Sstevel@tonic-gate
16720Sstevel@tonic-gate if (!dnlc_dir_enable) {
16730Sstevel@tonic-gate return (DNOCACHE);
16740Sstevel@tonic-gate }
16750Sstevel@tonic-gate
16760Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
16770Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
16780Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
167911066Srafael.vanoni@sun.com dcp->dc_actime = ddi_get_lbolt64();
16800Sstevel@tonic-gate if (dcp->dc_fhash_mask > 0) { /* ie not minimum */
16810Sstevel@tonic-gate capacity = (dcp->dc_fhash_mask + 1) <<
16820Sstevel@tonic-gate dnlc_dir_hash_size_shift;
16830Sstevel@tonic-gate if (dcp->dc_num_free <=
16840Sstevel@tonic-gate (capacity >> dnlc_dir_hash_resize_shift)) {
16850Sstevel@tonic-gate dnlc_dir_adjust_fhash(dcp);
16860Sstevel@tonic-gate }
16870Sstevel@tonic-gate }
16880Sstevel@tonic-gate /*
16890Sstevel@tonic-gate * Search for an entry of the appropriate size
16900Sstevel@tonic-gate * on a first fit basis.
16910Sstevel@tonic-gate */
16920Sstevel@tonic-gate fhtsize = dcp->dc_fhash_mask + 1;
16930Sstevel@tonic-gate for (i = 0; i < fhtsize; i++) { /* for each hash bucket */
16940Sstevel@tonic-gate prevpp = &(dcp->dc_freehash[i]);
16950Sstevel@tonic-gate while (*prevpp != NULL) {
16960Sstevel@tonic-gate if ((*prevpp)->df_len >= len) {
16970Sstevel@tonic-gate *handlep = (*prevpp)->df_handle;
16980Sstevel@tonic-gate tfp = *prevpp;
16990Sstevel@tonic-gate *prevpp = (*prevpp)->df_next;
17000Sstevel@tonic-gate dcp->dc_num_free--;
17010Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
17020Sstevel@tonic-gate kmem_cache_free(dnlc_dir_space_cache,
17030Sstevel@tonic-gate tfp);
17040Sstevel@tonic-gate ncs.ncs_dir_num_ents.value.ui64--;
17050Sstevel@tonic-gate return (DFOUND);
17060Sstevel@tonic-gate }
17070Sstevel@tonic-gate prevpp = &((*prevpp)->df_next);
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate }
17100Sstevel@tonic-gate if (dcp->dc_complete) {
17110Sstevel@tonic-gate ret = DNOENT;
17120Sstevel@tonic-gate } else {
17130Sstevel@tonic-gate ret = DNOCACHE;
17140Sstevel@tonic-gate }
17150Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
17160Sstevel@tonic-gate return (ret);
17170Sstevel@tonic-gate } else {
17180Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
17190Sstevel@tonic-gate return (DNOCACHE);
17200Sstevel@tonic-gate }
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate
17230Sstevel@tonic-gate /*
17240Sstevel@tonic-gate * Remove free space with the given handle from a complete or partial
17250Sstevel@tonic-gate * directory cache.
17260Sstevel@tonic-gate */
17270Sstevel@tonic-gate dcret_t
dnlc_dir_rem_space_by_handle(dcanchor_t * dcap,uint64_t handle)17280Sstevel@tonic-gate dnlc_dir_rem_space_by_handle(dcanchor_t *dcap, uint64_t handle)
17290Sstevel@tonic-gate {
17300Sstevel@tonic-gate dircache_t *dcp;
17310Sstevel@tonic-gate dcfree_t **prevpp, *tfp;
17320Sstevel@tonic-gate uint_t capacity;
17330Sstevel@tonic-gate int ret;
17340Sstevel@tonic-gate
17350Sstevel@tonic-gate if (!dnlc_dir_enable) {
17360Sstevel@tonic-gate return (DNOCACHE);
17370Sstevel@tonic-gate }
17380Sstevel@tonic-gate
17390Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
17400Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
17410Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
174211066Srafael.vanoni@sun.com dcp->dc_actime = ddi_get_lbolt64();
17430Sstevel@tonic-gate if (dcp->dc_fhash_mask > 0) { /* ie not minimum */
17440Sstevel@tonic-gate capacity = (dcp->dc_fhash_mask + 1) <<
17450Sstevel@tonic-gate dnlc_dir_hash_size_shift;
17460Sstevel@tonic-gate if (dcp->dc_num_free <=
17470Sstevel@tonic-gate (capacity >> dnlc_dir_hash_resize_shift)) {
17480Sstevel@tonic-gate dnlc_dir_adjust_fhash(dcp);
17490Sstevel@tonic-gate }
17500Sstevel@tonic-gate }
17510Sstevel@tonic-gate
17520Sstevel@tonic-gate /*
17530Sstevel@tonic-gate * search for the exact entry
17540Sstevel@tonic-gate */
17550Sstevel@tonic-gate prevpp = &(dcp->dc_freehash[DDFHASH(handle, dcp)]);
17560Sstevel@tonic-gate while (*prevpp != NULL) {
17570Sstevel@tonic-gate if ((*prevpp)->df_handle == handle) {
17580Sstevel@tonic-gate tfp = *prevpp;
17590Sstevel@tonic-gate *prevpp = (*prevpp)->df_next;
17600Sstevel@tonic-gate dcp->dc_num_free--;
17610Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
17620Sstevel@tonic-gate kmem_cache_free(dnlc_dir_space_cache, tfp);
17630Sstevel@tonic-gate ncs.ncs_dir_num_ents.value.ui64--;
17640Sstevel@tonic-gate return (DFOUND);
17650Sstevel@tonic-gate }
17660Sstevel@tonic-gate prevpp = &((*prevpp)->df_next);
17670Sstevel@tonic-gate }
17680Sstevel@tonic-gate if (dcp->dc_complete) {
17690Sstevel@tonic-gate ncs.ncs_dir_rems_fai.value.ui64++;
17700Sstevel@tonic-gate ret = DNOENT;
17710Sstevel@tonic-gate } else {
17720Sstevel@tonic-gate ret = DNOCACHE;
17730Sstevel@tonic-gate }
17740Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
17750Sstevel@tonic-gate return (ret);
17760Sstevel@tonic-gate } else {
17770Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
17780Sstevel@tonic-gate return (DNOCACHE);
17790Sstevel@tonic-gate }
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate
17820Sstevel@tonic-gate /*
17830Sstevel@tonic-gate * Update the handle of an directory cache entry.
17840Sstevel@tonic-gate */
17850Sstevel@tonic-gate dcret_t
dnlc_dir_update(dcanchor_t * dcap,const char * name,uint64_t handle)1786*12684STom.Erickson@Sun.COM dnlc_dir_update(dcanchor_t *dcap, const char *name, uint64_t handle)
17870Sstevel@tonic-gate {
17880Sstevel@tonic-gate dircache_t *dcp;
17890Sstevel@tonic-gate dcentry_t *dep;
17900Sstevel@tonic-gate int hash;
17910Sstevel@tonic-gate int ret;
17920Sstevel@tonic-gate uchar_t namlen;
17930Sstevel@tonic-gate
17940Sstevel@tonic-gate if (!dnlc_dir_enable) {
17950Sstevel@tonic-gate return (DNOCACHE);
17960Sstevel@tonic-gate }
17970Sstevel@tonic-gate
17980Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
17990Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
18000Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
180111066Srafael.vanoni@sun.com dcp->dc_actime = ddi_get_lbolt64();
18020Sstevel@tonic-gate DNLC_DIR_HASH(name, hash, namlen);
18030Sstevel@tonic-gate dep = dcp->dc_namehash[hash & dcp->dc_nhash_mask];
18040Sstevel@tonic-gate while (dep != NULL) {
18050Sstevel@tonic-gate if ((dep->de_hash == hash) &&
18060Sstevel@tonic-gate (namlen == dep->de_namelen) &&
18070Sstevel@tonic-gate bcmp(dep->de_name, name, namlen) == 0) {
18080Sstevel@tonic-gate dep->de_handle = handle;
18090Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
18100Sstevel@tonic-gate return (DFOUND);
18110Sstevel@tonic-gate }
18120Sstevel@tonic-gate dep = dep->de_next;
18130Sstevel@tonic-gate }
18140Sstevel@tonic-gate if (dcp->dc_complete) {
18150Sstevel@tonic-gate ncs.ncs_dir_upd_fail.value.ui64++;
18160Sstevel@tonic-gate ret = DNOENT;
18170Sstevel@tonic-gate } else {
18180Sstevel@tonic-gate ret = DNOCACHE;
18190Sstevel@tonic-gate }
18200Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
18210Sstevel@tonic-gate return (ret);
18220Sstevel@tonic-gate } else {
18230Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
18240Sstevel@tonic-gate return (DNOCACHE);
18250Sstevel@tonic-gate }
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate
18280Sstevel@tonic-gate void
dnlc_dir_fini(dcanchor_t * dcap)18290Sstevel@tonic-gate dnlc_dir_fini(dcanchor_t *dcap)
18300Sstevel@tonic-gate {
18310Sstevel@tonic-gate dircache_t *dcp;
18320Sstevel@tonic-gate
18330Sstevel@tonic-gate mutex_enter(&dc_head.dch_lock);
18340Sstevel@tonic-gate mutex_enter(&dcap->dca_lock);
18350Sstevel@tonic-gate dcp = (dircache_t *)dcap->dca_dircache;
18360Sstevel@tonic-gate if (VALID_DIR_CACHE(dcp)) {
18370Sstevel@tonic-gate /*
18380Sstevel@tonic-gate * Unchain from global list
18390Sstevel@tonic-gate */
18400Sstevel@tonic-gate ncs.ncs_dir_finipurg.value.ui64++;
18410Sstevel@tonic-gate dcp->dc_prev->dc_next = dcp->dc_next;
18420Sstevel@tonic-gate dcp->dc_next->dc_prev = dcp->dc_prev;
18430Sstevel@tonic-gate } else {
18440Sstevel@tonic-gate dcp = NULL;
18450Sstevel@tonic-gate }
18460Sstevel@tonic-gate dcap->dca_dircache = NULL;
18470Sstevel@tonic-gate mutex_exit(&dcap->dca_lock);
18480Sstevel@tonic-gate mutex_exit(&dc_head.dch_lock);
18490Sstevel@tonic-gate mutex_destroy(&dcap->dca_lock);
18500Sstevel@tonic-gate if (dcp) {
18510Sstevel@tonic-gate dnlc_dir_abort(dcp);
18520Sstevel@tonic-gate }
18530Sstevel@tonic-gate }
18540Sstevel@tonic-gate
18550Sstevel@tonic-gate /*
18560Sstevel@tonic-gate * Reclaim callback for dnlc directory caching.
18570Sstevel@tonic-gate * Invoked by the kernel memory allocator when memory gets tight.
18580Sstevel@tonic-gate * This is a pretty serious condition and can lead easily lead to system
18590Sstevel@tonic-gate * hangs if not enough space is returned.
18600Sstevel@tonic-gate *
18610Sstevel@tonic-gate * Deciding which directory (or directories) to purge is tricky.
18620Sstevel@tonic-gate * Purging everything is an overkill, but purging just the oldest used
18630Sstevel@tonic-gate * was found to lead to hangs. The largest cached directories use the
18640Sstevel@tonic-gate * most memory, but take the most effort to rebuild, whereas the smaller
18650Sstevel@tonic-gate * ones have little value and give back little space. So what to do?
18660Sstevel@tonic-gate *
18670Sstevel@tonic-gate * The current policy is to continue purging the oldest used directories
18680Sstevel@tonic-gate * until at least dnlc_dir_min_reclaim directory entries have been purged.
18690Sstevel@tonic-gate */
18700Sstevel@tonic-gate /*ARGSUSED*/
18710Sstevel@tonic-gate static void
dnlc_dir_reclaim(void * unused)18720Sstevel@tonic-gate dnlc_dir_reclaim(void *unused)
18730Sstevel@tonic-gate {
18740Sstevel@tonic-gate dircache_t *dcp, *oldest;
18750Sstevel@tonic-gate uint_t dirent_cnt = 0;
18760Sstevel@tonic-gate
18770Sstevel@tonic-gate mutex_enter(&dc_head.dch_lock);
18780Sstevel@tonic-gate while (dirent_cnt < dnlc_dir_min_reclaim) {
18790Sstevel@tonic-gate dcp = dc_head.dch_next;
18800Sstevel@tonic-gate oldest = NULL;
18810Sstevel@tonic-gate while (dcp != (dircache_t *)&dc_head) {
18820Sstevel@tonic-gate if (oldest == NULL) {
18830Sstevel@tonic-gate oldest = dcp;
18840Sstevel@tonic-gate } else {
18850Sstevel@tonic-gate if (dcp->dc_actime < oldest->dc_actime) {
18860Sstevel@tonic-gate oldest = dcp;
18870Sstevel@tonic-gate }
18880Sstevel@tonic-gate }
18890Sstevel@tonic-gate dcp = dcp->dc_next;
18900Sstevel@tonic-gate }
18910Sstevel@tonic-gate if (oldest == NULL) {
18920Sstevel@tonic-gate /* nothing to delete */
18930Sstevel@tonic-gate mutex_exit(&dc_head.dch_lock);
18940Sstevel@tonic-gate return;
18950Sstevel@tonic-gate }
18960Sstevel@tonic-gate /*
18970Sstevel@tonic-gate * remove from directory chain and purge
18980Sstevel@tonic-gate */
18990Sstevel@tonic-gate oldest->dc_prev->dc_next = oldest->dc_next;
19000Sstevel@tonic-gate oldest->dc_next->dc_prev = oldest->dc_prev;
19010Sstevel@tonic-gate mutex_enter(&oldest->dc_anchor->dca_lock);
19020Sstevel@tonic-gate /*
19030Sstevel@tonic-gate * If this was the last entry then it must be too large.
19040Sstevel@tonic-gate * Mark it as such by saving a special dircache_t
19050Sstevel@tonic-gate * pointer (DC_RET_LOW_MEM) in the anchor. The error DNOMEM
19060Sstevel@tonic-gate * will be presented to the caller of dnlc_dir_start()
19070Sstevel@tonic-gate */
19080Sstevel@tonic-gate if (oldest->dc_next == oldest->dc_prev) {
19090Sstevel@tonic-gate oldest->dc_anchor->dca_dircache = DC_RET_LOW_MEM;
19100Sstevel@tonic-gate ncs.ncs_dir_rec_last.value.ui64++;
19110Sstevel@tonic-gate } else {
19120Sstevel@tonic-gate oldest->dc_anchor->dca_dircache = NULL;
19130Sstevel@tonic-gate ncs.ncs_dir_recl_any.value.ui64++;
19140Sstevel@tonic-gate }
19150Sstevel@tonic-gate mutex_exit(&oldest->dc_anchor->dca_lock);
19160Sstevel@tonic-gate dirent_cnt += oldest->dc_num_entries;
19170Sstevel@tonic-gate dnlc_dir_abort(oldest);
19180Sstevel@tonic-gate }
19190Sstevel@tonic-gate mutex_exit(&dc_head.dch_lock);
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate
19220Sstevel@tonic-gate /*
19230Sstevel@tonic-gate * Dynamically grow or shrink the size of the name hash table
19240Sstevel@tonic-gate */
19250Sstevel@tonic-gate static void
dnlc_dir_adjust_nhash(dircache_t * dcp)19260Sstevel@tonic-gate dnlc_dir_adjust_nhash(dircache_t *dcp)
19270Sstevel@tonic-gate {
19280Sstevel@tonic-gate dcentry_t **newhash, *dep, **nhp, *tep;
19290Sstevel@tonic-gate uint_t newsize;
19300Sstevel@tonic-gate uint_t oldsize;
19310Sstevel@tonic-gate uint_t newsizemask;
19320Sstevel@tonic-gate int i;
19330Sstevel@tonic-gate
19340Sstevel@tonic-gate /*
19350Sstevel@tonic-gate * Allocate new hash table
19360Sstevel@tonic-gate */
19370Sstevel@tonic-gate newsize = dcp->dc_num_entries >> dnlc_dir_hash_size_shift;
19380Sstevel@tonic-gate newhash = kmem_zalloc(sizeof (dcentry_t *) * newsize, KM_NOSLEEP);
19390Sstevel@tonic-gate if (newhash == NULL) {
19400Sstevel@tonic-gate /*
19410Sstevel@tonic-gate * System is short on memory just return
19420Sstevel@tonic-gate * Note, the old hash table is still usable.
19430Sstevel@tonic-gate * This return is unlikely to repeatedy occur, because
19440Sstevel@tonic-gate * either some other directory caches will be reclaimed
19450Sstevel@tonic-gate * due to memory shortage, thus freeing memory, or this
19460Sstevel@tonic-gate * directory cahe will be reclaimed.
19470Sstevel@tonic-gate */
19480Sstevel@tonic-gate return;
19490Sstevel@tonic-gate }
19500Sstevel@tonic-gate oldsize = dcp->dc_nhash_mask + 1;
19510Sstevel@tonic-gate dcp->dc_nhash_mask = newsizemask = newsize - 1;
19520Sstevel@tonic-gate
19530Sstevel@tonic-gate /*
19540Sstevel@tonic-gate * Move entries from the old table to the new
19550Sstevel@tonic-gate */
19560Sstevel@tonic-gate for (i = 0; i < oldsize; i++) { /* for each hash bucket */
19570Sstevel@tonic-gate dep = dcp->dc_namehash[i];
19580Sstevel@tonic-gate while (dep != NULL) { /* for each chained entry */
19590Sstevel@tonic-gate tep = dep;
19600Sstevel@tonic-gate dep = dep->de_next;
19610Sstevel@tonic-gate nhp = &newhash[tep->de_hash & newsizemask];
19620Sstevel@tonic-gate tep->de_next = *nhp;
19630Sstevel@tonic-gate *nhp = tep;
19640Sstevel@tonic-gate }
19650Sstevel@tonic-gate }
19660Sstevel@tonic-gate
19670Sstevel@tonic-gate /*
19680Sstevel@tonic-gate * delete old hash table and set new one in place
19690Sstevel@tonic-gate */
19700Sstevel@tonic-gate kmem_free(dcp->dc_namehash, sizeof (dcentry_t *) * oldsize);
19710Sstevel@tonic-gate dcp->dc_namehash = newhash;
19720Sstevel@tonic-gate }
19730Sstevel@tonic-gate
19740Sstevel@tonic-gate /*
19750Sstevel@tonic-gate * Dynamically grow or shrink the size of the free space hash table
19760Sstevel@tonic-gate */
19770Sstevel@tonic-gate static void
dnlc_dir_adjust_fhash(dircache_t * dcp)19780Sstevel@tonic-gate dnlc_dir_adjust_fhash(dircache_t *dcp)
19790Sstevel@tonic-gate {
19800Sstevel@tonic-gate dcfree_t **newhash, *dfp, **nhp, *tfp;
19810Sstevel@tonic-gate uint_t newsize;
19820Sstevel@tonic-gate uint_t oldsize;
19830Sstevel@tonic-gate int i;
19840Sstevel@tonic-gate
19850Sstevel@tonic-gate /*
19860Sstevel@tonic-gate * Allocate new hash table
19870Sstevel@tonic-gate */
19880Sstevel@tonic-gate newsize = dcp->dc_num_free >> dnlc_dir_hash_size_shift;
19890Sstevel@tonic-gate newhash = kmem_zalloc(sizeof (dcfree_t *) * newsize, KM_NOSLEEP);
19900Sstevel@tonic-gate if (newhash == NULL) {
19910Sstevel@tonic-gate /*
19920Sstevel@tonic-gate * System is short on memory just return
19930Sstevel@tonic-gate * Note, the old hash table is still usable.
19940Sstevel@tonic-gate * This return is unlikely to repeatedy occur, because
19950Sstevel@tonic-gate * either some other directory caches will be reclaimed
19960Sstevel@tonic-gate * due to memory shortage, thus freeing memory, or this
19970Sstevel@tonic-gate * directory cahe will be reclaimed.
19980Sstevel@tonic-gate */
19990Sstevel@tonic-gate return;
20000Sstevel@tonic-gate }
20010Sstevel@tonic-gate oldsize = dcp->dc_fhash_mask + 1;
20020Sstevel@tonic-gate dcp->dc_fhash_mask = newsize - 1;
20030Sstevel@tonic-gate
20040Sstevel@tonic-gate /*
20050Sstevel@tonic-gate * Move entries from the old table to the new
20060Sstevel@tonic-gate */
20070Sstevel@tonic-gate for (i = 0; i < oldsize; i++) { /* for each hash bucket */
20080Sstevel@tonic-gate dfp = dcp->dc_freehash[i];
20090Sstevel@tonic-gate while (dfp != NULL) { /* for each chained entry */
20100Sstevel@tonic-gate tfp = dfp;
20110Sstevel@tonic-gate dfp = dfp->df_next;
20120Sstevel@tonic-gate nhp = &newhash[DDFHASH(tfp->df_handle, dcp)];
20130Sstevel@tonic-gate tfp->df_next = *nhp;
20140Sstevel@tonic-gate *nhp = tfp;
20150Sstevel@tonic-gate }
20160Sstevel@tonic-gate }
20170Sstevel@tonic-gate
20180Sstevel@tonic-gate /*
20190Sstevel@tonic-gate * delete old hash table and set new one in place
20200Sstevel@tonic-gate */
20210Sstevel@tonic-gate kmem_free(dcp->dc_freehash, sizeof (dcfree_t *) * oldsize);
20220Sstevel@tonic-gate dcp->dc_freehash = newhash;
20230Sstevel@tonic-gate }
2024