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"
16 #include "afs/sysincludes.h" /*Standard vendor system headers */
17 #include "afsincludes.h" /*AFS-based standard headers */
20 #include "afs/afs_stats.h"
21 #include "afs/afs_osidnlc.h"
24 * also cache failed lookups.
25 * look into interactions of dnlc and readdir.
26 * cache larger names, perhaps by using a better,longer key (SHA) and discarding
27 * the actual name itself.
28 * precompute a key and stuff for @sys, and combine the HandleAtName function with
29 * this, since we're looking at the name anyway.
32 struct afs_lock afs_xdnlc;
33 extern struct afs_lock afs_xvcache;
35 dnlcstats_t dnlcstats;
38 #define NHSIZE 256 /* must be power of 2. == NHASHENT in dir package */
39 struct nc *ncfreelist = NULL;
40 static struct nc nameCache[NCSIZE];
41 struct nc *nameHash[NHSIZE];
42 /* Hash table invariants:
43 * 1. If nameHash[i] is NULL, list is empty
44 * 2. A single element in a hash bucket has itself as prev and next.
47 typedef enum { osi_dnlc_enterT, InsertEntryT, osi_dnlc_lookupT,
48 ScavengeEntryT, osi_dnlc_removeT, RemoveEntryT, osi_dnlc_purgedpT,
49 osi_dnlc_purgevpT, osi_dnlc_purgeT
59 struct dt dnlctracetable[256];
62 #define TRACE(e,s) /* if (dnlct == 256) dnlct = 0; dnlctracetable[dnlct].event = e; dnlctracetable[dnlct++].slot = s; */
64 #define dnlcHash(ts, hval) for (hval=0; *ts; ts++) { hval *= 173; hval += *ts; }
69 static unsigned int nameptr = 0; /* next bucket to pull something from */
75 ncfreelist = tnc->next;
79 for (j = 0; j < NHSIZE + 2; j++, nameptr++) {
80 if (nameptr >= NHSIZE)
82 if (nameHash[nameptr])
86 TRACE(ScavengeEntryT, nameptr);
87 tnc = nameHash[nameptr];
88 if (!tnc) /* May want to consider changing this to return 0 */
89 osi_Panic("null tnc in GetMeAnEntry");
91 if (tnc->prev == tnc) { /* only thing in list, don't screw around */
92 nameHash[nameptr] = NULL;
96 tnc = tnc->prev; /* grab oldest one in this bucket */
97 /* remove it from list */
98 tnc->next->prev = tnc->prev;
99 tnc->prev->next = tnc->next;
105 InsertEntry(struct nc *tnc)
108 key = tnc->key & (NHSIZE - 1);
110 TRACE(InsertEntryT, key);
111 if (!nameHash[key]) {
113 tnc->next = tnc->prev = tnc;
115 tnc->next = nameHash[key];
116 tnc->prev = tnc->next->prev;
117 tnc->next->prev = tnc;
118 tnc->prev->next = tnc;
125 osi_dnlc_enter(struct vcache *adp, char *aname, struct vcache *avc,
129 unsigned int key, skey;
136 TRACE(osi_dnlc_enterT, 0);
137 dnlcHash(ts, key); /* leaves ts pointing at the NULL */
138 if (ts - aname >= AFSNCNAMESIZE) {
141 skey = key & (NHSIZE - 1);
145 ObtainWriteLock(&afs_xdnlc, 222);
147 /* Only cache entries from the latest version of the directory */
148 if (!(adp->states & CStatd) || !hsame(*avno, adp->m.DataVersion)) {
149 ReleaseWriteLock(&afs_xdnlc);
154 * Make sure each directory entry gets cached no more than once.
156 for (tnc = nameHash[skey], safety = 0; tnc; tnc = tnc->next, safety++) {
157 if ((tnc->dirp == adp) && (!strcmp((char *)tnc->name, aname))) {
158 /* duplicate entry */
160 } else if (tnc->next == nameHash[skey]) { /* end of list */
163 } else if (safety > NCSIZE) {
164 afs_warn("DNLC cycle");
166 ReleaseWriteLock(&afs_xdnlc);
173 tnc = GetMeAnEntry();
178 memcpy((char *)tnc->name, aname, ts - aname + 1); /* include the NULL */
185 ReleaseWriteLock(&afs_xdnlc);
192 osi_dnlc_lookup(struct vcache *adp, char *aname, int locktype)
196 unsigned int key, skey;
198 struct nc *tnc, *tnc1 = 0;
204 dnlcHash(ts, key); /* leaves ts pointing at the NULL */
205 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->states & CVInit) {
243 ReleaseReadLock(&afs_xvcache);
245 osi_dnlc_remove(adp, aname, tvc);
249 VN_HOLD((vnode_t *) tvc);
251 #ifdef AFS_DARWIN80_ENV
252 if (vnode_get(tvc->v)) {
253 ReleaseReadLock(&afs_xvcache);
255 osi_dnlc_remove(adp, aname, tvc);
261 ReleaseReadLock(&afs_xvcache);
265 * XX If LRUme ever is non-zero change the if statement around because
266 * aix's cc with optimizer on won't necessarily check things in order XX
268 if (LRUme && (0 == NBObtainWriteLock(&afs_xdnlc))) {
269 /* don't block to do this */
270 /* tnc might have been moved during race condition, */
271 /* but it's always in a legit hash chain when a lock is granted,
272 * or else it's on the freelist so prev == NULL,
273 * so at worst this is redundant */
274 /* Now that we've got it held, and a lock on the dnlc, we
275 * should check to be sure that there was no race, and
276 * bail out if there was. */
278 /* special case for only two elements on list - relative ordering
280 if (tnc->prev != tnc->next) {
281 /* remove from old location */
282 tnc->prev->next = tnc->next;
283 tnc->next->prev = tnc->prev;
284 /* insert into new location */
285 tnc->next = nameHash[skey];
286 tnc->prev = tnc->next->prev;
287 tnc->next->prev = tnc;
288 tnc->prev->next = tnc;
290 nameHash[skey] = tnc;
292 ReleaseWriteLock(&afs_xdnlc);
302 RemoveEntry(struct nc *tnc, unsigned int key)
304 if (!tnc->prev) /* things on freelist always have null prev ptrs */
305 osi_Panic("bogus free list");
307 TRACE(RemoveEntryT, key);
308 if (tnc == tnc->next) { /* only one in list */
309 nameHash[key] = NULL;
311 if (tnc == nameHash[key])
312 nameHash[key] = tnc->next;
313 tnc->prev->next = tnc->next;
314 tnc->next->prev = tnc->prev;
317 tnc->prev = NULL; /* everything not in hash table has 0 prev */
318 tnc->key = 0; /* just for safety's sake */
323 osi_dnlc_remove(struct vcache *adp, char *aname, struct vcache *avc)
325 unsigned int key, skey;
332 TRACE(osi_dnlc_removeT, skey);
333 dnlcHash(ts, key); /* leaves ts pointing at the NULL */
334 if (ts - aname >= AFSNCNAMESIZE) {
337 skey = key & (NHSIZE - 1);
338 TRACE(osi_dnlc_removeT, skey);
340 ObtainReadLock(&afs_xdnlc);
342 for (tnc = nameHash[skey]; tnc; tnc = tnc->next) {
343 if ((tnc->dirp == adp) && (tnc->key == key)
344 && (!strcmp((char *)tnc->name, aname))) {
345 tnc->dirp = NULL; /* now it won't match anything */
347 } else if (tnc->next == nameHash[skey]) { /* end of list */
352 ReleaseReadLock(&afs_xdnlc);
357 /* there is a little race condition here, but it's relatively
358 * harmless. At worst, I wind up removing a mapping that I just
360 if (EWOULDBLOCK == NBObtainWriteLock(&afs_xdnlc, 1)) {
361 return 0; /* no big deal, tnc will get recycled eventually */
363 RemoveEntry(tnc, skey);
364 tnc->next = ncfreelist;
366 ReleaseWriteLock(&afs_xdnlc);
371 /* remove anything pertaining to this directory. I can invalidate
372 * things without the lock, since I am just looking through the array,
373 * but to move things off the lists or into the freelist, I need the
376 osi_dnlc_purgedp(struct vcache *adp)
381 #ifdef AFS_DARWIN_ENV
382 if (!(adp->states & (CVInit | CVFlushed
383 #ifdef AFS_DARWIN80_ENV
387 cache_purge(AFSTOV(adp));
394 TRACE(osi_dnlc_purgedpT, 0);
395 writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 2));
397 for (i = 0; i < NCSIZE; i++) {
398 if ((nameCache[i].dirp == adp) || (nameCache[i].vp == adp)) {
399 nameCache[i].dirp = nameCache[i].vp = NULL;
400 if (writelocked && nameCache[i].prev) {
401 RemoveEntry(&nameCache[i], nameCache[i].key & (NHSIZE - 1));
402 nameCache[i].next = ncfreelist;
403 ncfreelist = &nameCache[i];
408 ReleaseWriteLock(&afs_xdnlc);
413 /* remove anything pertaining to this file */
415 osi_dnlc_purgevp(struct vcache *avc)
420 #ifdef AFS_DARWIN_ENV
421 if (!(avc->states & (CVInit | CVFlushed
422 #ifdef AFS_DARWIN80_ENV
426 cache_purge(AFSTOV(avc));
433 TRACE(osi_dnlc_purgevpT, 0);
434 writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 3));
436 for (i = 0; i < NCSIZE; i++) {
437 if ((nameCache[i].vp == avc)) {
438 nameCache[i].dirp = nameCache[i].vp = NULL;
439 /* can't simply break; because of hard links -- might be two */
440 /* different entries with same vnode */
441 if (writelocked && nameCache[i].prev) {
442 RemoveEntry(&nameCache[i], nameCache[i].key & (NHSIZE - 1));
443 nameCache[i].next = ncfreelist;
444 ncfreelist = &nameCache[i];
449 ReleaseWriteLock(&afs_xdnlc);
454 /* remove everything */
461 TRACE(osi_dnlc_purgeT, 0);
462 if (EWOULDBLOCK == NBObtainWriteLock(&afs_xdnlc, 4)) { /* couldn't get lock */
463 for (i = 0; i < NCSIZE; i++)
464 nameCache[i].dirp = nameCache[i].vp = NULL;
465 } else { /* did get the lock */
467 memset((char *)nameCache, 0, sizeof(struct nc) * NCSIZE);
468 memset((char *)nameHash, 0, sizeof(struct nc *) * NHSIZE);
469 for (i = 0; i < NCSIZE; i++) {
470 nameCache[i].next = ncfreelist;
471 ncfreelist = &nameCache[i];
473 ReleaseWriteLock(&afs_xdnlc);
479 /* remove everything referencing a specific volume */
481 osi_dnlc_purgevol(struct VenusFid *fidp)
484 dnlcstats.purgevols++;
495 Lock_Init(&afs_xdnlc);
496 memset((char *)&dnlcstats, 0, sizeof(dnlcstats));
497 memset((char *)dnlctracetable, 0, sizeof(dnlctracetable));
499 ObtainWriteLock(&afs_xdnlc, 223);
501 memset((char *)nameCache, 0, sizeof(struct nc) * NCSIZE);
502 memset((char *)nameHash, 0, sizeof(struct nc *) * NHSIZE);
503 for (i = 0; i < NCSIZE; i++) {
504 nameCache[i].next = ncfreelist;
505 ncfreelist = &nameCache[i];
507 ReleaseWriteLock(&afs_xdnlc);
513 osi_dnlc_shutdown(void)