#include <sys/types.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/t_lock.h>
#include <sys/systm.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/dnlc.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/vtrace.h>
#include <sys/bitmap.h>
#include <sys/var.h>
#include <sys/sysmacros.h>
#include <sys/kstat.h>
#include <sys/atomic.h>
#include <sys/taskq.h>
#define VN_HOLD_CALLER VN_HOLD
#define VN_HOLD_DNLC(vp) { \
mutex_enter(&(vp)->v_lock); \
if ((vp)->v_count_dnlc == 0) { \
VN_HOLD_LOCKED(vp); \
} \
(vp)->v_count_dnlc++; \
mutex_exit(&(vp)->v_lock); \
}
#define VN_RELE_DNLC(vp) { \
vn_rele_dnlc(vp); \
}
#define NC_HASHAVELEN_DEFAULT 4
int nc_hashavelen = NC_HASHAVELEN_DEFAULT;
#define NC_MOVETOFRONT 2
#define DNLC_MAX_RELE 8
nc_hash_t *nc_hash;
static nc_hash_t *dnlc_purge_fs1_rotor;
static nc_hash_t *dnlc_free_rotor;
int ncsize = -1;
volatile uint32_t dnlc_nentries = 0;
static int nc_hashsz;
static int nc_hashmask;
#define DNLC_LOW_WATER_DIVISOR_DEFAULT 100
uint_t dnlc_low_water_divisor = DNLC_LOW_WATER_DIVISOR_DEFAULT;
uint_t dnlc_nentries_low_water;
int dnlc_reduce_idle = 1;
uint_t ncsize_onepercent;
uint_t ncsize_min_percent;
uint_t dnlc_max_nentries;
uint64_t dnlc_max_nentries_cnt = 0;
#define DNLC_LONG_CHAIN 8
uint_t dnlc_long_chain = DNLC_LONG_CHAIN;
struct ncstats ncstats;
struct nc_stats ncs = {
{ "hits", KSTAT_DATA_UINT64 },
{ "misses", KSTAT_DATA_UINT64 },
{ "negative_cache_hits", KSTAT_DATA_UINT64 },
{ "enters", KSTAT_DATA_UINT64 },
{ "double_enters", KSTAT_DATA_UINT64 },
{ "purge_total_entries", KSTAT_DATA_UINT64 },
{ "purge_all", KSTAT_DATA_UINT64 },
{ "purge_vp", KSTAT_DATA_UINT64 },
{ "purge_vfs", KSTAT_DATA_UINT64 },
{ "purge_fs1", KSTAT_DATA_UINT64 },
{ "pick_free", KSTAT_DATA_UINT64 },
{ "pick_heuristic", KSTAT_DATA_UINT64 },
{ "pick_last", KSTAT_DATA_UINT64 },
{ "dir_hits", KSTAT_DATA_UINT64 },
{ "dir_misses", KSTAT_DATA_UINT64 },
{ "dir_cached_current", KSTAT_DATA_UINT64 },
{ "dir_entries_cached_current", KSTAT_DATA_UINT64 },
{ "dir_cached_total", KSTAT_DATA_UINT64 },
{ "dir_start_no_memory", KSTAT_DATA_UINT64 },
{ "dir_add_no_memory", KSTAT_DATA_UINT64 },
{ "dir_add_abort", KSTAT_DATA_UINT64 },
{ "dir_add_max", KSTAT_DATA_UINT64 },
{ "dir_remove_entry_fail", KSTAT_DATA_UINT64 },
{ "dir_remove_space_fail", KSTAT_DATA_UINT64 },
{ "dir_update_fail", KSTAT_DATA_UINT64 },
{ "dir_fini_purge", KSTAT_DATA_UINT64 },
{ "dir_reclaim_last", KSTAT_DATA_UINT64 },
{ "dir_reclaim_any", KSTAT_DATA_UINT64 },
};
static int doingcache = 1;
vnode_t negative_cache_vnode;
#define nc_inshash(ncp, hp) \
{ \
(ncp)->hash_next = (hp)->hash_next; \
(ncp)->hash_prev = (ncache_t *)(hp); \
(hp)->hash_next->hash_prev = (ncp); \
(hp)->hash_next = (ncp); \
}
#define nc_rmhash(ncp) \
{ \
(ncp)->hash_prev->hash_next = (ncp)->hash_next; \
(ncp)->hash_next->hash_prev = (ncp)->hash_prev; \
(ncp)->hash_prev = NULL; \
(ncp)->hash_next = NULL; \
}
#define dnlc_free(ncp) \
{ \
kmem_free((ncp), NCACHE_SIZE((ncp)->namlen)); \
atomic_dec_32(&dnlc_nentries); \
}
#define DDFHASH(handle, dcp) ((handle >> 2) & (dcp)->dc_fhash_mask)
#define DNLC_DIR_HASH(name, hash, namelen) \
{ \
char Xc; \
const char *Xcp; \
hash = *name; \
for (Xcp = (name + 1); (Xc = *Xcp) != 0; Xcp++) \
hash = (hash << 4) + hash + Xc; \
ASSERT((Xcp - (name)) <= ((1 << NBBY) - 1)); \
namelen = Xcp - (name); \
}
#define DC_RET_LOW_MEM (dircache_t *)1
#define VALID_DIR_CACHE(dcp) ((dircache_t *)(dcp) > DC_RET_LOW_MEM)
uint_t dnlc_dir_enable = 1;
uint_t dnlc_dir_min_size = 40;
uint_t dnlc_dir_max_size = UINT_MAX;
uint_t dnlc_dir_hash_size_shift = 3;
uint_t dnlc_dir_min_reclaim = 350000;
uint_t dnlc_dir_hash_resize_shift = 2;
static kmem_cache_t *dnlc_dir_space_cache;
static dchead_t dc_head;
static ncache_t *dnlc_get(uchar_t namlen);
static ncache_t *dnlc_search(vnode_t *dp, const char *name, uchar_t namlen,
int hash);
static void dnlc_dir_reclaim(void *unused);
static void dnlc_dir_abort(dircache_t *dcp);
static void dnlc_dir_adjust_fhash(dircache_t *dcp);
static void dnlc_dir_adjust_nhash(dircache_t *dcp);
static void do_dnlc_reduce_cache(void *);
void
dnlc_init()
{
nc_hash_t *hp;
kstat_t *ksp;
int i;
if (ncsize == -1) {
dnlc_nentries_low_water = 4 * (v.v_proc + maxusers) + 320;
ncsize = dnlc_nentries_low_water +
(dnlc_nentries_low_water / dnlc_low_water_divisor);
} else {
dnlc_nentries_low_water =
ncsize - (ncsize / dnlc_low_water_divisor);
}
if (ncsize <= 0) {
doingcache = 0;
dnlc_dir_enable = 0;
ncsize = 0;
cmn_err(CE_NOTE, "name cache (dnlc) disabled");
return;
}
dnlc_max_nentries = ncsize * 2;
ncsize_onepercent = ncsize / 100;
ncsize_min_percent = ncsize_onepercent * 3;
nc_hashsz = ncsize / nc_hashavelen;
nc_hashsz = 1 << highbit(nc_hashsz);
nc_hashmask = nc_hashsz - 1;
nc_hash = kmem_zalloc(nc_hashsz * sizeof (*nc_hash), KM_SLEEP);
for (i = 0; i < nc_hashsz; i++) {
hp = (nc_hash_t *)&nc_hash[i];
mutex_init(&hp->hash_lock, NULL, MUTEX_DEFAULT, NULL);
hp->hash_next = (ncache_t *)hp;
hp->hash_prev = (ncache_t *)hp;
}
dnlc_free_rotor = dnlc_purge_fs1_rotor = &nc_hash[0];
dnlc_dir_space_cache = kmem_cache_create("dnlc_space_cache",
sizeof (dcfree_t), 0, NULL, NULL, dnlc_dir_reclaim, NULL,
NULL, 0);
mutex_init(&dc_head.dch_lock, NULL, MUTEX_DEFAULT, NULL);
dc_head.dch_next = (dircache_t *)&dc_head;
dc_head.dch_prev = (dircache_t *)&dc_head;
vn_reinit(&negative_cache_vnode);
ksp = kstat_create("unix", 0, "ncstats", "misc", KSTAT_TYPE_RAW,
sizeof (struct ncstats), KSTAT_FLAG_VIRTUAL);
if (ksp) {
ksp->ks_data = (void *) &ncstats;
kstat_install(ksp);
}
ksp = kstat_create("unix", 0, "dnlcstats", "misc", KSTAT_TYPE_NAMED,
sizeof (ncs) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
if (ksp) {
ksp->ks_data = (void *) &ncs;
kstat_install(ksp);
}
}
void
dnlc_enter(vnode_t *dp, const char *name, vnode_t *vp)
{
ncache_t *ncp;
nc_hash_t *hp;
uchar_t namlen;
int hash;
TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_enter_start:");
if (!doingcache) {
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_enter_end:(%S) %d", "not caching", 0);
return;
}
DNLCHASH(name, dp, hash, namlen);
if ((ncp = dnlc_get(namlen)) == NULL)
return;
ncp->dp = dp;
VN_HOLD_DNLC(dp);
ncp->vp = vp;
VN_HOLD_DNLC(vp);
bcopy(name, ncp->name, namlen);
ncp->hash = hash;
hp = &nc_hash[hash & nc_hashmask];
mutex_enter(&hp->hash_lock);
if (dnlc_search(dp, name, namlen, hash) != NULL) {
mutex_exit(&hp->hash_lock);
ncstats.dbl_enters++;
ncs.ncs_dbl_enters.value.ui64++;
VN_RELE_DNLC(dp);
VN_RELE_DNLC(vp);
dnlc_free(ncp);
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_enter_end:(%S) %d", "dbl enter", ncstats.dbl_enters);
return;
}
nc_inshash(ncp, hp);
mutex_exit(&hp->hash_lock);
ncstats.enters++;
ncs.ncs_enters.value.ui64++;
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_enter_end:(%S) %d", "done", ncstats.enters);
}
void
dnlc_update(vnode_t *dp, const char *name, vnode_t *vp)
{
ncache_t *ncp;
ncache_t *tcp;
vnode_t *tvp;
nc_hash_t *hp;
int hash;
uchar_t namlen;
TRACE_0(TR_FAC_NFS, TR_DNLC_ENTER_START, "dnlc_update_start:");
if (!doingcache) {
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_update_end:(%S) %d", "not caching", 0);
return;
}
DNLCHASH(name, dp, hash, namlen);
if ((ncp = dnlc_get(namlen)) == NULL) {
dnlc_remove(dp, name);
return;
}
ncp->dp = dp;
VN_HOLD_DNLC(dp);
ncp->vp = vp;
VN_HOLD_DNLC(vp);
bcopy(name, ncp->name, namlen);
ncp->hash = hash;
hp = &nc_hash[hash & nc_hashmask];
mutex_enter(&hp->hash_lock);
if ((tcp = dnlc_search(dp, name, namlen, hash)) != NULL) {
if (tcp->vp != vp) {
tvp = tcp->vp;
tcp->vp = vp;
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(tvp);
ncstats.enters++;
ncs.ncs_enters.value.ui64++;
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_update_end:(%S) %d", "done", ncstats.enters);
} else {
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(vp);
ncstats.dbl_enters++;
ncs.ncs_dbl_enters.value.ui64++;
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_update_end:(%S) %d",
"dbl enter", ncstats.dbl_enters);
}
VN_RELE_DNLC(dp);
dnlc_free(ncp);
return;
}
nc_inshash(ncp, hp);
mutex_exit(&hp->hash_lock);
ncstats.enters++;
ncs.ncs_enters.value.ui64++;
TRACE_2(TR_FAC_NFS, TR_DNLC_ENTER_END,
"dnlc_update_end:(%S) %d", "done", ncstats.enters);
}
vnode_t *
dnlc_lookup(vnode_t *dp, const char *name)
{
ncache_t *ncp;
nc_hash_t *hp;
vnode_t *vp;
int hash, depth;
uchar_t namlen;
TRACE_2(TR_FAC_NFS, TR_DNLC_LOOKUP_START,
"dnlc_lookup_start:dp %x name %s", dp, name);
if (!doingcache) {
TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
"dnlc_lookup_end:%S %d vp %x name %s",
"not_caching", 0, NULL, name);
return (NULL);
}
DNLCHASH(name, dp, hash, namlen);
depth = 1;
hp = &nc_hash[hash & nc_hashmask];
mutex_enter(&hp->hash_lock);
for (ncp = hp->hash_next; ncp != (ncache_t *)hp;
ncp = ncp->hash_next) {
if (ncp->hash == hash &&
ncp->dp == dp &&
ncp->namlen == namlen &&
bcmp(ncp->name, name, namlen) == 0) {
if (depth > NC_MOVETOFRONT) {
ncache_t *next = ncp->hash_next;
ncache_t *prev = ncp->hash_prev;
prev->hash_next = next;
next->hash_prev = prev;
ncp->hash_next = next = hp->hash_next;
ncp->hash_prev = (ncache_t *)hp;
next->hash_prev = ncp;
hp->hash_next = ncp;
ncstats.move_to_front++;
}
vp = ncp->vp;
VN_HOLD_CALLER(vp);
mutex_exit(&hp->hash_lock);
ncstats.hits++;
ncs.ncs_hits.value.ui64++;
if (vp == DNLC_NO_VNODE) {
ncs.ncs_neg_hits.value.ui64++;
}
TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
"dnlc_lookup_end:%S %d vp %x name %s", "hit",
ncstats.hits, vp, name);
return (vp);
}
depth++;
}
mutex_exit(&hp->hash_lock);
ncstats.misses++;
ncs.ncs_misses.value.ui64++;
TRACE_4(TR_FAC_NFS, TR_DNLC_LOOKUP_END,
"dnlc_lookup_end:%S %d vp %x name %s", "miss", ncstats.misses,
NULL, name);
return (NULL);
}
void
dnlc_remove(vnode_t *dp, const char *name)
{
ncache_t *ncp;
nc_hash_t *hp;
uchar_t namlen;
int hash;
if (!doingcache)
return;
DNLCHASH(name, dp, hash, namlen);
hp = &nc_hash[hash & nc_hashmask];
mutex_enter(&hp->hash_lock);
if (ncp = dnlc_search(dp, name, namlen, hash)) {
nc_rmhash(ncp);
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(ncp->vp);
VN_RELE_DNLC(ncp->dp);
dnlc_free(ncp);
return;
}
mutex_exit(&hp->hash_lock);
}
void
dnlc_purge()
{
nc_hash_t *nch;
ncache_t *ncp;
int index;
int i;
vnode_t *nc_rele[DNLC_MAX_RELE];
if (!doingcache)
return;
ncstats.purges++;
ncs.ncs_purge_all.value.ui64++;
for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
index = 0;
mutex_enter(&nch->hash_lock);
ncp = nch->hash_next;
while (ncp != (ncache_t *)nch) {
ncache_t *np;
np = ncp->hash_next;
nc_rele[index++] = ncp->vp;
nc_rele[index++] = ncp->dp;
nc_rmhash(ncp);
dnlc_free(ncp);
ncp = np;
ncs.ncs_purge_total.value.ui64++;
if (index == DNLC_MAX_RELE)
break;
}
mutex_exit(&nch->hash_lock);
for (i = 0; i < index; i++) {
VN_RELE_DNLC(nc_rele[i]);
}
if (ncp != (ncache_t *)nch) {
nch--;
}
}
}
void
dnlc_purge_vp(vnode_t *vp)
{
nc_hash_t *nch;
ncache_t *ncp;
int index;
vnode_t *nc_rele[DNLC_MAX_RELE];
ASSERT(vp->v_count > 0);
if (vp->v_count_dnlc == 0) {
return;
}
if (!doingcache)
return;
ncstats.purges++;
ncs.ncs_purge_vp.value.ui64++;
for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
index = 0;
mutex_enter(&nch->hash_lock);
ncp = nch->hash_next;
while (ncp != (ncache_t *)nch) {
ncache_t *np;
np = ncp->hash_next;
if (ncp->dp == vp || ncp->vp == vp) {
nc_rele[index++] = ncp->vp;
nc_rele[index++] = ncp->dp;
nc_rmhash(ncp);
dnlc_free(ncp);
ncs.ncs_purge_total.value.ui64++;
if (index == DNLC_MAX_RELE) {
ncp = np;
break;
}
}
ncp = np;
}
mutex_exit(&nch->hash_lock);
while (index) {
VN_RELE_DNLC(nc_rele[--index]);
}
if (vp->v_count_dnlc == 0) {
return;
}
if (ncp != (ncache_t *)nch) {
nch--;
}
}
}
int
dnlc_purge_vfsp(vfs_t *vfsp, int count)
{
nc_hash_t *nch;
ncache_t *ncp;
int n = 0;
int index;
int i;
vnode_t *nc_rele[DNLC_MAX_RELE];
if (!doingcache)
return (0);
ncstats.purges++;
ncs.ncs_purge_vfs.value.ui64++;
for (nch = nc_hash; nch < &nc_hash[nc_hashsz]; nch++) {
index = 0;
mutex_enter(&nch->hash_lock);
ncp = nch->hash_next;
while (ncp != (ncache_t *)nch) {
ncache_t *np;
np = ncp->hash_next;
ASSERT(ncp->dp != NULL);
ASSERT(ncp->vp != NULL);
if ((ncp->dp->v_vfsp == vfsp) ||
(ncp->vp->v_vfsp == vfsp)) {
n++;
nc_rele[index++] = ncp->vp;
nc_rele[index++] = ncp->dp;
nc_rmhash(ncp);
dnlc_free(ncp);
ncs.ncs_purge_total.value.ui64++;
if (index == DNLC_MAX_RELE) {
ncp = np;
break;
}
if (count != 0 && n >= count) {
break;
}
}
ncp = np;
}
mutex_exit(&nch->hash_lock);
for (i = 0; i < index; i++) {
VN_RELE_DNLC(nc_rele[i]);
}
if (count != 0 && n >= count) {
return (n);
}
if (ncp != (ncache_t *)nch) {
nch--;
}
}
return (n);
}
int
dnlc_fs_purge1(vnodeops_t *vop)
{
nc_hash_t *end;
nc_hash_t *hp;
ncache_t *ncp;
vnode_t *vp;
if (!doingcache)
return (0);
ncs.ncs_purge_fs1.value.ui64++;
hp = end = dnlc_purge_fs1_rotor;
do {
if (++hp == &nc_hash[nc_hashsz])
hp = nc_hash;
dnlc_purge_fs1_rotor = hp;
if (hp->hash_next == (ncache_t *)hp)
continue;
mutex_enter(&hp->hash_lock);
for (ncp = hp->hash_prev;
ncp != (ncache_t *)hp;
ncp = ncp->hash_prev) {
vp = ncp->vp;
if (!vn_has_cached_data(vp) && (vp->v_count == 1) &&
vn_matchops(vp, vop))
break;
}
if (ncp != (ncache_t *)hp) {
nc_rmhash(ncp);
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(ncp->dp);
VN_RELE_DNLC(vp)
dnlc_free(ncp);
ncs.ncs_purge_total.value.ui64++;
return (1);
}
mutex_exit(&hp->hash_lock);
} while (hp != end);
return (0);
}
static ncache_t *
dnlc_search(vnode_t *dp, const char *name, uchar_t namlen, int hash)
{
nc_hash_t *hp;
ncache_t *ncp;
hp = &nc_hash[hash & nc_hashmask];
for (ncp = hp->hash_next; ncp != (ncache_t *)hp; ncp = ncp->hash_next) {
if (ncp->hash == hash &&
ncp->dp == dp &&
ncp->namlen == namlen &&
bcmp(ncp->name, name, namlen) == 0)
return (ncp);
}
return (NULL);
}
#if ((1 << NBBY) - 1) < (MAXNAMELEN - 1)
#error ncache_t name length representation is too small
#endif
void
dnlc_reduce_cache(void *reduce_percent)
{
if (dnlc_reduce_idle && (dnlc_nentries >= ncsize || reduce_percent)) {
dnlc_reduce_idle = 0;
if ((taskq_dispatch(system_taskq, do_dnlc_reduce_cache,
reduce_percent, TQ_NOSLEEP)) == TASKQID_INVALID)
dnlc_reduce_idle = 1;
}
}
static ncache_t *
dnlc_get(uchar_t namlen)
{
ncache_t *ncp;
if (dnlc_nentries > dnlc_max_nentries) {
dnlc_max_nentries_cnt++;
return (NULL);
}
ncp = kmem_alloc(NCACHE_SIZE(namlen), KM_NOSLEEP);
if (ncp == NULL) {
return (NULL);
}
ncp->namlen = namlen;
atomic_inc_32(&dnlc_nentries);
dnlc_reduce_cache(NULL);
return (ncp);
}
static void
do_dnlc_reduce_cache(void *reduce_percent)
{
nc_hash_t *hp = dnlc_free_rotor, *start_hp = hp;
vnode_t *vp;
ncache_t *ncp;
int cnt;
uint_t low_water = dnlc_nentries_low_water;
if (reduce_percent) {
uint_t reduce_cnt;
if (dnlc_nentries <= ncsize_min_percent) {
dnlc_reduce_idle = 1;
return;
}
reduce_cnt = ncsize_onepercent *
(uint_t)(uintptr_t)reduce_percent;
if (reduce_cnt > dnlc_nentries ||
dnlc_nentries - reduce_cnt < ncsize_min_percent)
low_water = ncsize_min_percent;
else
low_water = dnlc_nentries - reduce_cnt;
}
do {
do {
if (++hp == &nc_hash[nc_hashsz])
hp = nc_hash;
} while (hp->hash_next == (ncache_t *)hp && hp != start_hp);
if (hp->hash_next == (ncache_t *)hp) {
dnlc_reduce_idle = 1;
return;
}
mutex_enter(&hp->hash_lock);
for (cnt = 0, ncp = hp->hash_prev; ncp != (ncache_t *)hp;
ncp = ncp->hash_prev, cnt++) {
vp = ncp->vp;
if (!vn_has_cached_data(vp) &&
((vp->v_count == 1) || (vp == DNLC_NO_VNODE))) {
ncs.ncs_pick_heur.value.ui64++;
goto found;
}
if (cnt > dnlc_long_chain) {
ncp = hp->hash_prev;
ncs.ncs_pick_last.value.ui64++;
vp = ncp->vp;
goto found;
}
}
if (hp->hash_next == (ncache_t *)hp) {
mutex_exit(&hp->hash_lock);
continue;
}
ncp = hp->hash_prev;
ncs.ncs_pick_last.value.ui64++;
vp = ncp->vp;
found:
nc_rmhash(ncp);
mutex_exit(&hp->hash_lock);
VN_RELE_DNLC(vp);
VN_RELE_DNLC(ncp->dp);
dnlc_free(ncp);
} while (dnlc_nentries > low_water);
dnlc_free_rotor = hp;
dnlc_reduce_idle = 1;
}
dcret_t
dnlc_dir_lookup(dcanchor_t *dcap, const char *name, uint64_t *handle)
{
dircache_t *dcp;
dcentry_t *dep;
int hash;
int ret;
uchar_t namlen;
if (!VALID_DIR_CACHE(dcap->dca_dircache)) {
ncs.ncs_dir_misses.value.ui64++;
return (DNOCACHE);
}
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
DNLC_DIR_HASH(name, hash, namlen);
dep = dcp->dc_namehash[hash & dcp->dc_nhash_mask];
while (dep != NULL) {
if ((dep->de_hash == hash) &&
(namlen == dep->de_namelen) &&
bcmp(dep->de_name, name, namlen) == 0) {
*handle = dep->de_handle;
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_hits.value.ui64++;
return (DFOUND);
}
dep = dep->de_next;
}
if (dcp->dc_complete) {
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_misses.value.ui64++;
return (DNOCACHE);
}
}
dcret_t
dnlc_dir_start(dcanchor_t *dcap, uint_t num_entries)
{
dircache_t *dcp;
if (!dnlc_dir_enable ||
(num_entries < dnlc_dir_min_size)) {
return (DNOCACHE);
}
if (num_entries > dnlc_dir_max_size) {
return (DTOOBIG);
}
mutex_enter(&dc_head.dch_lock);
mutex_enter(&dcap->dca_lock);
if (dcap->dca_dircache == DC_RET_LOW_MEM) {
dcap->dca_dircache = NULL;
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
return (DNOMEM);
}
if (dcap->dca_dircache != NULL) {
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
return (DNOCACHE);
}
if ((dcp = kmem_zalloc(sizeof (dircache_t), KM_NOSLEEP)) == NULL) {
goto error;
}
if ((dcp->dc_namehash = kmem_zalloc(sizeof (dcentry_t *),
KM_NOSLEEP)) == NULL) {
goto error;
}
if ((dcp->dc_freehash = kmem_zalloc(sizeof (dcfree_t *),
KM_NOSLEEP)) == NULL) {
goto error;
}
dcp->dc_anchor = dcap;
dcap->dca_dircache = dcp;
dcp->dc_next = dc_head.dch_next;
dcp->dc_prev = (dircache_t *)&dc_head;
dcp->dc_next->dc_prev = dcp;
dc_head.dch_next = dcp;
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
ncs.ncs_cur_dirs.value.ui64++;
ncs.ncs_dirs_cached.value.ui64++;
return (DOK);
error:
if (dcp != NULL) {
if (dcp->dc_namehash) {
kmem_free(dcp->dc_namehash, sizeof (dcentry_t *));
}
kmem_free(dcp, sizeof (dircache_t));
}
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
ncs.ncs_dir_start_nm.value.ui64++;
return (DNOCACHE);
}
dcret_t
dnlc_dir_add_entry(dcanchor_t *dcap, const char *name, uint64_t handle)
{
dircache_t *dcp;
dcentry_t **hp, *dep;
int hash;
uint_t capacity;
uchar_t namlen;
DNLC_DIR_HASH(name, hash, namlen);
dep = kmem_alloc(DCENTTRY_SIZE(namlen), KM_NOSLEEP);
if (dep == NULL) {
#ifdef DEBUG
dep = kmem_alloc(DCENTTRY_SIZE(namlen), KM_NOSLEEP);
if (dep != NULL)
goto ok;
#endif
ncs.ncs_dir_add_nm.value.ui64++;
dnlc_dir_reclaim(NULL);
dep = kmem_alloc(DCENTTRY_SIZE(namlen), KM_NOSLEEP);
if (dep == NULL) {
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dnlc_dir_abort(dcp);
dcap->dca_dircache = DC_RET_LOW_MEM;
}
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_addabort.value.ui64++;
return (DNOCACHE);
}
}
#ifdef DEBUG
ok:
#endif
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
if ((dcp->dc_num_entries + dcp->dc_num_free) >
dnlc_dir_max_size) {
mutex_exit(&dcap->dca_lock);
dnlc_dir_purge(dcap);
kmem_free(dep, DCENTTRY_SIZE(namlen));
ncs.ncs_dir_add_max.value.ui64++;
return (DTOOBIG);
}
dcp->dc_num_entries++;
capacity = (dcp->dc_nhash_mask + 1) << dnlc_dir_hash_size_shift;
if (dcp->dc_num_entries >=
(capacity << dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_nhash(dcp);
}
hp = &dcp->dc_namehash[hash & dcp->dc_nhash_mask];
dep->de_handle = handle;
dep->de_hash = hash;
dep->de_namelen = namlen;
bcopy(name, dep->de_name, namlen);
dep->de_next = *hp;
*hp = dep;
dcp->dc_actime = ddi_get_lbolt64();
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_num_ents.value.ui64++;
return (DOK);
} else {
mutex_exit(&dcap->dca_lock);
kmem_free(dep, DCENTTRY_SIZE(namlen));
return (DNOCACHE);
}
}
dcret_t
dnlc_dir_add_space(dcanchor_t *dcap, uint_t len, uint64_t handle)
{
dircache_t *dcp;
dcfree_t *dfp, **hp;
uint_t capacity;
dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
if (dfp == NULL) {
#ifdef DEBUG
dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
if (dfp != NULL)
goto ok;
#endif
ncs.ncs_dir_add_nm.value.ui64++;
dnlc_dir_reclaim(NULL);
dfp = kmem_cache_alloc(dnlc_dir_space_cache, KM_NOSLEEP);
if (dfp == NULL) {
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dnlc_dir_abort(dcp);
dcap->dca_dircache = DC_RET_LOW_MEM;
}
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_addabort.value.ui64++;
return (DNOCACHE);
}
}
#ifdef DEBUG
ok:
#endif
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
if ((dcp->dc_num_entries + dcp->dc_num_free) >
dnlc_dir_max_size) {
mutex_exit(&dcap->dca_lock);
dnlc_dir_purge(dcap);
kmem_cache_free(dnlc_dir_space_cache, dfp);
ncs.ncs_dir_add_max.value.ui64++;
return (DTOOBIG);
}
dcp->dc_num_free++;
capacity = (dcp->dc_fhash_mask + 1) << dnlc_dir_hash_size_shift;
if (dcp->dc_num_free >=
(capacity << dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_fhash(dcp);
}
dfp->df_handle = handle;
dfp->df_len = len;
dcp->dc_actime = ddi_get_lbolt64();
hp = &(dcp->dc_freehash[DDFHASH(handle, dcp)]);
dfp->df_next = *hp;
*hp = dfp;
mutex_exit(&dcap->dca_lock);
ncs.ncs_dir_num_ents.value.ui64++;
return (DOK);
} else {
mutex_exit(&dcap->dca_lock);
kmem_cache_free(dnlc_dir_space_cache, dfp);
return (DNOCACHE);
}
}
void
dnlc_dir_complete(dcanchor_t *dcap)
{
dircache_t *dcp;
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_complete = B_TRUE;
}
mutex_exit(&dcap->dca_lock);
}
static void
dnlc_dir_abort(dircache_t *dcp)
{
dcentry_t *dep, *nhp;
dcfree_t *fep, *fhp;
uint_t nhtsize = dcp->dc_nhash_mask + 1;
uint_t fhtsize = dcp->dc_fhash_mask + 1;
uint_t i;
for (i = 0; i < nhtsize; i++) {
nhp = dcp->dc_namehash[i];
while (nhp != NULL) {
dep = nhp->de_next;
kmem_free(nhp, DCENTTRY_SIZE(nhp->de_namelen));
nhp = dep;
}
}
kmem_free(dcp->dc_namehash, sizeof (dcentry_t *) * nhtsize);
for (i = 0; i < fhtsize; i++) {
fhp = dcp->dc_freehash[i];
while (fhp != NULL) {
fep = fhp->df_next;
kmem_cache_free(dnlc_dir_space_cache, fhp);
fhp = fep;
}
}
kmem_free(dcp->dc_freehash, sizeof (dcfree_t *) * fhtsize);
ncs.ncs_dir_num_ents.value.ui64 -= (dcp->dc_num_entries +
dcp->dc_num_free);
kmem_free(dcp, sizeof (dircache_t));
ncs.ncs_cur_dirs.value.ui64--;
}
void
dnlc_dir_purge(dcanchor_t *dcap)
{
dircache_t *dcp;
mutex_enter(&dc_head.dch_lock);
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (!VALID_DIR_CACHE(dcp)) {
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
return;
}
dcap->dca_dircache = NULL;
dcp->dc_prev->dc_next = dcp->dc_next;
dcp->dc_next->dc_prev = dcp->dc_prev;
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
dnlc_dir_abort(dcp);
}
dcret_t
dnlc_dir_rem_entry(dcanchor_t *dcap, const char *name, uint64_t *handlep)
{
dircache_t *dcp;
dcentry_t **prevpp, *te;
uint_t capacity;
int hash;
int ret;
uchar_t namlen;
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
if (dcp->dc_nhash_mask > 0) {
capacity = (dcp->dc_nhash_mask + 1) <<
dnlc_dir_hash_size_shift;
if (dcp->dc_num_entries <=
(capacity >> dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_nhash(dcp);
}
}
DNLC_DIR_HASH(name, hash, namlen);
prevpp = &dcp->dc_namehash[hash & dcp->dc_nhash_mask];
while (*prevpp != NULL) {
if (((*prevpp)->de_hash == hash) &&
(namlen == (*prevpp)->de_namelen) &&
bcmp((*prevpp)->de_name, name, namlen) == 0) {
if (handlep != NULL) {
*handlep = (*prevpp)->de_handle;
}
te = *prevpp;
*prevpp = (*prevpp)->de_next;
kmem_free(te, DCENTTRY_SIZE(te->de_namelen));
if (--dcp->dc_num_entries <
(dnlc_dir_min_size >> 1)) {
mutex_exit(&dcap->dca_lock);
dnlc_dir_purge(dcap);
} else {
mutex_exit(&dcap->dca_lock);
}
ncs.ncs_dir_num_ents.value.ui64--;
return (DFOUND);
}
prevpp = &((*prevpp)->de_next);
}
if (dcp->dc_complete) {
ncs.ncs_dir_reme_fai.value.ui64++;
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
return (DNOCACHE);
}
}
dcret_t
dnlc_dir_rem_space_by_len(dcanchor_t *dcap, uint_t len, uint64_t *handlep)
{
dircache_t *dcp;
dcfree_t **prevpp, *tfp;
uint_t fhtsize;
uint_t i;
uint_t capacity;
int ret;
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
if (dcp->dc_fhash_mask > 0) {
capacity = (dcp->dc_fhash_mask + 1) <<
dnlc_dir_hash_size_shift;
if (dcp->dc_num_free <=
(capacity >> dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_fhash(dcp);
}
}
fhtsize = dcp->dc_fhash_mask + 1;
for (i = 0; i < fhtsize; i++) {
prevpp = &(dcp->dc_freehash[i]);
while (*prevpp != NULL) {
if ((*prevpp)->df_len >= len) {
*handlep = (*prevpp)->df_handle;
tfp = *prevpp;
*prevpp = (*prevpp)->df_next;
dcp->dc_num_free--;
mutex_exit(&dcap->dca_lock);
kmem_cache_free(dnlc_dir_space_cache,
tfp);
ncs.ncs_dir_num_ents.value.ui64--;
return (DFOUND);
}
prevpp = &((*prevpp)->df_next);
}
}
if (dcp->dc_complete) {
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
return (DNOCACHE);
}
}
dcret_t
dnlc_dir_rem_space_by_handle(dcanchor_t *dcap, uint64_t handle)
{
dircache_t *dcp;
dcfree_t **prevpp, *tfp;
uint_t capacity;
int ret;
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
if (dcp->dc_fhash_mask > 0) {
capacity = (dcp->dc_fhash_mask + 1) <<
dnlc_dir_hash_size_shift;
if (dcp->dc_num_free <=
(capacity >> dnlc_dir_hash_resize_shift)) {
dnlc_dir_adjust_fhash(dcp);
}
}
prevpp = &(dcp->dc_freehash[DDFHASH(handle, dcp)]);
while (*prevpp != NULL) {
if ((*prevpp)->df_handle == handle) {
tfp = *prevpp;
*prevpp = (*prevpp)->df_next;
dcp->dc_num_free--;
mutex_exit(&dcap->dca_lock);
kmem_cache_free(dnlc_dir_space_cache, tfp);
ncs.ncs_dir_num_ents.value.ui64--;
return (DFOUND);
}
prevpp = &((*prevpp)->df_next);
}
if (dcp->dc_complete) {
ncs.ncs_dir_rems_fai.value.ui64++;
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
return (DNOCACHE);
}
}
dcret_t
dnlc_dir_update(dcanchor_t *dcap, const char *name, uint64_t handle)
{
dircache_t *dcp;
dcentry_t *dep;
int hash;
int ret;
uchar_t namlen;
if (!dnlc_dir_enable) {
return (DNOCACHE);
}
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
dcp->dc_actime = ddi_get_lbolt64();
DNLC_DIR_HASH(name, hash, namlen);
dep = dcp->dc_namehash[hash & dcp->dc_nhash_mask];
while (dep != NULL) {
if ((dep->de_hash == hash) &&
(namlen == dep->de_namelen) &&
bcmp(dep->de_name, name, namlen) == 0) {
dep->de_handle = handle;
mutex_exit(&dcap->dca_lock);
return (DFOUND);
}
dep = dep->de_next;
}
if (dcp->dc_complete) {
ncs.ncs_dir_upd_fail.value.ui64++;
ret = DNOENT;
} else {
ret = DNOCACHE;
}
mutex_exit(&dcap->dca_lock);
return (ret);
} else {
mutex_exit(&dcap->dca_lock);
return (DNOCACHE);
}
}
void
dnlc_dir_fini(dcanchor_t *dcap)
{
dircache_t *dcp;
mutex_enter(&dc_head.dch_lock);
mutex_enter(&dcap->dca_lock);
dcp = (dircache_t *)dcap->dca_dircache;
if (VALID_DIR_CACHE(dcp)) {
ncs.ncs_dir_finipurg.value.ui64++;
dcp->dc_prev->dc_next = dcp->dc_next;
dcp->dc_next->dc_prev = dcp->dc_prev;
} else {
dcp = NULL;
}
dcap->dca_dircache = NULL;
mutex_exit(&dcap->dca_lock);
mutex_exit(&dc_head.dch_lock);
mutex_destroy(&dcap->dca_lock);
if (dcp) {
dnlc_dir_abort(dcp);
}
}
static void
dnlc_dir_reclaim(void *unused)
{
dircache_t *dcp, *oldest;
uint_t dirent_cnt = 0;
mutex_enter(&dc_head.dch_lock);
while (dirent_cnt < dnlc_dir_min_reclaim) {
dcp = dc_head.dch_next;
oldest = NULL;
while (dcp != (dircache_t *)&dc_head) {
if (oldest == NULL) {
oldest = dcp;
} else {
if (dcp->dc_actime < oldest->dc_actime) {
oldest = dcp;
}
}
dcp = dcp->dc_next;
}
if (oldest == NULL) {
mutex_exit(&dc_head.dch_lock);
return;
}
oldest->dc_prev->dc_next = oldest->dc_next;
oldest->dc_next->dc_prev = oldest->dc_prev;
mutex_enter(&oldest->dc_anchor->dca_lock);
if (oldest->dc_next == oldest->dc_prev) {
oldest->dc_anchor->dca_dircache = DC_RET_LOW_MEM;
ncs.ncs_dir_rec_last.value.ui64++;
} else {
oldest->dc_anchor->dca_dircache = NULL;
ncs.ncs_dir_recl_any.value.ui64++;
}
mutex_exit(&oldest->dc_anchor->dca_lock);
dirent_cnt += oldest->dc_num_entries;
dnlc_dir_abort(oldest);
}
mutex_exit(&dc_head.dch_lock);
}
static void
dnlc_dir_adjust_nhash(dircache_t *dcp)
{
dcentry_t **newhash, *dep, **nhp, *tep;
uint_t newsize;
uint_t oldsize;
uint_t newsizemask;
int i;
newsize = dcp->dc_num_entries >> dnlc_dir_hash_size_shift;
newhash = kmem_zalloc(sizeof (dcentry_t *) * newsize, KM_NOSLEEP);
if (newhash == NULL) {
return;
}
oldsize = dcp->dc_nhash_mask + 1;
dcp->dc_nhash_mask = newsizemask = newsize - 1;
for (i = 0; i < oldsize; i++) {
dep = dcp->dc_namehash[i];
while (dep != NULL) {
tep = dep;
dep = dep->de_next;
nhp = &newhash[tep->de_hash & newsizemask];
tep->de_next = *nhp;
*nhp = tep;
}
}
kmem_free(dcp->dc_namehash, sizeof (dcentry_t *) * oldsize);
dcp->dc_namehash = newhash;
}
static void
dnlc_dir_adjust_fhash(dircache_t *dcp)
{
dcfree_t **newhash, *dfp, **nhp, *tfp;
uint_t newsize;
uint_t oldsize;
int i;
newsize = dcp->dc_num_free >> dnlc_dir_hash_size_shift;
newhash = kmem_zalloc(sizeof (dcfree_t *) * newsize, KM_NOSLEEP);
if (newhash == NULL) {
return;
}
oldsize = dcp->dc_fhash_mask + 1;
dcp->dc_fhash_mask = newsize - 1;
for (i = 0; i < oldsize; i++) {
dfp = dcp->dc_freehash[i];
while (dfp != NULL) {
tfp = dfp;
dfp = dfp->df_next;
nhp = &newhash[DDFHASH(tfp->df_handle, dcp)];
tfp->df_next = *nhp;
*nhp = tfp;
}
}
kmem_free(dcp->dc_freehash, sizeof (dcfree_t *) * oldsize);
dcp->dc_freehash = newhash;
}