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