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