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_LINUX20_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 /* 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__)
39 int afs_icl_sizeofLong = ICL_LONG;
41 int afs_icl_inited = 0;
43 /* init function, called once, under afs_icl_lock */
51 /* Function called at shutdown - zap everything */
55 struct afs_icl_log *logp;
56 struct afs_icl_set *setp;
58 setp = afs_icl_FindSet("cm");
60 /* Release the reference from Find, and the initial one */
61 afs_icl_SetFree(setp);
62 afs_icl_SetFree(setp);
64 setp = afs_icl_FindSet("cmlongterm");
66 /* Release the reference from Find, and the initial one */
67 afs_icl_SetFree(setp);
68 afs_icl_SetFree(setp);
70 logp = afs_icl_FindLog("cmfx");
72 /* Release the reference from Find, and the initial one */
73 afs_icl_LogFree(logp);
74 afs_icl_LogFree(logp);
79 afs_icl_InitLogs(void)
81 struct afs_icl_log *logp;
84 /* initialize the ICL system */
85 code = afs_icl_CreateLog("cmfx", 60 * 1024, &logp);
88 afs_icl_CreateSetWithFlags("cm", logp, NULL,
89 ICL_CRSET_FLAG_DEFAULT_OFF,
92 afs_icl_CreateSet("cmlongterm", logp, NULL,
93 &afs_iclLongTermSetp);
98 struct afs_icl_log *afs_icl_FindLog(char *);
99 struct afs_icl_set *afs_icl_FindSet(char *);
102 #ifdef AFS_DARWIN100_ENV
103 #define AFSKPTR(X) k ## X
105 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
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)),
115 #define AFSKPTR(X) ((caddr_t)X)
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)
122 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
125 afs_int32 *lp, elts, flags;
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)
131 #else /* AFS_SGI61_ENV */
132 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
137 #endif /* AFS_SGI61_ENV */
139 afs_int32 startCookie;
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;
150 if (!afs_suser(CRED())) { /* only root can run this code */
154 if (!afs_suser(NULL)) { /* only root can run this code */
155 #if defined(KERNEL_HAVE_UERROR)
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
171 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
174 AFS_COPYIN(AFSKPTR(p4), (char *)&startCookie, sizeof(afs_int32), code);
177 logp = afs_icl_FindLog(tname);
180 #define BUFFERSIZE AFS_LRALLOCSIZ
181 lp = (afs_int32 *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
182 elts = BUFFERSIZE / sizeof(afs_int32);
185 flags = (opcode == ICL_OP_COPYOUT) ? 0 : ICL_COPYOUTF_CLRAFTERREAD;
187 afs_icl_CopyOut(logp, lp, &elts, (afs_uint32 *) & startCookie,
190 osi_FreeLargeSpace((struct osi_buffer *)lp);
193 AFS_COPYOUT((char *)lp, AFSKPTR(p2), elts * sizeof(afs_int32), code);
196 AFS_COPYOUT((char *)&startCookie, AFSKPTR(p4), sizeof(afs_int32),
200 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
202 *retval = ((long)((flags << 24) | (elts & 0xffffff))) << 32;
205 *retval = (flags << 24) | (elts & 0xffffff);
207 afs_icl_LogRele(logp);
208 osi_FreeLargeSpace((struct osi_buffer *)lp);
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.
215 for (tlp = afs_icl_allLogs; tlp; tlp = tlp->nextp) {
220 return ENOENT; /* past the end of file */
221 temp = strlen(tlp->name) + 1;
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),
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.
234 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
237 setp = afs_icl_FindSet(tname);
240 if (p2 > ICL_LOGSPERSET)
242 if (!(tlp = setp->logs[p2]))
244 temp = strlen(tlp->name) + 1;
247 AFS_COPYOUT(tlp->name, AFSKPTR(p3), temp, code);
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);
255 logp = afs_icl_FindLog(tname);
258 code = afs_icl_ZeroLog(logp);
259 afs_icl_LogRele(logp);
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);
267 setp = afs_icl_FindSet(tname);
270 code = afs_icl_ZeroSet(setp);
271 afs_icl_SetRele(setp);
274 case ICL_OP_CLRALL: /* clear all logs */
275 /* zero out all logs -- no args */
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)
290 ReleaseWriteLock(&afs_icl_lock);
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.
297 for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
302 return ENOENT; /* past the end of file */
303 temp = strlen(setp->name) + 1;
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),
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);
317 setp = afs_icl_FindSet(tname);
320 code = afs_icl_SetSetStat(setp, p2);
321 afs_icl_SetRele(setp);
324 case ICL_OP_SETSTATALL: /* set status on all sets */
325 /* activate the specified set: p1=op */
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);
340 ReleaseWriteLock(&afs_icl_lock);
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);
348 logp = afs_icl_FindLog(tname);
351 code = afs_icl_LogSetSize(logp, p2);
352 afs_icl_LogRele(logp);
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);
360 logp = afs_icl_FindLog(tname);
363 allocated = !!logp->datap;
364 AFS_COPYOUT((char *)&logp->logSize, AFSKPTR(p2), sizeof(afs_int32),
367 AFS_COPYOUT((char *)&allocated, AFSKPTR(p3), sizeof(afs_int32),
369 afs_icl_LogRele(logp);
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);
377 setp = afs_icl_FindSet(tname);
380 AFS_COPYOUT((char *)&setp->states, AFSKPTR(p2), sizeof(afs_int32),
382 afs_icl_SetRele(setp);
393 afs_lock_t afs_icl_lock;
395 /* exported routine: a 4 parameter event */
397 afs_icl_Event4(struct afs_icl_set *setp, afs_int32 eventID,
398 afs_int32 lAndT, long p1, long p2, long p3, long p4)
405 /* If things aren't init'ed yet (or the set is inactive), don't panic */
406 if (!ICL_SETACTIVE(setp))
410 mask = lAndT >> 24 & 0xff; /* mask of which logs to log to */
411 ix = ICL_EVENTBYTE(eventID);
412 ObtainReadLock(&setp->lock);
413 if (setp->eventFlags[ix] & ICL_EVENTMASK(eventID)) {
414 for (i = 0, tmask = 1; i < ICL_LOGSPERSET; i++, tmask <<= 1) {
416 afs_icl_AppendRecord(setp->logs[i], eventID, lAndT & 0xffffff,
421 break; /* break early */
424 ReleaseReadLock(&setp->lock);
428 /* Next 4 routines should be implemented via var-args or something.
429 * Whole purpose is to avoid compiler warnings about parameter # mismatches.
430 * Otherwise, could call afs_icl_Event4 directly.
433 afs_icl_Event3(struct afs_icl_set *setp, afs_int32 eventID,
434 afs_int32 lAndT, long p1, long p2, long p3)
436 return afs_icl_Event4(setp, eventID, lAndT, p1, p2, p3, (long)0);
440 afs_icl_Event2(struct afs_icl_set *setp, afs_int32 eventID,
441 afs_int32 lAndT, long p1, long p2)
443 return afs_icl_Event4(setp, eventID, lAndT, p1, p2, (long)0, (long)0);
447 afs_icl_Event1(struct afs_icl_set *setp, afs_int32 eventID,
448 afs_int32 lAndT, long p1)
450 return afs_icl_Event4(setp, eventID, lAndT, p1, (long)0, (long)0,
455 afs_icl_Event0(struct afs_icl_set *setp, afs_int32 eventID,
458 return afs_icl_Event4(setp, eventID, lAndT, (long)0, (long)0, (long)0,
462 struct afs_icl_log *afs_icl_allLogs = 0;
464 /* function to purge records from the start of the log, until there
465 * is at least minSpace long's worth of space available without
466 * making the head and the tail point to the same word.
468 * Log must be write-locked.
471 afs_icl_GetLogSpace(struct afs_icl_log *logp, afs_int32 minSpace)
475 while (logp->logSize - logp->logElements <= minSpace) {
477 tsize = ((logp->datap[logp->firstUsed]) >> 24) & 0xff;
478 logp->logElements -= tsize;
479 logp->firstUsed += tsize;
480 if (logp->firstUsed >= logp->logSize)
481 logp->firstUsed -= logp->logSize;
482 logp->baseCookie += tsize;
486 /* append string astr to buffer, including terminating null char.
488 * log must be write-locked.
490 #define ICL_CHARSPERLONG 4
492 afs_icl_AppendString(struct afs_icl_log *logp, char *astr)
494 char *op; /* ptr to char to write */
496 int bib; /* bytes in buffer */
499 op = (char *)&(logp->datap[logp->firstFree]);
503 if (++bib >= ICL_CHARSPERLONG) {
506 if (++(logp->firstFree) >= logp->logSize) {
508 op = (char *)&(logp->datap[0]);
516 /* if we've used this word at all, allocate it */
517 if (++(logp->firstFree) >= logp->logSize) {
524 /* add a long to the log, ignoring overflow (checked already) */
525 #define ICL_APPENDINT32(lp, x) \
527 (lp)->datap[(lp)->firstFree] = (x); \
528 if (++((lp)->firstFree) >= (lp)->logSize) { \
529 (lp)->firstFree = 0; \
531 (lp)->logElements++; \
535 #define ICL_APPENDLONG(lp, x) \
537 ICL_APPENDINT32((lp), ((x) >> 32) & 0xffffffffL); \
538 ICL_APPENDINT32((lp), (x) & 0xffffffffL); \
542 #define ICL_APPENDLONG(lp, x) ICL_APPENDINT32((lp), (x))
545 /* routine to tell whether we're dealing with the address or the
549 afs_icl_UseAddr(int type)
551 if (type == ICL_TYPE_HYPER || type == ICL_TYPE_STRING
552 || type == ICL_TYPE_FID || type == ICL_TYPE_INT64)
559 afs_icl_AppendOne(struct afs_icl_log *logp, int type, long parm)
562 /* marshall parameter 3 now */
563 if (type == ICL_TYPE_STRING)
564 afs_icl_AppendString(logp, (char *)parm);
565 else if (type == ICL_TYPE_HYPER) {
566 ICL_APPENDINT32(logp,
567 (afs_int32) ((struct afs_hyper_t *)parm)->high);
568 ICL_APPENDINT32(logp,
569 (afs_int32) ((struct afs_hyper_t *)parm)->low);
570 } else if (type == ICL_TYPE_INT64) {
571 #ifndef WORDS_BIGENDIAN
572 #ifdef AFS_64BIT_CLIENT
573 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
574 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
575 #else /* AFS_64BIT_CLIENT */
576 ICL_APPENDINT32(logp, (afs_int32) parm);
577 ICL_APPENDINT32(logp, (afs_int32) 0);
578 #endif /* AFS_64BIT_CLIENT */
579 #else /* AFSLITTLE_ENDIAN */
580 #ifdef AFS_64BIT_CLIENT
581 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
582 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
583 #else /* AFS_64BIT_CLIENT */
584 ICL_APPENDINT32(logp, (afs_int32) 0);
585 ICL_APPENDINT32(logp, (afs_int32) parm);
586 #endif /* AFS_64BIT_CLIENT */
587 #endif /* AFSLITTLE_ENDIAN */
588 } else if (type == ICL_TYPE_FID) {
589 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
590 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
591 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[2]);
592 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[3]);
595 else if (type == ICL_TYPE_INT32)
596 ICL_APPENDINT32(logp, (afs_int32) parm);
599 ICL_APPENDLONG(logp, parm);
603 /* Function to append a record to the log. Written for speed
604 * since we know that we're going to have to make this work fast
605 * pretty soon, anyway. The log must be unlocked.
609 afs_icl_AppendRecord(struct afs_icl_log *logp, afs_int32 op,
610 afs_int32 types, long p1, long p2, long p3, long p4)
612 int rsize; /* record size in longs */
613 int tsize; /* temp size */
617 t4 = types & 0x3f; /* decode types */
625 osi_GetTime(&tv); /* It panics for solaris if inside */
626 ObtainWriteLock(&logp->lock, 182);
628 ReleaseWriteLock(&logp->lock);
632 /* get timestamp as # of microseconds since some time that doesn't
633 * change that often. This algorithm ticks over every 20 minutes
634 * or so (1000 seconds). Write a timestamp record if it has.
636 if (tv.tv_sec - logp->lastTS > 1024) {
637 /* the timer has wrapped -- write a timestamp record */
638 if (logp->logSize - logp->logElements <= 5)
639 afs_icl_GetLogSpace(logp, 5);
641 ICL_APPENDINT32(logp,
642 (afs_int32) (5 << 24) + (ICL_TYPE_UNIXDATE << 18));
643 ICL_APPENDINT32(logp, (afs_int32) ICL_INFO_TIMESTAMP);
644 ICL_APPENDINT32(logp, (afs_int32) 0); /* use thread ID zero for clocks */
645 ICL_APPENDINT32(logp,
646 (afs_int32) (tv.tv_sec & 0x3ff) * 1000000 +
648 ICL_APPENDINT32(logp, (afs_int32) tv.tv_sec);
650 logp->lastTS = tv.tv_sec;
653 rsize = 4; /* base case: see 4 items below */
655 /* compute size of parameter p1. Only tricky case is string.
656 * In that case, we have to call strlen to get the string length.
658 ICL_SIZEHACK(t1, p1, tsize, rsize);
661 /* compute size of parameter p2. Only tricky case is string.
662 * In that case, we have to call strlen to get the string length.
664 ICL_SIZEHACK(t2, p2, tsize, rsize);
667 /* compute size of parameter p3. Only tricky case is string.
668 * In that case, we have to call strlen to get the string length.
670 ICL_SIZEHACK(t3, p3, tsize, rsize);
673 /* compute size of parameter p4. Only tricky case is string.
674 * In that case, we have to call strlen to get the string length.
676 ICL_SIZEHACK(t4, p4, tsize, rsize);
679 /* At this point, we've computed all of the parameter sizes, and
680 * have in rsize the size of the entire record we want to append.
681 * Next, we check that we actually have room in the log to do this
682 * work, and then we do the append.
685 ReleaseWriteLock(&logp->lock);
686 return; /* log record too big to express */
689 if (logp->logSize - logp->logElements <= rsize)
690 afs_icl_GetLogSpace(logp, rsize);
692 ICL_APPENDINT32(logp,
693 (afs_int32) (rsize << 24) + (t1 << 18) + (t2 << 12) +
695 ICL_APPENDINT32(logp, (afs_int32) op);
696 ICL_APPENDINT32(logp, (afs_int32) osi_ThreadUnique());
697 ICL_APPENDINT32(logp,
698 (afs_int32) (tv.tv_sec & 0x3ff) * 1000000 + tv.tv_usec);
700 afs_icl_AppendOne(logp, t1, p1);
701 afs_icl_AppendOne(logp, t2, p2);
702 afs_icl_AppendOne(logp, t3, p3);
703 afs_icl_AppendOne(logp, t4, p4);
704 ReleaseWriteLock(&logp->lock);
707 /* create a log with size logSize; return it in *outLogpp and tag
708 * it with name "name."
711 afs_icl_CreateLog(char *name, afs_int32 logSize,
712 struct afs_icl_log **outLogpp)
714 return afs_icl_CreateLogWithFlags(name, logSize, /*flags */ 0, outLogpp);
717 /* create a log with size logSize; return it in *outLogpp and tag
718 * it with name "name." 'flags' can be set to make the log unclearable.
721 afs_icl_CreateLogWithFlags(char *name, afs_int32 logSize, afs_uint32 flags,
722 struct afs_icl_log **outLogpp)
724 struct afs_icl_log *logp;
726 /* add into global list under lock */
727 ObtainWriteLock(&afs_icl_lock, 183);
731 for (logp = afs_icl_allLogs; logp; logp = logp->nextp) {
732 if (strcmp(logp->name, name) == 0) {
733 /* found it already created, just return it */
736 if (flags & ICL_CRLOG_FLAG_PERSISTENT) {
737 ObtainWriteLock(&logp->lock, 184);
738 logp->states |= ICL_LOGF_PERSISTENT;
739 ReleaseWriteLock(&logp->lock);
741 ReleaseWriteLock(&afs_icl_lock);
746 logp = (struct afs_icl_log *)
747 osi_AllocSmallSpace(sizeof(struct afs_icl_log));
748 memset((caddr_t) logp, 0, sizeof(*logp));
751 logp->name = osi_AllocSmallSpace(strlen(name) + 1);
752 strcpy(logp->name, name);
753 LOCK_INIT(&logp->lock, "logp lock");
754 logp->logSize = logSize;
755 logp->datap = NULL; /* don't allocate it until we need it */
757 if (flags & ICL_CRLOG_FLAG_PERSISTENT)
758 logp->states |= ICL_LOGF_PERSISTENT;
760 logp->nextp = afs_icl_allLogs;
761 afs_icl_allLogs = logp;
762 ReleaseWriteLock(&afs_icl_lock);
768 /* called with a log, a pointer to a buffer, the size of the buffer
769 * (in *bufSizep), the starting cookie (in *cookiep, use 0 at the start)
770 * and returns data in the provided buffer, and returns output flags
771 * in *flagsp. The flag ICL_COPYOUTF_MISSEDSOME is set if we can't
772 * find the record with cookie value cookie.
775 afs_icl_CopyOut(struct afs_icl_log *logp, afs_int32 * bufferp,
776 afs_int32 * bufSizep, afs_uint32 * cookiep,
779 afs_int32 nwords; /* number of words to copy out */
780 afs_uint32 startCookie; /* first cookie to use */
781 afs_int32 outWords; /* words we've copied out */
782 afs_int32 inWords; /* max words to copy out */
783 afs_int32 code; /* return code */
784 afs_int32 ix; /* index we're copying from */
785 afs_int32 outFlags; /* return flags */
786 afs_int32 inFlags; /* flags passed in */
789 inWords = *bufSizep; /* max to copy out */
790 outWords = 0; /* amount copied out */
791 startCookie = *cookiep;
796 ObtainWriteLock(&logp->lock, 185);
798 ReleaseWriteLock(&logp->lock);
802 /* first, compute the index of the start cookie we've been passed */
804 /* (re-)compute where we should start */
805 if (startCookie < logp->baseCookie) {
806 if (startCookie) /* missed some output */
807 outFlags |= ICL_COPYOUTF_MISSEDSOME;
808 /* skip to the first available record */
809 startCookie = logp->baseCookie;
810 *cookiep = startCookie;
813 /* compute where we find the first element to copy out */
814 ix = logp->firstUsed + startCookie - logp->baseCookie;
815 if (ix >= logp->logSize)
818 /* if have some data now, break out and process it */
819 if (startCookie - logp->baseCookie < logp->logElements)
822 /* At end of log, so clear it if we need to */
823 if (inFlags & ICL_COPYOUTF_CLRAFTERREAD) {
824 logp->firstUsed = logp->firstFree = 0;
825 logp->logElements = 0;
827 /* otherwise, either wait for the data to arrive, or return */
828 if (!(inFlags & ICL_COPYOUTF_WAITIO)) {
829 ReleaseWriteLock(&logp->lock);
833 logp->states |= ICL_LOGF_WAITING;
834 ReleaseWriteLock(&logp->lock);
835 afs_osi_Sleep(&logp->lock);
836 ObtainWriteLock(&logp->lock, 186);
838 /* copy out data from ix to logSize or firstFree, depending
839 * upon whether firstUsed <= firstFree (no wrap) or otherwise.
840 * be careful not to copy out more than nwords.
842 if (ix >= logp->firstUsed) {
843 if (logp->firstUsed <= logp->firstFree)
845 end = logp->firstFree; /* first element not to copy */
848 nwords = inWords; /* don't copy more than this */
849 if (end - ix < nwords)
852 memcpy((char *)bufferp, (char *)&logp->datap[ix],
853 sizeof(afs_int32) * nwords);
858 /* if we're going to copy more out below, we'll start here */
861 /* now, if active part of the log has wrapped, there's more stuff
862 * starting at the head of the log. Copy out more from there.
864 if (logp->firstUsed > logp->firstFree && ix < logp->firstFree
866 /* (more to) copy out from the wrapped section at the
867 * start of the log. May get here even if didn't copy any
868 * above, if the cookie points directly into the wrapped section.
871 if (logp->firstFree - ix < nwords)
872 nwords = logp->firstFree - ix;
873 memcpy((char *)bufferp, (char *)&logp->datap[ix],
874 sizeof(afs_int32) * nwords);
880 ReleaseWriteLock(&logp->lock);
884 *bufSizep = outWords;
890 /* return basic parameter information about a log */
892 afs_icl_GetLogParms(struct afs_icl_log *logp, afs_int32 * maxSizep,
893 afs_int32 * curSizep)
895 ObtainReadLock(&logp->lock);
896 *maxSizep = logp->logSize;
897 *curSizep = logp->logElements;
898 ReleaseReadLock(&logp->lock);
903 /* hold and release logs */
905 afs_icl_LogHold(struct afs_icl_log *logp)
907 ObtainWriteLock(&afs_icl_lock, 187);
909 ReleaseWriteLock(&afs_icl_lock);
913 /* hold and release logs, called with lock already held */
915 afs_icl_LogHoldNL(struct afs_icl_log *logp)
921 /* keep track of how many sets believe the log itself is allocated */
923 afs_icl_LogUse(struct afs_icl_log *logp)
925 ObtainWriteLock(&logp->lock, 188);
926 if (logp->setCount == 0) {
927 /* this is the first set actually using the log -- allocate it */
928 if (logp->logSize == 0) {
929 /* we weren't passed in a hint and it wasn't set */
930 logp->logSize = ICL_DEFAULT_LOGSIZE;
932 logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logp->logSize);
933 osi_Assert(logp->datap != NULL);
934 #ifdef KERNEL_HAVE_PIN
935 pin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
939 ReleaseWriteLock(&logp->lock);
943 /* decrement the number of real users of the log, free if possible */
945 afs_icl_LogFreeUse(struct afs_icl_log *logp)
947 ObtainWriteLock(&logp->lock, 189);
948 if (--logp->setCount == 0) {
949 /* no more users -- free it (but keep log structure around) */
950 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
951 #ifdef KERNEL_HAVE_PIN
952 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
954 logp->firstUsed = logp->firstFree = 0;
955 logp->logElements = 0;
958 ReleaseWriteLock(&logp->lock);
962 /* set the size of the log to 'logSize' */
964 afs_icl_LogSetSize(struct afs_icl_log *logp, afs_int32 logSize)
966 ObtainWriteLock(&logp->lock, 190);
968 /* nothing to worry about since it's not allocated */
969 logp->logSize = logSize;
972 logp->firstUsed = logp->firstFree = 0;
973 logp->logElements = 0;
975 /* free and allocate a new one */
976 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
977 #ifdef KERNEL_HAVE_PIN
978 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
980 logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logSize);
981 osi_Assert(logp->datap != NULL);
982 #ifdef KERNEL_HAVE_PIN
983 pin((char *)logp->datap, sizeof(afs_int32) * logSize);
985 logp->logSize = logSize;
987 ReleaseWriteLock(&logp->lock);
992 /* free a log. Called with afs_icl_lock locked. */
994 afs_icl_ZapLog(struct afs_icl_log *logp)
996 struct afs_icl_log **lpp, *tp;
998 for (lpp = &afs_icl_allLogs, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
1000 /* found the dude we want to remove */
1002 osi_FreeSmallSpace(logp->name);
1003 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
1004 #ifdef KERNEL_HAVE_PIN
1005 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
1007 osi_FreeSmallSpace(logp);
1008 break; /* won't find it twice */
1014 /* do the release, watching for deleted entries */
1016 afs_icl_LogRele(struct afs_icl_log *logp)
1018 ObtainWriteLock(&afs_icl_lock, 191);
1019 if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
1020 afs_icl_ZapLog(logp); /* destroys logp's lock! */
1022 ReleaseWriteLock(&afs_icl_lock);
1026 /* do the release, watching for deleted entries, log already held */
1028 afs_icl_LogReleNL(struct afs_icl_log *logp)
1030 if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
1031 afs_icl_ZapLog(logp); /* destroys logp's lock! */
1036 /* zero out the log */
1038 afs_icl_ZeroLog(struct afs_icl_log *logp)
1040 ObtainWriteLock(&logp->lock, 192);
1041 logp->firstUsed = logp->firstFree = 0;
1042 logp->logElements = 0;
1043 logp->baseCookie = 0;
1044 ReleaseWriteLock(&logp->lock);
1048 /* free a log entry, and drop its reference count */
1050 afs_icl_LogFree(struct afs_icl_log *logp)
1052 ObtainWriteLock(&logp->lock, 193);
1053 logp->states |= ICL_LOGF_DELETED;
1054 ReleaseWriteLock(&logp->lock);
1055 afs_icl_LogRele(logp);
1059 /* find a log by name, returning it held */
1060 struct afs_icl_log *
1061 afs_icl_FindLog(char *name)
1063 struct afs_icl_log *tp;
1064 ObtainWriteLock(&afs_icl_lock, 194);
1065 for (tp = afs_icl_allLogs; tp; tp = tp->nextp) {
1066 if (strcmp(tp->name, name) == 0) {
1067 /* this is the dude we want */
1072 ReleaseWriteLock(&afs_icl_lock);
1077 afs_icl_EnumerateLogs(int (*aproc)
1078 (char *name, char *arock, struct afs_icl_log * tp),
1081 struct afs_icl_log *tp;
1085 ObtainWriteLock(&afs_icl_lock, 195);
1086 for (tp = afs_icl_allLogs; tp; tp = tp->nextp) {
1087 tp->refCount++; /* hold this guy */
1088 ReleaseWriteLock(&afs_icl_lock);
1089 ObtainReadLock(&tp->lock);
1090 code = (*aproc) (tp->name, arock, tp);
1091 ReleaseReadLock(&tp->lock);
1092 ObtainWriteLock(&afs_icl_lock, 196);
1093 if (--tp->refCount == 0)
1098 ReleaseWriteLock(&afs_icl_lock);
1102 struct afs_icl_set *afs_icl_allSets = 0;
1105 afs_icl_CreateSet(char *name, struct afs_icl_log *baseLogp,
1106 struct afs_icl_log *fatalLogp,
1107 struct afs_icl_set **outSetpp)
1109 return afs_icl_CreateSetWithFlags(name, baseLogp, fatalLogp,
1110 /*flags */ 0, outSetpp);
1113 /* create a set, given pointers to base and fatal logs, if any.
1114 * Logs are unlocked, but referenced, and *outSetpp is returned
1115 * referenced. Function bumps reference count on logs, since it
1116 * addds references from the new afs_icl_set. When the set is destroyed,
1117 * those references will be released.
1120 afs_icl_CreateSetWithFlags(char *name, struct afs_icl_log *baseLogp,
1121 struct afs_icl_log *fatalLogp, afs_uint32 flags,
1122 struct afs_icl_set **outSetpp)
1124 struct afs_icl_set *setp;
1126 afs_int32 states = ICL_DEFAULT_SET_STATES;
1128 ObtainWriteLock(&afs_icl_lock, 197);
1129 if (!afs_icl_inited)
1132 for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
1133 if (strcmp(setp->name, name) == 0) {
1136 if (flags & ICL_CRSET_FLAG_PERSISTENT) {
1137 ObtainWriteLock(&setp->lock, 198);
1138 setp->states |= ICL_SETF_PERSISTENT;
1139 ReleaseWriteLock(&setp->lock);
1141 ReleaseWriteLock(&afs_icl_lock);
1146 /* determine initial state */
1147 if (flags & ICL_CRSET_FLAG_DEFAULT_ON)
1148 states = ICL_SETF_ACTIVE;
1149 else if (flags & ICL_CRSET_FLAG_DEFAULT_OFF)
1150 states = ICL_SETF_FREED;
1151 if (flags & ICL_CRSET_FLAG_PERSISTENT)
1152 states |= ICL_SETF_PERSISTENT;
1154 setp = (struct afs_icl_set *)osi_AllocSmallSpace(sizeof(struct afs_icl_set));
1155 memset((caddr_t) setp, 0, sizeof(*setp));
1157 if (states & ICL_SETF_FREED)
1158 states &= ~ICL_SETF_ACTIVE; /* if freed, can't be active */
1159 setp->states = states;
1161 LOCK_INIT(&setp->lock, "setp lock");
1162 /* next lock is obtained in wrong order, hierarchy-wise, but
1163 * it doesn't matter, since no one can find this lock yet, since
1164 * the afs_icl_lock is still held, and thus the obtain can't block.
1166 ObtainWriteLock(&setp->lock, 199);
1167 setp->name = osi_AllocSmallSpace(strlen(name) + 1);
1168 strcpy(setp->name, name);
1169 setp->nevents = ICL_DEFAULTEVENTS;
1170 setp->eventFlags = afs_osi_Alloc(ICL_DEFAULTEVENTS);
1171 osi_Assert(setp->eventFlags != NULL);
1172 #ifdef KERNEL_HAVE_PIN
1173 pin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1175 for (i = 0; i < ICL_DEFAULTEVENTS; i++)
1176 setp->eventFlags[i] = 0xff; /* default to enabled */
1178 /* update this global info under the afs_icl_lock */
1179 setp->nextp = afs_icl_allSets;
1180 afs_icl_allSets = setp;
1181 ReleaseWriteLock(&afs_icl_lock);
1183 /* set's basic lock is still held, so we can finish init */
1185 setp->logs[0] = baseLogp;
1186 afs_icl_LogHold(baseLogp);
1187 if (!(setp->states & ICL_SETF_FREED))
1188 afs_icl_LogUse(baseLogp); /* log is actually being used */
1191 setp->logs[1] = fatalLogp;
1192 afs_icl_LogHold(fatalLogp);
1193 if (!(setp->states & ICL_SETF_FREED))
1194 afs_icl_LogUse(fatalLogp); /* log is actually being used */
1196 ReleaseWriteLock(&setp->lock);
1202 /* function to change event enabling information for a particular set */
1204 afs_icl_SetEnable(struct afs_icl_set *setp, afs_int32 eventID, int setValue)
1208 ObtainWriteLock(&setp->lock, 200);
1209 if (!ICL_EVENTOK(setp, eventID)) {
1210 ReleaseWriteLock(&setp->lock);
1213 tp = &setp->eventFlags[ICL_EVENTBYTE(eventID)];
1215 *tp |= ICL_EVENTMASK(eventID);
1217 *tp &= ~(ICL_EVENTMASK(eventID));
1218 ReleaseWriteLock(&setp->lock);
1222 /* return indication of whether a particular event ID is enabled
1223 * for tracing. If *getValuep is set to 0, the event is disabled,
1224 * otherwise it is enabled. All events start out enabled by default.
1227 afs_icl_GetEnable(struct afs_icl_set *setp, afs_int32 eventID, int *getValuep)
1229 ObtainReadLock(&setp->lock);
1230 if (!ICL_EVENTOK(setp, eventID)) {
1231 ReleaseWriteLock(&setp->lock);
1234 if (setp->eventFlags[ICL_EVENTBYTE(eventID)] & ICL_EVENTMASK(eventID))
1238 ReleaseReadLock(&setp->lock);
1242 /* hold and release event sets */
1244 afs_icl_SetHold(struct afs_icl_set *setp)
1246 ObtainWriteLock(&afs_icl_lock, 201);
1248 ReleaseWriteLock(&afs_icl_lock);
1252 /* free a set. Called with afs_icl_lock locked */
1254 afs_icl_ZapSet(struct afs_icl_set *setp)
1256 struct afs_icl_set **lpp, *tp;
1258 struct afs_icl_log *tlp;
1260 for (lpp = &afs_icl_allSets, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
1262 /* found the dude we want to remove */
1264 osi_FreeSmallSpace(setp->name);
1265 afs_osi_Free(setp->eventFlags, ICL_DEFAULTEVENTS);
1266 #ifdef KERNEL_HAVE_PIN
1267 unpin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1269 for (i = 0; i < ICL_LOGSPERSET; i++) {
1270 if ((tlp = setp->logs[i]))
1271 afs_icl_LogReleNL(tlp);
1273 osi_FreeSmallSpace(setp);
1274 break; /* won't find it twice */
1280 /* do the release, watching for deleted entries */
1282 afs_icl_SetRele(struct afs_icl_set *setp)
1284 ObtainWriteLock(&afs_icl_lock, 202);
1285 if (--setp->refCount == 0 && (setp->states & ICL_SETF_DELETED)) {
1286 afs_icl_ZapSet(setp); /* destroys setp's lock! */
1288 ReleaseWriteLock(&afs_icl_lock);
1292 /* free a set entry, dropping its reference count */
1294 afs_icl_SetFree(struct afs_icl_set *setp)
1296 ObtainWriteLock(&setp->lock, 203);
1297 setp->states |= ICL_SETF_DELETED;
1298 ReleaseWriteLock(&setp->lock);
1299 afs_icl_SetRele(setp);
1303 /* find a set by name, returning it held */
1304 struct afs_icl_set *
1305 afs_icl_FindSet(char *name)
1307 struct afs_icl_set *tp;
1308 ObtainWriteLock(&afs_icl_lock, 204);
1309 for (tp = afs_icl_allSets; tp; tp = tp->nextp) {
1310 if (strcmp(tp->name, name) == 0) {
1311 /* this is the dude we want */
1316 ReleaseWriteLock(&afs_icl_lock);
1320 /* zero out all the logs in the set */
1322 afs_icl_ZeroSet(struct afs_icl_set *setp)
1327 struct afs_icl_log *logp;
1329 ObtainReadLock(&setp->lock);
1330 for (i = 0; i < ICL_LOGSPERSET; i++) {
1331 logp = setp->logs[i];
1333 afs_icl_LogHold(logp);
1334 tcode = afs_icl_ZeroLog(logp);
1336 code = tcode; /* save the last bad one */
1337 afs_icl_LogRele(logp);
1340 ReleaseReadLock(&setp->lock);
1345 afs_icl_EnumerateSets(int (*aproc)
1346 (char *name, char *arock, struct afs_icl_log * tp),
1349 struct afs_icl_set *tp, *np;
1353 ObtainWriteLock(&afs_icl_lock, 205);
1354 for (tp = afs_icl_allSets; tp; tp = np) {
1355 tp->refCount++; /* hold this guy */
1356 ReleaseWriteLock(&afs_icl_lock);
1357 code = (*aproc) (tp->name, arock, (struct afs_icl_log *)tp);
1358 ObtainWriteLock(&afs_icl_lock, 206);
1359 np = tp->nextp; /* tp may disappear next, but not np */
1360 if (--tp->refCount == 0 && (tp->states & ICL_SETF_DELETED))
1365 ReleaseWriteLock(&afs_icl_lock);
1370 afs_icl_AddLogToSet(struct afs_icl_set *setp, struct afs_icl_log *newlogp)
1375 ObtainWriteLock(&setp->lock, 207);
1376 for (i = 0; i < ICL_LOGSPERSET; i++) {
1377 if (!setp->logs[i]) {
1378 setp->logs[i] = newlogp;
1380 afs_icl_LogHold(newlogp);
1381 if (!(setp->states & ICL_SETF_FREED)) {
1382 /* bump up the number of sets using the log */
1383 afs_icl_LogUse(newlogp);
1388 ReleaseWriteLock(&setp->lock);
1393 afs_icl_SetSetStat(struct afs_icl_set *setp, int op)
1397 struct afs_icl_log *logp;
1399 ObtainWriteLock(&setp->lock, 208);
1401 case ICL_OP_SS_ACTIVATE: /* activate a log */
1403 * If we are not already active, see if we have released
1404 * our demand that the log be allocated (FREED set). If
1405 * we have, reassert our desire.
1407 if (!(setp->states & ICL_SETF_ACTIVE)) {
1408 if (setp->states & ICL_SETF_FREED) {
1409 /* have to reassert desire for logs */
1410 for (i = 0; i < ICL_LOGSPERSET; i++) {
1411 logp = setp->logs[i];
1413 afs_icl_LogHold(logp);
1414 afs_icl_LogUse(logp);
1415 afs_icl_LogRele(logp);
1418 setp->states &= ~ICL_SETF_FREED;
1420 setp->states |= ICL_SETF_ACTIVE;
1425 case ICL_OP_SS_DEACTIVATE: /* deactivate a log */
1426 /* this doesn't require anything beyond clearing the ACTIVE flag */
1427 setp->states &= ~ICL_SETF_ACTIVE;
1431 case ICL_OP_SS_FREE: /* deassert design for log */
1433 * if we are already in this state, do nothing; otherwise
1434 * deassert desire for log
1436 if (setp->states & ICL_SETF_ACTIVE)
1439 if (!(setp->states & ICL_SETF_FREED)) {
1440 for (i = 0; i < ICL_LOGSPERSET; i++) {
1441 logp = setp->logs[i];
1443 afs_icl_LogHold(logp);
1444 afs_icl_LogFreeUse(logp);
1445 afs_icl_LogRele(logp);
1448 setp->states |= ICL_SETF_FREED;
1457 ReleaseWriteLock(&setp->lock);