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