#include <nfs/nfs.h>
#include <nfs/nfs4.h>
#include <nfs/rnode4.h>
#include <nfs/nfs4_clnt.h>
#include <sys/bitmap.h>
static acache4_hash_t *acache4;
static long nacache;
static int acache4size;
static int acache4mask;
static struct kmem_cache *acache4_cache;
static int acache4_hashlen = 4;
#define ACACHE4_SHIFT_BITS 9
static int
acache4hash(rnode4_t *rp, cred_t *cred)
{
return ((((intptr_t)rp >> ACACHE4_SHIFT_BITS) + crgetuid(cred)) &
acache4mask);
}
#ifdef DEBUG
static long nfs4_access_cache_hits = 0;
static long nfs4_access_cache_misses = 0;
#endif
nfs4_access_type_t
nfs4_access_check(rnode4_t *rp, uint32_t acc, cred_t *cr)
{
acache4_t *ap;
acache4_hash_t *hp;
nfs4_access_type_t all;
vnode_t *vp;
vp = RTOV4(rp);
if (!ATTRCACHE4_VALID(vp) || nfs4_waitfor_purge_complete(vp))
return (NFS4_ACCESS_UNKNOWN);
if (rp->r_acache != NULL) {
hp = &acache4[acache4hash(rp, cr)];
rw_enter(&hp->lock, RW_READER);
ap = hp->next;
while (ap != (acache4_t *)hp) {
if (crcmp(ap->cred, cr) == 0 && ap->rnode == rp) {
if ((ap->known & acc) == acc) {
#ifdef DEBUG
nfs4_access_cache_hits++;
#endif
if ((ap->allowed & acc) == acc)
all = NFS4_ACCESS_ALLOWED;
else
all = NFS4_ACCESS_DENIED;
} else {
#ifdef DEBUG
nfs4_access_cache_misses++;
#endif
all = NFS4_ACCESS_UNKNOWN;
}
rw_exit(&hp->lock);
return (all);
}
ap = ap->next;
}
rw_exit(&hp->lock);
}
#ifdef DEBUG
nfs4_access_cache_misses++;
#endif
return (NFS4_ACCESS_UNKNOWN);
}
void
nfs4_access_cache(rnode4_t *rp, uint32_t acc, uint32_t resacc, cred_t *cr)
{
acache4_t *ap;
acache4_t *nap;
acache4_hash_t *hp;
hp = &acache4[acache4hash(rp, cr)];
nap = kmem_cache_alloc(acache4_cache, KM_NOSLEEP);
if (nap != NULL) {
nap->known = acc;
nap->allowed = resacc;
nap->rnode = rp;
crhold(cr);
nap->cred = cr;
nap->hashq = hp;
}
rw_enter(&hp->lock, RW_WRITER);
if (rp->r_acache != NULL) {
ap = hp->next;
while (ap != (acache4_t *)hp) {
if (crcmp(ap->cred, cr) == 0 && ap->rnode == rp) {
ap->known |= acc;
ap->allowed &= ~acc;
ap->allowed |= resacc;
rw_exit(&hp->lock);
if (nap != NULL) {
crfree(nap->cred);
kmem_cache_free(acache4_cache, nap);
}
return;
}
ap = ap->next;
}
}
if (nap != NULL) {
#ifdef DEBUG
clstat4_debug.access.value.ui64++;
#endif
nap->next = hp->next;
hp->next = nap;
nap->next->prev = nap;
nap->prev = (acache4_t *)hp;
mutex_enter(&rp->r_statelock);
nap->list = rp->r_acache;
rp->r_acache = nap;
mutex_exit(&rp->r_statelock);
}
rw_exit(&hp->lock);
}
int
nfs4_access_purge_rp(rnode4_t *rp)
{
acache4_t *ap, *tmpap, *rplist;
if (rp->r_acache == NULL)
return (0);
mutex_enter(&rp->r_statelock);
rplist = rp->r_acache;
rp->r_acache = NULL;
mutex_exit(&rp->r_statelock);
for (ap = rplist; ap != NULL; ap = tmpap) {
rw_enter(&ap->hashq->lock, RW_WRITER);
ap->prev->next = ap->next;
ap->next->prev = ap->prev;
rw_exit(&ap->hashq->lock);
tmpap = ap->list;
crfree(ap->cred);
kmem_cache_free(acache4_cache, ap);
#ifdef DEBUG
clstat4_debug.access.value.ui64--;
#endif
}
return (1);
}
int
nfs4_acache_init(void)
{
extern int rtable4size;
int i;
if (nacache > 0)
acache4size = 1 << highbit(nacache / acache4_hashlen);
else
acache4size = rtable4size;
acache4mask = acache4size - 1;
acache4 = kmem_alloc(acache4size * sizeof (*acache4), KM_SLEEP);
for (i = 0; i < acache4size; i++) {
acache4[i].next = (acache4_t *)&acache4[i];
acache4[i].prev = (acache4_t *)&acache4[i];
rw_init(&acache4[i].lock, NULL, RW_DEFAULT, NULL);
}
acache4_cache = kmem_cache_create("nfs4_access_cache",
sizeof (acache4_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
return (0);
}
int
nfs4_acache_fini(void)
{
int i;
kmem_cache_destroy(acache4_cache);
for (i = 0; i < acache4size; i++)
rw_destroy(&acache4[i].lock);
kmem_free(acache4, acache4size * sizeof (*acache4));
return (0);
}