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