113703a3ffe77caaf3c14d99f18bc564b95e8d3a
[openafs.git] / src / afs / afs_icl.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_stats.h"
17 #include "rx/rx_globals.h"
18 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
19 #include "net/if.h"
20 #ifdef AFS_SGI62_ENV
21 #include "h/hashing.h"
22 #endif
23 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_DARWIN_ENV)
24 #include "netinet/in_var.h"
25 #endif
26 #endif /* !defined(UKERNEL) */
27
28
29 struct afs_icl_set *afs_iclSetp = (struct afs_icl_set *)0;
30 struct afs_icl_set *afs_iclLongTermSetp = (struct afs_icl_set *)0;
31
32 /* Matches below where ICL_APPENDLONG is 2 INT32s */
33 #if (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64)) || (defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)) || defined(AFS_DARWIN_ENV) && defined(__amd64__)
34 #define ICL_LONG 2
35 #else
36 #define ICL_LONG 1
37 #endif
38
39 int afs_icl_sizeofLong = ICL_LONG;
40
41 int afs_icl_inited = 0;
42
43 /* init function, called once, under afs_icl_lock */
44 int
45 afs_icl_Init(void)
46 {
47     afs_icl_inited = 1;
48     return 0;
49 }
50
51 /* Function called at shutdown - zap everything */
52 void
53 shutdown_icl(void)
54 {
55     struct afs_icl_log *logp;
56     struct afs_icl_set *setp;
57
58     setp = afs_icl_FindSet("cm");
59     if (setp) {
60         /* Release the reference from Find, and the initial one */
61         afs_icl_SetFree(setp);
62         afs_icl_SetFree(setp);
63     }
64     setp = afs_icl_FindSet("cmlongterm");
65     if (setp) {
66         /* Release the reference from Find, and the initial one */
67         afs_icl_SetFree(setp);
68         afs_icl_SetFree(setp);
69     }
70     logp = afs_icl_FindLog("cmfx");
71     if (logp) {
72         /* Release the reference from Find, and the initial one */
73         afs_icl_LogFree(logp);
74         afs_icl_LogFree(logp);
75     }
76 }
77
78 int
79 afs_icl_InitLogs(void)
80 {
81     struct afs_icl_log *logp;
82     int code;
83
84     /* initialize the ICL system */
85     code = afs_icl_CreateLog("cmfx", 60 * 1024, &logp);
86     if (code == 0)
87         code =
88             afs_icl_CreateSetWithFlags("cm", logp, NULL,
89                                        ICL_CRSET_FLAG_DEFAULT_OFF,
90                                        &afs_iclSetp);
91     code =
92         afs_icl_CreateSet("cmlongterm", logp, NULL,
93                           &afs_iclLongTermSetp);
94     return code;
95 }
96
97
98 struct afs_icl_log *afs_icl_FindLog(char *);
99 struct afs_icl_set *afs_icl_FindSet(char *);
100
101
102 #ifdef AFS_DARWIN100_ENV
103 #define AFSKPTR(X) k ## X
104 int
105 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
106 {
107     return Afscall64_icl(opcode,
108                          CAST_USER_ADDR_T((p1)),
109                          CAST_USER_ADDR_T((p2)),
110                          CAST_USER_ADDR_T((p3)),
111                          CAST_USER_ADDR_T((p4)),
112                          retval);
113 }
114 #else
115 #define AFSKPTR(X) ((caddr_t)X)
116 #endif
117
118 int
119 #ifdef AFS_DARWIN100_ENV
120 Afscall64_icl(int opcode, user_addr_t kp1, user_addr_t kp2, user_addr_t kp3, user_addr_t kp4, int *retval)
121 #else
122 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
123 #endif
124 {
125     afs_int32 *lp, elts, flags;
126     afs_int32 code;
127     struct afs_icl_log *logp;
128     struct afs_icl_set *setp;
129 #if defined(AFS_SGI61_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
130     size_t temp;
131 #else /* AFS_SGI61_ENV */
132 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
133     afs_uint64 temp;
134 #else
135     afs_uint32 temp;
136 #endif
137 #endif /* AFS_SGI61_ENV */
138     char tname[65];
139     afs_int32 startCookie;
140     afs_int32 allocated;
141     struct afs_icl_log *tlp;
142 #ifdef AFS_DARWIN100_ENV
143     afs_uint32 p1 = (afs_uint32)kp1;
144     afs_uint32 p2 = (afs_uint32)kp2;
145     afs_uint32 p3 = (afs_uint32)kp3;
146     afs_uint32 p4 = (afs_uint32)kp4;
147 #endif
148
149 #ifdef  AFS_SUN5_ENV
150     if (!afs_suser(CRED())) {   /* only root can run this code */
151         return (EACCES);
152     }
153 #else
154     if (!afs_suser(NULL)) {     /* only root can run this code */
155 #if defined(KERNEL_HAVE_UERROR)
156         setuerror(EACCES);
157         return EACCES;
158 #else
159         return EPERM;
160 #endif
161     }
162 #endif
163     switch (opcode) {
164     case ICL_OP_COPYOUTCLR:     /* copy out data then clear */
165     case ICL_OP_COPYOUT:        /* copy ouy data */
166         /* copyout: p1=logname, p2=&buffer, p3=size(words), p4=&cookie
167          * return flags<<24 + nwords.
168          * updates cookie to updated start (not end) if we had to
169          * skip some records.
170          */
171         AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
172         if (code)
173             return code;
174         AFS_COPYIN(AFSKPTR(p4), (char *)&startCookie, sizeof(afs_int32), code);
175         if (code)
176             return code;
177         logp = afs_icl_FindLog(tname);
178         if (!logp)
179             return ENOENT;
180 #define BUFFERSIZE      AFS_LRALLOCSIZ
181         lp = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
182         elts = BUFFERSIZE / sizeof(afs_int32);
183         if (p3 < elts)
184             elts = p3;
185         flags = (opcode == ICL_OP_COPYOUT) ? 0 : ICL_COPYOUTF_CLRAFTERREAD;
186         code =
187             afs_icl_CopyOut(logp, lp, &elts, (afs_uint32 *) & startCookie,
188                             &flags);
189         if (code) {
190             osi_FreeLargeSpace((struct osi_buffer *)lp);
191             break;
192         }
193         AFS_COPYOUT((char *)lp, AFSKPTR(p2), elts * sizeof(afs_int32), code);
194         if (code)
195             goto done;
196         AFS_COPYOUT((char *)&startCookie, AFSKPTR(p4), sizeof(afs_int32),
197                     code);
198         if (code)
199             goto done;
200 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
201         if (!(IS64U))
202             *retval = ((long)((flags << 24) | (elts & 0xffffff))) << 32;
203         else
204 #endif
205             *retval = (flags << 24) | (elts & 0xffffff);
206       done:
207         afs_icl_LogRele(logp);
208         osi_FreeLargeSpace((struct osi_buffer *)lp);
209         break;
210
211     case ICL_OP_ENUMLOGS:       /* enumerate logs */
212         /* enumerate logs: p1=index, p2=&name, p3=sizeof(name), p4=&size.
213          * return 0 for success, otherwise error.
214          */
215         for (tlp = afs_icl_allLogs; tlp; tlp = tlp->nextp) {
216             if (p1-- == 0)
217                 break;
218         }
219         if (!tlp)
220             return ENOENT;      /* past the end of file */
221         temp = strlen(tlp->name) + 1;
222         if (temp > p3)
223             return EINVAL;
224         AFS_COPYOUT(tlp->name, AFSKPTR(p2), temp, code);
225         if (!code)              /* copy out size of log */
226             AFS_COPYOUT((char *)&tlp->logSize, AFSKPTR(p4), sizeof(afs_int32),
227                         code);
228         break;
229
230     case ICL_OP_ENUMLOGSBYSET:  /* enumerate logs by set name */
231         /* enumerate logs: p1=setname, p2=index, p3=&name, p4=sizeof(name).
232          * return 0 for success, otherwise error.
233          */
234         AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
235         if (code)
236             return code;
237         setp = afs_icl_FindSet(tname);
238         if (!setp)
239             return ENOENT;
240         if (p2 > ICL_LOGSPERSET)
241             return EINVAL;
242         if (!(tlp = setp->logs[p2]))
243             return EBADF;
244         temp = strlen(tlp->name) + 1;
245         if (temp > p4)
246             return EINVAL;
247         AFS_COPYOUT(tlp->name, AFSKPTR(p3), temp, code);
248         break;
249
250     case ICL_OP_CLRLOG: /* clear specified log */
251         /* zero out the specified log: p1=logname */
252         AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
253         if (code)
254             return code;
255         logp = afs_icl_FindLog(tname);
256         if (!logp)
257             return ENOENT;
258         code = afs_icl_ZeroLog(logp);
259         afs_icl_LogRele(logp);
260         break;
261
262     case ICL_OP_CLRSET: /* clear specified set */
263         /* zero out the specified set: p1=setname */
264         AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
265         if (code)
266             return code;
267         setp = afs_icl_FindSet(tname);
268         if (!setp)
269             return ENOENT;
270         code = afs_icl_ZeroSet(setp);
271         afs_icl_SetRele(setp);
272         break;
273
274     case ICL_OP_CLRALL: /* clear all logs */
275         /* zero out all logs -- no args */
276         code = 0;
277         ObtainWriteLock(&afs_icl_lock, 178);
278         for (tlp = afs_icl_allLogs; tlp; tlp = tlp->nextp) {
279             tlp->refCount++;    /* hold this guy */
280             ReleaseWriteLock(&afs_icl_lock);
281             /* don't clear persistent logs */
282             if ((tlp->states & ICL_LOGF_PERSISTENT) == 0)
283                 code = afs_icl_ZeroLog(tlp);
284             ObtainWriteLock(&afs_icl_lock, 179);
285             if (--tlp->refCount == 0)
286                 afs_icl_ZapLog(tlp);
287             if (code)
288                 break;
289         }
290         ReleaseWriteLock(&afs_icl_lock);
291         break;
292
293     case ICL_OP_ENUMSETS:       /* enumerate all sets */
294         /* enumerate sets: p1=index, p2=&name, p3=sizeof(name), p4=&states.
295          * return 0 for success, otherwise error.
296          */
297         for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
298             if (p1-- == 0)
299                 break;
300         }
301         if (!setp)
302             return ENOENT;      /* past the end of file */
303         temp = strlen(setp->name) + 1;
304         if (temp > p3)
305             return EINVAL;
306         AFS_COPYOUT(setp->name, AFSKPTR(p2), temp, code);
307         if (!code)              /* copy out size of log */
308             AFS_COPYOUT((char *)&setp->states, AFSKPTR(p4), sizeof(afs_int32),
309                         code);
310         break;
311
312     case ICL_OP_SETSTAT:        /* set status on a set */
313         /* activate the specified set: p1=setname, p2=op */
314         AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
315         if (code)
316             return code;
317         setp = afs_icl_FindSet(tname);
318         if (!setp)
319             return ENOENT;
320         code = afs_icl_SetSetStat(setp, p2);
321         afs_icl_SetRele(setp);
322         break;
323
324     case ICL_OP_SETSTATALL:     /* set status on all sets */
325         /* activate the specified set: p1=op */
326         code = 0;
327         ObtainWriteLock(&afs_icl_lock, 180);
328         for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
329             setp->refCount++;   /* hold this guy */
330             ReleaseWriteLock(&afs_icl_lock);
331             /* don't set states on persistent sets */
332             if ((setp->states & ICL_SETF_PERSISTENT) == 0)
333                 code = afs_icl_SetSetStat(setp, p1);
334             ObtainWriteLock(&afs_icl_lock, 181);
335             if (--setp->refCount == 0)
336                 afs_icl_ZapSet(setp);
337             if (code)
338                 break;
339         }
340         ReleaseWriteLock(&afs_icl_lock);
341         break;
342
343     case ICL_OP_SETLOGSIZE:     /* set size of log */
344         /* set the size of the specified log: p1=logname, p2=size (in words) */
345         AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
346         if (code)
347             return code;
348         logp = afs_icl_FindLog(tname);
349         if (!logp)
350             return ENOENT;
351         code = afs_icl_LogSetSize(logp, p2);
352         afs_icl_LogRele(logp);
353         break;
354
355     case ICL_OP_GETLOGINFO:     /* get size of log */
356         /* zero out the specified log: p1=logname, p2=&logSize, p3=&allocated */
357         AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
358         if (code)
359             return code;
360         logp = afs_icl_FindLog(tname);
361         if (!logp)
362             return ENOENT;
363         allocated = !!logp->datap;
364         AFS_COPYOUT((char *)&logp->logSize, AFSKPTR(p2), sizeof(afs_int32),
365                     code);
366         if (!code)
367             AFS_COPYOUT((char *)&allocated, AFSKPTR(p3), sizeof(afs_int32),
368                         code);
369         afs_icl_LogRele(logp);
370         break;
371
372     case ICL_OP_GETSETINFO:     /* get state of set */
373         /* zero out the specified set: p1=setname, p2=&state */
374         AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
375         if (code)
376             return code;
377         setp = afs_icl_FindSet(tname);
378         if (!setp)
379             return ENOENT;
380         AFS_COPYOUT((char *)&setp->states, AFSKPTR(p2), sizeof(afs_int32),
381                     code);
382         afs_icl_SetRele(setp);
383         break;
384
385     default:
386         code = EINVAL;
387     }
388
389     return code;
390 }
391
392
393 afs_lock_t afs_icl_lock;
394
395 /* exported routine: a 4 parameter event */
396 int
397 afs_icl_Event4(struct afs_icl_set *setp, afs_int32 eventID,
398                afs_int32 lAndT, long p1, long p2, long p3, long p4)
399 {
400     afs_int32 mask;
401     int i;
402     afs_int32 tmask;
403     int ix;
404
405     /* If things aren't init'ed yet (or the set is inactive), don't panic */
406     if (!ICL_SETACTIVE(setp))
407         return 0;
408
409     AFS_ASSERT_GLOCK();
410     afs_icl_SetHold(setp);
411     mask = lAndT >> 24 & 0xff;  /* mask of which logs to log to */
412     ix = ICL_EVENTBYTE(eventID);
413     ObtainReadLock(&setp->lock);
414     if (setp->eventFlags[ix] & ICL_EVENTMASK(eventID)) {
415         for (i = 0, tmask = 1; i < ICL_LOGSPERSET; i++, tmask <<= 1) {
416             if (mask & tmask) {
417                 afs_icl_AppendRecord(setp->logs[i], eventID, lAndT & 0xffffff,
418                                      p1, p2, p3, p4);
419             }
420             mask &= ~tmask;
421             if (mask == 0)
422                 break;          /* break early */
423         }
424     }
425     ReleaseReadLock(&setp->lock);
426     afs_icl_SetRele(setp);
427     return 0;
428 }
429
430 /* Next 4 routines should be implemented via var-args or something.
431  * Whole purpose is to avoid compiler warnings about parameter # mismatches.
432  * Otherwise, could call afs_icl_Event4 directly.
433  */
434 int
435 afs_icl_Event3(struct afs_icl_set *setp, afs_int32 eventID,
436                afs_int32 lAndT, long p1, long p2, long p3)
437 {
438     return afs_icl_Event4(setp, eventID, lAndT, p1, p2, p3, (long)0);
439 }
440
441 int
442 afs_icl_Event2(struct afs_icl_set *setp, afs_int32 eventID,
443                afs_int32 lAndT, long p1, long p2)
444 {
445     return afs_icl_Event4(setp, eventID, lAndT, p1, p2, (long)0, (long)0);
446 }
447
448 int
449 afs_icl_Event1(struct afs_icl_set *setp, afs_int32 eventID,
450                afs_int32 lAndT, long p1)
451 {
452     return afs_icl_Event4(setp, eventID, lAndT, p1, (long)0, (long)0,
453                           (long)0);
454 }
455
456 int
457 afs_icl_Event0(struct afs_icl_set *setp, afs_int32 eventID,
458                afs_int32 lAndT)
459 {
460     return afs_icl_Event4(setp, eventID, lAndT, (long)0, (long)0, (long)0,
461                           (long)0);
462 }
463
464 struct afs_icl_log *afs_icl_allLogs = 0;
465
466 /* function to purge records from the start of the log, until there
467  * is at least minSpace long's worth of space available without
468  * making the head and the tail point to the same word.
469  *
470  * Log must be write-locked.
471  */
472 static void
473 afs_icl_GetLogSpace(struct afs_icl_log *logp, afs_int32 minSpace)
474 {
475     unsigned int tsize;
476
477     while (logp->logSize - logp->logElements <= minSpace) {
478         /* eat a record */
479         tsize = ((logp->datap[logp->firstUsed]) >> 24) & 0xff;
480         logp->logElements -= tsize;
481         logp->firstUsed += tsize;
482         if (logp->firstUsed >= logp->logSize)
483             logp->firstUsed -= logp->logSize;
484         logp->baseCookie += tsize;
485     }
486 }
487
488 /* append string astr to buffer, including terminating null char.
489  *
490  * log must be write-locked.
491  */
492 #define ICL_CHARSPERLONG        4
493 static void
494 afs_icl_AppendString(struct afs_icl_log *logp, char *astr)
495 {
496     char *op;                   /* ptr to char to write */
497     int tc;
498     int bib;            /* bytes in buffer */
499
500     bib = 0;
501     op = (char *)&(logp->datap[logp->firstFree]);
502     while (1) {
503         tc = *astr++;
504         *op++ = tc;
505         if (++bib >= ICL_CHARSPERLONG) {
506             /* new word */
507             bib = 0;
508             if (++(logp->firstFree) >= logp->logSize) {
509                 logp->firstFree = 0;
510                 op = (char *)&(logp->datap[0]);
511             }
512             logp->logElements++;
513         }
514         if (tc == 0)
515             break;
516     }
517     if (bib > 0) {
518         /* if we've used this word at all, allocate it */
519         if (++(logp->firstFree) >= logp->logSize) {
520             logp->firstFree = 0;
521         }
522         logp->logElements++;
523     }
524 }
525
526 /* add a long to the log, ignoring overflow (checked already) */
527 #define ICL_APPENDINT32(lp, x) \
528     MACRO_BEGIN \
529         (lp)->datap[(lp)->firstFree] = (x); \
530         if (++((lp)->firstFree) >= (lp)->logSize) { \
531                 (lp)->firstFree = 0; \
532         } \
533         (lp)->logElements++; \
534     MACRO_END
535
536 #if ICL_LONG == 2
537 #define ICL_APPENDLONG(lp, x) \
538     MACRO_BEGIN \
539         ICL_APPENDINT32((lp), ((x) >> 32) & 0xffffffffL); \
540         ICL_APPENDINT32((lp), (x) & 0xffffffffL); \
541     MACRO_END
542
543 #else
544 #define ICL_APPENDLONG(lp, x) ICL_APPENDINT32((lp), (x))
545 #endif
546
547 /* routine to tell whether we're dealing with the address or the
548  * object itself
549  */
550 int
551 afs_icl_UseAddr(int type)
552 {
553     if (type == ICL_TYPE_HYPER || type == ICL_TYPE_STRING
554         || type == ICL_TYPE_FID || type == ICL_TYPE_INT64)
555         return 1;
556     else
557         return 0;
558 }
559
560 void
561 afs_icl_AppendOne(struct afs_icl_log *logp, int type, long parm)
562 {
563     if (type) {
564         /* marshall parameter 3 now */
565         if (type == ICL_TYPE_STRING)
566             afs_icl_AppendString(logp, (char *)parm);
567         else if (type == ICL_TYPE_HYPER) {
568             ICL_APPENDINT32(logp,
569                             (afs_int32) ((struct afs_hyper_t *)parm)->high);
570             ICL_APPENDINT32(logp,
571                             (afs_int32) ((struct afs_hyper_t *)parm)->low);
572         } else if (type == ICL_TYPE_INT64) {
573 #ifndef WORDS_BIGENDIAN
574 #ifdef AFS_64BIT_CLIENT
575             ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
576             ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
577 #else /* AFS_64BIT_CLIENT */
578             ICL_APPENDINT32(logp, (afs_int32) parm);
579             ICL_APPENDINT32(logp, (afs_int32) 0);
580 #endif /* AFS_64BIT_CLIENT */
581 #else /* AFSLITTLE_ENDIAN */
582 #ifdef AFS_64BIT_CLIENT
583             ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
584             ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
585 #else /* AFS_64BIT_CLIENT */
586             ICL_APPENDINT32(logp, (afs_int32) 0);
587             ICL_APPENDINT32(logp, (afs_int32) parm);
588 #endif /* AFS_64BIT_CLIENT */
589 #endif /* AFSLITTLE_ENDIAN */
590         } else if (type == ICL_TYPE_FID) {
591             ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
592             ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
593             ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[2]);
594             ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[3]);
595         }
596 #if ICL_LONG == 2
597         else if (type == ICL_TYPE_INT32)
598             ICL_APPENDINT32(logp, (afs_int32) parm);
599 #endif
600         else
601             ICL_APPENDLONG(logp, parm);
602     }
603 }
604
605 /* Function to append a record to the log.  Written for speed
606  * since we know that we're going to have to make this work fast
607  * pretty soon, anyway.  The log must be unlocked.
608  */
609
610 void
611 afs_icl_AppendRecord(struct afs_icl_log *logp, afs_int32 op,
612                      afs_int32 types, long p1, long p2, long p3, long p4)
613 {
614     int rsize;                  /* record size in longs */
615     int tsize;          /* temp size */
616     osi_timeval_t tv;
617     int t1, t2, t3, t4;
618
619     t4 = types & 0x3f;          /* decode types */
620     types >>= 6;
621     t3 = types & 0x3f;
622     types >>= 6;
623     t2 = types & 0x3f;
624     types >>= 6;
625     t1 = types & 0x3f;
626
627     osi_GetTime(&tv);           /* It panics for solaris if inside */
628     ObtainWriteLock(&logp->lock, 182);
629     if (!logp->datap) {
630         ReleaseWriteLock(&logp->lock);
631         return;
632     }
633
634     /* get timestamp as # of microseconds since some time that doesn't
635      * change that often.  This algorithm ticks over every 20 minutes
636      * or so (1000 seconds).  Write a timestamp record if it has.
637      */
638     if (tv.tv_sec - logp->lastTS > 1024) {
639         /* the timer has wrapped -- write a timestamp record */
640         if (logp->logSize - logp->logElements <= 5)
641             afs_icl_GetLogSpace(logp, 5);
642
643         ICL_APPENDINT32(logp,
644                         (afs_int32) (5 << 24) + (ICL_TYPE_UNIXDATE << 18));
645         ICL_APPENDINT32(logp, (afs_int32) ICL_INFO_TIMESTAMP);
646         ICL_APPENDINT32(logp, (afs_int32) 0);   /* use thread ID zero for clocks */
647         ICL_APPENDINT32(logp,
648                         (afs_int32) (tv.tv_sec & 0x3ff) * 1000000 +
649                         tv.tv_usec);
650         ICL_APPENDINT32(logp, (afs_int32) tv.tv_sec);
651
652         logp->lastTS = tv.tv_sec;
653     }
654
655     rsize = 4;                  /* base case: see 4 items below */
656     if (t1) {
657         /* compute size of parameter p1.  Only tricky case is string.
658          * In that case, we have to call strlen to get the string length.
659          */
660         ICL_SIZEHACK(t1, p1, tsize, rsize);
661     }
662     if (t2) {
663         /* compute size of parameter p2.  Only tricky case is string.
664          * In that case, we have to call strlen to get the string length.
665          */
666         ICL_SIZEHACK(t2, p2, tsize, rsize);
667     }
668     if (t3) {
669         /* compute size of parameter p3.  Only tricky case is string.
670          * In that case, we have to call strlen to get the string length.
671          */
672         ICL_SIZEHACK(t3, p3, tsize, rsize);
673     }
674     if (t4) {
675         /* compute size of parameter p4.  Only tricky case is string.
676          * In that case, we have to call strlen to get the string length.
677          */
678         ICL_SIZEHACK(t4, p4, tsize, rsize);
679     }
680
681     /* At this point, we've computed all of the parameter sizes, and
682      * have in rsize the size of the entire record we want to append.
683      * Next, we check that we actually have room in the log to do this
684      * work, and then we do the append.
685      */
686     if (rsize > 255) {
687         ReleaseWriteLock(&logp->lock);
688         return;                 /* log record too big to express */
689     }
690
691     if (logp->logSize - logp->logElements <= rsize)
692         afs_icl_GetLogSpace(logp, rsize);
693
694     ICL_APPENDINT32(logp,
695                     (afs_int32) (rsize << 24) + (t1 << 18) + (t2 << 12) +
696                     (t3 << 6) + t4);
697     ICL_APPENDINT32(logp, (afs_int32) op);
698     ICL_APPENDINT32(logp, (afs_int32) osi_ThreadUnique());
699     ICL_APPENDINT32(logp,
700                     (afs_int32) (tv.tv_sec & 0x3ff) * 1000000 + tv.tv_usec);
701
702     afs_icl_AppendOne(logp, t1, p1);
703     afs_icl_AppendOne(logp, t2, p2);
704     afs_icl_AppendOne(logp, t3, p3);
705     afs_icl_AppendOne(logp, t4, p4);
706     ReleaseWriteLock(&logp->lock);
707 }
708
709 /* create a log with size logSize; return it in *outLogpp and tag
710  * it with name "name."
711  */
712 int
713 afs_icl_CreateLog(char *name, afs_int32 logSize,
714                   struct afs_icl_log **outLogpp)
715 {
716     return afs_icl_CreateLogWithFlags(name, logSize, /*flags */ 0, outLogpp);
717 }
718
719 /* create a log with size logSize; return it in *outLogpp and tag
720  * it with name "name."  'flags' can be set to make the log unclearable.
721  */
722 int
723 afs_icl_CreateLogWithFlags(char *name, afs_int32 logSize, afs_uint32 flags,
724                            struct afs_icl_log **outLogpp)
725 {
726     struct afs_icl_log *logp;
727
728     /* add into global list under lock */
729     ObtainWriteLock(&afs_icl_lock, 183);
730     if (!afs_icl_inited)
731         afs_icl_Init();
732
733     for (logp = afs_icl_allLogs; logp; logp = logp->nextp) {
734         if (strcmp(logp->name, name) == 0) {
735             /* found it already created, just return it */
736             logp->refCount++;
737             *outLogpp = logp;
738             if (flags & ICL_CRLOG_FLAG_PERSISTENT) {
739                 ObtainWriteLock(&logp->lock, 184);
740                 logp->states |= ICL_LOGF_PERSISTENT;
741                 ReleaseWriteLock(&logp->lock);
742             }
743             ReleaseWriteLock(&afs_icl_lock);
744             return 0;
745         }
746     }
747
748     logp = osi_AllocSmallSpace(sizeof(struct afs_icl_log));
749     memset((caddr_t) logp, 0, sizeof(*logp));
750
751     logp->refCount = 1;
752     logp->name = osi_AllocSmallSpace(strlen(name) + 1);
753     strcpy(logp->name, name);
754     LOCK_INIT(&logp->lock, "logp lock");
755     logp->logSize = logSize;
756     logp->datap = NULL;         /* don't allocate it until we need it */
757
758     if (flags & ICL_CRLOG_FLAG_PERSISTENT)
759         logp->states |= ICL_LOGF_PERSISTENT;
760
761     logp->nextp = afs_icl_allLogs;
762     afs_icl_allLogs = logp;
763     ReleaseWriteLock(&afs_icl_lock);
764
765     *outLogpp = logp;
766     return 0;
767 }
768
769 /* called with a log, a pointer to a buffer, the size of the buffer
770  * (in *bufSizep), the starting cookie (in *cookiep, use 0 at the start)
771  * and returns data in the provided buffer, and returns output flags
772  * in *flagsp.  The flag ICL_COPYOUTF_MISSEDSOME is set if we can't
773  * find the record with cookie value cookie.
774  */
775 int
776 afs_icl_CopyOut(struct afs_icl_log *logp, afs_int32 * bufferp,
777                 afs_int32 * bufSizep, afs_uint32 * cookiep,
778                 afs_int32 * flagsp)
779 {
780     afs_int32 nwords;           /* number of words to copy out */
781     afs_uint32 startCookie;     /* first cookie to use */
782     afs_int32 outWords;         /* words we've copied out */
783     afs_int32 inWords;          /* max words to copy out */
784     afs_int32 code;             /* return code */
785     afs_int32 ix;               /* index we're copying from */
786     afs_int32 outFlags;         /* return flags */
787     afs_int32 inFlags;          /* flags passed in */
788     afs_int32 end;
789
790     inWords = *bufSizep;        /* max to copy out */
791     outWords = 0;               /* amount copied out */
792     startCookie = *cookiep;
793     outFlags = 0;
794     inFlags = *flagsp;
795     code = 0;
796
797     ObtainWriteLock(&logp->lock, 185);
798     if (!logp->datap) {
799         ReleaseWriteLock(&logp->lock);
800         goto done;
801     }
802
803     /* first, compute the index of the start cookie we've been passed */
804     while (1) {
805         /* (re-)compute where we should start */
806         if (startCookie < logp->baseCookie) {
807             if (startCookie)    /* missed some output */
808                 outFlags |= ICL_COPYOUTF_MISSEDSOME;
809             /* skip to the first available record */
810             startCookie = logp->baseCookie;
811             *cookiep = startCookie;
812         }
813
814         /* compute where we find the first element to copy out */
815         ix = logp->firstUsed + startCookie - logp->baseCookie;
816         if (ix >= logp->logSize)
817             ix -= logp->logSize;
818
819         /* if have some data now, break out and process it */
820         if (startCookie - logp->baseCookie < logp->logElements)
821             break;
822
823         /* At end of log, so clear it if we need to */
824         if (inFlags & ICL_COPYOUTF_CLRAFTERREAD) {
825             logp->firstUsed = logp->firstFree = 0;
826             logp->logElements = 0;
827         }
828         /* otherwise, either wait for the data to arrive, or return */
829         if (!(inFlags & ICL_COPYOUTF_WAITIO)) {
830             ReleaseWriteLock(&logp->lock);
831             code = 0;
832             goto done;
833         }
834         logp->states |= ICL_LOGF_WAITING;
835         ReleaseWriteLock(&logp->lock);
836         afs_osi_Sleep(&logp->lock);
837         ObtainWriteLock(&logp->lock, 186);
838     }
839     /* copy out data from ix to logSize or firstFree, depending
840      * upon whether firstUsed <= firstFree (no wrap) or otherwise.
841      * be careful not to copy out more than nwords.
842      */
843     if (ix >= logp->firstUsed) {
844         if (logp->firstUsed <= logp->firstFree)
845             /* no wrapping */
846             end = logp->firstFree;      /* first element not to copy */
847         else
848             end = logp->logSize;
849         nwords = inWords;       /* don't copy more than this */
850         if (end - ix < nwords)
851             nwords = end - ix;
852         if (nwords > 0) {
853             memcpy((char *)bufferp, (char *)&logp->datap[ix],
854                    sizeof(afs_int32) * nwords);
855             outWords += nwords;
856             inWords -= nwords;
857             bufferp += nwords;
858         }
859         /* if we're going to copy more out below, we'll start here */
860         ix = 0;
861     }
862     /* now, if active part of the log has wrapped, there's more stuff
863      * starting at the head of the log.  Copy out more from there.
864      */
865     if (logp->firstUsed > logp->firstFree && ix < logp->firstFree
866         && inWords > 0) {
867         /* (more to) copy out from the wrapped section at the
868          * start of the log.  May get here even if didn't copy any
869          * above, if the cookie points directly into the wrapped section.
870          */
871         nwords = inWords;
872         if (logp->firstFree - ix < nwords)
873             nwords = logp->firstFree - ix;
874         memcpy((char *)bufferp, (char *)&logp->datap[ix],
875                sizeof(afs_int32) * nwords);
876         outWords += nwords;
877         inWords -= nwords;
878         bufferp += nwords;
879     }
880
881     ReleaseWriteLock(&logp->lock);
882
883   done:
884     if (code == 0) {
885         *bufSizep = outWords;
886         *flagsp = outFlags;
887     }
888     return code;
889 }
890
891 /* return basic parameter information about a log */
892 int
893 afs_icl_GetLogParms(struct afs_icl_log *logp, afs_int32 * maxSizep,
894                     afs_int32 * curSizep)
895 {
896     ObtainReadLock(&logp->lock);
897     *maxSizep = logp->logSize;
898     *curSizep = logp->logElements;
899     ReleaseReadLock(&logp->lock);
900     return 0;
901 }
902
903
904 /* hold and release logs */
905 int
906 afs_icl_LogHold(struct afs_icl_log *logp)
907 {
908     ObtainWriteLock(&afs_icl_lock, 187);
909     logp->refCount++;
910     ReleaseWriteLock(&afs_icl_lock);
911     return 0;
912 }
913
914 /* hold and release logs, called with lock already held */
915 int
916 afs_icl_LogHoldNL(struct afs_icl_log *logp)
917 {
918     logp->refCount++;
919     return 0;
920 }
921
922 /* keep track of how many sets believe the log itself is allocated */
923 int
924 afs_icl_LogUse(struct afs_icl_log *logp)
925 {
926     ObtainWriteLock(&logp->lock, 188);
927     if (logp->setCount == 0) {
928         /* this is the first set actually using the log -- allocate it */
929         if (logp->logSize == 0) {
930             /* we weren't passed in a hint and it wasn't set */
931             logp->logSize = ICL_DEFAULT_LOGSIZE;
932         }
933         logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logp->logSize);
934         osi_Assert(logp->datap != NULL);
935 #ifdef  KERNEL_HAVE_PIN
936         pin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
937 #endif
938     }
939     logp->setCount++;
940     ReleaseWriteLock(&logp->lock);
941     return 0;
942 }
943
944 /* decrement the number of real users of the log, free if possible */
945 int
946 afs_icl_LogFreeUse(struct afs_icl_log *logp)
947 {
948     ObtainWriteLock(&logp->lock, 189);
949     if (--logp->setCount == 0) {
950         /* no more users -- free it (but keep log structure around) */
951         afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
952 #ifdef  KERNEL_HAVE_PIN
953         unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
954 #endif
955         logp->firstUsed = logp->firstFree = 0;
956         logp->logElements = 0;
957         logp->datap = NULL;
958     }
959     ReleaseWriteLock(&logp->lock);
960     return 0;
961 }
962
963 /* set the size of the log to 'logSize' */
964 int
965 afs_icl_LogSetSize(struct afs_icl_log *logp, afs_int32 logSize)
966 {
967     ObtainWriteLock(&logp->lock, 190);
968     if (!logp->datap) {
969         /* nothing to worry about since it's not allocated */
970         logp->logSize = logSize;
971     } else {
972         /* reset log */
973         logp->firstUsed = logp->firstFree = 0;
974         logp->logElements = 0;
975
976         /* free and allocate a new one */
977         afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
978 #ifdef  KERNEL_HAVE_PIN
979         unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
980 #endif
981         logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logSize);
982         osi_Assert(logp->datap != NULL);
983 #ifdef  KERNEL_HAVE_PIN
984         pin((char *)logp->datap, sizeof(afs_int32) * logSize);
985 #endif
986         logp->logSize = logSize;
987     }
988     ReleaseWriteLock(&logp->lock);
989
990     return 0;
991 }
992
993 /* free a log.  Called with afs_icl_lock locked. */
994 int
995 afs_icl_ZapLog(struct afs_icl_log *logp)
996 {
997     struct afs_icl_log **lpp, *tp;
998
999     for (lpp = &afs_icl_allLogs, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
1000         if (tp == logp) {
1001             /* found the dude we want to remove */
1002             *lpp = logp->nextp;
1003             osi_FreeSmallSpace(logp->name);
1004             afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
1005 #ifdef KERNEL_HAVE_PIN
1006             unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
1007 #endif
1008             osi_FreeSmallSpace(logp);
1009             break;              /* won't find it twice */
1010         }
1011     }
1012     return 0;
1013 }
1014
1015 /* do the release, watching for deleted entries */
1016 int
1017 afs_icl_LogRele(struct afs_icl_log *logp)
1018 {
1019     ObtainWriteLock(&afs_icl_lock, 191);
1020     if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
1021         afs_icl_ZapLog(logp);   /* destroys logp's lock! */
1022     }
1023     ReleaseWriteLock(&afs_icl_lock);
1024     return 0;
1025 }
1026
1027 /* do the release, watching for deleted entries, log already held */
1028 int
1029 afs_icl_LogReleNL(struct afs_icl_log *logp)
1030 {
1031     if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
1032         afs_icl_ZapLog(logp);   /* destroys logp's lock! */
1033     }
1034     return 0;
1035 }
1036
1037 /* zero out the log */
1038 int
1039 afs_icl_ZeroLog(struct afs_icl_log *logp)
1040 {
1041     ObtainWriteLock(&logp->lock, 192);
1042     logp->firstUsed = logp->firstFree = 0;
1043     logp->logElements = 0;
1044     logp->baseCookie = 0;
1045     ReleaseWriteLock(&logp->lock);
1046     return 0;
1047 }
1048
1049 /* free a log entry, and drop its reference count */
1050 int
1051 afs_icl_LogFree(struct afs_icl_log *logp)
1052 {
1053     ObtainWriteLock(&logp->lock, 193);
1054     logp->states |= ICL_LOGF_DELETED;
1055     ReleaseWriteLock(&logp->lock);
1056     afs_icl_LogRele(logp);
1057     return 0;
1058 }
1059
1060 /* find a log by name, returning it held */
1061 struct afs_icl_log *
1062 afs_icl_FindLog(char *name)
1063 {
1064     struct afs_icl_log *tp;
1065     ObtainWriteLock(&afs_icl_lock, 194);
1066     for (tp = afs_icl_allLogs; tp; tp = tp->nextp) {
1067         if (strcmp(tp->name, name) == 0) {
1068             /* this is the dude we want */
1069             tp->refCount++;
1070             break;
1071         }
1072     }
1073     ReleaseWriteLock(&afs_icl_lock);
1074     return tp;
1075 }
1076
1077 int
1078 afs_icl_EnumerateLogs(int (*aproc)
1079                         (char *name, char *arock, struct afs_icl_log * tp),
1080                       char *arock)
1081 {
1082     struct afs_icl_log *tp;
1083     afs_int32 code;
1084
1085     code = 0;
1086     ObtainWriteLock(&afs_icl_lock, 195);
1087     for (tp = afs_icl_allLogs; tp; tp = tp->nextp) {
1088         tp->refCount++;         /* hold this guy */
1089         ReleaseWriteLock(&afs_icl_lock);
1090         ObtainReadLock(&tp->lock);
1091         code = (*aproc) (tp->name, arock, tp);
1092         ReleaseReadLock(&tp->lock);
1093         ObtainWriteLock(&afs_icl_lock, 196);
1094         if (--tp->refCount == 0)
1095             afs_icl_ZapLog(tp);
1096         if (code)
1097             break;
1098     }
1099     ReleaseWriteLock(&afs_icl_lock);
1100     return code;
1101 }
1102
1103 struct afs_icl_set *afs_icl_allSets = 0;
1104
1105 int
1106 afs_icl_CreateSet(char *name, struct afs_icl_log *baseLogp,
1107                   struct afs_icl_log *fatalLogp,
1108                   struct afs_icl_set **outSetpp)
1109 {
1110     return afs_icl_CreateSetWithFlags(name, baseLogp, fatalLogp,
1111                                       /*flags */ 0, outSetpp);
1112 }
1113
1114 /* create a set, given pointers to base and fatal logs, if any.
1115  * Logs are unlocked, but referenced, and *outSetpp is returned
1116  * referenced.  Function bumps reference count on logs, since it
1117  * addds references from the new afs_icl_set.  When the set is destroyed,
1118  * those references will be released.
1119  */
1120 int
1121 afs_icl_CreateSetWithFlags(char *name, struct afs_icl_log *baseLogp,
1122                            struct afs_icl_log *fatalLogp, afs_uint32 flags,
1123                            struct afs_icl_set **outSetpp)
1124 {
1125     struct afs_icl_set *setp;
1126     int i;
1127     afs_int32 states = ICL_DEFAULT_SET_STATES;
1128
1129     ObtainWriteLock(&afs_icl_lock, 197);
1130     if (!afs_icl_inited)
1131         afs_icl_Init();
1132
1133     for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
1134         if (strcmp(setp->name, name) == 0) {
1135             setp->refCount++;
1136             *outSetpp = setp;
1137             if (flags & ICL_CRSET_FLAG_PERSISTENT) {
1138                 ObtainWriteLock(&setp->lock, 198);
1139                 setp->states |= ICL_SETF_PERSISTENT;
1140                 ReleaseWriteLock(&setp->lock);
1141             }
1142             ReleaseWriteLock(&afs_icl_lock);
1143             return 0;
1144         }
1145     }
1146
1147     /* determine initial state */
1148     if (flags & ICL_CRSET_FLAG_DEFAULT_ON)
1149         states = ICL_SETF_ACTIVE;
1150     else if (flags & ICL_CRSET_FLAG_DEFAULT_OFF)
1151         states = ICL_SETF_FREED;
1152     if (flags & ICL_CRSET_FLAG_PERSISTENT)
1153         states |= ICL_SETF_PERSISTENT;
1154
1155     setp = osi_AllocSmallSpace(sizeof(struct afs_icl_set));
1156     memset((caddr_t) setp, 0, sizeof(*setp));
1157     setp->refCount = 1;
1158     if (states & ICL_SETF_FREED)
1159         states &= ~ICL_SETF_ACTIVE;     /* if freed, can't be active */
1160     setp->states = states;
1161
1162     LOCK_INIT(&setp->lock, "setp lock");
1163     /* next lock is obtained in wrong order, hierarchy-wise, but
1164      * it doesn't matter, since no one can find this lock yet, since
1165      * the afs_icl_lock is still held, and thus the obtain can't block.
1166      */
1167     ObtainWriteLock(&setp->lock, 199);
1168     setp->name = osi_AllocSmallSpace(strlen(name) + 1);
1169     strcpy(setp->name, name);
1170     setp->nevents = ICL_DEFAULTEVENTS;
1171     setp->eventFlags = afs_osi_Alloc(ICL_DEFAULTEVENTS);
1172     osi_Assert(setp->eventFlags != NULL);
1173 #ifdef  KERNEL_HAVE_PIN
1174     pin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1175 #endif
1176     for (i = 0; i < ICL_DEFAULTEVENTS; i++)
1177         setp->eventFlags[i] = 0xff;     /* default to enabled */
1178
1179     /* update this global info under the afs_icl_lock */
1180     setp->nextp = afs_icl_allSets;
1181     afs_icl_allSets = setp;
1182     ReleaseWriteLock(&afs_icl_lock);
1183
1184     /* set's basic lock is still held, so we can finish init */
1185     if (baseLogp) {
1186         setp->logs[0] = baseLogp;
1187         afs_icl_LogHold(baseLogp);
1188         if (!(setp->states & ICL_SETF_FREED))
1189             afs_icl_LogUse(baseLogp);   /* log is actually being used */
1190     }
1191     if (fatalLogp) {
1192         setp->logs[1] = fatalLogp;
1193         afs_icl_LogHold(fatalLogp);
1194         if (!(setp->states & ICL_SETF_FREED))
1195             afs_icl_LogUse(fatalLogp);  /* log is actually being used */
1196     }
1197     ReleaseWriteLock(&setp->lock);
1198
1199     *outSetpp = setp;
1200     return 0;
1201 }
1202
1203 /* function to change event enabling information for a particular set */
1204 int
1205 afs_icl_SetEnable(struct afs_icl_set *setp, afs_int32 eventID, int setValue)
1206 {
1207     char *tp;
1208
1209     ObtainWriteLock(&setp->lock, 200);
1210     if (!ICL_EVENTOK(setp, eventID)) {
1211         ReleaseWriteLock(&setp->lock);
1212         return -1;
1213     }
1214     tp = &setp->eventFlags[ICL_EVENTBYTE(eventID)];
1215     if (setValue)
1216         *tp |= ICL_EVENTMASK(eventID);
1217     else
1218         *tp &= ~(ICL_EVENTMASK(eventID));
1219     ReleaseWriteLock(&setp->lock);
1220     return 0;
1221 }
1222
1223 /* return indication of whether a particular event ID is enabled
1224  * for tracing.  If *getValuep is set to 0, the event is disabled,
1225  * otherwise it is enabled.  All events start out enabled by default.
1226  */
1227 int
1228 afs_icl_GetEnable(struct afs_icl_set *setp, afs_int32 eventID, int *getValuep)
1229 {
1230     ObtainReadLock(&setp->lock);
1231     if (!ICL_EVENTOK(setp, eventID)) {
1232         ReleaseWriteLock(&setp->lock);
1233         return -1;
1234     }
1235     if (setp->eventFlags[ICL_EVENTBYTE(eventID)] & ICL_EVENTMASK(eventID))
1236         *getValuep = 1;
1237     else
1238         *getValuep = 0;
1239     ReleaseReadLock(&setp->lock);
1240     return 0;
1241 }
1242
1243 /* hold and release event sets */
1244 int
1245 afs_icl_SetHold(struct afs_icl_set *setp)
1246 {
1247     ObtainWriteLock(&afs_icl_lock, 201);
1248     setp->refCount++;
1249     ReleaseWriteLock(&afs_icl_lock);
1250     return 0;
1251 }
1252
1253 /* free a set.  Called with afs_icl_lock locked */
1254 int
1255 afs_icl_ZapSet(struct afs_icl_set *setp)
1256 {
1257     struct afs_icl_set **lpp, *tp;
1258     int i;
1259     struct afs_icl_log *tlp;
1260
1261     for (lpp = &afs_icl_allSets, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
1262         if (tp == setp) {
1263             /* found the dude we want to remove */
1264             *lpp = setp->nextp;
1265             osi_FreeSmallSpace(setp->name);
1266             afs_osi_Free(setp->eventFlags, ICL_DEFAULTEVENTS);
1267 #ifdef  KERNEL_HAVE_PIN
1268             unpin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1269 #endif
1270             for (i = 0; i < ICL_LOGSPERSET; i++) {
1271                 if ((tlp = setp->logs[i]))
1272                     afs_icl_LogReleNL(tlp);
1273             }
1274             osi_FreeSmallSpace(setp);
1275             break;              /* won't find it twice */
1276         }
1277     }
1278     return 0;
1279 }
1280
1281 /* do the release, watching for deleted entries */
1282 int
1283 afs_icl_SetRele(struct afs_icl_set *setp)
1284 {
1285     ObtainWriteLock(&afs_icl_lock, 202);
1286     if (--setp->refCount == 0 && (setp->states & ICL_SETF_DELETED)) {
1287         afs_icl_ZapSet(setp);   /* destroys setp's lock! */
1288     }
1289     ReleaseWriteLock(&afs_icl_lock);
1290     return 0;
1291 }
1292
1293 /* free a set entry, dropping its reference count */
1294 int
1295 afs_icl_SetFree(struct afs_icl_set *setp)
1296 {
1297     ObtainWriteLock(&setp->lock, 203);
1298     setp->states |= ICL_SETF_DELETED;
1299     ReleaseWriteLock(&setp->lock);
1300     afs_icl_SetRele(setp);
1301     return 0;
1302 }
1303
1304 /* find a set by name, returning it held */
1305 struct afs_icl_set *
1306 afs_icl_FindSet(char *name)
1307 {
1308     struct afs_icl_set *tp;
1309     ObtainWriteLock(&afs_icl_lock, 204);
1310     for (tp = afs_icl_allSets; tp; tp = tp->nextp) {
1311         if (strcmp(tp->name, name) == 0) {
1312             /* this is the dude we want */
1313             tp->refCount++;
1314             break;
1315         }
1316     }
1317     ReleaseWriteLock(&afs_icl_lock);
1318     return tp;
1319 }
1320
1321 /* zero out all the logs in the set */
1322 int
1323 afs_icl_ZeroSet(struct afs_icl_set *setp)
1324 {
1325     int i;
1326     int code = 0;
1327     int tcode;
1328     struct afs_icl_log *logp;
1329
1330     ObtainReadLock(&setp->lock);
1331     for (i = 0; i < ICL_LOGSPERSET; i++) {
1332         logp = setp->logs[i];
1333         if (logp) {
1334             afs_icl_LogHold(logp);
1335             tcode = afs_icl_ZeroLog(logp);
1336             if (tcode != 0)
1337                 code = tcode;   /* save the last bad one */
1338             afs_icl_LogRele(logp);
1339         }
1340     }
1341     ReleaseReadLock(&setp->lock);
1342     return code;
1343 }
1344
1345 int
1346 afs_icl_EnumerateSets(int (*aproc)
1347                         (char *name, char *arock, struct afs_icl_log * tp),
1348                       char *arock)
1349 {
1350     struct afs_icl_set *tp, *np;
1351     afs_int32 code;
1352
1353     code = 0;
1354     ObtainWriteLock(&afs_icl_lock, 205);
1355     for (tp = afs_icl_allSets; tp; tp = np) {
1356         tp->refCount++;         /* hold this guy */
1357         ReleaseWriteLock(&afs_icl_lock);
1358         code = (*aproc) (tp->name, arock, (struct afs_icl_log *)tp);
1359         ObtainWriteLock(&afs_icl_lock, 206);
1360         np = tp->nextp;         /* tp may disappear next, but not np */
1361         if (--tp->refCount == 0 && (tp->states & ICL_SETF_DELETED))
1362             afs_icl_ZapSet(tp);
1363         if (code)
1364             break;
1365     }
1366     ReleaseWriteLock(&afs_icl_lock);
1367     return code;
1368 }
1369
1370 int
1371 afs_icl_AddLogToSet(struct afs_icl_set *setp, struct afs_icl_log *newlogp)
1372 {
1373     int i;
1374     int code = -1;
1375
1376     ObtainWriteLock(&setp->lock, 207);
1377     for (i = 0; i < ICL_LOGSPERSET; i++) {
1378         if (!setp->logs[i]) {
1379             setp->logs[i] = newlogp;
1380             code = i;
1381             afs_icl_LogHold(newlogp);
1382             if (!(setp->states & ICL_SETF_FREED)) {
1383                 /* bump up the number of sets using the log */
1384                 afs_icl_LogUse(newlogp);
1385             }
1386             break;
1387         }
1388     }
1389     ReleaseWriteLock(&setp->lock);
1390     return code;
1391 }
1392
1393 int
1394 afs_icl_SetSetStat(struct afs_icl_set *setp, int op)
1395 {
1396     int i;
1397     afs_int32 code;
1398     struct afs_icl_log *logp;
1399
1400     ObtainWriteLock(&setp->lock, 208);
1401     switch (op) {
1402     case ICL_OP_SS_ACTIVATE:    /* activate a log */
1403         /*
1404          * If we are not already active, see if we have released
1405          * our demand that the log be allocated (FREED set).  If
1406          * we have, reassert our desire.
1407          */
1408         if (!(setp->states & ICL_SETF_ACTIVE)) {
1409             if (setp->states & ICL_SETF_FREED) {
1410                 /* have to reassert desire for logs */
1411                 for (i = 0; i < ICL_LOGSPERSET; i++) {
1412                     logp = setp->logs[i];
1413                     if (logp) {
1414                         afs_icl_LogHold(logp);
1415                         afs_icl_LogUse(logp);
1416                         afs_icl_LogRele(logp);
1417                     }
1418                 }
1419                 setp->states &= ~ICL_SETF_FREED;
1420             }
1421             setp->states |= ICL_SETF_ACTIVE;
1422         }
1423         code = 0;
1424         break;
1425
1426     case ICL_OP_SS_DEACTIVATE:  /* deactivate a log */
1427         /* this doesn't require anything beyond clearing the ACTIVE flag */
1428         setp->states &= ~ICL_SETF_ACTIVE;
1429         code = 0;
1430         break;
1431
1432     case ICL_OP_SS_FREE:        /* deassert design for log */
1433         /*
1434          * if we are already in this state, do nothing; otherwise
1435          * deassert desire for log
1436          */
1437         if (setp->states & ICL_SETF_ACTIVE)
1438             code = EINVAL;
1439         else {
1440             if (!(setp->states & ICL_SETF_FREED)) {
1441                 for (i = 0; i < ICL_LOGSPERSET; i++) {
1442                     logp = setp->logs[i];
1443                     if (logp) {
1444                         afs_icl_LogHold(logp);
1445                         afs_icl_LogFreeUse(logp);
1446                         afs_icl_LogRele(logp);
1447                     }
1448                 }
1449                 setp->states |= ICL_SETF_FREED;
1450             }
1451             code = 0;
1452         }
1453         break;
1454
1455     default:
1456         code = EINVAL;
1457     }
1458     ReleaseWriteLock(&setp->lock);
1459     return code;
1460 }