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