2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include "afs/param.h"
14 #include "afs/sysincludes.h" /*Standard vendor system headers */
15 #include "afsincludes.h" /*AFS-based standard headers */
18 #include "afs/afs_stats.h"
19 #include "afs/afs_osidnlc.h"
22 * also cache failed lookups.
23 * look into interactions of dnlc and readdir.
24 * cache larger names, perhaps by using a better,longer key (SHA) and discarding
25 * the actual name itself.
26 * precompute a key and stuff for \sys, and combine the HandleAtName function with
27 * this, since we're looking at the name anyway.
30 struct afs_lock afs_xdnlc;
31 extern struct afs_lock afs_xvcache;
33 dnlcstats_t dnlcstats;
36 #define NHSIZE 256 /* must be power of 2. == NHASHENT in dir package */
37 struct nc *ncfreelist = NULL;
38 static struct nc nameCache[NCSIZE];
39 struct nc *nameHash[NHSIZE];
40 /* Hash table invariants:
41 * 1. If nameHash[i] is NULL, list is empty
42 * 2. A single element in a hash bucket has itself as prev and next.
45 typedef enum { osi_dnlc_enterT, InsertEntryT, osi_dnlc_lookupT,
46 ScavengeEntryT, osi_dnlc_removeT, RemoveEntryT, osi_dnlc_purgedpT,
47 osi_dnlc_purgevpT, osi_dnlc_purgeT
57 struct dt dnlctracetable[256];
60 #define TRACE(e,s) /* if (dnlct == 256) dnlct = 0; dnlctracetable[dnlct].event = e; dnlctracetable[dnlct++].slot = s; */
62 #define dnlcHash(ts, hval) for (hval=0; *ts; ts++) { hval *= 173; hval += *ts; }
67 static unsigned int nameptr = 0; /* next bucket to pull something from */
73 ncfreelist = tnc->next;
77 for (j = 0; j < NHSIZE + 2; j++, nameptr++) {
78 if (nameptr >= NHSIZE)
80 if (nameHash[nameptr])
84 TRACE(ScavengeEntryT, nameptr);
85 tnc = nameHash[nameptr];
86 if (!tnc) /* May want to consider changing this to return 0 */
87 osi_Panic("null tnc in GetMeAnEntry");
89 if (tnc->prev == tnc) { /* only thing in list, don't screw around */
90 nameHash[nameptr] = NULL;
94 tnc = tnc->prev; /* grab oldest one in this bucket */
95 /* remove it from list */
96 tnc->next->prev = tnc->prev;
97 tnc->prev->next = tnc->next;
103 InsertEntry(struct nc *tnc)
106 key = tnc->key & (NHSIZE - 1);
108 TRACE(InsertEntryT, key);
109 if (!nameHash[key]) {
111 tnc->next = tnc->prev = tnc;
113 tnc->next = nameHash[key];
114 tnc->prev = tnc->next->prev;
115 tnc->next->prev = tnc;
116 tnc->prev->next = tnc;
123 osi_dnlc_enter(struct vcache *adp, char *aname, struct vcache *avc,
127 unsigned int key, skey;
134 TRACE(osi_dnlc_enterT, 0);
135 dnlcHash(ts, key); /* leaves ts pointing at the NULL */
136 if (ts - aname >= AFSNCNAMESIZE) {
139 skey = key & (NHSIZE - 1);
143 ObtainWriteLock(&afs_xdnlc, 222);
145 /* Only cache entries from the latest version of the directory */
146 if (!(adp->f.states & CStatd) || !hsame(*avno, adp->f.m.DataVersion)) {
147 ReleaseWriteLock(&afs_xdnlc);
152 * Make sure each directory entry gets cached no more than once.
154 for (tnc = nameHash[skey], safety = 0; tnc; tnc = tnc->next, safety++) {
155 if ((tnc->dirp == adp) && (!strcmp((char *)tnc->name, aname))) {
156 /* duplicate entry */
158 } else if (tnc->next == nameHash[skey]) { /* end of list */
161 } else if (safety > NCSIZE) {
162 afs_warn("DNLC cycle");
164 ReleaseWriteLock(&afs_xdnlc);
171 tnc = GetMeAnEntry();
176 memcpy((char *)tnc->name, aname, ts - aname + 1); /* include the NULL */
183 ReleaseWriteLock(&afs_xdnlc);
190 osi_dnlc_lookup(struct vcache *adp, char *aname, int locktype)
194 unsigned int key, skey;
196 struct nc *tnc, *tnc1 = 0;
198 #ifdef AFS_DARWIN80_ENV
205 dnlcHash(ts, key); /* leaves ts pointing at the NULL */
206 if (ts - aname >= AFSNCNAMESIZE)
208 skey = key & (NHSIZE - 1);
210 TRACE(osi_dnlc_lookupT, skey);
213 ObtainReadLock(&afs_xvcache);
214 ObtainReadLock(&afs_xdnlc);
216 for (tvc = NULL, tnc = nameHash[skey], safety = 0; tnc;
217 tnc = tnc->next, safety++) {
218 if ( /* (tnc->key == key) && */ (tnc->dirp == adp)
219 && (!strcmp((char *)tnc->name, aname))) {
223 } else if (tnc->next == nameHash[skey]) { /* end of list */
225 } else if (safety > NCSIZE) {
226 afs_warn("DNLC cycle");
228 ReleaseReadLock(&afs_xdnlc);
229 ReleaseReadLock(&afs_xvcache);
235 LRUme = 0; /* (tnc != nameHash[skey]); */
236 ReleaseReadLock(&afs_xdnlc);
239 ReleaseReadLock(&afs_xvcache);
242 if ((tvc->f.states & CVInit)
243 #ifdef AFS_DARWIN80_ENV
244 ||(tvc->f.states & CDeadVnode)
248 ReleaseReadLock(&afs_xvcache);
250 osi_dnlc_remove(adp, aname, tvc);
253 #if defined(AFS_DARWIN80_ENV)
255 if (vnode_get(tvp)) {
256 ReleaseReadLock(&afs_xvcache);
258 osi_dnlc_remove(adp, aname, tvc);
261 if (vnode_ref(tvp)) {
262 ReleaseReadLock(&afs_xvcache);
267 osi_dnlc_remove(adp, aname, tvc);
273 ReleaseReadLock(&afs_xvcache);
277 * XX If LRUme ever is non-zero change the if statement around because
278 * aix's cc with optimizer on won't necessarily check things in order XX
280 if (LRUme && (0 == NBObtainWriteLock(&afs_xdnlc))) {
281 /* don't block to do this */
282 /* tnc might have been moved during race condition, */
283 /* but it's always in a legit hash chain when a lock is granted,
284 * or else it's on the freelist so prev == NULL,
285 * so at worst this is redundant */
286 /* Now that we've got it held, and a lock on the dnlc, we
287 * should check to be sure that there was no race, and
288 * bail out if there was. */
290 /* special case for only two elements on list - relative ordering
292 if (tnc->prev != tnc->next) {
293 /* remove from old location */
294 tnc->prev->next = tnc->next;
295 tnc->next->prev = tnc->prev;
296 /* insert into new location */
297 tnc->next = nameHash[skey];
298 tnc->prev = tnc->next->prev;
299 tnc->next->prev = tnc;
300 tnc->prev->next = tnc;
302 nameHash[skey] = tnc;
304 ReleaseWriteLock(&afs_xdnlc);
314 RemoveEntry(struct nc *tnc, unsigned int key)
316 if (!tnc->prev) /* things on freelist always have null prev ptrs */
317 osi_Panic("bogus free list");
319 TRACE(RemoveEntryT, key);
320 if (tnc == tnc->next) { /* only one in list */
321 nameHash[key] = NULL;
323 if (tnc == nameHash[key])
324 nameHash[key] = tnc->next;
325 tnc->prev->next = tnc->next;
326 tnc->next->prev = tnc->prev;
329 tnc->prev = NULL; /* everything not in hash table has 0 prev */
330 tnc->key = 0; /* just for safety's sake */
335 osi_dnlc_remove(struct vcache *adp, char *aname, struct vcache *avc)
337 unsigned int key, skey;
344 TRACE(osi_dnlc_removeT, skey);
345 dnlcHash(ts, key); /* leaves ts pointing at the NULL */
346 if (ts - aname >= AFSNCNAMESIZE) {
349 skey = key & (NHSIZE - 1);
350 TRACE(osi_dnlc_removeT, skey);
352 ObtainReadLock(&afs_xdnlc);
354 for (tnc = nameHash[skey]; tnc; tnc = tnc->next) {
355 if ((tnc->dirp == adp) && (tnc->key == key)
356 && (!strcmp((char *)tnc->name, aname))) {
357 tnc->dirp = NULL; /* now it won't match anything */
359 } else if (tnc->next == nameHash[skey]) { /* end of list */
364 ReleaseReadLock(&afs_xdnlc);
369 /* there is a little race condition here, but it's relatively
370 * harmless. At worst, I wind up removing a mapping that I just
372 if (EWOULDBLOCK == NBObtainWriteLock(&afs_xdnlc, 1)) {
373 return 0; /* no big deal, tnc will get recycled eventually */
375 RemoveEntry(tnc, skey);
376 tnc->next = ncfreelist;
378 ReleaseWriteLock(&afs_xdnlc);
384 * Remove anything pertaining to this directory. I can invalidate
385 * things without the lock, since I am just looking through the array,
386 * but to move things off the lists or into the freelist, I need the
389 * \param adp vcache entry for the directory to be purged.
393 osi_dnlc_purgedp(struct vcache *adp)
398 #ifdef AFS_DARWIN_ENV
399 if (!(adp->f.states & (CVInit | CVFlushed
400 #ifdef AFS_DARWIN80_ENV
404 cache_purge(AFSTOV(adp));
411 TRACE(osi_dnlc_purgedpT, 0);
412 writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 2));
414 for (i = 0; i < NCSIZE; i++) {
415 if ((nameCache[i].dirp == adp) || (nameCache[i].vp == adp)) {
416 nameCache[i].dirp = nameCache[i].vp = NULL;
417 if (writelocked && nameCache[i].prev) {
418 RemoveEntry(&nameCache[i], nameCache[i].key & (NHSIZE - 1));
419 nameCache[i].next = ncfreelist;
420 ncfreelist = &nameCache[i];
425 ReleaseWriteLock(&afs_xdnlc);
431 * Remove anything pertaining to this file
433 * \param File vcache entry.
437 osi_dnlc_purgevp(struct vcache *avc)
442 #ifdef AFS_DARWIN_ENV
443 if (!(avc->f.states & (CVInit | CVFlushed
444 #ifdef AFS_DARWIN80_ENV
448 cache_purge(AFSTOV(avc));
455 TRACE(osi_dnlc_purgevpT, 0);
456 writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 3));
458 for (i = 0; i < NCSIZE; i++) {
459 if ((nameCache[i].vp == avc)) {
460 nameCache[i].dirp = nameCache[i].vp = NULL;
461 /* can't simply break; because of hard links -- might be two */
462 /* different entries with same vnode */
463 if (writelocked && nameCache[i].prev) {
464 RemoveEntry(&nameCache[i], nameCache[i].key & (NHSIZE - 1));
465 nameCache[i].next = ncfreelist;
466 ncfreelist = &nameCache[i];
471 ReleaseWriteLock(&afs_xdnlc);
476 /* remove everything */
483 TRACE(osi_dnlc_purgeT, 0);
484 if (EWOULDBLOCK == NBObtainWriteLock(&afs_xdnlc, 4)) { /* couldn't get lock */
485 for (i = 0; i < NCSIZE; i++)
486 nameCache[i].dirp = nameCache[i].vp = NULL;
487 } else { /* did get the lock */
489 memset(nameCache, 0, sizeof(struct nc) * NCSIZE);
490 memset(nameHash, 0, sizeof(struct nc *) * NHSIZE);
491 for (i = 0; i < NCSIZE; i++) {
492 nameCache[i].next = ncfreelist;
493 ncfreelist = &nameCache[i];
495 ReleaseWriteLock(&afs_xdnlc);
501 /* remove everything referencing a specific volume */
503 osi_dnlc_purgevol(struct VenusFid *fidp)
506 dnlcstats.purgevols++;
517 Lock_Init(&afs_xdnlc);
518 memset(&dnlcstats, 0, sizeof(dnlcstats));
519 memset(dnlctracetable, 0, sizeof(dnlctracetable));
521 ObtainWriteLock(&afs_xdnlc, 223);
523 memset(nameCache, 0, sizeof(struct nc) * NCSIZE);
524 memset(nameHash, 0, sizeof(struct nc *) * NHSIZE);
525 for (i = 0; i < NCSIZE; i++) {
526 nameCache[i].next = ncfreelist;
527 ncfreelist = &nameCache[i];
529 ReleaseWriteLock(&afs_xdnlc);
535 osi_dnlc_shutdown(void)