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 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) {
417 afs_icl_AppendRecord(setp->logs[i], eventID, lAndT & 0xffffff,
422 break; /* break early */
425 ReleaseReadLock(&setp->lock);
426 afs_icl_SetRele(setp);
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.
435 afs_icl_Event3(struct afs_icl_set *setp, afs_int32 eventID,
436 afs_int32 lAndT, long p1, long p2, long p3)
438 return afs_icl_Event4(setp, eventID, lAndT, p1, p2, p3, (long)0);
442 afs_icl_Event2(struct afs_icl_set *setp, afs_int32 eventID,
443 afs_int32 lAndT, long p1, long p2)
445 return afs_icl_Event4(setp, eventID, lAndT, p1, p2, (long)0, (long)0);
449 afs_icl_Event1(struct afs_icl_set *setp, afs_int32 eventID,
450 afs_int32 lAndT, long p1)
452 return afs_icl_Event4(setp, eventID, lAndT, p1, (long)0, (long)0,
457 afs_icl_Event0(struct afs_icl_set *setp, afs_int32 eventID,
460 return afs_icl_Event4(setp, eventID, lAndT, (long)0, (long)0, (long)0,
464 struct afs_icl_log *afs_icl_allLogs = 0;
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.
470 * Log must be write-locked.
473 afs_icl_GetLogSpace(struct afs_icl_log *logp, afs_int32 minSpace)
477 while (logp->logSize - logp->logElements <= minSpace) {
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;
488 /* append string astr to buffer, including terminating null char.
490 * log must be write-locked.
492 #define ICL_CHARSPERLONG 4
494 afs_icl_AppendString(struct afs_icl_log *logp, char *astr)
496 char *op; /* ptr to char to write */
498 int bib; /* bytes in buffer */
501 op = (char *)&(logp->datap[logp->firstFree]);
505 if (++bib >= ICL_CHARSPERLONG) {
508 if (++(logp->firstFree) >= logp->logSize) {
510 op = (char *)&(logp->datap[0]);
518 /* if we've used this word at all, allocate it */
519 if (++(logp->firstFree) >= logp->logSize) {
526 /* add a long to the log, ignoring overflow (checked already) */
527 #define ICL_APPENDINT32(lp, x) \
529 (lp)->datap[(lp)->firstFree] = (x); \
530 if (++((lp)->firstFree) >= (lp)->logSize) { \
531 (lp)->firstFree = 0; \
533 (lp)->logElements++; \
537 #define ICL_APPENDLONG(lp, x) \
539 ICL_APPENDINT32((lp), ((x) >> 32) & 0xffffffffL); \
540 ICL_APPENDINT32((lp), (x) & 0xffffffffL); \
544 #define ICL_APPENDLONG(lp, x) ICL_APPENDINT32((lp), (x))
547 /* routine to tell whether we're dealing with the address or the
551 afs_icl_UseAddr(int type)
553 if (type == ICL_TYPE_HYPER || type == ICL_TYPE_STRING
554 || type == ICL_TYPE_FID || type == ICL_TYPE_INT64)
561 afs_icl_AppendOne(struct afs_icl_log *logp, int type, long parm)
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]);
597 else if (type == ICL_TYPE_INT32)
598 ICL_APPENDINT32(logp, (afs_int32) parm);
601 ICL_APPENDLONG(logp, parm);
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.
611 afs_icl_AppendRecord(struct afs_icl_log *logp, afs_int32 op,
612 afs_int32 types, long p1, long p2, long p3, long p4)
614 int rsize; /* record size in longs */
615 int tsize; /* temp size */
619 t4 = types & 0x3f; /* decode types */
627 osi_GetTime(&tv); /* It panics for solaris if inside */
628 ObtainWriteLock(&logp->lock, 182);
630 ReleaseWriteLock(&logp->lock);
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.
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);
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 +
650 ICL_APPENDINT32(logp, (afs_int32) tv.tv_sec);
652 logp->lastTS = tv.tv_sec;
655 rsize = 4; /* base case: see 4 items below */
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.
660 ICL_SIZEHACK(t1, p1, tsize, rsize);
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.
666 ICL_SIZEHACK(t2, p2, tsize, rsize);
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.
672 ICL_SIZEHACK(t3, p3, tsize, rsize);
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.
678 ICL_SIZEHACK(t4, p4, tsize, rsize);
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.
687 ReleaseWriteLock(&logp->lock);
688 return; /* log record too big to express */
691 if (logp->logSize - logp->logElements <= rsize)
692 afs_icl_GetLogSpace(logp, rsize);
694 ICL_APPENDINT32(logp,
695 (afs_int32) (rsize << 24) + (t1 << 18) + (t2 << 12) +
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);
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);
709 /* create a log with size logSize; return it in *outLogpp and tag
710 * it with name "name."
713 afs_icl_CreateLog(char *name, afs_int32 logSize,
714 struct afs_icl_log **outLogpp)
716 return afs_icl_CreateLogWithFlags(name, logSize, /*flags */ 0, outLogpp);
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.
723 afs_icl_CreateLogWithFlags(char *name, afs_int32 logSize, afs_uint32 flags,
724 struct afs_icl_log **outLogpp)
726 struct afs_icl_log *logp;
728 /* add into global list under lock */
729 ObtainWriteLock(&afs_icl_lock, 183);
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 */
738 if (flags & ICL_CRLOG_FLAG_PERSISTENT) {
739 ObtainWriteLock(&logp->lock, 184);
740 logp->states |= ICL_LOGF_PERSISTENT;
741 ReleaseWriteLock(&logp->lock);
743 ReleaseWriteLock(&afs_icl_lock);
748 logp = (struct afs_icl_log *)
749 osi_AllocSmallSpace(sizeof(struct afs_icl_log));
750 memset((caddr_t) logp, 0, sizeof(*logp));
753 logp->name = osi_AllocSmallSpace(strlen(name) + 1);
754 strcpy(logp->name, name);
755 LOCK_INIT(&logp->lock, "logp lock");
756 logp->logSize = logSize;
757 logp->datap = NULL; /* don't allocate it until we need it */
759 if (flags & ICL_CRLOG_FLAG_PERSISTENT)
760 logp->states |= ICL_LOGF_PERSISTENT;
762 logp->nextp = afs_icl_allLogs;
763 afs_icl_allLogs = logp;
764 ReleaseWriteLock(&afs_icl_lock);
770 /* called with a log, a pointer to a buffer, the size of the buffer
771 * (in *bufSizep), the starting cookie (in *cookiep, use 0 at the start)
772 * and returns data in the provided buffer, and returns output flags
773 * in *flagsp. The flag ICL_COPYOUTF_MISSEDSOME is set if we can't
774 * find the record with cookie value cookie.
777 afs_icl_CopyOut(struct afs_icl_log *logp, afs_int32 * bufferp,
778 afs_int32 * bufSizep, afs_uint32 * cookiep,
781 afs_int32 nwords; /* number of words to copy out */
782 afs_uint32 startCookie; /* first cookie to use */
783 afs_int32 outWords; /* words we've copied out */
784 afs_int32 inWords; /* max words to copy out */
785 afs_int32 code; /* return code */
786 afs_int32 ix; /* index we're copying from */
787 afs_int32 outFlags; /* return flags */
788 afs_int32 inFlags; /* flags passed in */
791 inWords = *bufSizep; /* max to copy out */
792 outWords = 0; /* amount copied out */
793 startCookie = *cookiep;
798 ObtainWriteLock(&logp->lock, 185);
800 ReleaseWriteLock(&logp->lock);
804 /* first, compute the index of the start cookie we've been passed */
806 /* (re-)compute where we should start */
807 if (startCookie < logp->baseCookie) {
808 if (startCookie) /* missed some output */
809 outFlags |= ICL_COPYOUTF_MISSEDSOME;
810 /* skip to the first available record */
811 startCookie = logp->baseCookie;
812 *cookiep = startCookie;
815 /* compute where we find the first element to copy out */
816 ix = logp->firstUsed + startCookie - logp->baseCookie;
817 if (ix >= logp->logSize)
820 /* if have some data now, break out and process it */
821 if (startCookie - logp->baseCookie < logp->logElements)
824 /* At end of log, so clear it if we need to */
825 if (inFlags & ICL_COPYOUTF_CLRAFTERREAD) {
826 logp->firstUsed = logp->firstFree = 0;
827 logp->logElements = 0;
829 /* otherwise, either wait for the data to arrive, or return */
830 if (!(inFlags & ICL_COPYOUTF_WAITIO)) {
831 ReleaseWriteLock(&logp->lock);
835 logp->states |= ICL_LOGF_WAITING;
836 ReleaseWriteLock(&logp->lock);
837 afs_osi_Sleep(&logp->lock);
838 ObtainWriteLock(&logp->lock, 186);
840 /* copy out data from ix to logSize or firstFree, depending
841 * upon whether firstUsed <= firstFree (no wrap) or otherwise.
842 * be careful not to copy out more than nwords.
844 if (ix >= logp->firstUsed) {
845 if (logp->firstUsed <= logp->firstFree)
847 end = logp->firstFree; /* first element not to copy */
850 nwords = inWords; /* don't copy more than this */
851 if (end - ix < nwords)
854 memcpy((char *)bufferp, (char *)&logp->datap[ix],
855 sizeof(afs_int32) * nwords);
860 /* if we're going to copy more out below, we'll start here */
863 /* now, if active part of the log has wrapped, there's more stuff
864 * starting at the head of the log. Copy out more from there.
866 if (logp->firstUsed > logp->firstFree && ix < logp->firstFree
868 /* (more to) copy out from the wrapped section at the
869 * start of the log. May get here even if didn't copy any
870 * above, if the cookie points directly into the wrapped section.
873 if (logp->firstFree - ix < nwords)
874 nwords = logp->firstFree - ix;
875 memcpy((char *)bufferp, (char *)&logp->datap[ix],
876 sizeof(afs_int32) * nwords);
882 ReleaseWriteLock(&logp->lock);
886 *bufSizep = outWords;
892 /* return basic parameter information about a log */
894 afs_icl_GetLogParms(struct afs_icl_log *logp, afs_int32 * maxSizep,
895 afs_int32 * curSizep)
897 ObtainReadLock(&logp->lock);
898 *maxSizep = logp->logSize;
899 *curSizep = logp->logElements;
900 ReleaseReadLock(&logp->lock);
905 /* hold and release logs */
907 afs_icl_LogHold(struct afs_icl_log *logp)
909 ObtainWriteLock(&afs_icl_lock, 187);
911 ReleaseWriteLock(&afs_icl_lock);
915 /* hold and release logs, called with lock already held */
917 afs_icl_LogHoldNL(struct afs_icl_log *logp)
923 /* keep track of how many sets believe the log itself is allocated */
925 afs_icl_LogUse(struct afs_icl_log *logp)
927 ObtainWriteLock(&logp->lock, 188);
928 if (logp->setCount == 0) {
929 /* this is the first set actually using the log -- allocate it */
930 if (logp->logSize == 0) {
931 /* we weren't passed in a hint and it wasn't set */
932 logp->logSize = ICL_DEFAULT_LOGSIZE;
934 logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logp->logSize);
935 osi_Assert(logp->datap != NULL);
936 #ifdef KERNEL_HAVE_PIN
937 pin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
941 ReleaseWriteLock(&logp->lock);
945 /* decrement the number of real users of the log, free if possible */
947 afs_icl_LogFreeUse(struct afs_icl_log *logp)
949 ObtainWriteLock(&logp->lock, 189);
950 if (--logp->setCount == 0) {
951 /* no more users -- free it (but keep log structure around) */
952 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
953 #ifdef KERNEL_HAVE_PIN
954 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
956 logp->firstUsed = logp->firstFree = 0;
957 logp->logElements = 0;
960 ReleaseWriteLock(&logp->lock);
964 /* set the size of the log to 'logSize' */
966 afs_icl_LogSetSize(struct afs_icl_log *logp, afs_int32 logSize)
968 ObtainWriteLock(&logp->lock, 190);
970 /* nothing to worry about since it's not allocated */
971 logp->logSize = logSize;
974 logp->firstUsed = logp->firstFree = 0;
975 logp->logElements = 0;
977 /* free and allocate a new one */
978 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
979 #ifdef KERNEL_HAVE_PIN
980 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
982 logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logSize);
983 osi_Assert(logp->datap != NULL);
984 #ifdef KERNEL_HAVE_PIN
985 pin((char *)logp->datap, sizeof(afs_int32) * logSize);
987 logp->logSize = logSize;
989 ReleaseWriteLock(&logp->lock);
994 /* free a log. Called with afs_icl_lock locked. */
996 afs_icl_ZapLog(struct afs_icl_log *logp)
998 struct afs_icl_log **lpp, *tp;
1000 for (lpp = &afs_icl_allLogs, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
1002 /* found the dude we want to remove */
1004 osi_FreeSmallSpace(logp->name);
1005 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
1006 #ifdef KERNEL_HAVE_PIN
1007 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
1009 osi_FreeSmallSpace(logp);
1010 break; /* won't find it twice */
1016 /* do the release, watching for deleted entries */
1018 afs_icl_LogRele(struct afs_icl_log *logp)
1020 ObtainWriteLock(&afs_icl_lock, 191);
1021 if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
1022 afs_icl_ZapLog(logp); /* destroys logp's lock! */
1024 ReleaseWriteLock(&afs_icl_lock);
1028 /* do the release, watching for deleted entries, log already held */
1030 afs_icl_LogReleNL(struct afs_icl_log *logp)
1032 if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
1033 afs_icl_ZapLog(logp); /* destroys logp's lock! */
1038 /* zero out the log */
1040 afs_icl_ZeroLog(struct afs_icl_log *logp)
1042 ObtainWriteLock(&logp->lock, 192);
1043 logp->firstUsed = logp->firstFree = 0;
1044 logp->logElements = 0;
1045 logp->baseCookie = 0;
1046 ReleaseWriteLock(&logp->lock);
1050 /* free a log entry, and drop its reference count */
1052 afs_icl_LogFree(struct afs_icl_log *logp)
1054 ObtainWriteLock(&logp->lock, 193);
1055 logp->states |= ICL_LOGF_DELETED;
1056 ReleaseWriteLock(&logp->lock);
1057 afs_icl_LogRele(logp);
1061 /* find a log by name, returning it held */
1062 struct afs_icl_log *
1063 afs_icl_FindLog(char *name)
1065 struct afs_icl_log *tp;
1066 ObtainWriteLock(&afs_icl_lock, 194);
1067 for (tp = afs_icl_allLogs; tp; tp = tp->nextp) {
1068 if (strcmp(tp->name, name) == 0) {
1069 /* this is the dude we want */
1074 ReleaseWriteLock(&afs_icl_lock);
1079 afs_icl_EnumerateLogs(int (*aproc)
1080 (char *name, char *arock, struct afs_icl_log * tp),
1083 struct afs_icl_log *tp;
1087 ObtainWriteLock(&afs_icl_lock, 195);
1088 for (tp = afs_icl_allLogs; tp; tp = tp->nextp) {
1089 tp->refCount++; /* hold this guy */
1090 ReleaseWriteLock(&afs_icl_lock);
1091 ObtainReadLock(&tp->lock);
1092 code = (*aproc) (tp->name, arock, tp);
1093 ReleaseReadLock(&tp->lock);
1094 ObtainWriteLock(&afs_icl_lock, 196);
1095 if (--tp->refCount == 0)
1100 ReleaseWriteLock(&afs_icl_lock);
1104 struct afs_icl_set *afs_icl_allSets = 0;
1107 afs_icl_CreateSet(char *name, struct afs_icl_log *baseLogp,
1108 struct afs_icl_log *fatalLogp,
1109 struct afs_icl_set **outSetpp)
1111 return afs_icl_CreateSetWithFlags(name, baseLogp, fatalLogp,
1112 /*flags */ 0, outSetpp);
1115 /* create a set, given pointers to base and fatal logs, if any.
1116 * Logs are unlocked, but referenced, and *outSetpp is returned
1117 * referenced. Function bumps reference count on logs, since it
1118 * addds references from the new afs_icl_set. When the set is destroyed,
1119 * those references will be released.
1122 afs_icl_CreateSetWithFlags(char *name, struct afs_icl_log *baseLogp,
1123 struct afs_icl_log *fatalLogp, afs_uint32 flags,
1124 struct afs_icl_set **outSetpp)
1126 struct afs_icl_set *setp;
1128 afs_int32 states = ICL_DEFAULT_SET_STATES;
1130 ObtainWriteLock(&afs_icl_lock, 197);
1131 if (!afs_icl_inited)
1134 for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
1135 if (strcmp(setp->name, name) == 0) {
1138 if (flags & ICL_CRSET_FLAG_PERSISTENT) {
1139 ObtainWriteLock(&setp->lock, 198);
1140 setp->states |= ICL_SETF_PERSISTENT;
1141 ReleaseWriteLock(&setp->lock);
1143 ReleaseWriteLock(&afs_icl_lock);
1148 /* determine initial state */
1149 if (flags & ICL_CRSET_FLAG_DEFAULT_ON)
1150 states = ICL_SETF_ACTIVE;
1151 else if (flags & ICL_CRSET_FLAG_DEFAULT_OFF)
1152 states = ICL_SETF_FREED;
1153 if (flags & ICL_CRSET_FLAG_PERSISTENT)
1154 states |= ICL_SETF_PERSISTENT;
1156 setp = (struct afs_icl_set *)osi_AllocSmallSpace(sizeof(struct afs_icl_set));
1157 memset((caddr_t) setp, 0, sizeof(*setp));
1159 if (states & ICL_SETF_FREED)
1160 states &= ~ICL_SETF_ACTIVE; /* if freed, can't be active */
1161 setp->states = states;
1163 LOCK_INIT(&setp->lock, "setp lock");
1164 /* next lock is obtained in wrong order, hierarchy-wise, but
1165 * it doesn't matter, since no one can find this lock yet, since
1166 * the afs_icl_lock is still held, and thus the obtain can't block.
1168 ObtainWriteLock(&setp->lock, 199);
1169 setp->name = osi_AllocSmallSpace(strlen(name) + 1);
1170 strcpy(setp->name, name);
1171 setp->nevents = ICL_DEFAULTEVENTS;
1172 setp->eventFlags = afs_osi_Alloc(ICL_DEFAULTEVENTS);
1173 osi_Assert(setp->eventFlags != NULL);
1174 #ifdef KERNEL_HAVE_PIN
1175 pin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1177 for (i = 0; i < ICL_DEFAULTEVENTS; i++)
1178 setp->eventFlags[i] = 0xff; /* default to enabled */
1180 /* update this global info under the afs_icl_lock */
1181 setp->nextp = afs_icl_allSets;
1182 afs_icl_allSets = setp;
1183 ReleaseWriteLock(&afs_icl_lock);
1185 /* set's basic lock is still held, so we can finish init */
1187 setp->logs[0] = baseLogp;
1188 afs_icl_LogHold(baseLogp);
1189 if (!(setp->states & ICL_SETF_FREED))
1190 afs_icl_LogUse(baseLogp); /* log is actually being used */
1193 setp->logs[1] = fatalLogp;
1194 afs_icl_LogHold(fatalLogp);
1195 if (!(setp->states & ICL_SETF_FREED))
1196 afs_icl_LogUse(fatalLogp); /* log is actually being used */
1198 ReleaseWriteLock(&setp->lock);
1204 /* function to change event enabling information for a particular set */
1206 afs_icl_SetEnable(struct afs_icl_set *setp, afs_int32 eventID, int setValue)
1210 ObtainWriteLock(&setp->lock, 200);
1211 if (!ICL_EVENTOK(setp, eventID)) {
1212 ReleaseWriteLock(&setp->lock);
1215 tp = &setp->eventFlags[ICL_EVENTBYTE(eventID)];
1217 *tp |= ICL_EVENTMASK(eventID);
1219 *tp &= ~(ICL_EVENTMASK(eventID));
1220 ReleaseWriteLock(&setp->lock);
1224 /* return indication of whether a particular event ID is enabled
1225 * for tracing. If *getValuep is set to 0, the event is disabled,
1226 * otherwise it is enabled. All events start out enabled by default.
1229 afs_icl_GetEnable(struct afs_icl_set *setp, afs_int32 eventID, int *getValuep)
1231 ObtainReadLock(&setp->lock);
1232 if (!ICL_EVENTOK(setp, eventID)) {
1233 ReleaseWriteLock(&setp->lock);
1236 if (setp->eventFlags[ICL_EVENTBYTE(eventID)] & ICL_EVENTMASK(eventID))
1240 ReleaseReadLock(&setp->lock);
1244 /* hold and release event sets */
1246 afs_icl_SetHold(struct afs_icl_set *setp)
1248 ObtainWriteLock(&afs_icl_lock, 201);
1250 ReleaseWriteLock(&afs_icl_lock);
1254 /* free a set. Called with afs_icl_lock locked */
1256 afs_icl_ZapSet(struct afs_icl_set *setp)
1258 struct afs_icl_set **lpp, *tp;
1260 struct afs_icl_log *tlp;
1262 for (lpp = &afs_icl_allSets, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
1264 /* found the dude we want to remove */
1266 osi_FreeSmallSpace(setp->name);
1267 afs_osi_Free(setp->eventFlags, ICL_DEFAULTEVENTS);
1268 #ifdef KERNEL_HAVE_PIN
1269 unpin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1271 for (i = 0; i < ICL_LOGSPERSET; i++) {
1272 if ((tlp = setp->logs[i]))
1273 afs_icl_LogReleNL(tlp);
1275 osi_FreeSmallSpace(setp);
1276 break; /* won't find it twice */
1282 /* do the release, watching for deleted entries */
1284 afs_icl_SetRele(struct afs_icl_set *setp)
1286 ObtainWriteLock(&afs_icl_lock, 202);
1287 if (--setp->refCount == 0 && (setp->states & ICL_SETF_DELETED)) {
1288 afs_icl_ZapSet(setp); /* destroys setp's lock! */
1290 ReleaseWriteLock(&afs_icl_lock);
1294 /* free a set entry, dropping its reference count */
1296 afs_icl_SetFree(struct afs_icl_set *setp)
1298 ObtainWriteLock(&setp->lock, 203);
1299 setp->states |= ICL_SETF_DELETED;
1300 ReleaseWriteLock(&setp->lock);
1301 afs_icl_SetRele(setp);
1305 /* find a set by name, returning it held */
1306 struct afs_icl_set *
1307 afs_icl_FindSet(char *name)
1309 struct afs_icl_set *tp;
1310 ObtainWriteLock(&afs_icl_lock, 204);
1311 for (tp = afs_icl_allSets; tp; tp = tp->nextp) {
1312 if (strcmp(tp->name, name) == 0) {
1313 /* this is the dude we want */
1318 ReleaseWriteLock(&afs_icl_lock);
1322 /* zero out all the logs in the set */
1324 afs_icl_ZeroSet(struct afs_icl_set *setp)
1329 struct afs_icl_log *logp;
1331 ObtainReadLock(&setp->lock);
1332 for (i = 0; i < ICL_LOGSPERSET; i++) {
1333 logp = setp->logs[i];
1335 afs_icl_LogHold(logp);
1336 tcode = afs_icl_ZeroLog(logp);
1338 code = tcode; /* save the last bad one */
1339 afs_icl_LogRele(logp);
1342 ReleaseReadLock(&setp->lock);
1347 afs_icl_EnumerateSets(int (*aproc)
1348 (char *name, char *arock, struct afs_icl_log * tp),
1351 struct afs_icl_set *tp, *np;
1355 ObtainWriteLock(&afs_icl_lock, 205);
1356 for (tp = afs_icl_allSets; tp; tp = np) {
1357 tp->refCount++; /* hold this guy */
1358 ReleaseWriteLock(&afs_icl_lock);
1359 code = (*aproc) (tp->name, arock, (struct afs_icl_log *)tp);
1360 ObtainWriteLock(&afs_icl_lock, 206);
1361 np = tp->nextp; /* tp may disappear next, but not np */
1362 if (--tp->refCount == 0 && (tp->states & ICL_SETF_DELETED))
1367 ReleaseWriteLock(&afs_icl_lock);
1372 afs_icl_AddLogToSet(struct afs_icl_set *setp, struct afs_icl_log *newlogp)
1377 ObtainWriteLock(&setp->lock, 207);
1378 for (i = 0; i < ICL_LOGSPERSET; i++) {
1379 if (!setp->logs[i]) {
1380 setp->logs[i] = newlogp;
1382 afs_icl_LogHold(newlogp);
1383 if (!(setp->states & ICL_SETF_FREED)) {
1384 /* bump up the number of sets using the log */
1385 afs_icl_LogUse(newlogp);
1390 ReleaseWriteLock(&setp->lock);
1395 afs_icl_SetSetStat(struct afs_icl_set *setp, int op)
1399 struct afs_icl_log *logp;
1401 ObtainWriteLock(&setp->lock, 208);
1403 case ICL_OP_SS_ACTIVATE: /* activate a log */
1405 * If we are not already active, see if we have released
1406 * our demand that the log be allocated (FREED set). If
1407 * we have, reassert our desire.
1409 if (!(setp->states & ICL_SETF_ACTIVE)) {
1410 if (setp->states & ICL_SETF_FREED) {
1411 /* have to reassert desire for logs */
1412 for (i = 0; i < ICL_LOGSPERSET; i++) {
1413 logp = setp->logs[i];
1415 afs_icl_LogHold(logp);
1416 afs_icl_LogUse(logp);
1417 afs_icl_LogRele(logp);
1420 setp->states &= ~ICL_SETF_FREED;
1422 setp->states |= ICL_SETF_ACTIVE;
1427 case ICL_OP_SS_DEACTIVATE: /* deactivate a log */
1428 /* this doesn't require anything beyond clearing the ACTIVE flag */
1429 setp->states &= ~ICL_SETF_ACTIVE;
1433 case ICL_OP_SS_FREE: /* deassert design for log */
1435 * if we are already in this state, do nothing; otherwise
1436 * deassert desire for log
1438 if (setp->states & ICL_SETF_ACTIVE)
1441 if (!(setp->states & ICL_SETF_FREED)) {
1442 for (i = 0; i < ICL_LOGSPERSET; i++) {
1443 logp = setp->logs[i];
1445 afs_icl_LogHold(logp);
1446 afs_icl_LogFreeUse(logp);
1447 afs_icl_LogRele(logp);
1450 setp->states |= ICL_SETF_FREED;
1459 ReleaseWriteLock(&setp->lock);