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;
59 setp = afs_iclSetp; /* "cm" */
61 afs_icl_SetFree(setp);
63 if (afs_iclLongTermSetp) {
64 setp = afs_iclLongTermSetp; /* "cmlongterm" */
65 afs_iclLongTermSetp = NULL;
66 afs_icl_SetFree(setp);
68 logp = afs_icl_FindLog("cmfx");
70 /* Release the reference from Find, and the initial one */
71 afs_icl_LogFree(logp);
72 afs_icl_LogFree(logp);
77 afs_icl_InitLogs(void)
79 struct afs_icl_log *logp;
82 /* initialize the ICL system */
83 code = afs_icl_CreateLog("cmfx", 60 * 1024, &logp);
87 code = afs_icl_CreateSetWithFlags("cm", logp, NULL,
88 ICL_CRSET_FLAG_DEFAULT_OFF,
93 code = afs_icl_CreateSet("cmlongterm", logp, NULL,
94 &afs_iclLongTermSetp);
99 struct afs_icl_log *afs_icl_FindLog(char *);
100 struct afs_icl_set *afs_icl_FindSet(char *);
103 #ifdef AFS_DARWIN100_ENV
104 #define AFSKPTR(X) k ## X
106 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
108 return Afscall64_icl(opcode,
109 CAST_USER_ADDR_T((p1)),
110 CAST_USER_ADDR_T((p2)),
111 CAST_USER_ADDR_T((p3)),
112 CAST_USER_ADDR_T((p4)),
116 #define AFSKPTR(X) ((caddr_t)X)
120 #ifdef AFS_DARWIN100_ENV
121 Afscall64_icl(int opcode, user_addr_t kp1, user_addr_t kp2, user_addr_t kp3, user_addr_t kp4, int *retval)
123 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
126 afs_int32 *lp, elts, flags;
128 struct afs_icl_log *logp;
129 struct afs_icl_set *setp;
130 #if defined(AFS_SGI61_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
132 #else /* AFS_SGI61_ENV */
133 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
138 #endif /* AFS_SGI61_ENV */
140 afs_int32 startCookie;
142 struct afs_icl_log *tlp;
143 #ifdef AFS_DARWIN100_ENV
144 afs_uint32 p1 = (afs_uint32)kp1;
145 afs_uint32 p2 = (afs_uint32)kp2;
146 afs_uint32 p3 = (afs_uint32)kp3;
147 afs_uint32 p4 = (afs_uint32)kp4;
151 if (!afs_suser(CRED())) { /* only root can run this code */
155 if (!afs_suser(NULL)) { /* only root can run this code */
156 #if defined(KERNEL_HAVE_UERROR)
165 case ICL_OP_COPYOUTCLR: /* copy out data then clear */
166 case ICL_OP_COPYOUT: /* copy ouy data */
167 /* copyout: p1=logname, p2=&buffer, p3=size(words), p4=&cookie
168 * return flags<<24 + nwords.
169 * updates cookie to updated start (not end) if we had to
172 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
175 AFS_COPYIN(AFSKPTR(p4), (char *)&startCookie, sizeof(afs_int32), code);
178 logp = afs_icl_FindLog(tname);
181 #define BUFFERSIZE AFS_LRALLOCSIZ
182 lp = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
183 elts = BUFFERSIZE / sizeof(afs_int32);
186 flags = (opcode == ICL_OP_COPYOUT) ? 0 : ICL_COPYOUTF_CLRAFTERREAD;
188 afs_icl_CopyOut(logp, lp, &elts, (afs_uint32 *) & startCookie,
191 osi_FreeLargeSpace((struct osi_buffer *)lp);
194 AFS_COPYOUT((char *)lp, AFSKPTR(p2), elts * sizeof(afs_int32), code);
197 AFS_COPYOUT((char *)&startCookie, AFSKPTR(p4), sizeof(afs_int32),
201 #if defined(AFS_AIX51_ENV) && defined(AFS_64BIT_KERNEL)
203 *retval = ((long)((flags << 24) | (elts & 0xffffff))) << 32;
206 *retval = (flags << 24) | (elts & 0xffffff);
208 afs_icl_LogRele(logp);
209 osi_FreeLargeSpace((struct osi_buffer *)lp);
212 case ICL_OP_ENUMLOGS: /* enumerate logs */
213 /* enumerate logs: p1=index, p2=&name, p3=sizeof(name), p4=&size.
214 * return 0 for success, otherwise error.
216 for (tlp = afs_icl_allLogs; tlp; tlp = tlp->nextp) {
221 return ENOENT; /* past the end of file */
222 temp = strlen(tlp->name) + 1;
225 AFS_COPYOUT(tlp->name, AFSKPTR(p2), temp, code);
226 if (!code) /* copy out size of log */
227 AFS_COPYOUT((char *)&tlp->logSize, AFSKPTR(p4), sizeof(afs_int32),
231 case ICL_OP_ENUMLOGSBYSET: /* enumerate logs by set name */
232 /* enumerate logs: p1=setname, p2=index, p3=&name, p4=sizeof(name).
233 * return 0 for success, otherwise error.
235 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
238 setp = afs_icl_FindSet(tname);
241 if (p2 >= ICL_LOGSPERSET)
243 if (!(tlp = setp->logs[p2]))
245 temp = strlen(tlp->name) + 1;
248 AFS_COPYOUT(tlp->name, AFSKPTR(p3), temp, code);
251 case ICL_OP_CLRLOG: /* clear specified log */
252 /* zero out the specified log: p1=logname */
253 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
256 logp = afs_icl_FindLog(tname);
259 code = afs_icl_ZeroLog(logp);
260 afs_icl_LogRele(logp);
263 case ICL_OP_CLRSET: /* clear specified set */
264 /* zero out the specified set: p1=setname */
265 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
268 setp = afs_icl_FindSet(tname);
271 code = afs_icl_ZeroSet(setp);
272 afs_icl_SetRele(setp);
275 case ICL_OP_CLRALL: /* clear all logs */
276 /* zero out all logs -- no args */
278 ObtainWriteLock(&afs_icl_lock, 178);
279 for (tlp = afs_icl_allLogs; tlp; tlp = tlp->nextp) {
280 tlp->refCount++; /* hold this guy */
281 ReleaseWriteLock(&afs_icl_lock);
282 /* don't clear persistent logs */
283 if ((tlp->states & ICL_LOGF_PERSISTENT) == 0)
284 code = afs_icl_ZeroLog(tlp);
285 ObtainWriteLock(&afs_icl_lock, 179);
286 if (--tlp->refCount == 0)
291 ReleaseWriteLock(&afs_icl_lock);
294 case ICL_OP_ENUMSETS: /* enumerate all sets */
295 /* enumerate sets: p1=index, p2=&name, p3=sizeof(name), p4=&states.
296 * return 0 for success, otherwise error.
298 for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
303 return ENOENT; /* past the end of file */
304 temp = strlen(setp->name) + 1;
307 AFS_COPYOUT(setp->name, AFSKPTR(p2), temp, code);
308 if (!code) /* copy out size of log */
309 AFS_COPYOUT((char *)&setp->states, AFSKPTR(p4), sizeof(afs_int32),
313 case ICL_OP_SETSTAT: /* set status on a set */
314 /* activate the specified set: p1=setname, p2=op */
315 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
318 setp = afs_icl_FindSet(tname);
321 code = afs_icl_SetSetStat(setp, p2);
322 afs_icl_SetRele(setp);
325 case ICL_OP_SETSTATALL: /* set status on all sets */
326 /* activate the specified set: p1=op */
328 ObtainWriteLock(&afs_icl_lock, 180);
329 for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
330 setp->refCount++; /* hold this guy */
331 ReleaseWriteLock(&afs_icl_lock);
332 /* don't set states on persistent sets */
333 if ((setp->states & ICL_SETF_PERSISTENT) == 0)
334 code = afs_icl_SetSetStat(setp, p1);
335 ObtainWriteLock(&afs_icl_lock, 181);
336 if (--setp->refCount == 0)
337 afs_icl_ZapSet(setp);
341 ReleaseWriteLock(&afs_icl_lock);
344 case ICL_OP_SETLOGSIZE: /* set size of log */
345 /* set the size of the specified log: p1=logname, p2=size (in words) */
346 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
349 logp = afs_icl_FindLog(tname);
352 code = afs_icl_LogSetSize(logp, p2);
353 afs_icl_LogRele(logp);
356 case ICL_OP_GETLOGINFO: /* get size of log */
357 /* zero out the specified log: p1=logname, p2=&logSize, p3=&allocated */
358 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
361 logp = afs_icl_FindLog(tname);
364 allocated = !!logp->datap;
365 AFS_COPYOUT((char *)&logp->logSize, AFSKPTR(p2), sizeof(afs_int32),
368 AFS_COPYOUT((char *)&allocated, AFSKPTR(p3), sizeof(afs_int32),
370 afs_icl_LogRele(logp);
373 case ICL_OP_GETSETINFO: /* get state of set */
374 /* zero out the specified set: p1=setname, p2=&state */
375 AFS_COPYINSTR(AFSKPTR(p1), tname, sizeof(tname), &temp, code);
378 setp = afs_icl_FindSet(tname);
381 AFS_COPYOUT((char *)&setp->states, AFSKPTR(p2), sizeof(afs_int32),
383 afs_icl_SetRele(setp);
394 afs_lock_t afs_icl_lock;
396 /* exported routine: a 4 parameter event */
398 afs_icl_Event4(struct afs_icl_set *setp, afs_int32 eventID,
399 afs_int32 lAndT, long p1, long p2, long p3, long p4)
406 /* If things aren't init'ed yet (or the set is inactive), don't panic */
407 if (!ICL_SETACTIVE(setp))
411 afs_icl_SetHold(setp);
412 mask = lAndT >> 24 & 0xff; /* mask of which logs to log to */
413 ix = ICL_EVENTBYTE(eventID);
414 ObtainReadLock(&setp->lock);
415 if (setp->eventFlags[ix] & ICL_EVENTMASK(eventID)) {
416 for (i = 0, tmask = 1; i < ICL_LOGSPERSET; i++, tmask <<= 1) {
418 afs_icl_AppendRecord(setp->logs[i], eventID, lAndT & 0xffffff,
423 break; /* break early */
426 ReleaseReadLock(&setp->lock);
427 afs_icl_SetRele(setp);
431 /* Next 4 routines should be implemented via var-args or something.
432 * Whole purpose is to avoid compiler warnings about parameter # mismatches.
433 * Otherwise, could call afs_icl_Event4 directly.
436 afs_icl_Event3(struct afs_icl_set *setp, afs_int32 eventID,
437 afs_int32 lAndT, long p1, long p2, long p3)
439 return afs_icl_Event4(setp, eventID, lAndT, p1, p2, p3, (long)0);
443 afs_icl_Event2(struct afs_icl_set *setp, afs_int32 eventID,
444 afs_int32 lAndT, long p1, long p2)
446 return afs_icl_Event4(setp, eventID, lAndT, p1, p2, (long)0, (long)0);
450 afs_icl_Event1(struct afs_icl_set *setp, afs_int32 eventID,
451 afs_int32 lAndT, long p1)
453 return afs_icl_Event4(setp, eventID, lAndT, p1, (long)0, (long)0,
458 afs_icl_Event0(struct afs_icl_set *setp, afs_int32 eventID,
461 return afs_icl_Event4(setp, eventID, lAndT, (long)0, (long)0, (long)0,
465 struct afs_icl_log *afs_icl_allLogs = 0;
467 /* function to purge records from the start of the log, until there
468 * is at least minSpace long's worth of space available without
469 * making the head and the tail point to the same word.
471 * Log must be write-locked.
474 afs_icl_GetLogSpace(struct afs_icl_log *logp, afs_int32 minSpace)
478 while (logp->logSize - logp->logElements <= minSpace) {
480 tsize = ((logp->datap[logp->firstUsed]) >> 24) & 0xff;
481 logp->logElements -= tsize;
482 logp->firstUsed += tsize;
483 if (logp->firstUsed >= logp->logSize)
484 logp->firstUsed -= logp->logSize;
485 logp->baseCookie += tsize;
489 /* append string astr to buffer, including terminating null char.
491 * log must be write-locked.
493 #define ICL_CHARSPERLONG 4
495 afs_icl_AppendString(struct afs_icl_log *logp, char *astr)
497 char *op; /* ptr to char to write */
499 int bib; /* bytes in buffer */
502 op = (char *)&(logp->datap[logp->firstFree]);
506 if (++bib >= ICL_CHARSPERLONG) {
509 if (++(logp->firstFree) >= logp->logSize) {
511 op = (char *)&(logp->datap[0]);
519 /* if we've used this word at all, allocate it */
520 if (++(logp->firstFree) >= logp->logSize) {
527 /* add a long to the log, ignoring overflow (checked already) */
528 #define ICL_APPENDINT32(lp, x) \
530 (lp)->datap[(lp)->firstFree] = (x); \
531 if (++((lp)->firstFree) >= (lp)->logSize) { \
532 (lp)->firstFree = 0; \
534 (lp)->logElements++; \
538 #define ICL_APPENDLONG(lp, x) \
540 ICL_APPENDINT32((lp), ((x) >> 32) & 0xffffffffL); \
541 ICL_APPENDINT32((lp), (x) & 0xffffffffL); \
545 #define ICL_APPENDLONG(lp, x) ICL_APPENDINT32((lp), (x))
550 afs_icl_AppendOne(struct afs_icl_log *logp, int type, long parm)
553 /* marshall parameter 3 now */
554 if (type == ICL_TYPE_STRING)
555 afs_icl_AppendString(logp, (char *)parm);
556 else if (type == ICL_TYPE_HYPER) {
557 ICL_APPENDINT32(logp,
558 (afs_int32) ((struct afs_hyper_t *)parm)->high);
559 ICL_APPENDINT32(logp,
560 (afs_int32) ((struct afs_hyper_t *)parm)->low);
561 } else if (type == ICL_TYPE_INT64) {
562 #ifndef WORDS_BIGENDIAN
563 #ifdef AFS_64BIT_CLIENT
564 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
565 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
566 #else /* AFS_64BIT_CLIENT */
567 ICL_APPENDINT32(logp, (afs_int32) parm);
568 ICL_APPENDINT32(logp, (afs_int32) 0);
569 #endif /* AFS_64BIT_CLIENT */
570 #else /* AFSLITTLE_ENDIAN */
571 #ifdef AFS_64BIT_CLIENT
572 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
573 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
574 #else /* AFS_64BIT_CLIENT */
575 ICL_APPENDINT32(logp, (afs_int32) 0);
576 ICL_APPENDINT32(logp, (afs_int32) parm);
577 #endif /* AFS_64BIT_CLIENT */
578 #endif /* AFSLITTLE_ENDIAN */
579 } else if (type == ICL_TYPE_FID) {
580 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[0]);
581 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[1]);
582 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[2]);
583 ICL_APPENDINT32(logp, (afs_int32) ((afs_int32 *) parm)[3]);
586 else if (type == ICL_TYPE_INT32)
587 ICL_APPENDINT32(logp, (afs_int32) parm);
590 ICL_APPENDLONG(logp, parm);
594 /* Function to append a record to the log. Written for speed
595 * since we know that we're going to have to make this work fast
596 * pretty soon, anyway. The log must be unlocked.
600 afs_icl_AppendRecord(struct afs_icl_log *logp, afs_int32 op,
601 afs_int32 types, long p1, long p2, long p3, long p4)
603 int rsize; /* record size in longs */
604 int tsize; /* temp size */
608 t4 = types & 0x3f; /* decode types */
616 osi_GetTime(&tv); /* It panics for solaris if inside */
617 ObtainWriteLock(&logp->lock, 182);
619 ReleaseWriteLock(&logp->lock);
623 /* get timestamp as # of microseconds since some time that doesn't
624 * change that often. This algorithm ticks over every 20 minutes
625 * or so (1000 seconds). Write a timestamp record if it has.
627 if (tv.tv_sec - logp->lastTS > 1024) {
628 /* the timer has wrapped -- write a timestamp record */
629 if (logp->logSize - logp->logElements <= 5)
630 afs_icl_GetLogSpace(logp, 5);
632 ICL_APPENDINT32(logp,
633 (afs_int32) (5 << 24) + (ICL_TYPE_UNIXDATE << 18));
634 ICL_APPENDINT32(logp, (afs_int32) ICL_INFO_TIMESTAMP);
635 ICL_APPENDINT32(logp, (afs_int32) 0); /* use thread ID zero for clocks */
636 ICL_APPENDINT32(logp,
637 (afs_int32) (tv.tv_sec & 0x3ff) * 1000000 +
639 ICL_APPENDINT32(logp, (afs_int32) tv.tv_sec);
641 logp->lastTS = tv.tv_sec;
644 rsize = 4; /* base case: see 4 items below */
646 /* compute size of parameter p1. Only tricky case is string.
647 * In that case, we have to call strlen to get the string length.
649 ICL_SIZEHACK(t1, p1, tsize, rsize);
652 /* compute size of parameter p2. Only tricky case is string.
653 * In that case, we have to call strlen to get the string length.
655 ICL_SIZEHACK(t2, p2, tsize, rsize);
658 /* compute size of parameter p3. Only tricky case is string.
659 * In that case, we have to call strlen to get the string length.
661 ICL_SIZEHACK(t3, p3, tsize, rsize);
664 /* compute size of parameter p4. Only tricky case is string.
665 * In that case, we have to call strlen to get the string length.
667 ICL_SIZEHACK(t4, p4, tsize, rsize);
670 /* At this point, we've computed all of the parameter sizes, and
671 * have in rsize the size of the entire record we want to append.
672 * Next, we check that we actually have room in the log to do this
673 * work, and then we do the append.
676 ReleaseWriteLock(&logp->lock);
677 return; /* log record too big to express */
680 if (logp->logSize - logp->logElements <= rsize)
681 afs_icl_GetLogSpace(logp, rsize);
683 ICL_APPENDINT32(logp,
684 (afs_int32) (rsize << 24) + (t1 << 18) + (t2 << 12) +
686 ICL_APPENDINT32(logp, (afs_int32) op);
687 ICL_APPENDINT32(logp, (afs_int32) osi_ThreadUnique());
688 ICL_APPENDINT32(logp,
689 (afs_int32) (tv.tv_sec & 0x3ff) * 1000000 + tv.tv_usec);
691 afs_icl_AppendOne(logp, t1, p1);
692 afs_icl_AppendOne(logp, t2, p2);
693 afs_icl_AppendOne(logp, t3, p3);
694 afs_icl_AppendOne(logp, t4, p4);
695 ReleaseWriteLock(&logp->lock);
698 /* create a log with size logSize; return it in *outLogpp and tag
699 * it with name "name."
702 afs_icl_CreateLog(char *name, afs_int32 logSize,
703 struct afs_icl_log **outLogpp)
705 return afs_icl_CreateLogWithFlags(name, logSize, /*flags */ 0, outLogpp);
708 /* create a log with size logSize; return it in *outLogpp and tag
709 * it with name "name." 'flags' can be set to make the log unclearable.
712 afs_icl_CreateLogWithFlags(char *name, afs_int32 logSize, afs_uint32 flags,
713 struct afs_icl_log **outLogpp)
715 struct afs_icl_log *logp;
717 /* add into global list under lock */
718 ObtainWriteLock(&afs_icl_lock, 183);
722 for (logp = afs_icl_allLogs; logp; logp = logp->nextp) {
723 if (strcmp(logp->name, name) == 0) {
724 /* found it already created, just return it */
727 if (flags & ICL_CRLOG_FLAG_PERSISTENT) {
728 ObtainWriteLock(&logp->lock, 184);
729 logp->states |= ICL_LOGF_PERSISTENT;
730 ReleaseWriteLock(&logp->lock);
732 ReleaseWriteLock(&afs_icl_lock);
737 logp = osi_AllocSmallSpace(sizeof(struct afs_icl_log));
738 memset((caddr_t) logp, 0, sizeof(*logp));
741 logp->name = osi_AllocSmallSpace(strlen(name) + 1);
742 strcpy(logp->name, name);
743 LOCK_INIT(&logp->lock, "logp lock");
744 logp->logSize = logSize;
745 logp->datap = NULL; /* don't allocate it until we need it */
747 if (flags & ICL_CRLOG_FLAG_PERSISTENT)
748 logp->states |= ICL_LOGF_PERSISTENT;
750 logp->nextp = afs_icl_allLogs;
751 afs_icl_allLogs = logp;
752 ReleaseWriteLock(&afs_icl_lock);
758 /* called with a log, a pointer to a buffer, the size of the buffer
759 * (in *bufSizep), the starting cookie (in *cookiep, use 0 at the start)
760 * and returns data in the provided buffer, and returns output flags
761 * in *flagsp. The flag ICL_COPYOUTF_MISSEDSOME is set if we can't
762 * find the record with cookie value cookie.
765 afs_icl_CopyOut(struct afs_icl_log *logp, afs_int32 * bufferp,
766 afs_int32 * bufSizep, afs_uint32 * cookiep,
769 afs_int32 nwords; /* number of words to copy out */
770 afs_uint32 startCookie; /* first cookie to use */
771 afs_int32 outWords; /* words we've copied out */
772 afs_int32 inWords; /* max words to copy out */
773 afs_int32 code; /* return code */
774 afs_int32 ix; /* index we're copying from */
775 afs_int32 outFlags; /* return flags */
776 afs_int32 inFlags; /* flags passed in */
779 inWords = *bufSizep; /* max to copy out */
780 outWords = 0; /* amount copied out */
781 startCookie = *cookiep;
786 ObtainWriteLock(&logp->lock, 185);
788 ReleaseWriteLock(&logp->lock);
792 /* first, compute the index of the start cookie we've been passed */
794 /* (re-)compute where we should start */
795 if (startCookie < logp->baseCookie) {
796 if (startCookie) /* missed some output */
797 outFlags |= ICL_COPYOUTF_MISSEDSOME;
798 /* skip to the first available record */
799 startCookie = logp->baseCookie;
800 *cookiep = startCookie;
803 /* compute where we find the first element to copy out */
804 ix = logp->firstUsed + startCookie - logp->baseCookie;
805 if (ix >= logp->logSize)
808 /* if have some data now, break out and process it */
809 if (startCookie - logp->baseCookie < logp->logElements)
812 /* At end of log, so clear it if we need to */
813 if (inFlags & ICL_COPYOUTF_CLRAFTERREAD) {
814 logp->firstUsed = logp->firstFree = 0;
815 logp->logElements = 0;
817 /* otherwise, either wait for the data to arrive, or return */
818 if (!(inFlags & ICL_COPYOUTF_WAITIO)) {
819 ReleaseWriteLock(&logp->lock);
823 logp->states |= ICL_LOGF_WAITING;
824 ReleaseWriteLock(&logp->lock);
825 afs_osi_Sleep(&logp->lock);
826 ObtainWriteLock(&logp->lock, 186);
828 /* copy out data from ix to logSize or firstFree, depending
829 * upon whether firstUsed <= firstFree (no wrap) or otherwise.
830 * be careful not to copy out more than nwords.
832 if (ix >= logp->firstUsed) {
833 if (logp->firstUsed <= logp->firstFree)
835 end = logp->firstFree; /* first element not to copy */
838 nwords = inWords; /* don't copy more than this */
839 if (end - ix < nwords)
842 memcpy((char *)bufferp, (char *)&logp->datap[ix],
843 sizeof(afs_int32) * nwords);
848 /* if we're going to copy more out below, we'll start here */
851 /* now, if active part of the log has wrapped, there's more stuff
852 * starting at the head of the log. Copy out more from there.
854 if (logp->firstUsed > logp->firstFree && ix < logp->firstFree
856 /* (more to) copy out from the wrapped section at the
857 * start of the log. May get here even if didn't copy any
858 * above, if the cookie points directly into the wrapped section.
861 if (logp->firstFree - ix < nwords)
862 nwords = logp->firstFree - ix;
863 memcpy((char *)bufferp, (char *)&logp->datap[ix],
864 sizeof(afs_int32) * nwords);
870 ReleaseWriteLock(&logp->lock);
874 *bufSizep = outWords;
880 /* hold and release logs */
882 afs_icl_LogHold(struct afs_icl_log *logp)
884 ObtainWriteLock(&afs_icl_lock, 187);
886 ReleaseWriteLock(&afs_icl_lock);
890 /* keep track of how many sets believe the log itself is allocated */
892 afs_icl_LogUse(struct afs_icl_log *logp)
894 ObtainWriteLock(&logp->lock, 188);
895 if (logp->setCount == 0) {
896 /* this is the first set actually using the log -- allocate it */
897 if (logp->logSize == 0) {
898 /* we weren't passed in a hint and it wasn't set */
899 logp->logSize = ICL_DEFAULT_LOGSIZE;
901 logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logp->logSize);
902 osi_Assert(logp->datap != NULL);
903 #ifdef KERNEL_HAVE_PIN
904 pin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
908 ReleaseWriteLock(&logp->lock);
912 /* decrement the number of real users of the log, free if possible */
914 afs_icl_LogFreeUse(struct afs_icl_log *logp)
916 ObtainWriteLock(&logp->lock, 189);
917 if (--logp->setCount == 0) {
918 /* no more users -- free it (but keep log structure around) */
919 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
920 #ifdef KERNEL_HAVE_PIN
921 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
923 logp->firstUsed = logp->firstFree = 0;
924 logp->logElements = 0;
927 ReleaseWriteLock(&logp->lock);
931 /* set the size of the log to 'logSize' */
933 afs_icl_LogSetSize(struct afs_icl_log *logp, afs_int32 logSize)
935 ObtainWriteLock(&logp->lock, 190);
937 /* nothing to worry about since it's not allocated */
938 logp->logSize = logSize;
941 logp->firstUsed = logp->firstFree = 0;
942 logp->logElements = 0;
944 /* free and allocate a new one */
945 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
946 #ifdef KERNEL_HAVE_PIN
947 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
949 logp->datap = afs_osi_Alloc(sizeof(afs_int32) * logSize);
950 osi_Assert(logp->datap != NULL);
951 #ifdef KERNEL_HAVE_PIN
952 pin((char *)logp->datap, sizeof(afs_int32) * logSize);
954 logp->logSize = logSize;
956 ReleaseWriteLock(&logp->lock);
961 /* free a log. Called with afs_icl_lock locked. */
963 afs_icl_ZapLog(struct afs_icl_log *logp)
965 struct afs_icl_log **lpp, *tp;
967 for (lpp = &afs_icl_allLogs, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
969 /* found the dude we want to remove */
971 osi_FreeSmallSpace(logp->name);
972 afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
973 #ifdef KERNEL_HAVE_PIN
974 unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
976 osi_FreeSmallSpace(logp);
977 break; /* won't find it twice */
983 /* do the release, watching for deleted entries */
985 afs_icl_LogRele(struct afs_icl_log *logp)
987 ObtainWriteLock(&afs_icl_lock, 191);
988 if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
989 afs_icl_ZapLog(logp); /* destroys logp's lock! */
991 ReleaseWriteLock(&afs_icl_lock);
995 /* do the release, watching for deleted entries, log already held */
997 afs_icl_LogReleNL(struct afs_icl_log *logp)
999 if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
1000 afs_icl_ZapLog(logp); /* destroys logp's lock! */
1005 /* zero out the log */
1007 afs_icl_ZeroLog(struct afs_icl_log *logp)
1009 ObtainWriteLock(&logp->lock, 192);
1010 logp->firstUsed = logp->firstFree = 0;
1011 logp->logElements = 0;
1012 logp->baseCookie = 0;
1013 ReleaseWriteLock(&logp->lock);
1017 /* free a log entry, and drop its reference count */
1019 afs_icl_LogFree(struct afs_icl_log *logp)
1021 ObtainWriteLock(&logp->lock, 193);
1022 logp->states |= ICL_LOGF_DELETED;
1023 ReleaseWriteLock(&logp->lock);
1024 afs_icl_LogRele(logp);
1028 /* find a log by name, returning it held */
1029 struct afs_icl_log *
1030 afs_icl_FindLog(char *name)
1032 struct afs_icl_log *tp;
1033 ObtainWriteLock(&afs_icl_lock, 194);
1034 for (tp = afs_icl_allLogs; tp; tp = tp->nextp) {
1035 if (strcmp(tp->name, name) == 0) {
1036 /* this is the dude we want */
1041 ReleaseWriteLock(&afs_icl_lock);
1045 struct afs_icl_set *afs_icl_allSets = 0;
1048 afs_icl_CreateSet(char *name, struct afs_icl_log *baseLogp,
1049 struct afs_icl_log *fatalLogp,
1050 struct afs_icl_set **outSetpp)
1052 return afs_icl_CreateSetWithFlags(name, baseLogp, fatalLogp,
1053 /*flags */ 0, outSetpp);
1056 /* create a set, given pointers to base and fatal logs, if any.
1057 * Logs are unlocked, but referenced, and *outSetpp is returned
1058 * referenced. Function bumps reference count on logs, since it
1059 * addds references from the new afs_icl_set. When the set is destroyed,
1060 * those references will be released.
1063 afs_icl_CreateSetWithFlags(char *name, struct afs_icl_log *baseLogp,
1064 struct afs_icl_log *fatalLogp, afs_uint32 flags,
1065 struct afs_icl_set **outSetpp)
1067 struct afs_icl_set *setp;
1069 afs_int32 states = ICL_DEFAULT_SET_STATES;
1071 ObtainWriteLock(&afs_icl_lock, 197);
1072 if (!afs_icl_inited)
1075 for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
1076 if (strcmp(setp->name, name) == 0) {
1079 if (flags & ICL_CRSET_FLAG_PERSISTENT) {
1080 ObtainWriteLock(&setp->lock, 198);
1081 setp->states |= ICL_SETF_PERSISTENT;
1082 ReleaseWriteLock(&setp->lock);
1084 ReleaseWriteLock(&afs_icl_lock);
1089 /* determine initial state */
1090 if (flags & ICL_CRSET_FLAG_DEFAULT_ON)
1091 states = ICL_SETF_ACTIVE;
1092 else if (flags & ICL_CRSET_FLAG_DEFAULT_OFF)
1093 states = ICL_SETF_FREED;
1094 if (flags & ICL_CRSET_FLAG_PERSISTENT)
1095 states |= ICL_SETF_PERSISTENT;
1097 setp = osi_AllocSmallSpace(sizeof(struct afs_icl_set));
1098 memset((caddr_t) setp, 0, sizeof(*setp));
1100 if (states & ICL_SETF_FREED)
1101 states &= ~ICL_SETF_ACTIVE; /* if freed, can't be active */
1102 setp->states = states;
1104 LOCK_INIT(&setp->lock, "setp lock");
1105 /* next lock is obtained in wrong order, hierarchy-wise, but
1106 * it doesn't matter, since no one can find this lock yet, since
1107 * the afs_icl_lock is still held, and thus the obtain can't block.
1109 ObtainWriteLock(&setp->lock, 199);
1110 setp->name = osi_AllocSmallSpace(strlen(name) + 1);
1111 strcpy(setp->name, name);
1112 setp->nevents = ICL_DEFAULTEVENTS;
1113 setp->eventFlags = afs_osi_Alloc(ICL_DEFAULTEVENTS);
1114 osi_Assert(setp->eventFlags != NULL);
1115 #ifdef KERNEL_HAVE_PIN
1116 pin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1118 for (i = 0; i < ICL_DEFAULTEVENTS; i++)
1119 setp->eventFlags[i] = 0xff; /* default to enabled */
1121 /* update this global info under the afs_icl_lock */
1122 setp->nextp = afs_icl_allSets;
1123 afs_icl_allSets = setp;
1124 ReleaseWriteLock(&afs_icl_lock);
1126 /* set's basic lock is still held, so we can finish init */
1128 setp->logs[0] = baseLogp;
1129 afs_icl_LogHold(baseLogp);
1130 if (!(setp->states & ICL_SETF_FREED))
1131 afs_icl_LogUse(baseLogp); /* log is actually being used */
1134 setp->logs[1] = fatalLogp;
1135 afs_icl_LogHold(fatalLogp);
1136 if (!(setp->states & ICL_SETF_FREED))
1137 afs_icl_LogUse(fatalLogp); /* log is actually being used */
1139 ReleaseWriteLock(&setp->lock);
1145 /* hold and release event sets */
1147 afs_icl_SetHold(struct afs_icl_set *setp)
1149 ObtainWriteLock(&afs_icl_lock, 201);
1151 ReleaseWriteLock(&afs_icl_lock);
1155 /* free a set. Called with afs_icl_lock locked */
1157 afs_icl_ZapSet(struct afs_icl_set *setp)
1159 struct afs_icl_set **lpp, *tp;
1161 struct afs_icl_log *tlp;
1163 for (lpp = &afs_icl_allSets, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
1165 /* found the dude we want to remove */
1167 osi_FreeSmallSpace(setp->name);
1168 afs_osi_Free(setp->eventFlags, ICL_DEFAULTEVENTS);
1169 #ifdef KERNEL_HAVE_PIN
1170 unpin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
1172 for (i = 0; i < ICL_LOGSPERSET; i++) {
1173 if ((tlp = setp->logs[i]))
1174 afs_icl_LogReleNL(tlp);
1176 osi_FreeSmallSpace(setp);
1177 break; /* won't find it twice */
1183 /* do the release, watching for deleted entries */
1185 afs_icl_SetRele(struct afs_icl_set *setp)
1187 ObtainWriteLock(&afs_icl_lock, 202);
1188 if (--setp->refCount == 0 && (setp->states & ICL_SETF_DELETED)) {
1189 afs_icl_ZapSet(setp); /* destroys setp's lock! */
1191 ReleaseWriteLock(&afs_icl_lock);
1195 /* free a set entry, dropping its reference count */
1197 afs_icl_SetFree(struct afs_icl_set *setp)
1199 ObtainWriteLock(&setp->lock, 203);
1200 setp->states |= ICL_SETF_DELETED;
1201 ReleaseWriteLock(&setp->lock);
1202 afs_icl_SetRele(setp);
1206 /* find a set by name, returning it held */
1207 struct afs_icl_set *
1208 afs_icl_FindSet(char *name)
1210 struct afs_icl_set *tp;
1211 ObtainWriteLock(&afs_icl_lock, 204);
1212 for (tp = afs_icl_allSets; tp; tp = tp->nextp) {
1213 if (strcmp(tp->name, name) == 0) {
1214 /* this is the dude we want */
1219 ReleaseWriteLock(&afs_icl_lock);
1223 /* zero out all the logs in the set */
1225 afs_icl_ZeroSet(struct afs_icl_set *setp)
1230 struct afs_icl_log *logp;
1232 ObtainReadLock(&setp->lock);
1233 for (i = 0; i < ICL_LOGSPERSET; i++) {
1234 logp = setp->logs[i];
1236 afs_icl_LogHold(logp);
1237 tcode = afs_icl_ZeroLog(logp);
1239 code = tcode; /* save the last bad one */
1240 afs_icl_LogRele(logp);
1243 ReleaseReadLock(&setp->lock);
1248 afs_icl_SetSetStat(struct afs_icl_set *setp, int op)
1252 struct afs_icl_log *logp;
1254 ObtainWriteLock(&setp->lock, 208);
1256 case ICL_OP_SS_ACTIVATE: /* activate a log */
1258 * If we are not already active, see if we have released
1259 * our demand that the log be allocated (FREED set). If
1260 * we have, reassert our desire.
1262 if (!(setp->states & ICL_SETF_ACTIVE)) {
1263 if (setp->states & ICL_SETF_FREED) {
1264 /* have to reassert desire for logs */
1265 for (i = 0; i < ICL_LOGSPERSET; i++) {
1266 logp = setp->logs[i];
1268 afs_icl_LogHold(logp);
1269 afs_icl_LogUse(logp);
1270 afs_icl_LogRele(logp);
1273 setp->states &= ~ICL_SETF_FREED;
1275 setp->states |= ICL_SETF_ACTIVE;
1280 case ICL_OP_SS_DEACTIVATE: /* deactivate a log */
1281 /* this doesn't require anything beyond clearing the ACTIVE flag */
1282 setp->states &= ~ICL_SETF_ACTIVE;
1286 case ICL_OP_SS_FREE: /* deassert design for log */
1288 * if we are already in this state, do nothing; otherwise
1289 * deassert desire for log
1291 if (setp->states & ICL_SETF_ACTIVE)
1294 if (!(setp->states & ICL_SETF_FREED)) {
1295 for (i = 0; i < ICL_LOGSPERSET; i++) {
1296 logp = setp->logs[i];
1298 afs_icl_LogHold(logp);
1299 afs_icl_LogFreeUse(logp);
1300 afs_icl_LogRele(logp);
1303 setp->states |= ICL_SETF_FREED;
1312 ReleaseWriteLock(&setp->lock);