fix-linux22-20050310
[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 RCSID
14     ("$Header$");
15
16 #include "afs/sysincludes.h"    /*Standard vendor system headers */
17 #include "afsincludes.h"        /*AFS-based standard headers */
18 #include "afs/afs.h"
19 #include "afs/lock.h"
20 #include "afs/afs_stats.h"
21 #include "afs/afs_osidnlc.h"
22
23 /* Things to do:
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.  
30  */
31
32 struct afs_lock afs_xdnlc;
33 extern struct afs_lock afs_xvcache;
34
35 dnlcstats_t dnlcstats;
36
37 #define NCSIZE 300
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.
45  */
46
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
50 } traceevt;
51
52 int afs_usednlc = 1;
53
54 struct dt {
55     traceevt event;
56     unsigned char slot;
57 };
58
59 struct dt dnlctracetable[256];
60 int dnlct;
61
62 #define TRACE(e,s)              /* if (dnlct == 256) dnlct = 0; dnlctracetable[dnlct].event = e; dnlctracetable[dnlct++].slot = s; */
63
64 #define dnlcHash(ts, hval) for (hval=0; *ts; ts++) { hval *= 173;  hval  += *ts;   }
65
66 static struct nc *
67 GetMeAnEntry(void)
68 {
69     static unsigned int nameptr = 0;    /* next bucket to pull something from */
70     struct nc *tnc;
71     int j;
72
73     if (ncfreelist) {
74         tnc = ncfreelist;
75         ncfreelist = tnc->next;
76         return tnc;
77     }
78
79     for (j = 0; j < NHSIZE + 2; j++, nameptr++) {
80         if (nameptr >= NHSIZE)
81             nameptr = 0;
82         if (nameHash[nameptr])
83             break;
84     }
85
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");
90
91     if (tnc->prev == tnc) {     /* only thing in list, don't screw around */
92         nameHash[nameptr] = NULL;
93         return (tnc);
94     }
95
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;
100
101     return (tnc);
102 }
103
104 static void
105 InsertEntry(struct nc *tnc)
106 {
107     unsigned int key;
108     key = tnc->key & (NHSIZE - 1);
109
110     TRACE(InsertEntryT, key);
111     if (!nameHash[key]) {
112         nameHash[key] = tnc;
113         tnc->next = tnc->prev = tnc;
114     } else {
115         tnc->next = nameHash[key];
116         tnc->prev = tnc->next->prev;
117         tnc->next->prev = tnc;
118         tnc->prev->next = tnc;
119         nameHash[key] = tnc;
120     }
121 }
122
123
124 int
125 osi_dnlc_enter(struct vcache *adp, char *aname, struct vcache *avc,
126                afs_hyper_t * avno)
127 {
128     struct nc *tnc;
129     unsigned int key, skey;
130     char *ts = aname;
131     int safety;
132
133     if (!afs_usednlc)
134         return 0;
135
136     TRACE(osi_dnlc_enterT, 0);
137     dnlcHash(ts, key);          /* leaves ts pointing at the NULL */
138     if (ts - aname >= AFSNCNAMESIZE) {
139         return 0;
140     }
141     skey = key & (NHSIZE - 1);
142     dnlcstats.enters++;
143
144   retry:
145     ObtainWriteLock(&afs_xdnlc, 222);
146
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);
150         return 0;
151     }
152
153     /*
154      * Make sure each directory entry gets cached no more than once.
155      */
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 */
159             break;
160         } else if (tnc->next == nameHash[skey]) {       /* end of list */
161             tnc = NULL;
162             break;
163         } else if (safety > NCSIZE) {
164             afs_warn("DNLC cycle");
165             dnlcstats.cycles++;
166             ReleaseWriteLock(&afs_xdnlc);
167             osi_dnlc_purge();
168             goto retry;
169         }
170     }
171
172     if (tnc == NULL) {
173         tnc = GetMeAnEntry();
174
175         tnc->dirp = adp;
176         tnc->vp = avc;
177         tnc->key = key;
178         memcpy((char *)tnc->name, aname, ts - aname + 1);       /* include the NULL */
179
180         InsertEntry(tnc);
181     } else {
182         /* duplicate */
183         tnc->vp = avc;
184     }
185     ReleaseWriteLock(&afs_xdnlc);
186
187     return 0;
188 }
189
190
191 struct vcache *
192 osi_dnlc_lookup(struct vcache *adp, char *aname, int locktype)
193 {
194     struct vcache *tvc;
195     int LRUme;
196     unsigned int key, skey;
197     char *ts = aname;
198     struct nc *tnc, *tnc1 = 0;
199     int safety;
200
201     if (!afs_usednlc)
202         return 0;
203
204     dnlcHash(ts, key);          /* leaves ts pointing at the NULL */
205     if (ts - aname >= AFSNCNAMESIZE) {
206         return 0;
207     }
208     skey = key & (NHSIZE - 1);
209
210     TRACE(osi_dnlc_lookupT, skey);
211     dnlcstats.lookups++;
212
213     ObtainReadLock(&afs_xvcache);
214     ObtainReadLock(&afs_xdnlc);
215
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))) {
220             tvc = tnc->vp;
221             tnc1 = tnc;
222             break;
223         } else if (tnc->next == nameHash[skey]) {       /* end of list */
224             break;
225         } else if (safety > NCSIZE) {
226             afs_warn("DNLC cycle");
227             dnlcstats.cycles++;
228             ReleaseReadLock(&afs_xdnlc);
229             ReleaseReadLock(&afs_xvcache);
230             osi_dnlc_purge();
231             return (0);
232         }
233     }
234
235     LRUme = 0;                  /* (tnc != nameHash[skey]); */
236     ReleaseReadLock(&afs_xdnlc);
237
238     if (!tvc) {
239         ReleaseReadLock(&afs_xvcache);
240         dnlcstats.misses++;
241     } else {
242 #ifdef  AFS_OSF_ENV
243         VN_HOLD((vnode_t *) tvc);
244 #else
245         osi_vnhold(tvc, 0);
246 #endif
247         ReleaseReadLock(&afs_xvcache);
248
249 #ifdef  notdef
250         /* 
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
253          */
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. */
263             if (tnc->prev) {
264                 /* special case for only two elements on list - relative ordering
265                  * doesn't change */
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;
275                 }
276                 nameHash[skey] = tnc;
277             }
278             ReleaseWriteLock(&afs_xdnlc);
279         }
280 #endif
281     }
282
283     return tvc;
284 }
285
286
287 static void
288 RemoveEntry(struct nc *tnc, unsigned int key)
289 {
290     if (!tnc->prev)             /* things on freelist always have null prev ptrs */
291         osi_Panic("bogus free list");
292
293     TRACE(RemoveEntryT, key);
294     if (tnc == tnc->next) {     /* only one in list */
295         nameHash[key] = NULL;
296     } else {
297         if (tnc == nameHash[key])
298             nameHash[key] = tnc->next;
299         tnc->prev->next = tnc->next;
300         tnc->next->prev = tnc->prev;
301     }
302
303     tnc->prev = NULL;           /* everything not in hash table has 0 prev */
304     tnc->key = 0;               /* just for safety's sake */
305 }
306
307
308 int
309 osi_dnlc_remove(struct vcache *adp, char *aname, struct vcache *avc)
310 {
311     unsigned int key, skey;
312     char *ts = aname;
313     struct nc *tnc;
314
315     if (!afs_usednlc)
316         return 0;
317
318     TRACE(osi_dnlc_removeT, skey);
319     dnlcHash(ts, key);          /* leaves ts pointing at the NULL */
320     if (ts - aname >= AFSNCNAMESIZE) {
321         return 0;
322     }
323     skey = key & (NHSIZE - 1);
324     TRACE(osi_dnlc_removeT, skey);
325     dnlcstats.removes++;
326     ObtainReadLock(&afs_xdnlc);
327
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 */
332             break;
333         } else if (tnc->next == nameHash[skey]) {       /* end of list */
334             tnc = NULL;
335             break;
336         }
337     }
338     ReleaseReadLock(&afs_xdnlc);
339
340     if (!tnc)
341         return 0;
342
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
345      * created. */
346     if (EWOULDBLOCK == NBObtainWriteLock(&afs_xdnlc, 1)) {
347         return 0;               /* no big deal, tnc will get recycled eventually */
348     }
349     RemoveEntry(tnc, skey);
350     tnc->next = ncfreelist;
351     ncfreelist = tnc;
352     ReleaseWriteLock(&afs_xdnlc);
353
354     return 0;
355 }
356
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
360  * write lock */
361 int
362 osi_dnlc_purgedp(struct vcache *adp)
363 {
364     int i;
365     int writelocked;
366
367     if (!afs_usednlc)
368         return 0;
369
370     dnlcstats.purgeds++;
371     TRACE(osi_dnlc_purgedpT, 0);
372     writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 2));
373
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];
381             }
382         }
383     }
384     if (writelocked)
385         ReleaseWriteLock(&afs_xdnlc);
386
387     return 0;
388 }
389
390 /* remove anything pertaining to this file */
391 int
392 osi_dnlc_purgevp(struct vcache *avc)
393 {
394     int i;
395     int writelocked;
396
397     if (!afs_usednlc)
398         return 0;
399
400     dnlcstats.purgevs++;
401     TRACE(osi_dnlc_purgevpT, 0);
402     writelocked = (0 == NBObtainWriteLock(&afs_xdnlc, 3));
403
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];
413             }
414         }
415     }
416     if (writelocked)
417         ReleaseWriteLock(&afs_xdnlc);
418
419     return 0;
420 }
421
422 /* remove everything */
423 int
424 osi_dnlc_purge(void)
425 {
426     int i;
427
428     dnlcstats.purges++;
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 */
434         ncfreelist = NULL;
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];
440         }
441         ReleaseWriteLock(&afs_xdnlc);
442     }
443
444     return 0;
445 }
446
447 /* remove everything referencing a specific volume */
448 int
449 osi_dnlc_purgevol(struct VenusFid *fidp)
450 {
451
452     dnlcstats.purgevols++;
453     osi_dnlc_purge();
454
455     return 0;
456 }
457
458 int
459 osi_dnlc_init(void)
460 {
461     int i;
462
463     Lock_Init(&afs_xdnlc);
464     memset((char *)&dnlcstats, 0, sizeof(dnlcstats));
465     memset((char *)dnlctracetable, 0, sizeof(dnlctracetable));
466     dnlct = 0;
467     ObtainWriteLock(&afs_xdnlc, 223);
468     ncfreelist = NULL;
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];
474     }
475     ReleaseWriteLock(&afs_xdnlc);
476
477     return 0;
478 }
479
480 int
481 osi_dnlc_shutdown(void)
482 {
483     return 0;
484 }