#include <sys/param.h>
#include <sys/systm.h>
#include <sys/vnode.h>
#include <sys/lock.h>
#include <sys/namei.h>
#include <sys/errno.h>
#include <sys/pool.h>
long numcache;
long numneg;
TAILQ_HEAD(, namecache) nclruhead;
TAILQ_HEAD(, namecache) nclruneghead;
struct nchstats nchstats;
int doingcache = 1;
struct pool nch_pool;
void cache_zap(struct namecache *);
u_long nextvnodeid;
static inline int
namecache_compare(const struct namecache *n1, const struct namecache *n2)
{
if (n1->nc_nlen == n2->nc_nlen)
return (memcmp(n1->nc_name, n2->nc_name, n1->nc_nlen));
else
return (n1->nc_nlen - n2->nc_nlen);
}
RBT_PROTOTYPE(namecache_rb_cache, namecache, n_rbcache, namecache_compare);
RBT_GENERATE(namecache_rb_cache, namecache, n_rbcache, namecache_compare);
void
cache_tree_init(struct namecache_rb_cache *tree)
{
RBT_INIT(namecache_rb_cache, tree);
}
void
cache_zap(struct namecache *ncp)
{
struct vnode *dvp = NULL;
if (ncp->nc_vp != NULL) {
TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
numcache--;
} else {
TAILQ_REMOVE(&nclruneghead, ncp, nc_neg);
numneg--;
}
if (ncp->nc_dvp) {
RBT_REMOVE(namecache_rb_cache, &ncp->nc_dvp->v_nc_tree, ncp);
if (RBT_EMPTY(namecache_rb_cache, &ncp->nc_dvp->v_nc_tree))
dvp = ncp->nc_dvp;
}
if (ncp->nc_vp && (ncp->nc_vpid == ncp->nc_vp->v_id)) {
if (ncp->nc_vp != ncp->nc_dvp &&
ncp->nc_vp->v_type == VDIR &&
(ncp->nc_nlen > 2 ||
(ncp->nc_nlen > 1 &&
ncp->nc_name[1] != '.') ||
(ncp->nc_nlen > 0 &&
ncp->nc_name[0] != '.'))) {
TAILQ_REMOVE(&ncp->nc_vp->v_cache_dst, ncp, nc_me);
}
}
pool_put(&nch_pool, ncp);
if (dvp)
vdrop(dvp);
}
int
cache_lookup(struct vnode *dvp, struct vnode **vpp,
struct componentname *cnp)
{
struct namecache *ncp;
struct namecache n;
struct vnode *vp;
u_long vpid;
int error;
*vpp = NULL;
if (!doingcache) {
cnp->cn_flags &= ~MAKEENTRY;
return (-1);
}
if (cnp->cn_namelen > NAMECACHE_MAXLEN) {
nchstats.ncs_long++;
cnp->cn_flags &= ~MAKEENTRY;
return (-1);
}
n.nc_nlen = cnp->cn_namelen;
memcpy(n.nc_name, cnp->cn_nameptr, n.nc_nlen);
ncp = RBT_FIND(namecache_rb_cache, &dvp->v_nc_tree, &n);
if (ncp == NULL) {
nchstats.ncs_miss++;
return (-1);
}
if ((cnp->cn_flags & MAKEENTRY) == 0) {
nchstats.ncs_badhits++;
goto remove;
} else if (ncp->nc_vp == NULL) {
if (cnp->cn_nameiop != CREATE ||
(cnp->cn_flags & ISLASTCN) == 0) {
nchstats.ncs_neghits++;
if (TAILQ_NEXT(ncp, nc_neg) != NULL) {
TAILQ_REMOVE(&nclruneghead, ncp, nc_neg);
TAILQ_INSERT_TAIL(&nclruneghead, ncp,
nc_neg);
}
return (ENOENT);
} else {
nchstats.ncs_badhits++;
goto remove;
}
} else if (ncp->nc_vpid != ncp->nc_vp->v_id) {
nchstats.ncs_falsehits++;
goto remove;
}
if (TAILQ_NEXT(ncp, nc_lru) != NULL) {
TAILQ_REMOVE(&nclruhead, ncp, nc_lru);
TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
}
vp = ncp->nc_vp;
vpid = vp->v_id;
if (vp == dvp) {
vref(dvp);
error = 0;
} else if (cnp->cn_flags & ISDOTDOT) {
VOP_UNLOCK(dvp);
cnp->cn_flags |= PDIRUNLOCK;
error = vget(vp, LK_EXCLUSIVE);
if (!error && (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) == 0) {
if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0) {
vput(vp);
return (error);
}
cnp->cn_flags &= ~PDIRUNLOCK;
}
} else {
error = vget(vp, LK_EXCLUSIVE);
if (error || (~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
VOP_UNLOCK(dvp);
cnp->cn_flags |= PDIRUNLOCK;
}
}
if (error || vpid != vp->v_id) {
if (!error) {
vput(vp);
nchstats.ncs_falsehits++;
} else
nchstats.ncs_badhits++;
if (vp == dvp || error ||
(~cnp->cn_flags & (LOCKPARENT|ISLASTCN)) != 0) {
if ((error = vn_lock(dvp, LK_EXCLUSIVE)) != 0)
return (error);
cnp->cn_flags &= ~PDIRUNLOCK;
}
return (-1);
}
nchstats.ncs_goodhits++;
*vpp = vp;
return (0);
remove:
cache_zap(ncp);
return (-1);
}
int
cache_revlookup(struct vnode *vp, struct vnode **dvpp, char **bpp, char *bufp)
{
struct namecache *ncp;
struct vnode *dvp = NULL;
char *bp;
if (!doingcache)
goto out;
TAILQ_FOREACH(ncp, &vp->v_cache_dst, nc_me) {
dvp = ncp->nc_dvp;
if (dvp && dvp != vp && ncp->nc_dvpid == dvp->v_id)
goto found;
}
goto miss;
found:
#ifdef DIAGNOSTIC
if (ncp->nc_nlen == 1 &&
ncp->nc_name[0] == '.')
panic("cache_revlookup: found entry for .");
if (ncp->nc_nlen == 2 &&
ncp->nc_name[0] == '.' &&
ncp->nc_name[1] == '.')
panic("cache_revlookup: found entry for ..");
#endif
nchstats.ncs_revhits++;
if (bufp != NULL) {
bp = *bpp;
bp -= ncp->nc_nlen;
if (bp <= bufp) {
*dvpp = NULL;
return (ERANGE);
}
memcpy(bp, ncp->nc_name, ncp->nc_nlen);
*bpp = bp;
}
*dvpp = dvp;
return (0);
miss:
nchstats.ncs_revmiss++;
out:
*dvpp = NULL;
return (-1);
}
void
cache_enter(struct vnode *dvp, struct vnode *vp, struct componentname *cnp)
{
struct namecache *ncp, *lncp;
if (!doingcache || cnp->cn_namelen > NAMECACHE_MAXLEN)
return;
if (numcache >= initialvnodes) {
if ((ncp = TAILQ_FIRST(&nclruhead)) != NULL)
cache_zap(ncp);
else if ((ncp = TAILQ_FIRST(&nclruneghead)) != NULL)
cache_zap(ncp);
else
panic("wtf? leak?");
}
ncp = pool_get(&nch_pool, PR_WAITOK|PR_ZERO);
ncp->nc_vp = vp;
if (vp)
ncp->nc_vpid = vp->v_id;
ncp->nc_dvp = dvp;
ncp->nc_dvpid = dvp->v_id;
ncp->nc_nlen = cnp->cn_namelen;
memcpy(ncp->nc_name, cnp->cn_nameptr, ncp->nc_nlen);
if (RBT_EMPTY(namecache_rb_cache, &dvp->v_nc_tree)) {
vhold(dvp);
}
if ((lncp = RBT_INSERT(namecache_rb_cache, &dvp->v_nc_tree, ncp))
!= NULL) {
pool_put(&nch_pool, ncp);
goto done;
}
if (vp) {
TAILQ_INSERT_TAIL(&nclruhead, ncp, nc_lru);
numcache++;
if (vp != dvp && vp->v_type == VDIR &&
(ncp->nc_nlen > 2 ||
(ncp->nc_nlen > 1 &&
ncp->nc_name[1] != '.') ||
(ncp->nc_nlen > 0 &&
ncp->nc_name[0] != '.')))
TAILQ_INSERT_TAIL(&vp->v_cache_dst, ncp,
nc_me);
} else {
TAILQ_INSERT_TAIL(&nclruneghead, ncp, nc_neg);
numneg++;
}
if (numneg > initialvnodes) {
if ((ncp = TAILQ_FIRST(&nclruneghead))
!= NULL)
cache_zap(ncp);
}
done:
return;
}
void
nchinit(void)
{
TAILQ_INIT(&nclruhead);
TAILQ_INIT(&nclruneghead);
pool_init(&nch_pool, sizeof(struct namecache), 0, IPL_NONE, PR_WAITOK,
"nchpl", NULL);
}
void
cache_purge(struct vnode *vp)
{
struct namecache *ncp;
KASSERT(vp->v_type == VDIR || TAILQ_EMPTY(&vp->v_cache_dst));
while ((ncp = TAILQ_FIRST(&vp->v_cache_dst)))
cache_zap(ncp);
while ((ncp = RBT_ROOT(namecache_rb_cache, &vp->v_nc_tree)))
cache_zap(ncp);
vp->v_id = ++nextvnodeid;
if (vp->v_id == 0)
vp->v_id = ++nextvnodeid;
}
void
cache_purgevfs(struct mount *mp)
{
struct namecache *ncp, *nxtcp;
TAILQ_FOREACH_SAFE(ncp, &nclruhead, nc_lru, nxtcp) {
if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp)
continue;
cache_zap(ncp);
}
TAILQ_FOREACH_SAFE(ncp, &nclruneghead, nc_neg, nxtcp) {
if (ncp->nc_dvp == NULL || ncp->nc_dvp->v_mount != mp)
continue;
cache_zap(ncp);
}
}