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"
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_LINUX_ENV)
21 #include "h/hashing.h"
23 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_DARWIN_ENV)
24 #include "netinet/in_var.h"
26 #endif /* !defined(UKERNEL) */
29 struct afs_icl_set *afs_iclSetp = (struct afs_icl_set *)0;
30 struct afs_icl_set *afs_iclLongTermSetp = (struct afs_icl_set *)0;
32 int afs_icl_sizeofLong = ICL_LONG;
34 int afs_icl_inited = 0;
36 /* init function, called once, under afs_icl_lock */
44 /* Function called at shutdown - zap everything */
48 struct afs_icl_log *logp;
49 struct afs_icl_set *setp;
52 setp = afs_iclSetp; /* "cm" */
54 afs_icl_SetFree(setp);
56 if (afs_iclLongTermSetp) {
57 setp = afs_iclLongTermSetp; /* "cmlongterm" */
58 afs_iclLongTermSetp = NULL;
59 afs_icl_SetFree(setp);
61 logp = afs_icl_FindLog("cmfx");
63 /* Release the reference from Find, and the initial one */
64 afs_icl_LogFree(logp);
65 afs_icl_LogFree(logp);
70 afs_icl_InitLogs(void)
72 struct afs_icl_log *logp;
75 /* initialize the ICL system */
76 code = afs_icl_CreateLog("cmfx", 60 * 1024, &logp);
80 code = afs_icl_CreateSetWithFlags("cm", logp, NULL,
81 ICL_CRSET_FLAG_DEFAULT_OFF,
86 code = afs_icl_CreateSet("cmlongterm", logp, NULL,
87 &afs_iclLongTermSetp);
92 struct afs_icl_log *afs_icl_FindLog(char *);
93 struct afs_icl_set *afs_icl_FindSet(char *);
96 #ifdef AFS_DARWIN100_ENV
97 #define AFSKPTR(X) k ## X
99 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
101 return Afscall64_icl(opcode,
102 CAST_USER_ADDR_T((p1)),
103 CAST_USER_ADDR_T((p2)),
104 CAST_USER_ADDR_T((p3)),
105 CAST_USER_ADDR_T((p4)),
109 #define AFSKPTR(X) ((caddr_t)X)
113 #ifdef AFS_DARWIN100_ENV
114 Afscall64_icl(int opcode, user_addr_t kp1, user_addr_t kp2, user_addr_t kp3, user_addr_t kp4, int *retval)
116 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
119 afs_int32 *lp, elts, flags;
121 struct afs_icl_log *logp;
122 struct afs_icl_set *setp;
123 #if defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
125 #else /* AFS_SGI_ENV */
126 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
131 #endif /* AFS_SGI_ENV */
133 afs_int32 startCookie;
135 struct afs_icl_log *tlp;
136 #ifdef AFS_DARWIN100_ENV
137 afs_uint32 p1 = (afs_uint32)kp1;
138 afs_uint32 p2 = (afs_uint32)kp2;
139 afs_uint32 p3 = (afs_uint32)kp3;
140 afs_uint32 p4 = (afs_uint32)kp4;
144 if (!afs_suser(CRED())) { /* only root can run this code */
148 if (!afs_suser(NULL)) { /* only root can run this code */
149 #if defined(KERNEL_HAVE_UERROR)
158 case ICL_OP_COPYOUTCLR: /* copy out data then clear */
159 case ICL_OP_COPYOUT: /* copy ouy data */
160 /* copyout: p1=logname, p2=&buffer, p3=size(words), p4=&cookie
161 * return flags<<24 + nwords.
162 * updates cookie to updated start (not end) if we had to
165 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
168 AFS_COPYIN(AFSKPTR(p4), (char *)&startCookie, sizeof(afs_int32), code);
171 logp = afs_icl_FindLog(tname);
174 #define BUFFERSIZE AFS_LRALLOCSIZ
175 lp = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
176 elts = BUFFERSIZE / sizeof(afs_int32);
179 flags = (opcode == ICL_OP_COPYOUT) ? 0 : ICL_COPYOUTF_CLRAFTERREAD;
181 afs_icl_CopyOut(logp, lp, &elts, (afs_uint32 *) & startCookie,
184 osi_FreeLargeSpace((struct osi_buffer *)lp);
187 AFS_COPYOUT((char *)lp, AFSKPTR(p2), elts * sizeof(afs_int32), code);
190 AFS_COPYOUT((char *)&startCookie, AFSKPTR(p4), sizeof(afs_int32),
194 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
196 *retval = ((long)((flags << 24) | (elts & 0xffffff))) << 32;
199 *retval = (flags << 24) | (elts & 0xffffff);
201 afs_icl_LogRele(logp);
202 osi_FreeLargeSpace((struct osi_buffer *)lp);
205 case ICL_OP_ENUMLOGS: /* enumerate logs */
206 /* enumerate logs: p1=index, p2=&name, p3=sizeof(name), p4=&size.
207 * return 0 for success, otherwise error.
209 for (tlp = afs_icl_allLogs; tlp; tlp = tlp->nextp) {
214 return ENOENT; /* past the end of file */
215 temp = strlen(tlp->name) + 1;
218 AFS_COPYOUT(tlp->name, AFSKPTR(p2), temp, code);
219 if (!code) /* copy out size of log */
220 AFS_COPYOUT((char *)&tlp->logSize, AFSKPTR(p4), sizeof(afs_int32),
224 case ICL_OP_ENUMLOGSBYSET: /* enumerate logs by set name */
225 /* enumerate logs: p1=setname, p2=index, p3=&name, p4=sizeof(name).
226 * return 0 for success, otherwise error.
228 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
231 setp = afs_icl_FindSet(tname);
234 if (p2 >= ICL_LOGSPERSET)
236 if (!(tlp = setp->logs[p2]))
238 temp = strlen(tlp->name) + 1;
241 AFS_COPYOUT(tlp->name, AFSKPTR(p3), temp, code);
244 case ICL_OP_CLRLOG: /* clear specified log */
245 /* zero out the specified log: p1=logname */
246 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
249 logp = afs_icl_FindLog(tname);
252 code = afs_icl_ZeroLog(logp);
253 afs_icl_LogRele(logp);
256 case ICL_OP_CLRSET: /* clear specified set */
257 /* zero out the specified set: p1=setname */
258 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
261 setp = afs_icl_FindSet(tname);
264 code = afs_icl_ZeroSet(setp);
265 afs_icl_SetRele(setp);
268 case ICL_OP_CLRALL: /* clear all logs */
269 /* zero out all logs -- no args */
271 ObtainWriteLock(&afs_icl_lock, 178);
272 for (tlp = afs_icl_allLogs; tlp; tlp = tlp->nextp) {
273 tlp->refCount++; /* hold this guy */
274 ReleaseWriteLock(&afs_icl_lock);
275 /* don't clear persistent logs */
276 if ((tlp->states & ICL_LOGF_PERSISTENT) == 0)
277 code = afs_icl_ZeroLog(tlp);
278 ObtainWriteLock(&afs_icl_lock, 179);
279 if (--tlp->refCount == 0)
284 ReleaseWriteLock(&afs_icl_lock);
287 case ICL_OP_ENUMSETS: /* enumerate all sets */
288 /* enumerate sets: p1=index, p2=&name, p3=sizeof(name), p4=&states.
289 * return 0 for success, otherwise error.
291 for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
296 return ENOENT; /* past the end of file */
297 temp = strlen(setp->name) + 1;
300 AFS_COPYOUT(setp->name, AFSKPTR(p2), temp, code);
301 if (!code) /* copy out size of log */
302 AFS_COPYOUT((char *)&setp->states, AFSKPTR(p4), sizeof(afs_int32),
306 case ICL_OP_SETSTAT: /* set status on a set */
307 /* activate the specified set: p1=setname, p2=op */
308 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
311 setp = afs_icl_FindSet(tname);
314 code = afs_icl_SetSetStat(setp, p2);
315 afs_icl_SetRele(setp);
318 case ICL_OP_SETSTATALL: /* set status on all sets */
319 /* activate the specified set: p1=op */
321 ObtainWriteLock(&afs_icl_lock, 180);
322 for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
323 setp->refCount++; /* hold this guy */
324 ReleaseWriteLock(&afs_icl_lock);
325 /* don't set states on persistent sets */
326 if ((setp->states & ICL_SETF_PERSISTENT) == 0)
327 code = afs_icl_SetSetStat(setp, p1);
328 ObtainWriteLock(&afs_icl_lock, 181);
329 if (--setp->refCount == 0)
330 afs_icl_ZapSet(setp);
334 ReleaseWriteLock(&afs_icl_lock);
337 case ICL_OP_SETLOGSIZE: /* set size of log */
338 /* set the size of the specified log: p1=logname, p2=size (in words) */
339 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
342 logp = afs_icl_FindLog(tname);
345 code = afs_icl_LogSetSize(logp, p2);
346 afs_icl_LogRele(logp);
349 case ICL_OP_GETLOGINFO: /* get size of log */
350 /* zero out the specified log: p1=logname, p2=&logSize, p3=&allocated */
351 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
354 logp = afs_icl_FindLog(tname);
357 allocated = !!logp->datap;
358 AFS_COPYOUT((char *)&logp->logSize, AFSKPTR(p2), sizeof(afs_int32),
361 AFS_COPYOUT((char *)&allocated, AFSKPTR(p3), sizeof(afs_int32),
363 afs_icl_LogRele(logp);
366 case ICL_OP_GETSETINFO: /* get state of set */
367 /* zero out the specified set: p1=setname, p2=&state */
368 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
371 setp = afs_icl_FindSet(tname);
374 AFS_COPYOUT((char *)&setp->states, AFSKPTR(p2), sizeof(afs_int32),
376 afs_icl_SetRele(setp);
387 afs_lock_t afs_icl_lock;
389 /* exported routine: a 4 parameter event */
391 afs_icl_Event4(struct afs_icl_set *setp, afs_int32 eventID,
392 afs_int32 lAndT, long p1, long p2, long p3, long p4)
399 /* If things aren't init'ed yet (or the set is inactive), don't panic */
400 if (!ICL_SETACTIVE(setp))
404 afs_icl_SetHold(setp);
405 mask = lAndT >> 24 & 0xff; /* mask of which logs to log to */
406 ix = ICL_EVENTBYTE(eventID);
407 ObtainReadLock(&setp->lock);
408 if (setp->eventFlags[ix] & ICL_EVENTMASK(eventID)) {
409 for (i = 0, tmask = 1; i < ICL_LOGSPERSET; i++, tmask <<= 1) {
411 afs_icl_AppendRecord(setp->logs[i], eventID, lAndT & 0xffffff,
416 break; /* break early */
419 ReleaseReadLock(&setp->lock);
420 afs_icl_SetRele(setp);
424 /* Next 4 routines should be implemented via var-args or something.
425 * Whole purpose is to avoid compiler warnings about parameter # mismatches.
426 * Otherwise, could call afs_icl_Event4 directly.
429 afs_icl_Event3(struct afs_icl_set *setp, afs_int32 eventID,
430 afs_int32 lAndT, long p1, long p2, long p3)
432 return afs_icl_Event4(setp, eventID, lAndT, p1, p2, p3, (long)0);
436 afs_icl_Event2(struct afs_icl_set *setp, afs_int32 eventID,
437 afs_int32 lAndT, long p1, long p2)
439 return afs_icl_Event4(setp, eventID, lAndT, p1, p2, (long)0, (long)0);
443 afs_icl_Event1(struct afs_icl_set *setp, afs_int32 eventID,
444 afs_int32 lAndT, long p1)
446 return afs_icl_Event4(setp, eventID, lAndT, p1, (long)0, (long)0,
451 afs_icl_Event0(struct afs_icl_set *setp, afs_int32 eventID,
454 return afs_icl_Event4(setp, eventID, lAndT, (long)0, (long)0, (long)0,
458 struct afs_icl_log *afs_icl_allLogs = 0;
460 /* function to purge records from the start of the log, until there
461 * is at least minSpace long's worth of space available without
462 * making the head and the tail point to the same word.
464 * Log must be write-locked.
467 afs_icl_GetLogSpace(struct afs_icl_log *logp, afs_int32 minSpace)
471 while (logp->logSize - logp->logElements <= minSpace) {
473 tsize = ((logp->datap[logp->firstUsed]) >> 24) & 0xff;
474 logp->logElements -= tsize;
475 logp->firstUsed += tsize;
476 if (logp->firstUsed >= logp->logSize)
477 logp->firstUsed -= logp->logSize;
478 logp->baseCookie += tsize;
482 /* append string astr to buffer, including terminating null char.
484 * log must be write-locked.
486 #define ICL_CHARSPERLONG 4
488 afs_icl_AppendString(struct afs_icl_log *logp, char *astr)
490 char *op; /* ptr to char to write */
492 int bib; /* bytes in buffer */
495 op = (char *)&(logp->datap[logp->firstFree]);
499 if (++bib >= ICL_CHARSPERLONG) {
502 if (++(logp->firstFree) >= logp->logSize) {
504 op = (char *)&(logp->datap[0]);
512 /* if we've used this word at all, allocate it */
513 if (++(logp->firstFree) >= logp->logSize) {
520 /* add a long to the log, ignoring overflow (checked already) */
521 #define ICL_APPENDINT32(lp, x) \
523 (lp)->datap[(lp)->firstFree] = (x); \
524 if (++((lp)->firstFree) >= (lp)->logSize) { \
525 (lp)->firstFree = 0; \
527 (lp)->logElements++; \
531 #define ICL_APPENDLONG(lp, x) \
533 ICL_APPENDINT32((lp), ((x) >> 32) & 0xffffffffL); \
534 ICL_APPENDINT32((lp), (x) & 0xffffffffL); \
538 #define ICL_APPENDLONG(lp, x) ICL_APPENDINT32((lp), (x))
543 afs_icl_AppendOne(struct afs_icl_log *logp, int type, long parm)
546 /* marshall parameter 3 now */
547 if (type == ICL_TYPE_STRING)
548 afs_icl_AppendString(logp, (char *)parm);
549 else if (type == ICL_TYPE_HYPER) {
550 ICL_APPENDINT32(logp,
551 (afs_int32) ((struct afs_hyper_t *)parm)->high);
552 ICL_APPENDINT32(logp,
553 (afs_int32) ((struct afs_hyper_t *)parm)->low);
554 } else if (type == ICL_TYPE_INT64) {
555 #ifndef WORDS_BIGENDIAN
556 #ifdef AFS_64BIT_CLIENT
557 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
558 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
559 #else /* AFS_64BIT_CLIENT */
560 ICL_APPENDINT32(logp, (afs_int32) parm);
561 ICL_APPENDINT32(logp, (afs_int32) 0);
562 #endif /* AFS_64BIT_CLIENT */
563 #else /* AFSLITTLE_ENDIAN */
564 #ifdef AFS_64BIT_CLIENT
565 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
566 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
567 #else /* AFS_64BIT_CLIENT */
568 ICL_APPENDINT32(logp, (afs_int32) 0);
569 ICL_APPENDINT32(logp, (afs_int32) parm);
570 #endif /* AFS_64BIT_CLIENT */
571 #endif /* AFSLITTLE_ENDIAN */
572 } else if (type == ICL_TYPE_FID) {
573 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
574 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
575 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[2]);
576 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[3]);
579 else if (type == ICL_TYPE_INT32)
580 ICL_APPENDINT32(logp, (afs_int32) parm);
583 ICL_APPENDLONG(logp, parm);
587 /* Function to append a record to the log. Written for speed
588 * since we know that we're going to have to make this work fast
589 * pretty soon, anyway. The log must be unlocked.
593 afs_icl_AppendRecord(struct afs_icl_log *logp, afs_int32 op,
594 afs_int32 types, long p1, long p2, long p3, long p4)
596 int rsize; /* record size in longs */
597 int tsize; /* temp size */
601 t4 = types & 0x3f; /* decode types */
609 osi_GetTime(&tv); /* It panics for solaris if inside */
610 ObtainWriteLock(&logp->lock, 182);
612 ReleaseWriteLock(&logp->lock);
616 /* get timestamp as # of microseconds since some time that doesn't
617 * change that often. This algorithm ticks over every 20 minutes
618 * or so (1000 seconds). Write a timestamp record if it has.
620 if (tv.tv_sec - logp->lastTS > 1024) {
621 /* the timer has wrapped -- write a timestamp record */
622 if (logp->logSize - logp->logElements <= 5)
623 afs_icl_GetLogSpace(logp, 5);
625 ICL_APPENDINT32(logp,
626 (afs_int32) (5 << 24) + (ICL_TYPE_UNIXDATE << 18));
627 ICL_APPENDINT32(logp, (afs_int32) ICL_INFO_TIMESTAMP);
628 ICL_APPENDINT32(logp, (afs_int32) 0); /* use thread ID zero for clocks */
629 ICL_APPENDINT32(logp,
630 (afs_int32) (tv.tv_sec & 0x3ff) * 1000000 +
632 ICL_APPENDINT32(logp, (afs_int32) tv.tv_sec);
634 logp->lastTS = tv.tv_sec;
637 rsize = 4; /* base case: see 4 items below */
639 /* compute size of parameter p1. Only tricky case is string.
640 * In that case, we have to call strlen to get the string length.
642 ICL_SIZEHACK(t1, p1, tsize, rsize);
645 /* compute size of parameter p2. Only tricky case is string.
646 * In that case, we have to call strlen to get the string length.
648 ICL_SIZEHACK(t2, p2, tsize, rsize);
651 /* compute size of parameter p3. Only tricky case is string.
652 * In that case, we have to call strlen to get the string length.
654 ICL_SIZEHACK(t3, p3, tsize, rsize);
657 /* compute size of parameter p4. Only tricky case is string.
658 * In that case, we have to call strlen to get the string length.
660 ICL_SIZEHACK(t4, p4, tsize, rsize);
663 /* At this point, we've computed all of the parameter sizes, and
664 * have in rsize the size of the entire record we want to append.
665 * Next, we check that we actually have room in the log to do this
666 * work, and then we do the append.
669 ReleaseWriteLock(&logp->lock);
670 return; /* log record too big to express */
673 if (logp->logSize - logp->logElements <= rsize)
674 afs_icl_GetLogSpace(logp, rsize);
676 ICL_APPENDINT32(logp,
677 (afs_int32) (rsize << 24) + (t1 << 18) + (t2 << 12) +
679 ICL_APPENDINT32(logp, (afs_int32) op);
680 ICL_APPENDINT32(logp, (afs_int32) osi_ThreadUnique());
681 ICL_APPENDINT32(logp,
682 (afs_int32) (tv.tv_sec & 0x3ff) * 1000000 + tv.tv_usec);
684 afs_icl_AppendOne(logp, t1, p1);
685 afs_icl_AppendOne(logp, t2, p2);
686 afs_icl_AppendOne(logp, t3, p3);
687 afs_icl_AppendOne(logp, t4, p4);
688 ReleaseWriteLock(&logp->lock);
691 /* create a log with size logSize; return it in *outLogpp and tag
692 * it with name "name."
695 afs_icl_CreateLog(char *name, afs_int32 logSize,
696 struct afs_icl_log **outLogpp)
698 return afs_icl_CreateLogWithFlags(name, logSize, /*flags */ 0, outLogpp);
701 /* create a log with size logSize; return it in *outLogpp and tag
702 * it with name "name." 'flags' can be set to make the log unclearable.
705 afs_icl_CreateLogWithFlags(char *name, afs_int32 logSize, afs_uint32 flags,
706 struct afs_icl_log **outLogpp)
708 struct afs_icl_log *logp;
710 /* add into global list under lock */
711 ObtainWriteLock(&afs_icl_lock, 183);
715 for (logp = afs_icl_allLogs; logp; logp = logp->nextp) {
716 if (strcmp(logp->name, name) == 0) {
717 /* found it already created, just return it */
720 if (flags & ICL_CRLOG_FLAG_PERSISTENT) {
721 ObtainWriteLock(&logp->lock, 184);
722 logp->states |= ICL_LOGF_PERSISTENT;
723 ReleaseWriteLock(&logp->lock);
725 ReleaseWriteLock(&afs_icl_lock);
730 logp = osi_AllocSmallSpace(sizeof(struct afs_icl_log));
731 memset((caddr_t) logp, 0, sizeof(*logp));
734 logp->name = osi_AllocSmallSpace(strlen(name) + 1);
735 strcpy(logp->name, name);
736 LOCK_INIT(&logp->lock, "logp lock");
737 logp->logSize = logSize;
738 logp->datap = NULL; /* don't allocate it until we need it */
740 if (flags & ICL_CRLOG_FLAG_PERSISTENT)
741 logp->states |= ICL_LOGF_PERSISTENT;
743 logp->nextp = afs_icl_allLogs;
744 afs_icl_allLogs = logp;
745 ReleaseWriteLock(&afs_icl_lock);
751 /* called with a log, a pointer to a buffer, the size of the buffer
752 * (in *bufSizep), the starting cookie (in *cookiep, use 0 at the start)
753 * and returns data in the provided buffer, and returns output flags
754 * in *flagsp. The flag ICL_COPYOUTF_MISSEDSOME is set if we can't
755 * find the record with cookie value cookie.
758 afs_icl_CopyOut(struct afs_icl_log *logp, afs_int32 * bufferp,
759 afs_int32 * bufSizep, afs_uint32 * cookiep,
762 afs_int32 nwords; /* number of words to copy out */
763 afs_uint32 startCookie; /* first cookie to use */
764 afs_int32 outWords; /* words we've copied out */
765 afs_int32 inWords; /* max words to copy out */
766 afs_int32 code; /* return code */
767 afs_int32 ix; /* index we're copying from */
768 afs_int32 outFlags; /* return flags */
769 afs_int32 inFlags; /* flags passed in */
772 inWords = *bufSizep; /* max to copy out */
773 outWords = 0; /* amount copied out */
774 startCookie = *cookiep;
779 ObtainWriteLock(&logp->lock, 185);
781 ReleaseWriteLock(&logp->lock);
785 /* first, compute the index of the start cookie we've been passed */
787 /* (re-)compute where we should start */
788 if (startCookie < logp->baseCookie) {
789 if (startCookie) /* missed some output */
790 outFlags |= ICL_COPYOUTF_MISSEDSOME;
791 /* skip to the first available record */
792 startCookie = logp->baseCookie;
793 *cookiep = startCookie;
796 /* compute where we find the first element to copy out */
797 ix = logp->firstUsed + startCookie - logp->baseCookie;
798 if (ix >= logp->logSize)
801 /* if have some data now, break out and process it */
802 if (startCookie - logp->baseCookie < logp->logElements)
805 /* At end of log, so clear it if we need to */
806 if (inFlags & ICL_COPYOUTF_CLRAFTERREAD) {
807 logp->firstUsed = logp->firstFree = 0;
808 logp->logElements = 0;
810 /* otherwise, either wait for the data to arrive, or return */
811 if (!(inFlags & ICL_COPYOUTF_WAITIO)) {
812 ReleaseWriteLock(&logp->lock);
816 logp->states |= ICL_LOGF_WAITING;
817 ReleaseWriteLock(&logp->lock);
818 afs_osi_Sleep(&logp->lock);
819 ObtainWriteLock(&logp->lock, 186);
821 /* copy out data from ix to logSize or firstFree, depending
822 * upon whether firstUsed <= firstFree (no wrap) or otherwise.
823 * be careful not to copy out more than nwords.
825 if (ix >= logp->firstUsed) {
826 if (logp->firstUsed <= logp->firstFree)
828 end = logp->firstFree; /* first element not to copy */
831 nwords = inWords; /* don't copy more than this */
832 if (end - ix < nwords)
835 memcpy((char *)bufferp, (char *)&logp->datap[ix],
836 sizeof(afs_int32) * nwords);
841 /* if we're going to copy more out below, we'll start here */
844 /* now, if active part of the log has wrapped, there's more stuff
845 * starting at the head of the log. Copy out more from there.
847 if (logp->firstUsed > logp->firstFree && ix < logp->firstFree
849 /* (more to) copy out from the wrapped section at the
850 * start of the log. May get here even if didn't copy any
851 * above, if the cookie points directly into the wrapped section.
854 if (logp->firstFree - ix < nwords)
855 nwords = logp->firstFree - ix;
856 memcpy((char *)bufferp, (char *)&logp->datap[ix],
857 sizeof(afs_int32) * nwords);
863 ReleaseWriteLock(&logp->lock);
867 *bufSizep = outWords;
873 /* hold and release logs */
875 afs_icl_LogHold(struct afs_icl_log *logp)
877 ObtainWriteLock(&afs_icl_lock, 187);
879 ReleaseWriteLock(&afs_icl_lock);
883 /* keep track of how many sets believe the log itself is allocated */
885 afs_icl_LogUse(struct afs_icl_log *logp)
887 ObtainWriteLock(&logp->lock, 188);
888 if (logp->setCount == 0) {
889 /* this is the first set actually using the log -- allocate it */
890 if (logp->logSize == 0) {
891 /* we weren't passed in a hint and it wasn't set */
892 logp->logSize = ICL_DEFAULT_LOGSIZE;
894 logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logp->logSize);
895 osi_Assert(logp->datap != NULL);
896 #ifdef KERNEL_HAVE_PIN
897 pin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
901 ReleaseWriteLock(&logp->lock);
905 /* decrement the number of real users of the log, free if possible */
907 afs_icl_LogFreeUse(struct afs_icl_log *logp)
909 ObtainWriteLock(&logp->lock, 189);
910 if (--logp->setCount == 0) {
911 /* no more users -- free it (but keep log structure around) */
912 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
913 #ifdef KERNEL_HAVE_PIN
914 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
916 logp->firstUsed = logp->firstFree = 0;
917 logp->logElements = 0;
920 ReleaseWriteLock(&logp->lock);
924 /* set the size of the log to 'logSize' */
926 afs_icl_LogSetSize(struct afs_icl_log *logp, afs_int32 logSize)
928 ObtainWriteLock(&logp->lock, 190);
930 /* nothing to worry about since it's not allocated */
931 logp->logSize = logSize;
934 logp->firstUsed = logp->firstFree = 0;
935 logp->logElements = 0;
937 /* free and allocate a new one */
938 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
939 #ifdef KERNEL_HAVE_PIN
940 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
942 logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logSize);
943 osi_Assert(logp->datap != NULL);
944 #ifdef KERNEL_HAVE_PIN
945 pin((char *)logp->datap, sizeof(afs_int32) * logSize);
947 logp->logSize = logSize;
949 ReleaseWriteLock(&logp->lock);
954 /* free a log. Called with afs_icl_lock locked. */
956 afs_icl_ZapLog(struct afs_icl_log *logp)
958 struct afs_icl_log **lpp, *tp;
960 for (lpp = &afs_icl_allLogs, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
962 /* found the dude we want to remove */
964 osi_FreeSmallSpace(logp->name);
965 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
966 #ifdef KERNEL_HAVE_PIN
967 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
969 osi_FreeSmallSpace(logp);
970 break; /* won't find it twice */
976 /* do the release, watching for deleted entries */
978 afs_icl_LogRele(struct afs_icl_log *logp)
980 ObtainWriteLock(&afs_icl_lock, 191);
981 if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
982 afs_icl_ZapLog(logp); /* destroys logp's lock! */
984 ReleaseWriteLock(&afs_icl_lock);
988 /* do the release, watching for deleted entries, log already held */
990 afs_icl_LogReleNL(struct afs_icl_log *logp)
992 if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
993 afs_icl_ZapLog(logp); /* destroys logp's lock! */
998 /* zero out the log */
1000 afs_icl_ZeroLog(struct afs_icl_log *logp)
1002 ObtainWriteLock(&logp->lock, 192);
1003 logp->firstUsed = logp->firstFree = 0;
1004 logp->logElements = 0;
1005 logp->baseCookie = 0;
1006 ReleaseWriteLock(&logp->lock);
1010 /* free a log entry, and drop its reference count */
1012 afs_icl_LogFree(struct afs_icl_log *logp)
1014 ObtainWriteLock(&logp->lock, 193);
1015 logp->states |= ICL_LOGF_DELETED;
1016 ReleaseWriteLock(&logp->lock);
1017 afs_icl_LogRele(logp);
1021 /* find a log by name, returning it held */
1022 struct afs_icl_log *
1023 afs_icl_FindLog(char *name)
1025 struct afs_icl_log *tp;
1026 ObtainWriteLock(&afs_icl_lock, 194);
1027 for (tp = afs_icl_allLogs; tp; tp = tp->nextp) {
1028 if (strcmp(tp->name, name) == 0) {
1029 /* this is the dude we want */
1034 ReleaseWriteLock(&afs_icl_lock);
1038 struct afs_icl_set *afs_icl_allSets = 0;
1041 afs_icl_CreateSet(char *name, struct afs_icl_log *baseLogp,
1042 struct afs_icl_log *fatalLogp,
1043 struct afs_icl_set **outSetpp)
1045 return afs_icl_CreateSetWithFlags(name, baseLogp, fatalLogp,
1046 /*flags */ 0, outSetpp);
1049 /* create a set, given pointers to base and fatal logs, if any.
1050 * Logs are unlocked, but referenced, and *outSetpp is returned
1051 * referenced. Function bumps reference count on logs, since it
1052 * addds references from the new afs_icl_set. When the set is destroyed,
1053 * those references will be released.
1056 afs_icl_CreateSetWithFlags(char *name, struct afs_icl_log *baseLogp,
1057 struct afs_icl_log *fatalLogp, afs_uint32 flags,
1058 struct afs_icl_set **outSetpp)
1060 struct afs_icl_set *setp;
1062 afs_int32 states = ICL_DEFAULT_SET_STATES;
1064 ObtainWriteLock(&afs_icl_lock, 197);
1065 if (!afs_icl_inited)
1068 for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
1069 if (strcmp(setp->name, name) == 0) {
1072 if (flags & ICL_CRSET_FLAG_PERSISTENT) {
1073 ObtainWriteLock(&setp->lock, 198);
1074 setp->states |= ICL_SETF_PERSISTENT;
1075 ReleaseWriteLock(&setp->lock);
1077 ReleaseWriteLock(&afs_icl_lock);
1082 /* determine initial state */
1083 if (flags & ICL_CRSET_FLAG_DEFAULT_ON)
1084 states = ICL_SETF_ACTIVE;
1085 else if (flags & ICL_CRSET_FLAG_DEFAULT_OFF)
1086 states = ICL_SETF_FREED;
1087 if (flags & ICL_CRSET_FLAG_PERSISTENT)
1088 states |= ICL_SETF_PERSISTENT;
1090 setp = osi_AllocSmallSpace(sizeof(struct afs_icl_set));
1091 memset((caddr_t) setp, 0, sizeof(*setp));
1093 if (states & ICL_SETF_FREED)
1094 states &= ~ICL_SETF_ACTIVE; /* if freed, can't be active */
1095 setp->states = states;
1097 LOCK_INIT(&setp->lock, "setp lock");
1098 /* next lock is obtained in wrong order, hierarchy-wise, but
1099 * it doesn't matter, since no one can find this lock yet, since
1100 * the afs_icl_lock is still held, and thus the obtain can't block.
1102 ObtainWriteLock(&setp->lock, 199);
1103 setp->name = osi_AllocSmallSpace(strlen(name) + 1);
1104 strcpy(setp->name, name);
1105 setp->nevents = ICL_DEFAULTEVENTS;
1106 setp->eventFlags = afs_osi_Alloc(ICL_DEFAULTEVENTS);
1107 osi_Assert(setp->eventFlags != NULL);
1108 #ifdef KERNEL_HAVE_PIN
1109 pin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1111 for (i = 0; i < ICL_DEFAULTEVENTS; i++)
1112 setp->eventFlags[i] = 0xff; /* default to enabled */
1114 /* update this global info under the afs_icl_lock */
1115 setp->nextp = afs_icl_allSets;
1116 afs_icl_allSets = setp;
1117 ReleaseWriteLock(&afs_icl_lock);
1119 /* set's basic lock is still held, so we can finish init */
1121 setp->logs[0] = baseLogp;
1122 afs_icl_LogHold(baseLogp);
1123 if (!(setp->states & ICL_SETF_FREED))
1124 afs_icl_LogUse(baseLogp); /* log is actually being used */
1127 setp->logs[1] = fatalLogp;
1128 afs_icl_LogHold(fatalLogp);
1129 if (!(setp->states & ICL_SETF_FREED))
1130 afs_icl_LogUse(fatalLogp); /* log is actually being used */
1132 ReleaseWriteLock(&setp->lock);
1138 /* hold and release event sets */
1140 afs_icl_SetHold(struct afs_icl_set *setp)
1142 ObtainWriteLock(&afs_icl_lock, 201);
1144 ReleaseWriteLock(&afs_icl_lock);
1148 /* free a set. Called with afs_icl_lock locked */
1150 afs_icl_ZapSet(struct afs_icl_set *setp)
1152 struct afs_icl_set **lpp, *tp;
1154 struct afs_icl_log *tlp;
1156 for (lpp = &afs_icl_allSets, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
1158 /* found the dude we want to remove */
1160 osi_FreeSmallSpace(setp->name);
1161 afs_osi_Free(setp->eventFlags, ICL_DEFAULTEVENTS);
1162 #ifdef KERNEL_HAVE_PIN
1163 unpin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1165 for (i = 0; i < ICL_LOGSPERSET; i++) {
1166 if ((tlp = setp->logs[i]))
1167 afs_icl_LogReleNL(tlp);
1169 osi_FreeSmallSpace(setp);
1170 break; /* won't find it twice */
1176 /* do the release, watching for deleted entries */
1178 afs_icl_SetRele(struct afs_icl_set *setp)
1180 ObtainWriteLock(&afs_icl_lock, 202);
1181 if (--setp->refCount == 0 && (setp->states & ICL_SETF_DELETED)) {
1182 afs_icl_ZapSet(setp); /* destroys setp's lock! */
1184 ReleaseWriteLock(&afs_icl_lock);
1188 /* free a set entry, dropping its reference count */
1190 afs_icl_SetFree(struct afs_icl_set *setp)
1192 ObtainWriteLock(&setp->lock, 203);
1193 setp->states |= ICL_SETF_DELETED;
1194 ReleaseWriteLock(&setp->lock);
1195 afs_icl_SetRele(setp);
1199 /* find a set by name, returning it held */
1200 struct afs_icl_set *
1201 afs_icl_FindSet(char *name)
1203 struct afs_icl_set *tp;
1204 ObtainWriteLock(&afs_icl_lock, 204);
1205 for (tp = afs_icl_allSets; tp; tp = tp->nextp) {
1206 if (strcmp(tp->name, name) == 0) {
1207 /* this is the dude we want */
1212 ReleaseWriteLock(&afs_icl_lock);
1216 /* zero out all the logs in the set */
1218 afs_icl_ZeroSet(struct afs_icl_set *setp)
1223 struct afs_icl_log *logp;
1225 ObtainReadLock(&setp->lock);
1226 for (i = 0; i < ICL_LOGSPERSET; i++) {
1227 logp = setp->logs[i];
1229 afs_icl_LogHold(logp);
1230 tcode = afs_icl_ZeroLog(logp);
1232 code = tcode; /* save the last bad one */
1233 afs_icl_LogRele(logp);
1236 ReleaseReadLock(&setp->lock);
1241 afs_icl_SetSetStat(struct afs_icl_set *setp, int op)
1245 struct afs_icl_log *logp;
1247 ObtainWriteLock(&setp->lock, 208);
1249 case ICL_OP_SS_ACTIVATE: /* activate a log */
1251 * If we are not already active, see if we have released
1252 * our demand that the log be allocated (FREED set). If
1253 * we have, reassert our desire.
1255 if (!(setp->states & ICL_SETF_ACTIVE)) {
1256 if (setp->states & ICL_SETF_FREED) {
1257 /* have to reassert desire for logs */
1258 for (i = 0; i < ICL_LOGSPERSET; i++) {
1259 logp = setp->logs[i];
1261 afs_icl_LogHold(logp);
1262 afs_icl_LogUse(logp);
1263 afs_icl_LogRele(logp);
1266 setp->states &= ~ICL_SETF_FREED;
1268 setp->states |= ICL_SETF_ACTIVE;
1273 case ICL_OP_SS_DEACTIVATE: /* deactivate a log */
1274 /* this doesn't require anything beyond clearing the ACTIVE flag */
1275 setp->states &= ~ICL_SETF_ACTIVE;
1279 case ICL_OP_SS_FREE: /* deassert design for log */
1281 * if we are already in this state, do nothing; otherwise
1282 * deassert desire for log
1284 if (setp->states & ICL_SETF_ACTIVE)
1287 if (!(setp->states & ICL_SETF_FREED)) {
1288 for (i = 0; i < ICL_LOGSPERSET; i++) {
1289 logp = setp->logs[i];
1291 afs_icl_LogHold(logp);
1292 afs_icl_LogFreeUse(logp);
1293 afs_icl_LogRele(logp);
1296 setp->states |= ICL_SETF_FREED;
1305 ReleaseWriteLock(&setp->lock);