wiki url changed
[openafs.git] / src / afs / afs_osidnlc.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include "afs/sysincludes.h"    /*Standard vendor system headers */
15 #include "afsincludes.h"        /*AFS-based standard headers */
16 #include "afs/afs.h"
17 #include "afs/lock.h"
18 #include "afs/afs_stats.h"
19 #include "afs/afs_osidnlc.h"
20
21 /* Things to do:
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.  
28  */
29
30 struct afs_lock afs_xdnlc;
31 extern struct afs_lock afs_xvcache;
32
33 dnlcstats_t dnlcstats;
34
35 #define NCSIZE 300
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.
43  */
44
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
48 } traceevt;
49
50 int afs_usednlc = 1;
51
52 struct dt {
53     traceevt event;
54     unsigned char slot;
55 };
56
57 struct dt dnlctracetable[256];
58 int dnlct;
59
60 #define TRACE(e,s)              /* if (dnlct == 256) dnlct = 0; dnlctracetable[dnlct].event = e; dnlctracetable[dnlct++].slot = s; */
61
62 #define dnlcHash(ts, hval) for (hval=0; *ts; ts++) { hval *= 173;  hval  += *ts;   }
63
64 #if defined(AFS_FBSD80_ENV) && !defined(UKERNEL)
65 #define ma_critical_enter critical_enter
66 #define ma_critical_exit critical_exit
67 #else
68 #define ma_critical_enter() {}
69 #define ma_critical_exit() {}
70 #endif
71
72 static struct nc *
73 GetMeAnEntry(void)
74 {
75     static unsigned int nameptr = 0;    /* next bucket to pull something from */
76     struct nc *tnc;
77     int j;
78
79     if (ncfreelist) {
80         tnc = ncfreelist;
81         ncfreelist = tnc->next;
82         return tnc;
83     }
84
85     for (j = 0; j < NHSIZE + 2; j++, nameptr++) {
86         if (nameptr >= NHSIZE)
87             nameptr = 0;
88         if (nameHash[nameptr])
89             break;
90     }
91
92     TRACE(ScavengeEntryT, nameptr);
93     tnc = nameHash[nameptr];
94     if (!tnc)                   /* May want to consider changing this to return 0 */
95         osi_Panic("null tnc in GetMeAnEntry");
96
97     if (tnc->prev == tnc) {     /* only thing in list, don't screw around */
98         nameHash[nameptr] = NULL;
99         return (tnc);
100     }
101
102     tnc = tnc->prev;            /* grab oldest one in this bucket */
103     /* remove it from list */
104     tnc->next->prev = tnc->prev;
105     tnc->prev->next = tnc->next;
106
107     return (tnc);
108 }
109
110 static void
111 InsertEntry(struct nc *tnc)
112 {
113     unsigned int key;
114     key = tnc->key & (NHSIZE - 1);
115
116     TRACE(InsertEntryT, key);
117     if (!nameHash[key]) {
118         nameHash[key] = tnc;
119         tnc->next = tnc->prev = tnc;
120     } else {
121         tnc->next = nameHash[key];
122         tnc->prev = tnc->next->prev;
123         tnc->next->prev = tnc;
124         tnc->prev->next = tnc;
125         nameHash[key] = tnc;
126     }
127 }
128
129
130 int
131 osi_dnlc_enter(struct vcache *adp, char *aname, struct vcache *avc,
132                afs_hyper_t * avno)
133 {
134     struct nc *tnc;
135     unsigned int key, skey;
136     char *ts = aname;
137     int safety;
138
139     if (!afs_usednlc)
140         return 0;
141
142     TRACE(osi_dnlc_enterT, 0);
143     dnlcHash(ts, key);          /* leaves ts pointing at the NULL */
144     if (ts - aname >= AFSNCNAMESIZE) {
145         return 0;
146     }
147     skey = key & (NHSIZE - 1);
148     dnlcstats.enters++;
149
150   retry:
151     ObtainWriteLock(&afs_xdnlc, 222);
152
153     /* Only cache entries from the latest version of the directory */
154     if (!(adp->f.states & CStatd) || !hsame(*avno, adp->f.m.DataVersion)) {
155         ReleaseWriteLock(&afs_xdnlc);
156         return 0;
157     }
158
159     /*
160      * Make sure each directory entry gets cached no more than once.
161      */
162     for (tnc = nameHash[skey], safety = 0; tnc; tnc = tnc->next, safety++) {
163         if ((tnc->dirp == adp) && (!strcmp((char *)tnc->name, aname))) {
164             /* duplicate entry */
165             break;
166         } else if (tnc->next == nameHash[skey]) {       /* end of list */
167             tnc = NULL;
168             break;
169         } else if (safety > NCSIZE) {
170             afs_warn("DNLC cycle");
171             dnlcstats.cycles++;
172             ReleaseWriteLock(&afs_xdnlc);
173             osi_dnlc_purge();
174             goto retry;
175         }
176     }
177
178     if (tnc == NULL) {
179         tnc = GetMeAnEntry();
180
181         tnc->dirp = adp;
182         tnc->vp = avc;
183         tnc->key = key;
184         memcpy((char *)tnc->name, aname, ts - aname + 1);       /* include the NULL */
185
186         InsertEntry(tnc);
187     } else {
188         /* duplicate */
189         tnc->vp = avc;
190     }
191     ReleaseWriteLock(&afs_xdnlc);
192
193     return 0;
194 }
195
196
197 struct vcache *
198 osi_dnlc_lookup(struct vcache *adp, char *aname, int locktype)
199 {
200     struct vcache *tvc;
201     int LRUme;
202     unsigned int key, skey;
203     char *ts = aname;
204     struct nc *tnc, *tnc1 = 0;
205     int safety;
206 #ifdef AFS_DARWIN80_ENV
207     vnode_t tvp;
208 #endif
209
210     ma_critical_enter();
211
212     if (!afs_usednlc) {
213       ma_critical_exit();
214       return 0;
215     }
216
217     dnlcHash(ts, key);          /* leaves ts pointing at the NULL */
218     if (ts - aname >= AFSNCNAMESIZE) {
219       ma_critical_exit();
220       return 0;
221     }
222     skey = key & (NHSIZE - 1);
223
224     TRACE(osi_dnlc_lookupT, skey);
225     dnlcstats.lookups++;
226
227     ObtainReadLock(&afs_xvcache);
228     ObtainReadLock(&afs_xdnlc);
229
230     for (tvc = NULL, tnc = nameHash[skey], safety = 0; tnc;
231          tnc = tnc->next, safety++) {
232         if ( /* (tnc->key == key)  && */ (tnc->dirp == adp)
233             && (!strcmp((char *)tnc->name, aname))) {
234             tvc = tnc->vp;
235             tnc1 = tnc;
236             break;
237         } else if (tnc->next == nameHash[skey]) {       /* end of list */
238             break;
239         } else if (safety > NCSIZE) {
240             afs_warn("DNLC cycle");
241             dnlcstats.cycles++;
242             ReleaseReadLock(&afs_xdnlc);
243             ReleaseReadLock(&afs_xvcache);
244             osi_dnlc_purge();
245             ma_critical_exit();
246             return (0);
247         }
248     }
249
250     LRUme = 0;                  /* (tnc != nameHash[skey]); */
251     ReleaseReadLock(&afs_xdnlc);
252
253     if (!tvc) {
254         ReleaseReadLock(&afs_xvcache);
255         dnlcstats.misses++;
256     } else {
257         if ((tvc->f.states & CVInit)
258 #ifdef  AFS_DARWIN80_ENV
259             ||(tvc->f.states & CDeadVnode)
260 #endif
261             )      
262         {
263             ReleaseReadLock(&afs_xvcache);
264             dnlcstats.misses++;
265             osi_dnlc_remove(adp, aname, tvc);
266             ma_critical_exit();
267             return 0;
268         }
269 #if defined(AFS_DARWIN80_ENV)
270         tvp = AFSTOV(tvc);
271         if (vnode_get(tvp)) {
272             ReleaseReadLock(&afs_xvcache);
273             dnlcstats.misses++;
274             osi_dnlc_remove(adp, aname, tvc);
275             ma_critical_exit();
276             return 0;
277         }
278         if (vnode_ref(tvp)) {
279             ReleaseReadLock(&afs_xvcache);
280             AFS_GUNLOCK();
281             vnode_put(tvp);
282             AFS_GLOCK();
283             dnlcstats.misses++;
284             osi_dnlc_remove(adp, aname, tvc);
285             ma_critical_exit();
286             return 0;
287         }
288 #elif defined(AFS_FBSD_ENV)
289         /* can't sleep in a critical section */
290         ma_critical_exit();
291         osi_vnhold(tvc, 0);
292         ma_critical_enter();
293 #else
294         osi_vnhold(tvc, 0);
295 #endif
296         ReleaseReadLock(&afs_xvcache);
297
298 #ifdef  notdef
299         /* 
300          * XX If LRUme ever is non-zero change the if statement around because
301          * aix's cc with optimizer on won't necessarily check things in order XX
302          */
303         if (LRUme && (0 == NBObtainWriteLock(&afs_xdnlc))) {
304             /* don't block to do this */
305             /* tnc might have been moved during race condition, */
306             /* but it's always in a legit hash chain when a lock is granted, 
307              * or else it's on the freelist so prev == NULL, 
308              * so at worst this is redundant */
309             /* Now that we've got it held, and a lock on the dnlc, we 
310              * should check to be sure that there was no race, and 
311              * bail out if there was. */
312             if (tnc->prev) {
313                 /* special case for only two elements on list - relative ordering
314                  * doesn't change */
315                 if (tnc->prev != tnc->next) {
316                     /* remove from old location */
317                     tnc->prev->next = tnc->next;
318                     tnc->next->prev = tnc->prev;
319                     /* insert into new location */
320                     tnc->next = nameHash[skey];
321                     tnc->prev = tnc->next->prev;
322                     tnc->next->prev = tnc;
323                     tnc->prev->next = tnc;
324                 }
325                 nameHash[skey] = tnc;
326             }
327             ReleaseWriteLock(&afs_xdnlc);
328         }
329 #endif
330     }
331
332     ma_critical_exit();
333     return tvc;
334 }
335
336
337 static void
338 RemoveEntry(struct nc *tnc, unsigned int key)
339 {
340     if (!tnc->prev)             /* things on freelist always have null prev ptrs */
341         osi_Panic("bogus free list");
342
343     TRACE(RemoveEntryT, key);
344     if (tnc == tnc->next) {     /* only one in list */
345         nameHash[key] = NULL;
346     } else {
347         if (tnc == nameHash[key])
348             nameHash[key] = tnc->next;
349         tnc->prev->next = tnc->next;
350         tnc->next->prev = tnc->prev;
351     }
352
353     tnc->prev = NULL;           /* everything not in hash table has 0 prev */
354     tnc->key = 0;               /* just for safety's sake */
355 }
356
357
358 int
359 osi_dnlc_remove(struct vcache *adp, char *aname, struct vcache *avc)
360 {
361     unsigned int key, skey;
362     char *ts = aname;
363     struct nc *tnc;
364
365     if (!afs_usednlc)
366         return 0;
367
368     TRACE(osi_dnlc_removeT, skey);
369     dnlcHash(ts, key);          /* leaves ts pointing at the NULL */
370     if (ts - aname >= AFSNCNAMESIZE) {
371         return 0;
372     }
373     skey = key & (NHSIZE - 1);
374     TRACE(osi_dnlc_removeT, skey);
375     dnlcstats.removes++;
376     ObtainReadLock(&afs_xdnlc);
377
378     for (tnc = nameHash[skey]; tnc; tnc = tnc->next) {
379         if ((tnc->dirp == adp) && (tnc->key == key)
380             && (!strcmp((char *)tnc->name, aname))) {
381             tnc->dirp = NULL;   /* now it won't match anything */
382             break;
383         } else if (tnc->next == nameHash[skey]) {       /* end of list */
384             tnc = NULL;
385             break;
386         }
387     }
388     ReleaseReadLock(&afs_xdnlc);
389
390     if (!tnc)
391         return 0;
392
393     /* there is a little race condition here, but it's relatively
394      * harmless.  At worst, I wind up removing a mapping that I just
395      * created. */
396     if (EWOULDBLOCK == NBObtainWriteLock(&afs_xdnlc, 1)) {
397         return 0;               /* no big deal, tnc will get recycled eventually */
398     }
399     RemoveEntry(tnc, skey);
400     tnc->next = ncfreelist;
401     ncfreelist = tnc;
402     ReleaseWriteLock(&afs_xdnlc);
403
404     return 0;
405 }
406
407 /*! 
408  * Remove anything pertaining to this directory.  I can invalidate
409  * things without the lock, since I am just looking through the array,
410  * but to move things off the lists or into the freelist, I need the
411  * write lock 
412  *
413  * \param adp vcache entry for the directory to be purged.
414  * \return 0
415  */
416 int
417 osi_dnlc_purgedp(struct vcache *adp)
418 {
419     int i;
420     int writelocked;
421
422 #ifdef AFS_DARWIN_ENV
423     if (!(adp->f.states & (CVInit | CVFlushed
424 #ifdef AFS_DARWIN80_ENV
425                         | CDeadVnode
426 #endif
427         )) && AFSTOV(adp))
428     cache_purge(AFSTOV(adp));
429 #endif
430
431     if (!afs_usednlc)
432         return 0;
433
434     dnlcstats.purgeds++;
435     TRACE(osi_dnlc_purgedpT, 0);
436     writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 2));
437
438     for (i = 0; i < NCSIZE; i++) {
439         if ((nameCache[i].dirp == adp) || (nameCache[i].vp == adp)) {
440             nameCache[i].dirp = nameCache[i].vp = NULL;
441             if (writelocked && nameCache[i].prev) {
442                 RemoveEntry(&nameCache[i], nameCache[i].key & (NHSIZE - 1));
443                 nameCache[i].next = ncfreelist;
444                 ncfreelist = &nameCache[i];
445             }
446         }
447     }
448     if (writelocked)
449         ReleaseWriteLock(&afs_xdnlc);
450
451     return 0;
452 }
453
454 /*! 
455  * Remove anything pertaining to this file 
456  *
457  * \param File vcache entry.
458  * \return 0
459  */
460 int
461 osi_dnlc_purgevp(struct vcache *avc)
462 {
463     int i;
464     int writelocked;
465
466 #ifdef AFS_DARWIN_ENV
467     if (!(avc->f.states & (CVInit | CVFlushed
468 #ifdef AFS_DARWIN80_ENV
469                         | CDeadVnode
470 #endif
471         )) && AFSTOV(avc))
472     cache_purge(AFSTOV(avc));
473 #endif
474
475     if (!afs_usednlc)
476         return 0;
477
478     dnlcstats.purgevs++;
479     TRACE(osi_dnlc_purgevpT, 0);
480     writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 3));
481
482     for (i = 0; i < NCSIZE; i++) {
483         if ((nameCache[i].vp == avc)) {
484             nameCache[i].dirp = nameCache[i].vp = NULL;
485             /* can't simply break; because of hard links -- might be two */
486             /* different entries with same vnode */
487             if (writelocked && nameCache[i].prev) {
488                 RemoveEntry(&nameCache[i], nameCache[i].key & (NHSIZE - 1));
489                 nameCache[i].next = ncfreelist;
490                 ncfreelist = &nameCache[i];
491             }
492         }
493     }
494     if (writelocked)
495         ReleaseWriteLock(&afs_xdnlc);
496
497     return 0;
498 }
499
500 /* remove everything */
501 int
502 osi_dnlc_purge(void)
503 {
504     int i;
505
506     dnlcstats.purges++;
507     TRACE(osi_dnlc_purgeT, 0);
508     if (EWOULDBLOCK == NBObtainWriteLock(&afs_xdnlc, 4)) {      /* couldn't get lock */
509         for (i = 0; i < NCSIZE; i++)
510             nameCache[i].dirp = nameCache[i].vp = NULL;
511     } else {                    /* did get the lock */
512         ncfreelist = NULL;
513         memset(nameCache, 0, sizeof(struct nc) * NCSIZE);
514         memset(nameHash, 0, sizeof(struct nc *) * NHSIZE);
515         for (i = 0; i < NCSIZE; i++) {
516             nameCache[i].next = ncfreelist;
517             ncfreelist = &nameCache[i];
518         }
519         ReleaseWriteLock(&afs_xdnlc);
520     }
521
522     return 0;
523 }
524
525 /* remove everything referencing a specific volume */
526 int
527 osi_dnlc_purgevol(struct VenusFid *fidp)
528 {
529
530     dnlcstats.purgevols++;
531     osi_dnlc_purge();
532
533     return 0;
534 }
535
536 int
537 osi_dnlc_init(void)
538 {
539     int i;
540
541     Lock_Init(&afs_xdnlc);
542     memset(&dnlcstats, 0, sizeof(dnlcstats));
543     memset(dnlctracetable, 0, sizeof(dnlctracetable));
544     dnlct = 0;
545     ObtainWriteLock(&afs_xdnlc, 223);
546     ncfreelist = NULL;
547     memset(nameCache, 0, sizeof(struct nc) * NCSIZE);
548     memset(nameHash, 0, sizeof(struct nc *) * NHSIZE);
549     for (i = 0; i < NCSIZE; i++) {
550         nameCache[i].next = ncfreelist;
551         ncfreelist = &nameCache[i];
552     }
553     ReleaseWriteLock(&afs_xdnlc);
554
555     return 0;
556 }
557
558 int
559 osi_dnlc_shutdown(void)
560 {
561     return 0;
562 }