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