fix-rx-mtu-params-20030131
[openafs.git] / src / afs / afs_call.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 RCSID("$Header$");
14
15 #include "afs/sysincludes.h"    /* Standard vendor system headers */
16 #include "afsincludes.h"        /* Afs-based standard headers */
17 #include "afs/afs_stats.h"
18 #include "rx/rx_globals.h"
19 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
20 #include "net/if.h"
21 #ifdef AFS_SGI62_ENV
22 #include "h/hashing.h"
23 #endif
24 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_DARWIN60_ENV)
25 #include "netinet/in_var.h"
26 #endif
27 #endif /* !defined(UKERNEL) */
28 #ifdef AFS_LINUX22_ENV
29 #include "h/smp_lock.h"
30 #endif
31
32
33 #if     defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
34 #define AFS_MINBUFFERS  100
35 #else
36 #define AFS_MINBUFFERS  50
37 #endif
38
39 struct afsop_cell {
40     afs_int32 hosts[MAXCELLHOSTS];
41     char cellName[100];
42 };
43
44 char afs_zeros[AFS_ZEROS];
45 char afs_rootVolumeName[64]="";
46 struct afs_icl_set *afs_iclSetp = (struct afs_icl_set*)0;
47 struct afs_icl_set *afs_iclLongTermSetp = (struct afs_icl_set*)0;
48
49 #if defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV) 
50 kmutex_t afs_global_lock;
51 kmutex_t afs_rxglobal_lock;
52 #endif
53
54 #if defined(AFS_SGI_ENV) && !defined(AFS_SGI64_ENV)
55 long afs_global_owner;
56 #endif
57
58 #if defined(AFS_OSF_ENV)
59 simple_lock_data_t afs_global_lock;
60 #endif
61
62 #if defined(AFS_DARWIN_ENV)
63 struct lock__bsd__ afs_global_lock;
64 #endif
65
66 #if defined(AFS_XBSD_ENV)
67 struct lock afs_global_lock;
68 struct proc *afs_global_owner;
69 #endif
70
71 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV)
72 thread_t afs_global_owner;
73 #endif /* AFS_OSF_ENV */
74
75 #if defined(AFS_AIX41_ENV)
76 simple_lock_data afs_global_lock;
77 #endif
78
79 afs_int32 afs_initState = 0;
80 afs_int32 afs_termState = 0;
81 afs_int32 afs_setTime = 0;
82 int afs_cold_shutdown = 0;
83 char afs_SynchronousCloses = '\0';
84 static int afs_CB_Running = 0;
85 static int AFS_Running = 0;
86 static int afs_CacheInit_Done = 0;
87 static int afs_Go_Done = 0;
88 extern struct interfaceAddr afs_cb_interface;
89 static int afs_RX_Running = 0;
90 static int afs_InitSetup_done = 0;
91
92 afs_int32 afs_rx_deadtime = AFS_RXDEADTIME;
93 afs_int32 afs_rx_harddead = AFS_HARDDEADTIME;
94
95 static int
96 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval);
97
98 #if defined(AFS_HPUX_ENV)
99 extern int afs_vfs_mount();
100 #endif /* defined(AFS_HPUX_ENV) */
101
102 /* This is code which needs to be called once when the first daemon enters
103  * the client. A non-zero return means an error and AFS should not start.
104  */
105 static int afs_InitSetup(int preallocs)
106 {
107     extern void afs_InitStats();
108     int code;
109
110     if (afs_InitSetup_done)
111         return EAGAIN;
112
113 #ifndef AFS_NOSTATS
114     /*
115      * Set up all the AFS statistics variables.  This should be done
116      * exactly once, and it should be done here, the first resource-setting
117      * routine to be called by the CM/RX.
118      */
119     afs_InitStats();
120 #endif /* AFS_NOSTATS */
121     
122     memset(afs_zeros, 0, AFS_ZEROS);
123
124     /* start RX */
125     rx_extraPackets = AFS_NRXPACKETS;   /* smaller # of packets */
126     code = rx_Init(htons(7001));
127     if (code) {
128         printf("AFS: RX failed to initialize.\n");
129         return code;
130     }
131     rx_SetRxDeadTime(afs_rx_deadtime);
132     /* resource init creates the services */
133     afs_ResourceInit(preallocs);
134
135     afs_InitSetup_done = 1;
136     afs_osi_Wakeup(&afs_InitSetup_done);
137
138     return code;
139 }
140
141 #if defined(AFS_LINUX24_ENV) && defined(COMPLETION_H_EXISTS)
142 struct afsd_thread_info {
143     unsigned long parm;
144     struct completion *complete;
145 };
146
147 static int afsd_thread(void *rock) {
148     struct afsd_thread_info  *arg=rock;
149     unsigned long parm=arg->parm;
150 #ifdef SYS_SETPRIORITY_EXPORTED
151     int (*sys_setpriority)(int,int,int) = sys_call_table[__NR_setpriority]; 
152 #endif
153     daemonize(); /* doesn't do much, since we were forked from keventd, but
154                     does call mm_release, which wakes up our parent (since it
155                     used CLONE_VFORK) */
156     afs_osi_MaskSignals();
157     switch (parm) {
158     case AFSOP_START_RXCALLBACK:
159         sprintf(current->comm, "afs_cbstart");
160         AFS_GLOCK();
161         complete(arg->complete);
162         afs_CB_Running = 1;     
163         while (afs_RX_Running != 2)
164             afs_osi_Sleep(&afs_RX_Running);
165         sprintf(current->comm, "afs_callback");
166         afs_RXCallBackServer();
167         AFS_GUNLOCK();
168         complete_and_exit(0,0);
169         break;
170     case AFSOP_START_AFS:
171         sprintf(current->comm, "afs_afsstart");
172         AFS_GLOCK();
173         complete(arg->complete);
174         AFS_Running = 1;
175         while (afs_initState < AFSOP_START_AFS) 
176             afs_osi_Sleep(&afs_initState);
177         afs_initState = AFSOP_START_BKG;
178         afs_osi_Wakeup(&afs_initState);
179         sprintf(current->comm, "afsd");
180         afs_Daemon();
181         AFS_GUNLOCK();
182         complete_and_exit(0,0);
183         break;
184     case AFSOP_START_BKG:
185         sprintf(current->comm, "afs_bkgstart");
186         AFS_GLOCK();
187         complete(arg->complete);
188         while (afs_initState < AFSOP_START_BKG) 
189             afs_osi_Sleep(&afs_initState);
190         if (afs_initState < AFSOP_GO) {
191             afs_initState = AFSOP_GO;
192             afs_osi_Wakeup(&afs_initState);
193         }
194         sprintf(current->comm, "afs_background");          
195         afs_BackgroundDaemon();
196         AFS_GUNLOCK();
197         complete_and_exit(0,0);
198         break;
199     case AFSOP_START_TRUNCDAEMON:
200         sprintf(current->comm, "afs_trimstart");
201         AFS_GLOCK();
202         complete(arg->complete);
203         while (afs_initState < AFSOP_GO) 
204             afs_osi_Sleep(&afs_initState);
205         sprintf(current->comm, "afs_cachetrim");
206         afs_CacheTruncateDaemon();
207         AFS_GUNLOCK();
208         complete_and_exit(0,0);
209         break;
210     case AFSOP_START_CS:
211         sprintf(current->comm, "afs_checkserver");
212         AFS_GLOCK();
213         complete(arg->complete);
214         afs_CheckServerDaemon();
215         AFS_GUNLOCK();
216         complete_and_exit(0,0);
217         break;
218     case AFSOP_RXEVENT_DAEMON:
219         sprintf(current->comm, "afs_evtstart");
220 #ifdef SYS_SETPRIORITY_EXPORTED
221         sys_setpriority(PRIO_PROCESS,0,-10);
222 #else
223 #ifdef CURRENT_INCLUDES_NICE
224         current->nice=-10;
225 #endif
226 #endif
227         AFS_GLOCK();
228         complete(arg->complete);
229         while (afs_initState < AFSOP_START_BKG) 
230             afs_osi_Sleep(&afs_initState);
231         sprintf(current->comm, "afs_rxevent");
232         afs_rxevent_daemon();
233         AFS_GUNLOCK();
234         complete_and_exit(0,0);
235         break;
236     case AFSOP_RXLISTENER_DAEMON:
237         sprintf(current->comm, "afs_lsnstart");
238 #ifdef SYS_SETPRIORITY_EXPORTED
239         sys_setpriority(PRIO_PROCESS,0,-10);
240 #else
241 #ifdef CURRENT_INCLUDES_NICE
242         current->nice=-10;
243 #endif
244 #endif
245         AFS_GLOCK();
246         complete(arg->complete);
247         afs_initState = AFSOP_START_AFS;
248         afs_osi_Wakeup(&afs_initState);
249         afs_RX_Running = 2;
250         afs_osi_Wakeup(&afs_RX_Running);
251         afs_osi_RxkRegister();
252         sprintf(current->comm, "afs_rxlistener");
253         rxk_Listener();
254         AFS_GUNLOCK();
255         complete_and_exit(0,0);
256         break;
257     default:
258         printf("Unknown op %d in StartDaemon()\n");
259         break;
260     }
261     return 0;
262 }
263
264 void afsd_launcher(void *rock) {
265     if (!kernel_thread(afsd_thread,rock, CLONE_VFORK|SIGCHLD))
266         printf("kernel_thread failed. afs startup will not complete\n");
267 }
268
269 void afs_DaemonOp(long parm, long parm2, long parm3, long parm4, long parm5, 
270                   long parm6) 
271 {
272     int code;
273     DECLARE_COMPLETION(c);
274     struct tq_struct tq;
275     struct afsd_thread_info info;     
276     if (parm == AFSOP_START_RXCALLBACK) {
277         if (afs_CB_Running) return;
278     } else if (parm == AFSOP_RXLISTENER_DAEMON) {
279         if (afs_RX_Running) return;
280         afs_RX_Running=1;
281         code = afs_InitSetup(parm2);
282         if (parm3) {
283             rx_enablePeerRPCStats();
284         }
285         if (parm4) {
286             rx_enableProcessRPCStats();
287         }
288         if (code)
289             return;
290     } else if (parm == AFSOP_START_AFS) {
291         if (AFS_Running) return;
292     } /* other functions don't need setup in the parent */     
293     info.complete=&c;
294     info.parm=parm;
295     tq.sync=0;
296     INIT_LIST_HEAD(&tq.list);
297     tq.routine=afsd_launcher;
298     tq.data=&info;
299     schedule_task(&tq);
300     AFS_GUNLOCK();
301     /* we need to wait cause we passed stack pointers around.... */
302     wait_for_completion(&c);
303     AFS_GLOCK();
304 }
305 #endif
306
307 /* leaving as is, probably will barf if we add prototypes here since it's likely being called
308 with partial list */
309 int
310 afs_syscall_call(parm, parm2, parm3, parm4, parm5, parm6)
311 long parm, parm2, parm3, parm4, parm5, parm6;
312 {
313     afs_int32 code = 0;
314 #if defined(AFS_SGI61_ENV) || defined(AFS_SUN57_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
315     size_t bufferSize;  
316 #else /* AFS_SGI61_ENV */
317     u_int bufferSize;   
318 #endif /* AFS_SGI61_ENV */
319
320     AFS_STATCNT(afs_syscall_call);
321 #ifdef  AFS_SUN5_ENV
322     if (!afs_suser(CRED()) && (parm != AFSOP_GETMTU) 
323         && (parm != AFSOP_GETMASK)) {
324       /* only root can run this code */
325         return (EACCES);
326 #else
327     if (!afs_suser() && (parm != AFSOP_GETMTU)
328         && (parm != AFSOP_GETMASK)) {
329       /* only root can run this code */
330 #if defined(KERNEL_HAVE_UERROR)
331         setuerror(EACCES);
332         return(EACCES);
333 #else
334 #if defined(AFS_OSF_ENV)
335         return EACCES;
336 #else /* AFS_OSF_ENV */
337         return EPERM;
338 #endif /* AFS_OSF_ENV */
339 #endif
340 #endif
341     }
342     AFS_GLOCK();
343 #if defined(AFS_LINUX24_ENV) && defined(COMPLETION_H_EXISTS) && !defined(UKERNEL)
344     if (parm < AFSOP_ADDCELL || parm == AFSOP_RXEVENT_DAEMON
345          || parm == AFSOP_RXLISTENER_DAEMON) {
346          afs_DaemonOp(parm,parm2,parm3,parm4,parm5,parm6);
347     }
348 #else /* !(AFS_LINUX24_ENV && !UKERNEL) */
349     if (parm == AFSOP_START_RXCALLBACK) {
350         if (afs_CB_Running) goto out;
351         afs_CB_Running = 1;
352 #ifndef RXK_LISTENER_ENV
353         code = afs_InitSetup(parm2);
354         if (!code) 
355 #endif /* !RXK_LISTENER_ENV */
356             {
357 #ifdef RXK_LISTENER_ENV
358                 while (afs_RX_Running != 2)
359                     afs_osi_Sleep(&afs_RX_Running);
360 #else /* !RXK_LISTENER_ENV */
361                 afs_initState = AFSOP_START_AFS;
362                 afs_osi_Wakeup(&afs_initState);
363 #endif /* RXK_LISTENER_ENV */
364                 afs_osi_Invisible();
365                 afs_RXCallBackServer();
366             }
367 #ifdef AFS_SGI_ENV
368         AFS_GUNLOCK();
369         exit(CLD_EXITED, code);
370 #endif /* AFS_SGI_ENV */
371     }
372 #ifdef RXK_LISTENER_ENV
373     else if (parm == AFSOP_RXLISTENER_DAEMON) {
374         if (afs_RX_Running) goto out;
375         afs_RX_Running = 1;
376         code = afs_InitSetup(parm2);
377         if (parm3) {
378             rx_enablePeerRPCStats();
379         }
380         if (parm4) {
381             rx_enableProcessRPCStats();
382         }
383         if (!code) {
384             afs_initState = AFSOP_START_AFS;
385             afs_osi_Wakeup(&afs_initState);
386             afs_osi_Invisible();
387             afs_RX_Running = 2;
388             afs_osi_Wakeup(&afs_RX_Running);
389 #ifndef UKERNEL
390             afs_osi_RxkRegister();
391 #endif /* !UKERNEL */
392             rxk_Listener();
393         }
394 #ifdef  AFS_SGI_ENV
395         AFS_GUNLOCK();
396         exit(CLD_EXITED, code);
397 #endif /* AFS_SGI_ENV */
398     }
399 #endif /* RXK_LISTENER_ENV */
400     else if (parm == AFSOP_START_AFS) {
401         /* afs daemon */
402         if (AFS_Running) goto out;
403         AFS_Running = 1;
404         while (afs_initState < AFSOP_START_AFS) 
405             afs_osi_Sleep(&afs_initState);
406
407         afs_initState = AFSOP_START_BKG;
408         afs_osi_Wakeup(&afs_initState);
409         afs_osi_Invisible();
410         afs_Daemon();
411 #ifdef AFS_SGI_ENV
412         AFS_GUNLOCK();
413         exit(CLD_EXITED, 0);
414 #endif /* AFS_SGI_ENV */
415     }
416     else if (parm == AFSOP_START_CS) {
417         afs_osi_Invisible();
418         afs_CheckServerDaemon();
419 #ifdef AFS_SGI_ENV
420         AFS_GUNLOCK();
421         exit(CLD_EXITED, 0);
422 #endif /* AFS_SGI_ENV */
423     }
424     else if (parm == AFSOP_START_BKG) {
425         while (afs_initState < AFSOP_START_BKG) 
426             afs_osi_Sleep(&afs_initState);
427         if (afs_initState < AFSOP_GO) {
428             afs_initState = AFSOP_GO;
429             afs_osi_Wakeup(&afs_initState);
430         }
431         /* start the bkg daemon */
432         afs_osi_Invisible();
433 #ifdef AFS_AIX32_ENV
434         if (parm2)
435             afs_BioDaemon(parm2);
436         else
437 #endif /* AFS_AIX32_ENV */
438             afs_BackgroundDaemon();
439 #ifdef AFS_SGI_ENV
440         AFS_GUNLOCK();
441         exit(CLD_EXITED, 0);
442 #endif /* AFS_SGI_ENV */
443     }
444     else if (parm == AFSOP_START_TRUNCDAEMON) {
445         while (afs_initState < AFSOP_GO) 
446             afs_osi_Sleep(&afs_initState);
447         /* start the bkg daemon */
448         afs_osi_Invisible();
449         afs_CacheTruncateDaemon();
450 #ifdef  AFS_SGI_ENV
451         AFS_GUNLOCK();
452         exit(CLD_EXITED, 0);
453 #endif /* AFS_SGI_ENV */
454     }
455 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
456     else if (parm == AFSOP_RXEVENT_DAEMON) {
457         while (afs_initState < AFSOP_START_BKG) afs_osi_Sleep(&afs_initState);
458         afs_osi_Invisible();
459         afs_rxevent_daemon();
460 #ifdef AFS_SGI_ENV
461         AFS_GUNLOCK();
462         exit(CLD_EXITED, 0);
463 #endif /* AFS_SGI_ENV */
464     }
465 #endif /* AFS_SUN5_ENV || RXK_LISTENER_ENV */
466 #endif /* AFS_LINUX24_ENV && !UKERNEL */
467     else if (parm == AFSOP_BASIC_INIT) {
468         afs_int32 temp;
469
470         while (!afs_InitSetup_done)
471             afs_osi_Sleep(&afs_InitSetup_done);
472
473 #if defined(AFS_SUN_ENV) || defined(AFS_SGI_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
474         temp = AFS_MINBUFFERS;  /* Should fix this soon */
475 #else
476         /* number of 2k buffers we could get from all of the buffer space */
477         temp = ((afs_bufferpages * NBPG)>>11);
478         temp = temp>>2; /* don't take more than 25% (our magic parameter) */
479         if (temp < AFS_MINBUFFERS)
480             temp = AFS_MINBUFFERS; /* though we really should have this many */
481 #endif
482         DInit(temp);
483         afs_rootFid.Fid.Volume = 0;
484         code = 0;
485     }
486     else if (parm == AFSOP_ADDCELL) {
487         /* add a cell.  Parameter 2 is 8 hosts (in net order),  parm 3 is the null-terminated
488          name.  Parameter 4 is the length of the name, including the null.  Parm 5 is the
489          home cell flag (0x1 bit) and the nosuid flag (0x2 bit) */
490         struct afsop_cell tcell;
491
492         AFS_COPYIN((char *)parm2, (char *)tcell.hosts, sizeof(tcell.hosts), code);
493         if (!code) {
494             if (parm4 > sizeof(tcell.cellName)) 
495                 code = EFAULT;
496             else {
497                 AFS_COPYIN((char *)parm3, tcell.cellName, parm4, code);
498                 if (!code) 
499                     afs_NewCell(tcell.cellName, tcell.hosts, parm5,
500                                 NULL, 0, 0, 0);
501             }
502         }
503     } else if (parm == AFSOP_ADDCELL2) {
504         struct afsop_cell tcell;
505         char *tbuffer = osi_AllocSmallSpace(AFS_SMALLOCSIZ), *lcnamep = 0;
506         char *tbuffer1 = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
507         int cflags = parm4;
508
509         /* wait for basic init */
510         while (afs_initState < AFSOP_START_BKG) afs_osi_Sleep(&afs_initState);
511
512         AFS_COPYIN((char *)parm2, (char *)tcell.hosts, sizeof(tcell.hosts), code);
513         if (!code) {
514             AFS_COPYINSTR((char *)parm3, tbuffer1, AFS_SMALLOCSIZ, &bufferSize, code);
515             if (!code) {
516                 if (parm4 & 4) {
517                     AFS_COPYINSTR((char *)parm5, tbuffer, AFS_SMALLOCSIZ, &bufferSize, code);
518                     if (!code) {
519                         lcnamep = tbuffer;
520                         cflags |= CLinkedCell;
521                     }
522                 }
523                 if (!code)
524                     code = afs_NewCell(tbuffer1, tcell.hosts, cflags,
525                                        lcnamep, 0, 0, 0);
526             }
527         }
528         osi_FreeSmallSpace(tbuffer);
529         osi_FreeSmallSpace(tbuffer1);
530     }
531     else if (parm == AFSOP_ADDCELLALIAS) {
532         /*
533          * Call arguments:
534          * parm2 is the alias name
535          * parm3 is the real cell name
536          */
537         char *aliasName = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
538         char *cellName = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
539
540         AFS_COPYINSTR((char *)parm2, aliasName, AFS_SMALLOCSIZ, &bufferSize, code);
541         if (!code) AFS_COPYINSTR((char *)parm3, cellName, AFS_SMALLOCSIZ, &bufferSize, code);
542         if (!code) afs_NewCellAlias(aliasName, cellName);
543         osi_FreeSmallSpace(aliasName);
544         osi_FreeSmallSpace(cellName);
545     }
546     else if (parm == AFSOP_SET_THISCELL) {
547         /*
548          * Call arguments:
549          * parm2 is the primary cell name
550          */
551         char *cell = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
552
553         AFS_COPYINSTR((char *) parm2, cell, AFS_SMALLOCSIZ, &bufferSize, code);
554         if (!code)
555             afs_SetPrimaryCell(cell);
556         osi_FreeSmallSpace(cell);
557     }
558     else if (parm == AFSOP_CACHEINIT) {
559         struct afs_cacheParams cparms;
560
561         if (afs_CacheInit_Done) goto out;
562
563         AFS_COPYIN((char *)parm2, (caddr_t) &cparms, sizeof(cparms), code);
564         if (code) {
565 #if defined(KERNEL_HAVE_UERROR)
566             setuerror(code);
567             code = -1;
568 #endif
569             goto out;
570         }
571         afs_CacheInit_Done = 1;
572     {
573         struct afs_icl_log *logp;
574         /* initialize the ICL system */
575         code = afs_icl_CreateLog("cmfx", 60*1024, &logp);
576         if (code == 0)
577             code = afs_icl_CreateSetWithFlags("cm", logp,
578                                               (struct icl_log *) 0,
579                                               ICL_CRSET_FLAG_DEFAULT_OFF,
580                                               &afs_iclSetp);
581             code = afs_icl_CreateSet("cmlongterm", logp, (struct icl_log*) 0,
582                                  &afs_iclLongTermSetp);
583     }
584         afs_setTime = cparms.setTimeFlag;
585
586         code = afs_CacheInit(cparms.cacheScaches,
587                              cparms.cacheFiles,
588                              cparms.cacheBlocks,
589                              cparms.cacheDcaches,
590                              cparms.cacheVolumes,
591                              cparms.chunkSize,
592                              cparms.memCacheFlag,
593                              cparms.inodes,
594                              cparms.users);
595
596     }
597     else if (parm == AFSOP_CACHEINODE) {
598         ino_t ainode = parm2;
599         /* wait for basic init */
600         while (afs_initState < AFSOP_START_BKG) afs_osi_Sleep(&afs_initState);
601
602         /* do it by inode */
603 #ifdef AFS_SGI62_ENV
604         ainode = (ainode << 32) | (parm3 & 0xffffffff);
605 #endif
606         code = afs_InitCacheFile(NULL, ainode);
607     }
608     else if (parm == AFSOP_ROOTVOLUME) {
609         /* wait for basic init */
610         while (afs_initState < AFSOP_START_BKG) afs_osi_Sleep(&afs_initState);
611
612         if (parm2) {
613             AFS_COPYINSTR((char *)parm2, afs_rootVolumeName, sizeof(afs_rootVolumeName), &bufferSize, code);
614             afs_rootVolumeName[sizeof(afs_rootVolumeName)-1] = 0;
615         }
616         else code = 0;
617     }
618     else if (parm == AFSOP_CACHEFILE ||
619              parm == AFSOP_CACHEINFO ||
620              parm == AFSOP_VOLUMEINFO ||
621              parm == AFSOP_AFSLOG ||
622              parm == AFSOP_CELLINFO) {
623         char *tbuffer = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
624
625         code = 0;
626         AFS_COPYINSTR((char *) parm2, tbuffer, AFS_SMALLOCSIZ,
627                       &bufferSize, code);
628         if (code) {
629             osi_FreeSmallSpace(tbuffer);
630             goto out;
631         }
632         if (!code) {
633             tbuffer[AFS_SMALLOCSIZ-1] = '\0';   /* null-terminate the name */
634             /* We have the cache dir copied in.  Call the cache init routine */
635             if (parm == AFSOP_CACHEFILE)
636                 code = afs_InitCacheFile(tbuffer, 0);
637             else if (parm == AFSOP_CACHEINFO)
638                 code = afs_InitCacheInfo(tbuffer);
639             else if (parm == AFSOP_VOLUMEINFO)
640                 code = afs_InitVolumeInfo(tbuffer);
641             else if (parm == AFSOP_CELLINFO)
642                 code = afs_InitCellInfo(tbuffer);
643         }
644         osi_FreeSmallSpace(tbuffer);
645     }
646     else if (parm == AFSOP_GO) {
647         /* the generic initialization calls come here.  One parameter: should we do the
648               set-time operation on this workstation */
649         if (afs_Go_Done) goto out;
650         afs_Go_Done = 1;
651         while (afs_initState < AFSOP_GO) afs_osi_Sleep(&afs_initState);
652         afs_initState = 101;
653         afs_setTime = parm2;
654         afs_osi_Wakeup(&afs_initState);
655 #if     (!defined(AFS_NONFSTRANS) && !defined(AFS_DEC_ENV)) || defined(AFS_AIX_IAUTH_ENV)
656         afs_nfsclient_init();
657 #endif
658         printf("found %d non-empty cache files (%d%%).\n", afs_stats_cmperf.cacheFilesReused,
659                (100*afs_stats_cmperf.cacheFilesReused) /
660                (afs_stats_cmperf.cacheNumEntries?afs_stats_cmperf.cacheNumEntries : 1));
661     }
662     else if (parm == AFSOP_ADVISEADDR) {
663         /* pass in the host address to the rx package */
664         afs_int32       count        = parm2;
665         afs_int32       buffer[AFS_MAX_INTERFACE_ADDR];
666         afs_int32       maskbuffer[AFS_MAX_INTERFACE_ADDR];
667         afs_int32       mtubuffer[AFS_MAX_INTERFACE_ADDR];
668         int     i;
669         int     code;
670
671         if (  count > AFS_MAX_INTERFACE_ADDR ) {
672            code = ENOMEM;
673            count = AFS_MAX_INTERFACE_ADDR;
674         }
675            
676         AFS_COPYIN( (char *)parm3, (char *)buffer, count*sizeof(afs_int32), code);
677         if (parm4)
678           AFS_COPYIN((char *)parm4, (char *)maskbuffer, count*sizeof(afs_int32), code);
679         if (parm5)
680           AFS_COPYIN((char *)parm5, (char *)mtubuffer, count*sizeof(afs_int32), code);
681
682         afs_cb_interface.numberOfInterfaces = count;
683         for (i=0; i < count ; i++) {
684            afs_cb_interface.addr_in[i] = buffer[i];
685 #ifdef AFS_USERSPACE_IP_ADDR    
686            /* AFS_USERSPACE_IP_ADDR means we have no way of finding the
687             * machines IP addresses when in the kernel (the in_ifaddr
688             * struct is not available), so we pass the info in at
689             * startup. We also pass in the subnetmask and mtu size. The
690             * subnetmask is used when setting the rank:
691             * afsi_SetServerIPRank(); and the mtu size is used when
692             * finding the best mtu size. rxi_FindIfnet() is replaced
693             * with rxi_Findcbi().
694             */
695            afs_cb_interface.subnetmask[i] = (parm4 ? maskbuffer[i] : 0xffffffff);
696            afs_cb_interface.mtu[i]        = (parm5 ? mtubuffer[i]  : htonl(1500));
697 #endif
698         }
699         afs_uuid_create(&afs_cb_interface.uuid);
700         rxi_setaddr(buffer[0]);
701     }
702
703 #ifdef  AFS_SGI53_ENV
704     else if (parm == AFSOP_NFSSTATICADDR) {
705         extern int (*nfs_rfsdisptab_v2)();
706         nfs_rfsdisptab_v2 = (int (*)())parm2;
707     }
708     else if (parm == AFSOP_NFSSTATICADDR2) {
709         extern int (*nfs_rfsdisptab_v2)();
710 #ifdef _K64U64
711         nfs_rfsdisptab_v2 = (int (*)())((parm2<<32) | (parm3 & 0xffffffff));
712 #else /* _K64U64 */
713         nfs_rfsdisptab_v2 = (int (*)())(parm3 & 0xffffffff);
714 #endif /* _K64U64 */
715     }
716 #if defined(AFS_SGI62_ENV) && !defined(AFS_SGI65_ENV)
717     else if (parm == AFSOP_SBLOCKSTATICADDR2) {
718         extern int (*afs_sblockp)();
719         extern void (*afs_sbunlockp)();
720 #ifdef _K64U64
721         afs_sblockp = (int (*)())((parm2<<32) | (parm3 & 0xffffffff));
722         afs_sbunlockp = (void (*)())((parm4<<32) | (parm5 & 0xffffffff));
723 #else 
724         afs_sblockp = (int (*)())(parm3 & 0xffffffff);
725         afs_sbunlockp = (void (*)())(parm5 & 0xffffffff);
726 #endif /* _K64U64 */
727     }
728 #endif /* AFS_SGI62_ENV && !AFS_SGI65_ENV */
729 #endif /* AFS_SGI53_ENV */
730     else if (parm == AFSOP_SHUTDOWN) {
731         afs_cold_shutdown = 0;
732         if (parm == 1) afs_cold_shutdown = 1;
733         if (afs_globalVFS != 0) {
734             afs_warn("AFS isn't unmounted yet! Call aborted\n");
735             code = EACCES;
736         } else
737             afs_shutdown();
738     }
739     else if (parm == AFSOP_AFS_VFSMOUNT) {
740 #ifdef  AFS_HPUX_ENV
741         vfsmount(parm2, parm3, parm4, parm5);
742 #else /* defined(AFS_HPUX_ENV) */
743 #if defined(KERNEL_HAVE_UERROR)
744       setuerror(EINVAL);
745 #else
746       code = EINVAL;
747 #endif
748 #endif /* defined(AFS_HPUX_ENV) */
749     }
750     else if (parm == AFSOP_CLOSEWAIT) {
751         afs_SynchronousCloses = 'S';
752     }
753     else if (parm == AFSOP_GETMTU) { 
754       afs_uint32 mtu = 0;
755 #if     !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
756 #ifdef AFS_USERSPACE_IP_ADDR
757       afs_int32 i;
758       i = rxi_Findcbi(parm2);
759       mtu = ((i == -1) ? htonl(1500) : afs_cb_interface.mtu[i]);
760 #else /* AFS_USERSPACE_IP_ADDR */
761       struct ifnet *tifnp;
762
763       tifnp = rxi_FindIfnet(parm2, NULL);  /*  make iterative */
764       mtu = (tifnp ? tifnp->if_mtu : htonl(1500));
765 #endif /* else AFS_USERSPACE_IP_ADDR */
766 #endif /* !AFS_SUN5_ENV */
767       if (!code) 
768          AFS_COPYOUT ((caddr_t)&mtu, (caddr_t)parm3, sizeof(afs_int32), code);
769 #ifdef AFS_AIX32_ENV
770 /* this is disabled for now because I can't figure out how to get access
771  * to these kernel variables.  It's only for supporting user-mode rx
772  * programs -- it makes a huge difference on the 220's in my testbed,
773  * though I don't know why. The bosserver does this with /etc/no, so it's
774  * being handled a different way for the servers right now.  */
775 /*      {
776         static adjusted = 0;
777         extern u_long sb_max_dflt;
778         if (!adjusted) {
779           adjusted = 1;
780           if (sb_max_dflt < 131072) sb_max_dflt = 131072; 
781           if (sb_max < 131072) sb_max = 131072; 
782         }
783       } */
784 #endif /* AFS_AIX32_ENV */
785     }
786     else if (parm == AFSOP_GETMASK) {  /* parm2 == addr in net order */
787       afs_uint32 mask = 0;
788 #if     !defined(AFS_SUN5_ENV)
789 #ifdef AFS_USERSPACE_IP_ADDR
790       afs_int32 i;
791       i = rxi_Findcbi(parm2);
792       if (i != -1) {
793          mask = afs_cb_interface.subnetmask[i];
794       } else {
795          code = -1;
796       }
797 #else /* AFS_USERSPACE_IP_ADDR */
798       struct ifnet *tifnp;
799
800       tifnp = rxi_FindIfnet(parm2, &mask);  /* make iterative */
801       if (!tifnp)
802          code = -1;
803 #endif /* else AFS_USERSPACE_IP_ADDR */
804 #endif /* !AFS_SUN5_ENV */
805       if (!code) 
806          AFS_COPYOUT ((caddr_t)&mask, (caddr_t)parm3, sizeof(afs_int32), code);
807     }
808 #ifdef AFS_AFSDB_ENV
809     else if (parm == AFSOP_AFSDB_HANDLER) {
810         int sizeArg = (int)parm4;
811         int kmsgLen = sizeArg & 0xffff;
812         int cellLen = (sizeArg & 0xffff0000) >> 16;
813         afs_int32 *kmsg = afs_osi_Alloc(kmsgLen);
814         char *cellname = afs_osi_Alloc(cellLen);
815
816 #ifndef UKERNEL
817         afs_osi_MaskSignals();
818 #endif
819         AFS_COPYIN((afs_int32 *)parm2, cellname, cellLen, code);
820         AFS_COPYIN((afs_int32 *)parm3, kmsg, kmsgLen, code);
821         if (!code) {
822             code = afs_AFSDBHandler(cellname, cellLen, kmsg);
823             if (*cellname == 1) *cellname = 0;
824             if (code == -2) {   /* Shutting down? */
825                 *cellname = 1;
826                 code = 0;
827             }
828         }
829         if (!code) AFS_COPYOUT(cellname, (char *)parm2, cellLen, code);
830         afs_osi_Free(kmsg, kmsgLen);
831         afs_osi_Free(cellname, cellLen);
832     }
833 #endif
834     else if (parm == AFSOP_SET_DYNROOT) {
835         code = afs_SetDynrootEnable(parm2);
836     }
837     else if (parm == AFSOP_SET_FAKESTAT) {
838         afs_fakestat_enable = parm2;
839         code = 0;
840     }
841     else
842       code = EINVAL;
843
844 out:
845   AFS_GUNLOCK();
846 #ifdef AFS_LINUX20_ENV
847   return -code;
848 #else
849   return code;
850 #endif
851 }
852
853 #ifdef AFS_AIX32_ENV
854
855 #include "sys/lockl.h"
856
857 /*
858  * syscall -    this is the VRMIX system call entry point.
859  *
860  * NOTE:
861  *      THIS SHOULD BE CHANGED TO afs_syscall(), but requires
862  *      all the user-level calls to `syscall' to change.
863  */
864 syscall(syscall, p1, p2, p3, p4, p5, p6) {
865         register rval1=0, code;
866         register monster;
867         int retval=0;
868 #ifndef AFS_AIX41_ENV
869         extern lock_t kernel_lock;
870         monster = lockl(&kernel_lock, LOCK_SHORT);
871 #endif /* !AFS_AIX41_ENV */
872
873         AFS_STATCNT(syscall);
874         setuerror(0);
875         switch (syscall) {
876             case AFSCALL_CALL:
877                 rval1 = afs_syscall_call(p1, p2, p3, p4, p5, p6);
878                 break;
879
880             case AFSCALL_SETPAG:
881                 AFS_GLOCK();
882                 rval1 = afs_setpag();
883                 AFS_GUNLOCK();
884                 break;
885
886             case AFSCALL_PIOCTL:
887                 AFS_GLOCK();
888                 rval1 = afs_syscall_pioctl(p1, p2, p3, p4);
889                 AFS_GUNLOCK();
890                 break;
891
892             case AFSCALL_ICREATE:
893                 rval1 = afs_syscall_icreate(p1, p2, p3, p4, p5, p6);
894                 break;
895
896             case AFSCALL_IOPEN:
897                 rval1 = afs_syscall_iopen(p1, p2, p3);
898                 break;
899
900             case AFSCALL_IDEC:
901                 rval1 = afs_syscall_iincdec(p1, p2, p3, -1);
902                 break;
903
904             case AFSCALL_IINC:
905                 rval1 = afs_syscall_iincdec(p1, p2, p3, 1);
906                 break;
907
908             case AFSCALL_ICL:
909                 AFS_GLOCK();
910                 code = Afscall_icl(p1, p2, p3, p4, p5, &retval);
911                 AFS_GUNLOCK();
912                 if (!code) rval1 = retval;
913                 if (!rval1) rval1 = code;
914                 break;
915
916             default:
917                 rval1 = EINVAL;
918                 setuerror(EINVAL);
919                 break;
920         }
921
922     out:
923 #ifndef AFS_AIX41_ENV
924         if (monster != LOCK_NEST)
925                 unlockl(&kernel_lock);
926 #endif /* !AFS_AIX41_ENV */
927         return getuerror() ? -1 : rval1;
928 }
929
930 /*
931  * lsetpag -    interface to afs_setpag().
932  */
933 lsetpag() {
934
935     AFS_STATCNT(lsetpag);
936     return syscall(AFSCALL_SETPAG, 0, 0, 0, 0, 0);
937 }
938
939 /*
940  * lpioctl -    interface to pioctl()
941  */
942 lpioctl(path, cmd, cmarg, follow)
943 char *path, *cmarg; {
944
945     AFS_STATCNT(lpioctl);
946     return syscall(AFSCALL_PIOCTL, path, cmd, cmarg, follow);
947 }
948
949 #else   /* !AFS_AIX32_ENV       */
950
951 #if defined(AFS_SGI_ENV)
952 struct afsargs
953 {
954         sysarg_t syscall;
955         sysarg_t parm1;
956         sysarg_t parm2;
957         sysarg_t parm3;
958         sysarg_t parm4;
959         sysarg_t parm5;
960 };
961
962
963 int
964 Afs_syscall (struct afsargs *uap, rval_t *rvp)
965 {
966     int error;
967     long retval;
968
969     AFS_STATCNT(afs_syscall);
970     switch(uap->syscall) {
971     case AFSCALL_ICL:
972         retval = 0;
973         AFS_GLOCK();
974         error=Afscall_icl(uap->parm1,uap->parm2,uap->parm3,uap->parm4,uap->parm5, &retval);
975         AFS_GUNLOCK();
976         rvp->r_val1 = retval;
977         break;
978 #ifdef AFS_SGI_XFS_IOPS_ENV
979     case AFSCALL_IDEC64:
980         error = afs_syscall_idec64(uap->parm1, uap->parm2, uap->parm3,
981                                    uap->parm4, uap->parm5);
982         break;
983     case AFSCALL_IINC64:
984         error = afs_syscall_iinc64(uap->parm1, uap->parm2, uap->parm3,
985                                    uap->parm4, uap->parm5);
986         break;
987     case AFSCALL_ILISTINODE64:
988         error = afs_syscall_ilistinode64(uap->parm1, uap->parm2, uap->parm3,
989                                    uap->parm4, uap->parm5);
990         break;
991     case AFSCALL_ICREATENAME64:
992         error = afs_syscall_icreatename64(uap->parm1, uap->parm2, uap->parm3,
993                                    uap->parm4, uap->parm5);
994         break;
995 #endif
996 #ifdef AFS_SGI_VNODE_GLUE
997     case AFSCALL_INIT_KERNEL_CONFIG:
998         error = afs_init_kernel_config(uap->parm1);
999         break;
1000 #endif
1001     default:
1002         error = afs_syscall_call(uap->syscall, uap->parm1, uap->parm2,
1003                                  uap->parm3, uap->parm4, uap->parm5);
1004     }
1005     return error;
1006 }
1007
1008 #else /* AFS_SGI_ENV */
1009
1010 struct iparam {
1011     long param1;
1012     long param2;
1013     long param3;
1014     long param4;
1015 };
1016
1017 struct iparam32 {
1018     int param1;
1019     int param2;
1020     int param3;
1021     int param4;
1022 };
1023
1024
1025 static void
1026 iparam32_to_iparam(const struct iparam32 *src, struct iparam *dst)
1027 {
1028         dst->param1 = src->param1;
1029         dst->param2 = src->param2;
1030         dst->param3 = src->param3;
1031         dst->param4 = src->param4;
1032 }
1033
1034 /*
1035  * If you need to change copyin_iparam(), you may also need to change
1036  * copyin_afs_ioctl().
1037  */
1038
1039 static int
1040 copyin_iparam(caddr_t cmarg, struct iparam *dst)
1041 {
1042         int code;
1043
1044 #if defined(AFS_HPUX_64BIT_ENV)
1045         struct iparam32 dst32;
1046
1047         if (is_32bit(u.u_procp))        /* is_32bit() in proc_iface.h */
1048         {
1049                 AFS_COPYIN(cmarg, (caddr_t) &dst32, sizeof dst32, code);
1050                 if (!code)
1051                         iparam32_to_iparam(&dst32, dst);
1052                 return code;
1053         }
1054 #endif /* AFS_HPUX_64BIT_ENV */
1055
1056 #if defined(AFS_SUN57_64BIT_ENV)
1057         struct iparam32 dst32;
1058
1059         if (get_udatamodel() == DATAMODEL_ILP32) {
1060                 AFS_COPYIN(cmarg, (caddr_t) &dst32, sizeof dst32, code);
1061                 if (!code)
1062                         iparam32_to_iparam(&dst32, dst);
1063                 return code;
1064         }
1065 #endif /* AFS_SUN57_64BIT_ENV */
1066
1067 #if defined(AFS_LINUX_64BIT_KERNEL) && !defined(AFS_ALPHA_LINUX20_ENV) && !defined(AFS_IA64_LINUX20_ENV)
1068         struct iparam32 dst32;
1069
1070 #ifdef AFS_SPARC64_LINUX24_ENV
1071         if (current->thread.flags & SPARC_FLAG_32BIT) 
1072 #elif AFS_SPARC64_LINUX20_ENV
1073         if (current->tss.flags & SPARC_FLAG_32BIT) 
1074 #else
1075 #error Not done for this linux version
1076 #endif /* AFS_SPARC64_LINUX20_ENV */
1077         {
1078                 AFS_COPYIN(cmarg, (caddr_t) &dst32, sizeof dst32, code);
1079                 if (!code)
1080                         iparam32_to_iparam(&dst32, dst);
1081                 return code;
1082         }
1083 #endif /* AFS_LINUX_64BIT_KERNEL */
1084
1085         AFS_COPYIN(cmarg, (caddr_t) dst, sizeof *dst, code);
1086         return code;
1087 }
1088
1089 /* Main entry of all afs system calls */
1090 #ifdef  AFS_SUN5_ENV
1091 extern int afs_sinited;
1092
1093 /** The 32 bit OS expects the members of this structure to be 32 bit
1094  * quantities and the 64 bit OS expects them as 64 bit quanties. Hence
1095  * to accomodate both, *long* is used instead of afs_int32
1096  */
1097
1098 #ifdef AFS_SUN57_ENV
1099 struct afssysa {
1100     long syscall;
1101     long parm1;
1102     long parm2;
1103     long parm3;
1104     long parm4;
1105     long parm5;
1106     long parm6;
1107 };
1108 #else
1109 struct afssysa {
1110     afs_int32 syscall;
1111     afs_int32 parm1;
1112     afs_int32 parm2;
1113     afs_int32 parm3;
1114     afs_int32 parm4;
1115     afs_int32 parm5;
1116     afs_int32 parm6;
1117 };
1118 #endif
1119
1120 Afs_syscall(register struct afssysa *uap, rval_t *rvp)
1121 {
1122     int *retval = &rvp->r_val1;
1123 #else /* AFS_SUN5_ENV */
1124 #if     defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1125 int
1126 afs3_syscall(p, args, retval)
1127         struct proc *p;
1128         void *args;
1129         int *retval;
1130 {
1131     register struct a {
1132             long syscall;
1133             long parm1;
1134             long parm2;
1135             long parm3;
1136             long parm4;
1137             long parm5;
1138             long parm6;
1139         } *uap = (struct a *)args;
1140 #else   /* AFS_OSF_ENV */
1141 #ifdef AFS_LINUX20_ENV
1142 struct afssysargs {
1143     long syscall;
1144     long parm1;
1145     long parm2;
1146     long parm3;
1147     long parm4;
1148     long parm5;
1149     long parm6; /* not actually used - should be removed */
1150 };
1151 /* Linux system calls only set up for 5 arguments. */
1152 asmlinkage int afs_syscall(long syscall, long parm1, long parm2, long parm3,
1153                            long parm4)
1154 {
1155     struct afssysargs args, *uap = &args;
1156     long linux_ret=0;
1157     long *retval = &linux_ret;
1158     long eparm[4]; /* matches AFSCALL_ICL in fstrace.c */
1159 #ifdef AFS_SPARC64_LINUX24_ENV
1160     afs_int32 eparm32[4];
1161 #endif
1162     /* eparm is also used by AFSCALL_CALL in afsd.c */
1163 #else
1164 #if defined(UKERNEL)
1165 Afs_syscall ()
1166 {
1167     register struct a {
1168             long syscall;
1169             long parm1;
1170             long parm2;
1171             long parm3;
1172             long parm4;
1173             long parm5;
1174             long parm6;
1175         } *uap = (struct a *)u.u_ap;
1176 #else /* UKERNEL */
1177 int
1178 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
1179 afs_syscall ()
1180 #else
1181 Afs_syscall ()
1182 #endif /* SUN && !SUN5 */
1183 {
1184     register struct a {
1185             long syscall;
1186             long parm1;
1187             long parm2;
1188             long parm3;
1189             long parm4;
1190             long parm5;
1191             long parm6;
1192         } *uap = (struct a *)u.u_ap;
1193 #endif /* UKERNEL */
1194 #if  defined(AFS_DEC_ENV)
1195     int *retval = &u.u_r.r_val1;
1196 #elif defined(AFS_HPUX_ENV)
1197     long *retval = &u.u_rval1;
1198 #else
1199     int *retval = &u.u_rval1;
1200 #endif
1201 #endif /* AFS_LINUX20_ENV */
1202 #endif /* AFS_OSF_ENV */
1203 #endif /* AFS_SUN5_ENV */
1204     register int code = 0;
1205
1206     AFS_STATCNT(afs_syscall);
1207 #ifdef        AFS_SUN5_ENV
1208     rvp->r_vals = 0;
1209     if (!afs_sinited) {
1210         return (ENODEV);
1211     }
1212 #endif
1213 #ifdef AFS_LINUX20_ENV
1214     lock_kernel();
1215     /* setup uap for use below - pull out the magic decoder ring to know
1216      * which syscalls have folded argument lists.
1217      */
1218     uap->syscall = syscall;
1219     uap->parm1 = parm1;
1220     uap->parm2 = parm2;
1221     uap->parm3 = parm3;
1222     if (syscall == AFSCALL_ICL || syscall == AFSCALL_CALL) {
1223 #ifdef AFS_SPARC64_LINUX24_ENV
1224 /* from arch/sparc64/kernel/sys_sparc32.c */
1225 #define AA(__x)                                \
1226 ({     unsigned long __ret;            \
1227        __asm__ ("srl   %0, 0, %0"      \
1228                 : "=r" (__ret)         \
1229                 : "0" (__x));          \
1230        __ret;                          \
1231 })
1232
1233
1234         if (current->thread.flags & SPARC_FLAG_32BIT) {
1235         AFS_COPYIN((char*)parm4, (char*)eparm32, sizeof(eparm32), code);
1236         eparm[0]=AA(eparm32[0]);
1237         eparm[1]=AA(eparm32[1]);
1238         eparm[2]=AA(eparm32[2]);
1239 #undef AA
1240 } else
1241 #endif
1242         AFS_COPYIN((char*)parm4, (char*)eparm, sizeof(eparm), code);
1243         uap->parm4 = eparm[0];
1244         uap->parm5 = eparm[1];
1245         uap->parm6 = eparm[2];
1246     }
1247     else {
1248         uap->parm4 = parm4;
1249         uap->parm5 = 0;
1250         uap->parm6 = 0;
1251     }
1252 #endif
1253
1254 #if defined(AFS_HPUX_ENV)
1255     /*
1256      * There used to be code here (duplicated from osi_Init()) for
1257      * initializing the semaphore used by AFS_GLOCK().  Was the
1258      * duplication to handle the case of a dynamically loaded kernel
1259      * module?
1260      */
1261     osi_InitGlock();
1262 #endif
1263     if (uap->syscall == AFSCALL_CALL) {
1264 #ifdef  AFS_SUN5_ENV
1265         code =  afs_syscall_call(uap->parm1, uap->parm2, uap->parm3, 
1266                                 uap->parm4, uap->parm5, uap->parm6, rvp, CRED());
1267 #else
1268         code = afs_syscall_call(uap->parm1, uap->parm2, uap->parm3, uap->parm4, uap->parm5, uap->parm6);
1269 #endif
1270     } else if (uap->syscall == AFSCALL_SETPAG) {
1271 #ifdef  AFS_SUN5_ENV
1272         register proc_t *procp;
1273
1274         procp = ttoproc(curthread);
1275         AFS_GLOCK();
1276         code =  afs_setpag(&procp->p_cred);
1277         AFS_GUNLOCK();
1278 #else
1279         AFS_GLOCK();
1280 #if     defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1281         code = afs_setpag(p, args, retval);
1282 #else   /* AFS_OSF_ENV */
1283         code = afs_setpag();
1284 #endif
1285         AFS_GUNLOCK();
1286 #endif
1287     } else if (uap->syscall == AFSCALL_PIOCTL) {
1288         AFS_GLOCK();
1289 #ifdef  AFS_SUN5_ENV
1290         code = afs_syscall_pioctl(uap->parm1, uap->parm2, uap->parm3, uap->parm4, rvp, CRED());
1291 #else
1292 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1293         code = afs_syscall_pioctl(uap->parm1, uap->parm2, uap->parm3, uap->parm4, p->p_cred->pc_ucred);
1294 #else
1295         code = afs_syscall_pioctl(uap->parm1, uap->parm2, uap->parm3, uap->parm4);
1296 #endif
1297 #endif
1298         AFS_GUNLOCK();
1299     } else if (uap->syscall == AFSCALL_ICREATE) {
1300         struct iparam iparams;
1301
1302         code = copyin_iparam((char *)uap->parm3, &iparams);
1303         if (code) {
1304 #if defined(KERNEL_HAVE_UERROR)
1305             setuerror(code);
1306 #endif
1307         } else
1308 #ifdef  AFS_SUN5_ENV
1309         code =  afs_syscall_icreate(uap->parm1, uap->parm2, iparams.param1, iparams.param2, 
1310                                    iparams.param3, iparams.param4, rvp, CRED());
1311 #else
1312         code =  afs_syscall_icreate(uap->parm1, uap->parm2, iparams.param1, iparams.param2,
1313 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1314                                    iparams.param3, iparams.param4, retval);
1315 #else
1316                                    iparams.param3, iparams.param4);
1317 #endif
1318 #endif  /* AFS_SUN5_ENV */
1319     } else if (uap->syscall == AFSCALL_IOPEN) {
1320 #ifdef  AFS_SUN5_ENV
1321         code = afs_syscall_iopen(uap->parm1, uap->parm2, uap->parm3, rvp, CRED());
1322 #else
1323 #if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1324         code = afs_syscall_iopen(uap->parm1, uap->parm2, uap->parm3, retval);
1325 #else
1326         code = afs_syscall_iopen(uap->parm1, uap->parm2, uap->parm3);
1327 #endif
1328 #endif  /* AFS_SUN5_ENV */
1329     } else if (uap->syscall == AFSCALL_IDEC) {
1330 #ifdef  AFS_SUN5_ENV
1331         code = afs_syscall_iincdec(uap->parm1, uap->parm2, uap->parm3, -1, rvp, CRED());
1332 #else
1333         code = afs_syscall_iincdec(uap->parm1, uap->parm2, uap->parm3, -1);
1334 #endif  /* AFS_SUN5_ENV */
1335     } else if (uap->syscall == AFSCALL_IINC) {
1336 #ifdef  AFS_SUN5_ENV
1337         code = afs_syscall_iincdec(uap->parm1, uap->parm2, uap->parm3, 1, rvp, CRED());
1338 #else
1339         code = afs_syscall_iincdec(uap->parm1, uap->parm2, uap->parm3, 1);
1340 #endif  /* AFS_SUN5_ENV */
1341     } else if (uap->syscall == AFSCALL_ICL) {
1342         AFS_GLOCK();
1343         code = Afscall_icl(uap->parm1, uap->parm2, uap->parm3, uap->parm4, uap->parm5, retval);
1344         AFS_GUNLOCK();
1345 #ifdef AFS_LINUX20_ENV
1346         if (!code) {
1347             /* ICL commands can return values. */
1348             code = -linux_ret; /* Gets negated again at exit below */
1349         }
1350 #else
1351         if (code) {
1352 #if defined(KERNEL_HAVE_UERROR)
1353             setuerror(code);
1354 #endif
1355         }
1356 #endif /* !AFS_LINUX20_ENV */
1357     } else {
1358 #if defined(KERNEL_HAVE_UERROR)
1359         setuerror(EINVAL);
1360 #else
1361         code = EINVAL;
1362 #endif
1363     }
1364
1365 #ifdef AFS_LINUX20_ENV
1366     code = -code;
1367     unlock_kernel();
1368 #endif
1369     return code;
1370 }
1371 #endif /* AFS_SGI_ENV */
1372 #endif  /* !AFS_AIX32_ENV       */
1373
1374 /*
1375  * Initstate in the range 0 < x < 100 are early initialization states.
1376  * Initstate of 100 means a AFSOP_START operation has been done.  After this,
1377  *  the cache may be initialized.
1378  * Initstate of 101 means a AFSOP_GO operation has been done.  This operation
1379  *  is done after all the cache initialization has been done.
1380  * Initstate of 200 means that the volume has been looked up once, possibly
1381  *  incorrectly.
1382  * Initstate of 300 means that the volume has been *successfully* looked up.
1383  */
1384 int afs_CheckInit(void)
1385 {
1386     register int code = 0;
1387
1388     AFS_STATCNT(afs_CheckInit);
1389     if (afs_initState <= 100)
1390         code =  ENXIO;   /* never finished init phase */
1391     else if (afs_initState == 101) {    /* init done, wait for afs_daemon */
1392         while (afs_initState < 200) afs_osi_Sleep(&afs_initState);
1393     } else  if (afs_initState == 200) 
1394         code =  ETIMEDOUT; /* didn't find root volume */
1395     return code;
1396 }
1397
1398 int afs_shuttingdown = 0; 
1399 void afs_shutdown(void)
1400 {
1401     extern short afs_brsDaemons;
1402     extern afs_int32 afs_CheckServerDaemonStarted;
1403     extern struct afs_osi_WaitHandle AFS_WaitHandler, AFS_CSWaitHandler;
1404     extern struct osi_file *afs_cacheInodep;
1405
1406     AFS_STATCNT(afs_shutdown);
1407     if (afs_shuttingdown) return;
1408     afs_shuttingdown = 1;
1409     if (afs_cold_shutdown) afs_warn("COLD ");
1410     else afs_warn("WARM ");
1411     afs_warn("shutting down of: CB... "); 
1412
1413     afs_termState = AFSOP_STOP_RXCALLBACK;
1414     rx_WakeupServerProcs();
1415     /* shutdown_rxkernel(); */
1416     while (afs_termState == AFSOP_STOP_RXCALLBACK)
1417         afs_osi_Sleep(&afs_termState);
1418
1419     afs_warn("afs... ");
1420     while (afs_termState == AFSOP_STOP_AFS) {
1421         afs_osi_CancelWait(&AFS_WaitHandler);
1422         afs_osi_Sleep(&afs_termState);
1423     }
1424     if (afs_CheckServerDaemonStarted) {
1425         while (afs_termState == AFSOP_STOP_CS) {
1426             afs_osi_CancelWait(&AFS_CSWaitHandler);
1427             afs_osi_Sleep(&afs_termState);
1428         }
1429     }
1430     afs_warn("BkG... ");
1431     /* Wake-up afs_brsDaemons so that we don't have to wait for a bkg job! */
1432     while (afs_termState == AFSOP_STOP_BKG) {
1433         afs_osi_Wakeup(&afs_brsDaemons);
1434         afs_osi_Sleep(&afs_termState);
1435     }
1436     afs_warn("CTrunc... ");
1437     /* Cancel cache truncate daemon. */
1438     while (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
1439         afs_osi_Wakeup((char*)&afs_CacheTruncateDaemon);
1440         afs_osi_Sleep(&afs_termState);
1441     }
1442 #ifdef AFS_AFSDB_ENV
1443     afs_warn("AFSDB... ");
1444     afs_StopAFSDB();
1445     while (afs_termState == AFSOP_STOP_AFSDB)
1446         afs_osi_Sleep(&afs_termState);
1447 #endif
1448 #if     defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
1449     afs_warn("RxEvent... ");
1450     /* cancel rx event daemon */
1451     while (afs_termState == AFSOP_STOP_RXEVENT) 
1452         afs_osi_Sleep(&afs_termState);
1453 #if defined(RXK_LISTENER_ENV)
1454 #ifndef UKERNEL
1455     afs_warn("UnmaskRxkSignals... ");
1456     afs_osi_UnmaskRxkSignals();
1457 #endif
1458     /* cancel rx listener */
1459     afs_warn("RxListener... ");
1460     osi_StopListener(); /* This closes rx_socket. */
1461     while (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1462         afs_warn("Sleep... ");
1463         afs_osi_Sleep(&afs_termState);
1464     }
1465 #endif
1466 #else
1467     afs_termState =  AFSOP_STOP_COMPLETE;
1468 #endif
1469     afs_warn("\n");
1470
1471     /* Close file only after daemons which can write to it are stopped. */
1472     if (afs_cacheInodep)        /* memcache won't set this */
1473     {
1474         osi_UFSClose(afs_cacheInodep);    /* Since we always leave it open */
1475         afs_cacheInodep = 0;
1476     }
1477     return;     /* Just kill daemons for now */
1478 #ifdef notdef
1479     shutdown_CB();  
1480     shutdown_AFS();
1481     shutdown_rxkernel();
1482     shutdown_rxevent(); 
1483     shutdown_rx();
1484     afs_shutdown_BKG(); 
1485     shutdown_bufferpackage();
1486     shutdown_daemons();
1487     shutdown_cache();
1488     shutdown_osi();
1489     shutdown_osinet();
1490     shutdown_osifile();
1491     shutdown_vnodeops();
1492     shutdown_vfsops();
1493     shutdown_exporter();
1494     shutdown_memcache();
1495 #if !defined(AFS_NONFSTRANS) || defined(AFS_AIX_IAUTH_ENV)
1496 #if !defined(AFS_DEC_ENV) && !defined(AFS_OSF_ENV)
1497     /* this routine does not exist in Ultrix systems... 93.01.19 */
1498     shutdown_nfsclnt();
1499 #endif /* AFS_DEC_ENV */
1500 #endif
1501     shutdown_afstest();
1502     /* The following hold the cm stats */
1503 /*
1504     memset(&afs_cmstats, 0, sizeof(struct afs_CMStats));
1505     memset(&afs_stats_cmperf, 0, sizeof(struct afs_stats_CMPerf));
1506     memset(&afs_stats_cmfullperf, 0, sizeof(struct afs_stats_CMFullPerf));
1507 */
1508     afs_warn(" ALL allocated tables\n");
1509     afs_shuttingdown = 0;
1510 #endif
1511 }
1512
1513 void shutdown_afstest(void)
1514 {
1515     AFS_STATCNT(shutdown_afstest);
1516     afs_initState = afs_termState = afs_setTime = 0;
1517     AFS_Running = afs_CB_Running = 0;
1518     afs_CacheInit_Done = afs_Go_Done = 0;
1519     if (afs_cold_shutdown) {
1520       *afs_rootVolumeName = 0;
1521     }
1522 }
1523
1524
1525 /* In case there is a bunch of dynamically build bkg daemons to free */
1526 void afs_shutdown_BKG(void)
1527 {
1528     AFS_STATCNT(shutdown_BKG);
1529 }
1530
1531
1532 #if defined(AFS_ALPHA_ENV) || defined(AFS_SGI61_ENV)
1533 /* For SGI 6.2, this can is changed to 1 if it's a 32 bit kernel. */
1534 #if defined(AFS_SGI62_ENV) && defined(KERNEL) && !defined(_K64U64)
1535 int afs_icl_sizeofLong = 1;
1536 #else
1537 int afs_icl_sizeofLong = 2;
1538 #endif /* SGI62 */
1539 #else
1540 int afs_icl_sizeofLong = 1;
1541 #endif
1542
1543 int afs_icl_inited = 0;
1544
1545 /* init function, called once, under afs_icl_lock */
1546 int afs_icl_Init(void)
1547 {
1548     afs_icl_inited = 1;
1549     return 0;
1550 }
1551
1552 extern struct afs_icl_log *afs_icl_FindLog();
1553 extern struct afs_icl_set *afs_icl_FindSet();
1554
1555
1556 static int
1557 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
1558 {
1559     afs_int32 *lp, elts, flags;
1560     register afs_int32 code;
1561     struct afs_icl_log *logp;
1562     struct afs_icl_set *setp;
1563 #if defined(AFS_SGI61_ENV) || defined(AFS_SUN57_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1564     size_t temp;
1565 #else /* AFS_SGI61_ENV */
1566     afs_uint32 temp;
1567 #endif /* AFS_SGI61_ENV */
1568     char tname[65];
1569     afs_int32 startCookie;
1570     afs_int32 allocated;
1571     struct afs_icl_log *tlp;
1572
1573 #ifdef  AFS_SUN5_ENV
1574     if (!afs_suser(CRED())) {   /* only root can run this code */
1575         return (EACCES);
1576     }
1577 #else
1578     if (!afs_suser()) { /* only root can run this code */
1579 #if defined(KERNEL_HAVE_UERROR)
1580         setuerror(EACCES);
1581         return EACCES;
1582 #else
1583         return EPERM;
1584 #endif
1585     }
1586 #endif
1587     switch (opcode) {
1588     case ICL_OP_COPYOUTCLR:     /* copy out data then clear */
1589     case ICL_OP_COPYOUT:        /* copy ouy data */
1590         /* copyout: p1=logname, p2=&buffer, p3=size(words), p4=&cookie
1591          * return flags<<24 + nwords.
1592          * updates cookie to updated start (not end) if we had to
1593          * skip some records.
1594          */
1595         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1596         if (code) return code;
1597         AFS_COPYIN((char *)p4, (char *)&startCookie, sizeof(afs_int32), code);
1598         if (code) return code;
1599         logp = afs_icl_FindLog(tname);
1600         if (!logp) return ENOENT;
1601 #define BUFFERSIZE      AFS_LRALLOCSIZ
1602         lp = (afs_int32 *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1603         elts = BUFFERSIZE / sizeof(afs_int32);
1604         if (p3 < elts) elts = p3;
1605         flags = (opcode == ICL_OP_COPYOUT) ? 0 : ICL_COPYOUTF_CLRAFTERREAD;
1606         code = afs_icl_CopyOut(logp, lp, &elts, (afs_uint32 *) &startCookie,
1607                            &flags);
1608         if (code) {
1609             osi_FreeLargeSpace((struct osi_buffer *) lp);
1610             break;
1611         }
1612         AFS_COPYOUT((char *)lp, (char *)p2, elts * sizeof(afs_int32), code);
1613         if (code) goto done;
1614         AFS_COPYOUT((char *) &startCookie, (char *)p4, sizeof(afs_int32), code);
1615         if (code) goto done;
1616         *retval = (flags<<24) | (elts & 0xffffff);
1617       done:
1618         afs_icl_LogRele(logp);
1619         osi_FreeLargeSpace((struct osi_buffer *) lp);
1620         break;
1621
1622     case ICL_OP_ENUMLOGS:       /* enumerate logs */
1623         /* enumerate logs: p1=index, p2=&name, p3=sizeof(name), p4=&size.
1624          * return 0 for success, otherwise error.
1625          */
1626         for(tlp = afs_icl_allLogs; tlp; tlp=tlp->nextp) {
1627             if (p1-- == 0) break;
1628         }
1629         if (!tlp) return ENOENT;     /* past the end of file */
1630         temp = strlen(tlp->name)+1;
1631         if (temp > p3) return EINVAL;
1632         AFS_COPYOUT(tlp->name, (char *) p2, temp, code);
1633         if (!code)      /* copy out size of log */
1634             AFS_COPYOUT((char *)&tlp->logSize, (char *)p4, sizeof (afs_int32), code);
1635         break;
1636
1637     case ICL_OP_ENUMLOGSBYSET:  /* enumerate logs by set name */
1638         /* enumerate logs: p1=setname, p2=index, p3=&name, p4=sizeof(name).
1639          * return 0 for success, otherwise error.
1640          */
1641         AFS_COPYINSTR((char *)p1, tname, sizeof (tname), &temp, code);
1642         if (code) return code;
1643         setp = afs_icl_FindSet(tname);
1644         if (!setp) return ENOENT;
1645         if (p2 > ICL_LOGSPERSET)
1646             return EINVAL;
1647         if (!(tlp = setp->logs[p2]))
1648             return EBADF;
1649         temp = strlen(tlp->name)+1;
1650         if (temp > p4) return EINVAL;
1651         AFS_COPYOUT(tlp->name, (char *)p3, temp, code);
1652         break;
1653
1654     case ICL_OP_CLRLOG:         /* clear specified log */
1655         /* zero out the specified log: p1=logname */
1656         AFS_COPYINSTR((char *)p1, tname, sizeof (tname), &temp, code);
1657         if (code) return code;
1658         logp = afs_icl_FindLog(tname);
1659         if (!logp) return ENOENT;
1660         code = afs_icl_ZeroLog(logp);
1661         afs_icl_LogRele(logp);
1662         break;
1663
1664     case ICL_OP_CLRSET:         /* clear specified set */
1665         /* zero out the specified set: p1=setname */
1666         AFS_COPYINSTR((char *)p1, tname, sizeof (tname), &temp, code);
1667         if (code) return code;
1668         setp = afs_icl_FindSet(tname);
1669         if (!setp) return ENOENT;
1670         code = afs_icl_ZeroSet(setp);
1671         afs_icl_SetRele(setp);
1672         break;
1673
1674     case ICL_OP_CLRALL:         /* clear all logs */
1675         /* zero out all logs -- no args */
1676         code = 0;
1677         ObtainWriteLock(&afs_icl_lock,178);
1678         for(tlp = afs_icl_allLogs; tlp; tlp=tlp->nextp) {
1679             tlp->refCount++;    /* hold this guy */
1680             ReleaseWriteLock(&afs_icl_lock);
1681             /* don't clear persistent logs */
1682             if ((tlp->states & ICL_LOGF_PERSISTENT) == 0)
1683                 code = afs_icl_ZeroLog(tlp);
1684             ObtainWriteLock(&afs_icl_lock,179);
1685             if (--tlp->refCount == 0)
1686                 afs_icl_ZapLog(tlp);
1687             if (code) break;
1688         }
1689         ReleaseWriteLock(&afs_icl_lock);
1690         break;
1691
1692     case ICL_OP_ENUMSETS:       /* enumerate all sets */
1693         /* enumerate sets: p1=index, p2=&name, p3=sizeof(name), p4=&states.
1694          * return 0 for success, otherwise error.
1695          */
1696         for(setp = afs_icl_allSets; setp; setp = setp->nextp) {
1697             if (p1-- == 0) break;
1698         }
1699         if (!setp) return ENOENT;       /* past the end of file */
1700         temp = strlen(setp->name)+1;
1701         if (temp > p3) return EINVAL;
1702         AFS_COPYOUT(setp->name, (char *)p2, temp, code);
1703         if (!code)      /* copy out size of log */
1704             AFS_COPYOUT((char *)&setp->states,(char *)p4, sizeof (afs_int32), code);
1705         break;
1706
1707     case ICL_OP_SETSTAT:        /* set status on a set */
1708         /* activate the specified set: p1=setname, p2=op */
1709         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1710         if (code) return code;
1711         setp = afs_icl_FindSet(tname);
1712         if (!setp) return ENOENT;
1713         code = afs_icl_SetSetStat(setp, p2);
1714         afs_icl_SetRele(setp);
1715         break;
1716
1717     case ICL_OP_SETSTATALL:     /* set status on all sets */
1718         /* activate the specified set: p1=op */
1719         code = 0;
1720         ObtainWriteLock(&afs_icl_lock,180);
1721         for(setp = afs_icl_allSets; setp; setp=setp->nextp) {
1722             setp->refCount++;   /* hold this guy */
1723             ReleaseWriteLock(&afs_icl_lock);
1724             /* don't set states on persistent sets */
1725             if ((setp->states & ICL_SETF_PERSISTENT) == 0)
1726                 code = afs_icl_SetSetStat(setp, p1);
1727             ObtainWriteLock(&afs_icl_lock,181);
1728             if (--setp->refCount == 0)
1729                 afs_icl_ZapSet(setp);
1730             if (code) break;
1731         }
1732         ReleaseWriteLock(&afs_icl_lock);
1733         break;
1734
1735     case ICL_OP_SETLOGSIZE:             /* set size of log */
1736         /* set the size of the specified log: p1=logname, p2=size (in words) */
1737         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1738         if (code) return code;
1739         logp = afs_icl_FindLog(tname);
1740         if (!logp) return ENOENT;
1741         code = afs_icl_LogSetSize(logp, p2);
1742         afs_icl_LogRele(logp);
1743         break;
1744
1745     case ICL_OP_GETLOGINFO:             /* get size of log */
1746         /* zero out the specified log: p1=logname, p2=&logSize, p3=&allocated */
1747         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1748         if (code) return code;
1749         logp = afs_icl_FindLog(tname);
1750         if (!logp) return ENOENT;
1751         allocated = !!logp->datap;
1752         AFS_COPYOUT((char *)&logp->logSize, (char *) p2, sizeof(afs_int32), code);
1753         if (!code)
1754             AFS_COPYOUT((char *)&allocated, (char *) p3, sizeof(afs_int32), code);
1755         afs_icl_LogRele(logp);
1756         break;
1757
1758     case ICL_OP_GETSETINFO:             /* get state of set */
1759         /* zero out the specified set: p1=setname, p2=&state */
1760         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1761         if (code) return code;
1762         setp = afs_icl_FindSet(tname);
1763         if (!setp) return ENOENT;
1764         AFS_COPYOUT((char *)&setp->states, (char *) p2, sizeof(afs_int32), code);
1765         afs_icl_SetRele(setp);
1766         break;
1767
1768     default:
1769         code = EINVAL;
1770     }
1771
1772     return code;
1773 }
1774
1775
1776 afs_lock_t afs_icl_lock;
1777
1778 /* exported routine: a 4 parameter event */
1779 int afs_icl_Event4(register struct afs_icl_set *setp, afs_int32 eventID, 
1780         afs_int32 lAndT, long p1, long p2, long p3, long p4)
1781 {
1782     afs_int32 mask;
1783     register int i;
1784     register afs_int32 tmask;
1785     int ix;
1786
1787     /* If things aren't init'ed yet (or the set is inactive), don't panic */
1788     if (!ICL_SETACTIVE(setp))
1789         return 0;
1790
1791     AFS_ASSERT_GLOCK();
1792     mask = lAndT>>24 & 0xff;    /* mask of which logs to log to */
1793     ix = ICL_EVENTBYTE(eventID);
1794     ObtainReadLock(&setp->lock);
1795     if (setp->eventFlags[ix] & ICL_EVENTMASK(eventID)) {
1796         for(i=0, tmask = 1; i<ICL_LOGSPERSET; i++, tmask <<= 1) {
1797             if (mask & tmask) {
1798                 afs_icl_AppendRecord(setp->logs[i], eventID, lAndT & 0xffffff,
1799                               p1, p2, p3, p4);
1800             }
1801             mask &= ~tmask;
1802             if (mask == 0) break;       /* break early */
1803         }
1804     }
1805     ReleaseReadLock(&setp->lock);
1806     return 0;
1807 }
1808
1809 /* Next 4 routines should be implemented via var-args or something.
1810  * Whole purpose is to avoid compiler warnings about parameter # mismatches.
1811  * Otherwise, could call afs_icl_Event4 directly.
1812  */
1813 int afs_icl_Event3(register struct afs_icl_set *setp, afs_int32 eventID, 
1814         afs_int32 lAndT, long p1, long p2, long p3)
1815 {
1816     return afs_icl_Event4(setp, eventID, lAndT, p1, p2, p3, (long)0);
1817 }
1818
1819 int afs_icl_Event2(register struct afs_icl_set *setp, afs_int32 eventID, 
1820         afs_int32 lAndT, long p1, long p2)
1821 {
1822     return afs_icl_Event4(setp, eventID, lAndT, p1, p2, (long)0, (long)0);
1823 }
1824
1825 int afs_icl_Event1(register struct afs_icl_set *setp, afs_int32 eventID, 
1826         afs_int32 lAndT, long p1)
1827 {
1828     return afs_icl_Event4(setp, eventID, lAndT, p1, (long)0, (long)0, (long)0);
1829 }
1830
1831 int afs_icl_Event0(register struct afs_icl_set *setp, afs_int32 eventID, 
1832         afs_int32 lAndT)
1833 {
1834     return afs_icl_Event4(setp, eventID, lAndT, (long)0, (long)0, (long)0, (long)0);
1835 }
1836
1837 struct afs_icl_log *afs_icl_allLogs = 0;
1838
1839 /* function to purge records from the start of the log, until there
1840  * is at least minSpace long's worth of space available without
1841  * making the head and the tail point to the same word.
1842  *
1843  * Log must be write-locked.
1844  */
1845 static void afs_icl_GetLogSpace(register struct afs_icl_log *logp, afs_int32 minSpace)
1846 {
1847     register unsigned int tsize;
1848
1849     while (logp->logSize - logp->logElements <= minSpace) {
1850         /* eat a record */
1851         tsize = ((logp->datap[logp->firstUsed]) >> 24) & 0xff;
1852         logp->logElements -= tsize;
1853         logp->firstUsed += tsize;
1854         if (logp->firstUsed >= logp->logSize)
1855             logp->firstUsed -= logp->logSize;
1856         logp->baseCookie += tsize;
1857     }
1858 }
1859
1860 /* append string astr to buffer, including terminating null char.
1861  *
1862  * log must be write-locked.
1863  */
1864 #define ICL_CHARSPERLONG        4
1865 static void afs_icl_AppendString(struct afs_icl_log *logp, char *astr)
1866 {
1867     char *op;           /* ptr to char to write */
1868     int tc;
1869     register int bib;   /* bytes in buffer */
1870
1871     bib = 0;
1872     op = (char *) &(logp->datap[logp->firstFree]);
1873     while (1) {
1874         tc = *astr++;
1875         *op++ = tc;
1876         if (++bib >= ICL_CHARSPERLONG) {
1877             /* new word */
1878             bib = 0;
1879             if (++(logp->firstFree) >= logp->logSize) {
1880                 logp->firstFree = 0;
1881                 op = (char *) &(logp->datap[0]);
1882             }
1883             logp->logElements++;
1884         }
1885         if (tc == 0) break;
1886     }
1887     if (bib > 0) {
1888         /* if we've used this word at all, allocate it */
1889         if (++(logp->firstFree) >= logp->logSize) {
1890             logp->firstFree = 0;
1891         }
1892         logp->logElements++;
1893     }
1894 }
1895
1896 /* add a long to the log, ignoring overflow (checked already) */
1897 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
1898 #define ICL_APPENDINT32(lp, x) \
1899     MACRO_BEGIN \
1900         (lp)->datap[(lp)->firstFree] = (x); \
1901         if (++((lp)->firstFree) >= (lp)->logSize) { \
1902                 (lp)->firstFree = 0; \
1903         } \
1904         (lp)->logElements++; \
1905     MACRO_END
1906
1907 #define ICL_APPENDLONG(lp, x) \
1908     MACRO_BEGIN \
1909         ICL_APPENDINT32((lp), ((x) >> 32) & 0xffffffffL); \
1910         ICL_APPENDINT32((lp), (x) & 0xffffffffL); \
1911     MACRO_END
1912
1913 #else /* AFS_ALPHA_ENV */
1914 #define ICL_APPENDLONG(lp, x) \
1915     MACRO_BEGIN \
1916         (lp)->datap[(lp)->firstFree] = (x); \
1917         if (++((lp)->firstFree) >= (lp)->logSize) { \
1918                 (lp)->firstFree = 0; \
1919         } \
1920         (lp)->logElements++; \
1921     MACRO_END
1922 #define ICL_APPENDINT32(lp, x) ICL_APPENDLONG((lp), (x))
1923 #endif /* AFS_ALPHA_ENV */
1924
1925 /* routine to tell whether we're dealing with the address or the
1926  * object itself
1927  */
1928 int afs_icl_UseAddr(int type)
1929 {
1930     if (type == ICL_TYPE_HYPER || type == ICL_TYPE_STRING
1931         || type == ICL_TYPE_FID || type == ICL_TYPE_INT64)
1932         return 1;
1933     else
1934         return 0;
1935 }
1936
1937 /* Function to append a record to the log.  Written for speed
1938  * since we know that we're going to have to make this work fast
1939  * pretty soon, anyway.  The log must be unlocked.
1940  */
1941
1942 void afs_icl_AppendRecord(register struct afs_icl_log *logp, afs_int32 op, 
1943         afs_int32 types, long p1, long p2, long p3, long p4)
1944 {
1945     int rsize;                  /* record size in longs */
1946     register int tsize;         /* temp size */
1947     osi_timeval_t tv;
1948     int t1, t2, t3, t4;
1949
1950     t4 = types & 0x3f;          /* decode types */
1951     types >>= 6;
1952     t3 = types & 0x3f;
1953     types >>= 6;
1954     t2 = types & 0x3f;
1955     types >>= 6;
1956     t1 = types & 0x3f;
1957
1958     osi_GetTime(&tv);           /* It panics for solaris if inside */
1959     ObtainWriteLock(&logp->lock,182);
1960     if (!logp->datap) {
1961         ReleaseWriteLock(&logp->lock);
1962         return;
1963     }
1964
1965     /* get timestamp as # of microseconds since some time that doesn't
1966      * change that often.  This algorithm ticks over every 20 minutes
1967      * or so (1000 seconds).  Write a timestamp record if it has.
1968      */
1969     if (tv.tv_sec - logp->lastTS > 1024)
1970     {
1971         /* the timer has wrapped -- write a timestamp record */
1972         if (logp->logSize - logp->logElements <= 5)
1973             afs_icl_GetLogSpace(logp, 5);
1974
1975         ICL_APPENDINT32(logp, (afs_int32)(5<<24) + (ICL_TYPE_UNIXDATE<<18));
1976         ICL_APPENDINT32(logp, (afs_int32)ICL_INFO_TIMESTAMP);
1977         ICL_APPENDINT32(logp, (afs_int32)0); /* use thread ID zero for clocks */
1978         ICL_APPENDINT32(logp,
1979                         (afs_int32)(tv.tv_sec & 0x3ff) * 1000000 + tv.tv_usec);
1980         ICL_APPENDINT32(logp, (afs_int32)tv.tv_sec);
1981
1982         logp->lastTS = tv.tv_sec;
1983     }
1984
1985     rsize = 4;                          /* base case */
1986     if (t1) {
1987         /* compute size of parameter p1.  Only tricky case is string.
1988          * In that case, we have to call strlen to get the string length.
1989          */
1990         ICL_SIZEHACK(t1, p1);
1991     }
1992     if (t2) {
1993         /* compute size of parameter p2.  Only tricky case is string.
1994          * In that case, we have to call strlen to get the string length.
1995          */
1996         ICL_SIZEHACK(t2, p2);
1997     }
1998     if (t3) {
1999         /* compute size of parameter p3.  Only tricky case is string.
2000          * In that case, we have to call strlen to get the string length.
2001          */
2002         ICL_SIZEHACK(t3, p3);
2003     }
2004     if (t4) {
2005         /* compute size of parameter p4.  Only tricky case is string.
2006          * In that case, we have to call strlen to get the string length.
2007          */
2008         ICL_SIZEHACK(t4, p4);
2009     }
2010
2011     /* At this point, we've computed all of the parameter sizes, and
2012      * have in rsize the size of the entire record we want to append.
2013      * Next, we check that we actually have room in the log to do this
2014      * work, and then we do the append.
2015      */
2016     if (rsize > 255) {
2017         ReleaseWriteLock(&logp->lock);
2018         return;         /* log record too big to express */
2019     }
2020
2021     if (logp->logSize - logp->logElements <= rsize)
2022         afs_icl_GetLogSpace(logp, rsize);
2023
2024     ICL_APPENDINT32(logp,
2025                     (afs_int32)(rsize<<24) + (t1<<18) + (t2<<12) + (t3<<6) + t4);
2026     ICL_APPENDINT32(logp, (afs_int32)op);
2027     ICL_APPENDINT32(logp, (afs_int32)osi_ThreadUnique());
2028     ICL_APPENDINT32(logp, (afs_int32)(tv.tv_sec & 0x3ff) * 1000000 + tv.tv_usec);
2029
2030     if (t1) {
2031         /* marshall parameter 1 now */
2032         if (t1 == ICL_TYPE_STRING) {
2033             afs_icl_AppendString(logp, (char *) p1);
2034         }
2035         else if (t1 == ICL_TYPE_HYPER) {
2036             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p1)->high);
2037             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p1)->low);
2038         }
2039         else if (t1 == ICL_TYPE_INT64) {
2040 #ifdef AFSLITTLE_ENDIAN
2041 #ifdef AFS_64BIT_CLIENT
2042             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[1]);
2043             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[0]);
2044 #else /* AFS_64BIT_CLIENT */
2045             ICL_APPENDINT32(logp, (afs_int32) p1);
2046             ICL_APPENDINT32(logp, (afs_int32) 0);
2047 #endif /* AFS_64BIT_CLIENT */
2048 #else /* AFSLITTLE_ENDIAN */
2049 #ifdef AFS_64BIT_CLIENT
2050             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[0]);
2051             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[1]);
2052 #else /* AFS_64BIT_CLIENT */
2053             ICL_APPENDINT32(logp, (afs_int32) 0);
2054             ICL_APPENDINT32(logp, (afs_int32) p1);
2055 #endif /* AFS_64BIT_CLIENT */
2056 #endif /* AFSLITTLE_ENDIAN */
2057         }
2058         else if (t1 == ICL_TYPE_FID) {
2059             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[0]);
2060             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[1]);
2061             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[2]);
2062             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[3]);
2063         }
2064 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
2065         else if (t1 == ICL_TYPE_INT32)
2066             ICL_APPENDINT32(logp, (afs_int32)p1);
2067 #endif /* AFS_ALPHA_ENV */
2068         else ICL_APPENDLONG(logp, p1); 
2069     }
2070     if (t2) {
2071         /* marshall parameter 2 now */
2072         if (t2 == ICL_TYPE_STRING) afs_icl_AppendString(logp, (char *) p2);
2073         else if (t2 == ICL_TYPE_HYPER) {
2074             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p2)->high);
2075             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p2)->low);
2076         }
2077         else if (t2 == ICL_TYPE_INT64) {
2078 #ifdef AFSLITTLE_ENDIAN
2079 #ifdef AFS_64BIT_CLIENT
2080             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[1]);
2081             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[0]);
2082 #else /* AFS_64BIT_CLIENT */
2083             ICL_APPENDINT32(logp, (afs_int32) p2);
2084             ICL_APPENDINT32(logp, (afs_int32) 0);
2085 #endif /* AFS_64BIT_CLIENT */
2086 #else /* AFSLITTLE_ENDIAN */
2087 #ifdef AFS_64BIT_CLIENT
2088             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[0]);
2089             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[1]);
2090 #else /* AFS_64BIT_CLIENT */
2091             ICL_APPENDINT32(logp, (afs_int32) 0);
2092             ICL_APPENDINT32(logp, (afs_int32) p2);
2093 #endif /* AFS_64BIT_CLIENT */
2094 #endif /* AFSLITTLE_ENDIAN */
2095         }
2096         else if (t2 == ICL_TYPE_FID) {
2097             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[0]);
2098             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[1]);
2099             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[2]);
2100             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[3]);
2101         }
2102 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
2103         else if (t2 == ICL_TYPE_INT32)
2104             ICL_APPENDINT32(logp, (afs_int32)p2);
2105 #endif /* AFS_ALPHA_ENV */
2106         else ICL_APPENDLONG(logp, p2);
2107     }
2108     if (t3) {
2109         /* marshall parameter 3 now */
2110         if (t3 == ICL_TYPE_STRING) afs_icl_AppendString(logp, (char *) p3);
2111         else if (t3 == ICL_TYPE_HYPER) {
2112             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p3)->high);
2113             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p3)->low);
2114         }
2115         else if (t3 == ICL_TYPE_INT64) {
2116 #ifdef AFSLITTLE_ENDIAN
2117 #ifdef AFS_64BIT_CLIENT
2118             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[1]);
2119             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[0]);
2120 #else /* AFS_64BIT_CLIENT */
2121             ICL_APPENDINT32(logp, (afs_int32) p3);
2122             ICL_APPENDINT32(logp, (afs_int32) 0);
2123 #endif /* AFS_64BIT_CLIENT */
2124 #else /* AFSLITTLE_ENDIAN */
2125 #ifdef AFS_64BIT_CLIENT
2126             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[0]);
2127             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[1]);
2128 #else /* AFS_64BIT_CLIENT */
2129             ICL_APPENDINT32(logp, (afs_int32) 0);
2130             ICL_APPENDINT32(logp, (afs_int32) p3);
2131 #endif /* AFS_64BIT_CLIENT */
2132 #endif /* AFSLITTLE_ENDIAN */
2133         }
2134         else if (t3 == ICL_TYPE_FID) {
2135             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[0]);
2136             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[1]);
2137             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[2]);
2138             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[3]);
2139         }
2140 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
2141         else if (t3 == ICL_TYPE_INT32)
2142             ICL_APPENDINT32(logp, (afs_int32)p3);
2143 #endif /* AFS_ALPHA_ENV */
2144         else ICL_APPENDLONG(logp, p3);
2145     }
2146     if (t4) {
2147         /* marshall parameter 4 now */
2148         if (t4 == ICL_TYPE_STRING) afs_icl_AppendString(logp, (char *) p4);
2149         else if (t4 == ICL_TYPE_HYPER) {
2150             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p4)->high);
2151             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p4)->low);
2152         }
2153         else if (t4 == ICL_TYPE_INT64) {
2154 #ifdef AFSLITTLE_ENDIAN
2155 #ifdef AFS_64BIT_CLIENT
2156             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[1]);
2157             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[0]);
2158 #else /* AFS_64BIT_CLIENT */
2159             ICL_APPENDINT32(logp, (afs_int32) p4);
2160             ICL_APPENDINT32(logp, (afs_int32) 0);
2161 #endif /* AFS_64BIT_CLIENT */
2162 #else /* AFSLITTLE_ENDIAN */
2163 #ifdef AFS_64BIT_CLIENT
2164             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[0]);
2165             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[1]);
2166 #else /* AFS_64BIT_CLIENT */
2167             ICL_APPENDINT32(logp, (afs_int32) 0);
2168             ICL_APPENDINT32(logp, (afs_int32) p4);
2169 #endif /* AFS_64BIT_CLIENT */
2170 #endif /* AFSLITTLE_ENDIAN */
2171         }
2172         else if (t4 == ICL_TYPE_FID) {
2173             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[0]);
2174             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[1]);
2175             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[2]);
2176             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[3]);
2177         }
2178 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
2179         else if (t4 == ICL_TYPE_INT32)
2180             ICL_APPENDINT32(logp, (afs_int32)p4);
2181 #endif /* AFS_ALPHA_ENV */
2182         else ICL_APPENDLONG(logp, p4);
2183     }
2184     ReleaseWriteLock(&logp->lock);
2185 }
2186
2187 /* create a log with size logSize; return it in *outLogpp and tag
2188  * it with name "name."
2189  */
2190 int afs_icl_CreateLog(char *name, afs_int32 logSize, struct afs_icl_log **outLogpp)
2191 {
2192     return afs_icl_CreateLogWithFlags(name, logSize, /*flags*/0, outLogpp);
2193 }
2194
2195 /* create a log with size logSize; return it in *outLogpp and tag
2196  * it with name "name."  'flags' can be set to make the log unclearable.
2197  */
2198 int afs_icl_CreateLogWithFlags(char *name, afs_int32 logSize, afs_uint32 flags, 
2199         struct afs_icl_log **outLogpp)
2200 {
2201     register struct afs_icl_log *logp;
2202
2203     /* add into global list under lock */
2204     ObtainWriteLock(&afs_icl_lock,183);
2205     if (!afs_icl_inited) afs_icl_Init();
2206
2207     for (logp = afs_icl_allLogs; logp; logp=logp->nextp) {
2208         if (strcmp(logp->name, name) == 0) {
2209             /* found it already created, just return it */
2210             logp->refCount++;
2211             *outLogpp = logp;
2212             if (flags & ICL_CRLOG_FLAG_PERSISTENT)
2213             {
2214                 ObtainWriteLock(&logp->lock,184);
2215                 logp->states |= ICL_LOGF_PERSISTENT;
2216                 ReleaseWriteLock(&logp->lock);
2217             }
2218             ReleaseWriteLock(&afs_icl_lock);
2219             return 0;
2220         }
2221     }
2222     
2223     logp = (struct afs_icl_log *)
2224         osi_AllocSmallSpace(sizeof(struct afs_icl_log));
2225     memset((caddr_t)logp, 0, sizeof(*logp));
2226
2227     logp->refCount = 1;
2228     logp->name = osi_AllocSmallSpace(strlen(name)+1);
2229     strcpy(logp->name, name);
2230     LOCK_INIT(&logp->lock, "logp lock");
2231     logp->logSize = logSize;
2232     logp->datap = NULL; /* don't allocate it until we need it */
2233
2234     if (flags & ICL_CRLOG_FLAG_PERSISTENT)
2235         logp->states |= ICL_LOGF_PERSISTENT;
2236
2237     logp->nextp = afs_icl_allLogs;
2238     afs_icl_allLogs = logp;
2239     ReleaseWriteLock(&afs_icl_lock);
2240
2241     *outLogpp = logp;
2242     return 0;
2243 }
2244
2245 /* called with a log, a pointer to a buffer, the size of the buffer
2246  * (in *bufSizep), the starting cookie (in *cookiep, use 0 at the start)
2247  * and returns data in the provided buffer, and returns output flags
2248  * in *flagsp.  The flag ICL_COPYOUTF_MISSEDSOME is set if we can't
2249  * find the record with cookie value cookie.
2250  */
2251 int afs_icl_CopyOut(register struct afs_icl_log *logp, afs_int32 *bufferp, 
2252         afs_int32 *bufSizep, afs_uint32 *cookiep, afs_int32 *flagsp)
2253 {
2254     afs_int32 nwords;           /* number of words to copy out */
2255     afs_uint32 startCookie;     /* first cookie to use */
2256     afs_int32 outWords;         /* words we've copied out */
2257     afs_int32 inWords;          /* max words to copy out */
2258     afs_int32 code;                     /* return code */
2259     afs_int32 ix;                       /* index we're copying from */
2260     afs_int32 outFlags;         /* return flags */
2261     afs_int32 inFlags;          /* flags passed in */
2262     afs_int32 end;
2263
2264     inWords = *bufSizep;        /* max to copy out */
2265     outWords = 0;               /* amount copied out */
2266     startCookie = *cookiep;
2267     outFlags = 0;
2268     inFlags = *flagsp;
2269     code = 0;
2270
2271     ObtainWriteLock(&logp->lock,185);
2272     if (!logp->datap) {
2273         ReleaseWriteLock(&logp->lock);
2274         goto done;
2275     }
2276
2277     /* first, compute the index of the start cookie we've been passed */
2278     while (1) {
2279         /* (re-)compute where we should start */
2280         if (startCookie < logp->baseCookie) {
2281             if (startCookie)  /* missed some output */
2282                 outFlags |= ICL_COPYOUTF_MISSEDSOME;
2283             /* skip to the first available record */
2284             startCookie = logp->baseCookie;
2285             *cookiep = startCookie;
2286         }
2287
2288         /* compute where we find the first element to copy out */
2289         ix = logp->firstUsed + startCookie - logp->baseCookie;
2290         if (ix >= logp->logSize) ix -= logp->logSize;
2291
2292         /* if have some data now, break out and process it */
2293         if (startCookie - logp->baseCookie < logp->logElements) break;
2294
2295         /* At end of log, so clear it if we need to */
2296         if (inFlags & ICL_COPYOUTF_CLRAFTERREAD)
2297         {
2298             logp->firstUsed = logp->firstFree = 0;
2299             logp->logElements = 0;
2300         }
2301         /* otherwise, either wait for the data to arrive, or return */
2302         if (!(inFlags & ICL_COPYOUTF_WAITIO)) {
2303             ReleaseWriteLock(&logp->lock);
2304             code = 0;
2305             goto done;
2306         }
2307         logp->states |= ICL_LOGF_WAITING;
2308         ReleaseWriteLock(&logp->lock);
2309         afs_osi_Sleep(&logp->lock);
2310         ObtainWriteLock(&logp->lock,186);
2311     }
2312     /* copy out data from ix to logSize or firstFree, depending
2313      * upon whether firstUsed <= firstFree (no wrap) or otherwise.
2314      * be careful not to copy out more than nwords.
2315      */
2316     if (ix >= logp->firstUsed) {
2317         if (logp->firstUsed <= logp->firstFree)
2318             /* no wrapping */
2319             end = logp->firstFree;      /* first element not to copy */
2320         else
2321             end = logp->logSize;
2322         nwords = inWords;       /* don't copy more than this */
2323         if (end - ix < nwords)
2324             nwords = end - ix;
2325         if (nwords > 0) {
2326             memcpy((char *) bufferp, (char *) &logp->datap[ix], sizeof(afs_int32) * nwords);
2327             outWords += nwords;
2328             inWords -= nwords;
2329             bufferp += nwords;
2330         }
2331         /* if we're going to copy more out below, we'll start here */
2332         ix = 0;
2333     }
2334     /* now, if active part of the log has wrapped, there's more stuff
2335      * starting at the head of the log.  Copy out more from there.
2336      */
2337     if (logp->firstUsed > logp->firstFree
2338         && ix < logp->firstFree && inWords > 0) {
2339         /* (more to) copy out from the wrapped section at the
2340          * start of the log.  May get here even if didn't copy any
2341          * above, if the cookie points directly into the wrapped section.
2342          */
2343         nwords = inWords;
2344         if (logp->firstFree - ix < nwords)
2345             nwords = logp->firstFree - ix;
2346         memcpy((char *) bufferp, (char *) &logp->datap[ix], sizeof(afs_int32) * nwords);
2347         outWords += nwords;
2348         inWords -= nwords;
2349         bufferp += nwords;
2350     }
2351
2352     ReleaseWriteLock(&logp->lock);
2353
2354   done:
2355     if (code == 0) {
2356         *bufSizep = outWords;
2357         *flagsp = outFlags;
2358     }
2359     return code;
2360 }
2361
2362 /* return basic parameter information about a log */
2363 int afs_icl_GetLogParms(struct afs_icl_log *logp, afs_int32 *maxSizep, 
2364         afs_int32 *curSizep)
2365 {
2366     ObtainReadLock(&logp->lock);
2367     *maxSizep = logp->logSize;
2368     *curSizep = logp->logElements;
2369     ReleaseReadLock(&logp->lock);
2370     return 0;
2371 }
2372
2373
2374 /* hold and release logs */
2375 int afs_icl_LogHold(register struct afs_icl_log *logp)
2376 {
2377     ObtainWriteLock(&afs_icl_lock,187);
2378     logp->refCount++;
2379     ReleaseWriteLock(&afs_icl_lock);
2380     return 0;
2381 }
2382
2383 /* hold and release logs, called with lock already held */
2384 int afs_icl_LogHoldNL(register struct afs_icl_log *logp)
2385 {
2386     logp->refCount++;
2387     return 0;
2388 }
2389
2390 /* keep track of how many sets believe the log itself is allocated */
2391 int afs_icl_LogUse(register struct afs_icl_log *logp)
2392 {
2393     ObtainWriteLock(&logp->lock,188);
2394     if (logp->setCount == 0) {
2395         /* this is the first set actually using the log -- allocate it */
2396         if (logp->logSize == 0) {
2397             /* we weren't passed in a hint and it wasn't set */
2398             logp->logSize = ICL_DEFAULT_LOGSIZE;
2399         }
2400         logp->datap = (afs_int32 *) afs_osi_Alloc(sizeof(afs_int32) * logp->logSize);
2401 #ifdef  KERNEL_HAVE_PIN
2402         pin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
2403 #endif
2404     }
2405     logp->setCount++;
2406     ReleaseWriteLock(&logp->lock);
2407     return 0;
2408 }
2409
2410 /* decrement the number of real users of the log, free if possible */
2411 int afs_icl_LogFreeUse(register struct afs_icl_log *logp)
2412 {
2413     ObtainWriteLock(&logp->lock,189);
2414     if (--logp->setCount == 0) {
2415         /* no more users -- free it (but keep log structure around)*/
2416         afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
2417 #ifdef  KERNEL_HAVE_PIN
2418         unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
2419 #endif
2420         logp->firstUsed = logp->firstFree = 0;
2421         logp->logElements = 0;
2422         logp->datap = NULL;
2423     }
2424     ReleaseWriteLock(&logp->lock);
2425     return 0;
2426 }
2427
2428 /* set the size of the log to 'logSize' */
2429 int afs_icl_LogSetSize(register struct afs_icl_log *logp, afs_int32 logSize)
2430 {  
2431     ObtainWriteLock(&logp->lock,190);
2432     if (!logp->datap) {
2433         /* nothing to worry about since it's not allocated */
2434         logp->logSize = logSize;
2435     }
2436     else {
2437         /* reset log */
2438         logp->firstUsed = logp->firstFree = 0;
2439         logp->logElements = 0;
2440
2441         /* free and allocate a new one */
2442         afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
2443 #ifdef  KERNEL_HAVE_PIN
2444         unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
2445 #endif
2446         logp->datap = (afs_int32 *) afs_osi_Alloc(sizeof(afs_int32) * logSize);
2447 #ifdef  KERNEL_HAVE_PIN
2448         pin((char *)logp->datap, sizeof(afs_int32) * logSize);
2449 #endif
2450         logp->logSize = logSize;
2451     }
2452     ReleaseWriteLock(&logp->lock);
2453
2454     return 0;
2455 }
2456
2457 /* free a log.  Called with afs_icl_lock locked. */
2458 int afs_icl_ZapLog(register struct afs_icl_log *logp)
2459 {
2460     register struct afs_icl_log **lpp, *tp;
2461
2462     for(lpp = &afs_icl_allLogs, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
2463         if (tp == logp) {
2464             /* found the dude we want to remove */
2465             *lpp = logp->nextp;
2466             osi_FreeSmallSpace(logp->name);
2467             osi_FreeSmallSpace(logp->datap);
2468             osi_FreeSmallSpace(logp);
2469             break;      /* won't find it twice */
2470         }
2471     }
2472     return 0;
2473 }
2474
2475 /* do the release, watching for deleted entries */
2476 int afs_icl_LogRele(register struct afs_icl_log *logp)
2477 {
2478     ObtainWriteLock(&afs_icl_lock,191);
2479     if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
2480         afs_icl_ZapLog(logp);   /* destroys logp's lock! */
2481     }
2482     ReleaseWriteLock(&afs_icl_lock);
2483     return 0;
2484 }
2485
2486 /* do the release, watching for deleted entries, log already held */
2487 int afs_icl_LogReleNL(register struct afs_icl_log *logp)
2488 {
2489     if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
2490         afs_icl_ZapLog(logp);   /* destroys logp's lock! */
2491     }
2492     return 0;
2493 }
2494
2495 /* zero out the log */
2496 int afs_icl_ZeroLog(register struct afs_icl_log *logp)
2497 {
2498     ObtainWriteLock(&logp->lock,192);
2499     logp->firstUsed = logp->firstFree = 0;
2500     logp->logElements = 0;
2501     logp->baseCookie = 0;
2502     ReleaseWriteLock(&logp->lock);
2503     return 0;
2504 }
2505
2506 /* free a log entry, and drop its reference count */
2507 int afs_icl_LogFree(register struct afs_icl_log *logp)
2508 {
2509     ObtainWriteLock(&logp->lock,193);
2510     logp->states |= ICL_LOGF_DELETED;
2511     ReleaseWriteLock(&logp->lock);
2512     afs_icl_LogRele(logp);
2513     return 0;
2514 }
2515
2516 /* find a log by name, returning it held */
2517 struct afs_icl_log *afs_icl_FindLog(char *name)
2518 {
2519     register struct afs_icl_log *tp;
2520     ObtainWriteLock(&afs_icl_lock,194);
2521     for(tp = afs_icl_allLogs; tp; tp=tp->nextp) {
2522         if (strcmp(tp->name, name) == 0) {
2523             /* this is the dude we want */
2524             tp->refCount++;
2525             break;
2526         }
2527     }
2528     ReleaseWriteLock(&afs_icl_lock);
2529     return tp;
2530 }
2531
2532 int afs_icl_EnumerateLogs(int (*aproc)(), char *arock)
2533 {
2534     register struct afs_icl_log *tp;
2535     register afs_int32 code;
2536
2537     code = 0;
2538     ObtainWriteLock(&afs_icl_lock,195);
2539     for(tp = afs_icl_allLogs; tp; tp=tp->nextp) {
2540         tp->refCount++; /* hold this guy */
2541         ReleaseWriteLock(&afs_icl_lock);
2542         ObtainReadLock(&tp->lock);
2543         code = (*aproc)(tp->name, arock, tp);
2544         ReleaseReadLock(&tp->lock);
2545         ObtainWriteLock(&afs_icl_lock,196);
2546         if (--tp->refCount == 0)
2547             afs_icl_ZapLog(tp);
2548         if (code) break;
2549     }
2550     ReleaseWriteLock(&afs_icl_lock);
2551     return code;
2552 }
2553
2554 struct afs_icl_set *afs_icl_allSets = 0;
2555
2556 int afs_icl_CreateSet(char *name, struct afs_icl_log *baseLogp, 
2557         struct afs_icl_log *fatalLogp, struct afs_icl_set **outSetpp)
2558 {
2559     return afs_icl_CreateSetWithFlags(name, baseLogp, fatalLogp,
2560                                       /*flags*/0, outSetpp);
2561 }
2562
2563 /* create a set, given pointers to base and fatal logs, if any.
2564  * Logs are unlocked, but referenced, and *outSetpp is returned
2565  * referenced.  Function bumps reference count on logs, since it
2566  * addds references from the new afs_icl_set.  When the set is destroyed,
2567  * those references will be released.
2568  */
2569 int afs_icl_CreateSetWithFlags(char *name, struct afs_icl_log *baseLogp, 
2570         struct afs_icl_log *fatalLogp, afs_uint32 flags, struct afs_icl_set **outSetpp)
2571 {
2572     register struct afs_icl_set *setp;
2573     register int i;
2574     afs_int32 states = ICL_DEFAULT_SET_STATES;
2575
2576     ObtainWriteLock(&afs_icl_lock,197);
2577     if (!afs_icl_inited) afs_icl_Init();
2578
2579     for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
2580         if (strcmp(setp->name, name) == 0) {
2581             setp->refCount++;
2582             *outSetpp = setp;
2583             if (flags & ICL_CRSET_FLAG_PERSISTENT)
2584             {
2585                 ObtainWriteLock(&setp->lock,198);
2586                 setp->states |= ICL_SETF_PERSISTENT;
2587                 ReleaseWriteLock(&setp->lock);
2588             }
2589             ReleaseWriteLock(&afs_icl_lock);
2590             return 0;
2591         }
2592     }
2593     
2594     /* determine initial state */
2595     if (flags & ICL_CRSET_FLAG_DEFAULT_ON)
2596         states = ICL_SETF_ACTIVE;
2597     else if (flags & ICL_CRSET_FLAG_DEFAULT_OFF)
2598         states = ICL_SETF_FREED;
2599     if (flags & ICL_CRSET_FLAG_PERSISTENT)
2600         states |= ICL_SETF_PERSISTENT;
2601
2602     setp = (struct afs_icl_set *) afs_osi_Alloc(sizeof(struct afs_icl_set));
2603     memset((caddr_t)setp, 0, sizeof(*setp));
2604     setp->refCount = 1;
2605     if (states & ICL_SETF_FREED)
2606         states &= ~ICL_SETF_ACTIVE;     /* if freed, can't be active */
2607     setp->states = states;
2608
2609     LOCK_INIT(&setp->lock, "setp lock");
2610     /* next lock is obtained in wrong order, hierarchy-wise, but
2611      * it doesn't matter, since no one can find this lock yet, since
2612      * the afs_icl_lock is still held, and thus the obtain can't block.
2613      */
2614     ObtainWriteLock(&setp->lock,199);
2615     setp->name = osi_AllocSmallSpace(strlen(name)+1);
2616     strcpy(setp->name, name);
2617     setp->nevents = ICL_DEFAULTEVENTS;
2618     setp->eventFlags = afs_osi_Alloc(ICL_DEFAULTEVENTS);
2619 #ifdef  KERNEL_HAVE_PIN
2620     pin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
2621 #endif
2622     for(i=0; i<ICL_DEFAULTEVENTS; i++)
2623         setp->eventFlags[i] = 0xff;     /* default to enabled */
2624
2625     /* update this global info under the afs_icl_lock */
2626     setp->nextp = afs_icl_allSets;
2627     afs_icl_allSets = setp;
2628     ReleaseWriteLock(&afs_icl_lock);
2629
2630     /* set's basic lock is still held, so we can finish init */
2631     if (baseLogp) {
2632         setp->logs[0] = baseLogp;
2633         afs_icl_LogHold(baseLogp);
2634         if (!(setp->states & ICL_SETF_FREED))
2635             afs_icl_LogUse(baseLogp);   /* log is actually being used */
2636     }
2637     if (fatalLogp) {
2638         setp->logs[1] = fatalLogp;
2639         afs_icl_LogHold(fatalLogp);
2640         if (!(setp->states & ICL_SETF_FREED))
2641             afs_icl_LogUse(fatalLogp);  /* log is actually being used */
2642     }
2643     ReleaseWriteLock(&setp->lock);
2644
2645     *outSetpp = setp;
2646     return 0;
2647 }
2648
2649 /* function to change event enabling information for a particular set */
2650 int afs_icl_SetEnable(struct afs_icl_set *setp, afs_int32 eventID, int setValue)
2651 {
2652     char *tp;
2653
2654     ObtainWriteLock(&setp->lock,200);
2655     if (!ICL_EVENTOK(setp, eventID)) {
2656         ReleaseWriteLock(&setp->lock);
2657         return -1;
2658     }
2659     tp = &setp->eventFlags[ICL_EVENTBYTE(eventID)];
2660     if (setValue)
2661         *tp |= ICL_EVENTMASK(eventID);
2662     else
2663         *tp &= ~(ICL_EVENTMASK(eventID));
2664     ReleaseWriteLock(&setp->lock);
2665     return 0;
2666 }
2667
2668 /* return indication of whether a particular event ID is enabled
2669  * for tracing.  If *getValuep is set to 0, the event is disabled,
2670  * otherwise it is enabled.  All events start out enabled by default.
2671  */
2672 int afs_icl_GetEnable(struct afs_icl_set *setp, afs_int32 eventID, 
2673         int *getValuep)
2674 {
2675     ObtainReadLock(&setp->lock);
2676     if (!ICL_EVENTOK(setp, eventID)) {
2677         ReleaseWriteLock(&setp->lock);
2678         return -1;
2679     }
2680     if (setp->eventFlags[ICL_EVENTBYTE(eventID)] & ICL_EVENTMASK(eventID))
2681         *getValuep = 1;
2682     else
2683         *getValuep = 0;
2684     ReleaseReadLock(&setp->lock);
2685     return 0;
2686 }
2687
2688 /* hold and release event sets */
2689 int afs_icl_SetHold(register struct afs_icl_set *setp)
2690 {
2691     ObtainWriteLock(&afs_icl_lock,201);
2692     setp->refCount++;
2693     ReleaseWriteLock(&afs_icl_lock);
2694     return 0;
2695 }
2696
2697 /* free a set.  Called with afs_icl_lock locked */
2698 int afs_icl_ZapSet(register struct afs_icl_set *setp)
2699 {
2700     register struct afs_icl_set **lpp, *tp;
2701     int i;
2702     register struct afs_icl_log *tlp;
2703
2704     for(lpp = &afs_icl_allSets, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
2705         if (tp == setp) {
2706             /* found the dude we want to remove */
2707             *lpp = setp->nextp;
2708             osi_FreeSmallSpace(setp->name);
2709             afs_osi_Free(setp->eventFlags, ICL_DEFAULTEVENTS);
2710 #ifdef  KERNEL_HAVE_PIN
2711             unpin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
2712 #endif
2713             for(i=0; i < ICL_LOGSPERSET; i++) {
2714                 if ((tlp = setp->logs[i]))
2715                     afs_icl_LogReleNL(tlp);
2716             }
2717             osi_FreeSmallSpace(setp);
2718             break;      /* won't find it twice */
2719         }
2720     }
2721     return 0;
2722 }
2723
2724 /* do the release, watching for deleted entries */
2725 int afs_icl_SetRele(register struct afs_icl_set *setp)
2726 {
2727     ObtainWriteLock(&afs_icl_lock,202);
2728     if (--setp->refCount == 0 && (setp->states & ICL_SETF_DELETED)) {
2729         afs_icl_ZapSet(setp);   /* destroys setp's lock! */
2730     }
2731     ReleaseWriteLock(&afs_icl_lock);
2732     return 0;
2733 }
2734
2735 /* free a set entry, dropping its reference count */
2736 int afs_icl_SetFree(register struct afs_icl_set *setp)
2737 {
2738     ObtainWriteLock(&setp->lock,203);
2739     setp->states |= ICL_SETF_DELETED;
2740     ReleaseWriteLock(&setp->lock);
2741     afs_icl_SetRele(setp);
2742     return 0;
2743 }
2744
2745 /* find a set by name, returning it held */
2746 struct afs_icl_set *afs_icl_FindSet(char *name)
2747 {
2748     register struct afs_icl_set *tp;
2749     ObtainWriteLock(&afs_icl_lock,204);
2750     for(tp = afs_icl_allSets; tp; tp=tp->nextp) {
2751         if (strcmp(tp->name, name) == 0) {
2752             /* this is the dude we want */
2753             tp->refCount++;
2754             break;
2755         }
2756     }
2757     ReleaseWriteLock(&afs_icl_lock);
2758     return tp;
2759 }
2760
2761 /* zero out all the logs in the set */
2762 int afs_icl_ZeroSet(struct afs_icl_set *setp)
2763 {
2764     register int i;
2765     int code = 0;
2766     int tcode;
2767     struct afs_icl_log *logp;
2768     
2769     ObtainReadLock(&setp->lock);
2770     for(i = 0; i < ICL_LOGSPERSET; i++) {
2771         logp = setp->logs[i];
2772         if (logp) {
2773             afs_icl_LogHold(logp);
2774             tcode = afs_icl_ZeroLog(logp);
2775             if (tcode != 0) code = tcode;       /* save the last bad one */
2776             afs_icl_LogRele(logp);
2777         }
2778     }
2779     ReleaseReadLock(&setp->lock);
2780     return code;
2781 }
2782
2783 int afs_icl_EnumerateSets(int (*aproc)(), char *arock)
2784 {
2785     register struct afs_icl_set *tp, *np;
2786     register afs_int32 code;
2787
2788     code = 0;
2789     ObtainWriteLock(&afs_icl_lock,205);
2790     for(tp = afs_icl_allSets; tp; tp=np) {
2791         tp->refCount++; /* hold this guy */
2792         ReleaseWriteLock(&afs_icl_lock);
2793         code = (*aproc)(tp->name, arock, tp);
2794         ObtainWriteLock(&afs_icl_lock,206);
2795         np = tp->nextp; /* tp may disappear next, but not np */
2796         if (--tp->refCount == 0 && (tp->states & ICL_SETF_DELETED))
2797             afs_icl_ZapSet(tp);
2798         if (code) break;
2799     }
2800     ReleaseWriteLock(&afs_icl_lock);
2801     return code;
2802 }
2803
2804 int afs_icl_AddLogToSet(struct afs_icl_set *setp, struct afs_icl_log *newlogp)
2805 {
2806     register int i;
2807     int code = -1;
2808     
2809     ObtainWriteLock(&setp->lock,207);
2810     for(i = 0; i < ICL_LOGSPERSET; i++) {
2811         if (!setp->logs[i]) {
2812             setp->logs[i] = newlogp;
2813             code = i;
2814             afs_icl_LogHold(newlogp);
2815             if (!(setp->states & ICL_SETF_FREED)) {
2816                 /* bump up the number of sets using the log */
2817                 afs_icl_LogUse(newlogp);
2818             }
2819             break;
2820         }
2821     }
2822     ReleaseWriteLock(&setp->lock);
2823     return code;
2824 }
2825
2826 int afs_icl_SetSetStat(struct afs_icl_set *setp, int op)
2827 {
2828     int i;
2829     afs_int32 code;
2830     struct afs_icl_log *logp;
2831
2832     ObtainWriteLock(&setp->lock,208);
2833     switch(op) {
2834     case ICL_OP_SS_ACTIVATE:    /* activate a log */
2835         /*
2836          * If we are not already active, see if we have released
2837          * our demand that the log be allocated (FREED set).  If
2838          * we have, reassert our desire.
2839          */
2840         if (!(setp->states & ICL_SETF_ACTIVE)) {
2841             if (setp->states & ICL_SETF_FREED) {
2842                 /* have to reassert desire for logs */
2843                 for(i = 0; i < ICL_LOGSPERSET; i++) {
2844                     logp = setp->logs[i];
2845                     if (logp) {
2846                         afs_icl_LogHold(logp);
2847                         afs_icl_LogUse(logp);
2848                         afs_icl_LogRele(logp);
2849                     }
2850                 }
2851                 setp->states &= ~ICL_SETF_FREED;
2852             }
2853             setp->states |= ICL_SETF_ACTIVE;
2854         }
2855         code = 0;
2856         break;
2857
2858     case ICL_OP_SS_DEACTIVATE:  /* deactivate a log */
2859         /* this doesn't require anything beyond clearing the ACTIVE flag */
2860         setp->states &= ~ICL_SETF_ACTIVE;
2861         code = 0;
2862         break;
2863
2864     case ICL_OP_SS_FREE:        /* deassert design for log */
2865         /* 
2866          * if we are already in this state, do nothing; otherwise
2867          * deassert desire for log
2868          */
2869         if (setp->states & ICL_SETF_ACTIVE)
2870             code = EINVAL;
2871         else {
2872             if (!(setp->states & ICL_SETF_FREED)) {
2873                 for(i = 0; i < ICL_LOGSPERSET; i++) {
2874                     logp = setp->logs[i];
2875                     if (logp) {
2876                         afs_icl_LogHold(logp);
2877                         afs_icl_LogFreeUse(logp);
2878                         afs_icl_LogRele(logp);
2879                     }
2880                 }
2881                 setp->states |= ICL_SETF_FREED;
2882             }
2883             code = 0;
2884         }
2885         break;
2886
2887     default:
2888         code = EINVAL;
2889     }
2890     ReleaseWriteLock(&setp->lock);
2891     return code;
2892 }
2893