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);
243 VN_HOLD((vnode_t *) tvc);
247 ReleaseReadLock(&afs_xvcache);
251 * XX If LRUme ever is non-zero change the if statement around because
252 * aix's cc with optimizer on won't necessarily check things in order XX
254 if (LRUme && (0 == NBObtainWriteLock(&afs_xdnlc))) {
255 /* don't block to do this */
256 /* tnc might have been moved during race condition, */
257 /* but it's always in a legit hash chain when a lock is granted,
258 * or else it's on the freelist so prev == NULL,
259 * so at worst this is redundant */
260 /* Now that we've got it held, and a lock on the dnlc, we
261 * should check to be sure that there was no race, and
262 * bail out if there was. */
264 /* special case for only two elements on list - relative ordering
266 if (tnc->prev != tnc->next) {
267 /* remove from old location */
268 tnc->prev->next = tnc->next;
269 tnc->next->prev = tnc->prev;
270 /* insert into new location */
271 tnc->next = nameHash[skey];
272 tnc->prev = tnc->next->prev;
273 tnc->next->prev = tnc;
274 tnc->prev->next = tnc;
276 nameHash[skey] = tnc;
278 ReleaseWriteLock(&afs_xdnlc);
288 RemoveEntry(struct nc *tnc, unsigned int key)
290 if (!tnc->prev) /* things on freelist always have null prev ptrs */
291 osi_Panic("bogus free list");
293 TRACE(RemoveEntryT, key);
294 if (tnc == tnc->next) { /* only one in list */
295 nameHash[key] = NULL;
297 if (tnc == nameHash[key])
298 nameHash[key] = tnc->next;
299 tnc->prev->next = tnc->next;
300 tnc->next->prev = tnc->prev;
303 tnc->prev = NULL; /* everything not in hash table has 0 prev */
304 tnc->key = 0; /* just for safety's sake */
309 osi_dnlc_remove(struct vcache *adp, char *aname, struct vcache *avc)
311 unsigned int key, skey;
318 TRACE(osi_dnlc_removeT, skey);
319 dnlcHash(ts, key); /* leaves ts pointing at the NULL */
320 if (ts - aname >= AFSNCNAMESIZE) {
323 skey = key & (NHSIZE - 1);
324 TRACE(osi_dnlc_removeT, skey);
326 ObtainReadLock(&afs_xdnlc);
328 for (tnc = nameHash[skey]; tnc; tnc = tnc->next) {
329 if ((tnc->dirp == adp) && (tnc->key == key)
330 && (!strcmp((char *)tnc->name, aname))) {
331 tnc->dirp = NULL; /* now it won't match anything */
333 } else if (tnc->next == nameHash[skey]) { /* end of list */
338 ReleaseReadLock(&afs_xdnlc);
343 /* there is a little race condition here, but it's relatively
344 * harmless. At worst, I wind up removing a mapping that I just
346 if (EWOULDBLOCK == NBObtainWriteLock(&afs_xdnlc, 1)) {
347 return 0; /* no big deal, tnc will get recycled eventually */
349 RemoveEntry(tnc, skey);
350 tnc->next = ncfreelist;
352 ReleaseWriteLock(&afs_xdnlc);
357 /* remove anything pertaining to this directory. I can invalidate
358 * things without the lock, since I am just looking through the array,
359 * but to move things off the lists or into the freelist, I need the
362 osi_dnlc_purgedp(struct vcache *adp)
371 TRACE(osi_dnlc_purgedpT, 0);
372 writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 2));
374 for (i = 0; i < NCSIZE; i++) {
375 if ((nameCache[i].dirp == adp) || (nameCache[i].vp == adp)) {
376 nameCache[i].dirp = nameCache[i].vp = NULL;
377 if (writelocked && nameCache[i].prev) {
378 RemoveEntry(&nameCache[i], nameCache[i].key & (NHSIZE - 1));
379 nameCache[i].next = ncfreelist;
380 ncfreelist = &nameCache[i];
385 ReleaseWriteLock(&afs_xdnlc);
390 /* remove anything pertaining to this file */
392 osi_dnlc_purgevp(struct vcache *avc)
401 TRACE(osi_dnlc_purgevpT, 0);
402 writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 3));
404 for (i = 0; i < NCSIZE; i++) {
405 if ((nameCache[i].vp == avc)) {
406 nameCache[i].dirp = nameCache[i].vp = NULL;
407 /* can't simply break; because of hard links -- might be two */
408 /* different entries with same vnode */
409 if (writelocked && nameCache[i].prev) {
410 RemoveEntry(&nameCache[i], nameCache[i].key & (NHSIZE - 1));
411 nameCache[i].next = ncfreelist;
412 ncfreelist = &nameCache[i];
417 ReleaseWriteLock(&afs_xdnlc);
422 /* remove everything */
429 TRACE(osi_dnlc_purgeT, 0);
430 if (EWOULDBLOCK == NBObtainWriteLock(&afs_xdnlc, 4)) { /* couldn't get lock */
431 for (i = 0; i < NCSIZE; i++)
432 nameCache[i].dirp = nameCache[i].vp = NULL;
433 } else { /* did get the lock */
435 memset((char *)nameCache, 0, sizeof(struct nc) * NCSIZE);
436 memset((char *)nameHash, 0, sizeof(struct nc *) * NHSIZE);
437 for (i = 0; i < NCSIZE; i++) {
438 nameCache[i].next = ncfreelist;
439 ncfreelist = &nameCache[i];
441 ReleaseWriteLock(&afs_xdnlc);
447 /* remove everything referencing a specific volume */
449 osi_dnlc_purgevol(struct VenusFid *fidp)
452 dnlcstats.purgevols++;
463 Lock_Init(&afs_xdnlc);
464 memset((char *)&dnlcstats, 0, sizeof(dnlcstats));
465 memset((char *)dnlctracetable, 0, sizeof(dnlctracetable));
467 ObtainWriteLock(&afs_xdnlc, 223);
469 memset((char *)nameCache, 0, sizeof(struct nc) * NCSIZE);
470 memset((char *)nameHash, 0, sizeof(struct nc *) * NHSIZE);
471 for (i = 0; i < NCSIZE; i++) {
472 nameCache[i].next = ncfreelist;
473 ncfreelist = &nameCache[i];
475 ReleaseWriteLock(&afs_xdnlc);
481 osi_dnlc_shutdown(void)