1792e2c1f2e4728dbe0d34c9f014c7c1f592a6dc
[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         AFS_COPYIN(cmarg, (caddr_t) dst, sizeof *dst, code);
820         return code;
821 }
822
823 /* Main entry of all afs system calls */
824 #ifdef  AFS_SUN5_ENV
825 extern int afs_sinited;
826
827 /** The 32 bit OS expects the members of this structure to be 32 bit
828  * quantities and the 64 bit OS expects them as 64 bit quanties. Hence
829  * to accomodate both, *long* is used instead of afs_int32
830  */
831
832 #ifdef AFS_SUN57_ENV
833 struct afssysa {
834     long syscall;
835     long parm1;
836     long parm2;
837     long parm3;
838     long parm4;
839     long parm5;
840     long parm6;
841 };
842 #else
843 struct afssysa {
844     afs_int32 syscall;
845     afs_int32 parm1;
846     afs_int32 parm2;
847     afs_int32 parm3;
848     afs_int32 parm4;
849     afs_int32 parm5;
850     afs_int32 parm6;
851 };
852 #endif
853
854 Afs_syscall (uap, rvp)
855     register struct afssysa *uap;
856     rval_t *rvp;
857 {
858     int *retval = &rvp->r_val1;
859 #else /* AFS_SUN5_ENV */
860 #if     defined(AFS_OSF_ENV)
861 afs3_syscall(p, args, retval)
862         struct proc *p;
863         void *args;
864         int *retval;
865 {
866     register struct a {
867             long syscall;
868             long parm1;
869             long parm2;
870             long parm3;
871             long parm4;
872             long parm5;
873             long parm6;
874         } *uap = (struct a *)args;
875 #else   /* AFS_OSF_ENV */
876 #ifdef AFS_LINUX20_ENV
877 struct afssysargs {
878     long syscall;
879     long parm1;
880     long parm2;
881     long parm3;
882     long parm4;
883     long parm5;
884     long parm6; /* not actually used - should be removed */
885 };
886 /* Linux system calls only set up for 5 arguments. */
887 asmlinkage int afs_syscall(long syscall, long parm1, long parm2, long parm3,
888                            long parm4)
889 {
890     struct afssysargs args, *uap = &args;
891     int linux_ret=0;
892     int *retval = &linux_ret;
893     int eparm[4]; /* matches AFSCALL_ICL in fstrace.c */
894 #else
895 #if defined(UKERNEL)
896 Afs_syscall ()
897 {
898     register struct a {
899             long syscall;
900             long parm1;
901             long parm2;
902             long parm3;
903             long parm4;
904             long parm5;
905             long parm6;
906         } *uap = (struct a *)u.u_ap;
907 #else /* UKERNEL */
908 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
909 afs_syscall ()
910 #else
911 Afs_syscall ()
912 #endif /* SUN && !SUN5 */
913 {
914     register struct a {
915             long syscall;
916             long parm1;
917             long parm2;
918             long parm3;
919             long parm4;
920             long parm5;
921             long parm6;
922         } *uap = (struct a *)u.u_ap;
923 #endif /* UKERNEL */
924 #if  defined(AFS_DEC_ENV)
925     int *retval = &u.u_r.r_val1;
926 #else
927 #if defined(AFS_HPUX_ENV)
928     long *retval = &u.u_rval1;
929 #else
930     int *retval = &u.u_rval1;
931 #endif
932 #endif
933 #endif /* AFS_LINUX20_ENV */
934 #endif /* AFS_OSF_ENV */
935 #endif /* AFS_SUN5_ENV */
936     register int code = 0;
937
938     AFS_STATCNT(afs_syscall);
939 #ifdef        AFS_SUN5_ENV
940     rvp->r_vals = 0;
941     if (!afs_sinited) {
942         return (ENODEV);
943     }
944 #endif
945 #ifdef AFS_LINUX20_ENV
946     lock_kernel();
947     /* setup uap for use below - pull out the magic decoder ring to know
948      * which syscalls have folded argument lists.
949      */
950     uap->syscall = syscall;
951     uap->parm1 = parm1;
952     uap->parm2 = parm2;
953     uap->parm3 = parm3;
954     if (syscall == AFSCALL_ICL) {
955         AFS_COPYIN((char*)parm4, (char*)eparm, sizeof(eparm), code);
956         uap->parm4 = eparm[0];
957         uap->parm5 = eparm[1];
958         uap->parm6 = eparm[2];
959     }
960     else {
961         uap->parm4 = parm4;
962         uap->parm5 = 0;
963         uap->parm6 = 0;
964     }
965 #endif
966
967 #if defined(AFS_HPUX_ENV)
968     /*
969      * There used to be code here (duplicated from osi_Init()) for
970      * initializing the semaphore used by AFS_GLOCK().  Was the
971      * duplication to handle the case of a dynamically loaded kernel
972      * module?
973      */
974     osi_InitGlock();
975 #endif
976     if (uap->syscall == AFSCALL_CALL) {
977 #ifdef  AFS_SUN5_ENV
978         code =  afs_syscall_call(uap->parm1, uap->parm2, uap->parm3, 
979                                 uap->parm4, uap->parm5, uap->parm6, rvp, CRED());
980 #else
981         code = afs_syscall_call(uap->parm1, uap->parm2, uap->parm3, uap->parm4, uap->parm5, uap->parm6);
982 #endif
983     } else if (uap->syscall == AFSCALL_SETPAG) {
984 #ifdef  AFS_SUN5_ENV
985         struct cred *cred;
986         register proc_t *procp;
987
988         procp = ttoproc(curthread);
989         mutex_enter(&procp->p_crlock);
990         cred = procp->p_cred; 
991         AFS_GLOCK();
992         code =  afs_setpag(&cred);
993         AFS_GUNLOCK();
994         procp->p_cred = cred;
995         mutex_exit(&procp->p_crlock);
996 #else
997         AFS_GLOCK();
998 #if     defined(AFS_OSF_ENV)
999         code = afs_setpag(p, args, retval);
1000 #else   /* AFS_OSF_ENV */
1001         code = afs_setpag();
1002 #endif
1003         AFS_GUNLOCK();
1004 #endif
1005     } else if (uap->syscall == AFSCALL_PIOCTL) {
1006         AFS_GLOCK();
1007 #ifdef  AFS_SUN5_ENV
1008         code = afs_syscall_pioctl(uap->parm1, uap->parm2, uap->parm3, uap->parm4, rvp, CRED());
1009 #else
1010         code = afs_syscall_pioctl(uap->parm1, uap->parm2, uap->parm3, uap->parm4);
1011 #endif
1012         AFS_GUNLOCK();
1013     } else if (uap->syscall == AFSCALL_ICREATE) {
1014         struct iparam iparams;
1015
1016         code = copyin_iparam((char *)uap->parm3, &iparams);
1017         if (code) {
1018 #if !defined(AFS_SUN5_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_LINUX20_ENV)
1019             setuerror(code);
1020 #endif
1021         } else
1022 #ifdef  AFS_SUN5_ENV
1023         code =  afs_syscall_icreate(uap->parm1, uap->parm2, iparams.param1, iparams.param2, 
1024                                    iparams.param3, iparams.param4, rvp, CRED());
1025 #else
1026         code =  afs_syscall_icreate(uap->parm1, uap->parm2, iparams.param1, iparams.param2,
1027 #ifdef  AFS_OSF_ENV
1028                                    iparams.param3, iparams.param4, retval);
1029 #else
1030                                    iparams.param3, iparams.param4);
1031 #endif
1032 #endif  /* AFS_SUN5_ENV */
1033     } else if (uap->syscall == AFSCALL_IOPEN) {
1034 #ifdef  AFS_SUN5_ENV
1035         code = afs_syscall_iopen(uap->parm1, uap->parm2, uap->parm3, rvp, CRED());
1036 #else
1037 #ifdef  AFS_OSF_ENV
1038         code = afs_syscall_iopen(uap->parm1, uap->parm2, uap->parm3, retval);
1039 #else
1040         code = afs_syscall_iopen(uap->parm1, uap->parm2, uap->parm3);
1041 #endif
1042 #endif  /* AFS_SUN5_ENV */
1043     } else if (uap->syscall == AFSCALL_IDEC) {
1044 #ifdef  AFS_SUN5_ENV
1045         code = afs_syscall_iincdec(uap->parm1, uap->parm2, uap->parm3, -1, rvp, CRED());
1046 #else
1047         code = afs_syscall_iincdec(uap->parm1, uap->parm2, uap->parm3, -1);
1048 #endif  /* AFS_SUN5_ENV */
1049     } else if (uap->syscall == AFSCALL_IINC) {
1050 #ifdef  AFS_SUN5_ENV
1051         code = afs_syscall_iincdec(uap->parm1, uap->parm2, uap->parm3, 1, rvp, CRED());
1052 #else
1053         code = afs_syscall_iincdec(uap->parm1, uap->parm2, uap->parm3, 1);
1054 #endif  /* AFS_SUN5_ENV */
1055     } else if (uap->syscall == AFSCALL_ICL) {
1056         AFS_GLOCK();
1057         code = Afscall_icl(uap->parm1, uap->parm2, uap->parm3, uap->parm4, uap->parm5, retval);
1058         AFS_GUNLOCK();
1059 #ifdef AFS_LINUX20_ENV
1060         if (!code) {
1061             /* ICL commands can return values. */
1062             code = -linux_ret; /* Gets negated again at exit below */
1063         }
1064 #else
1065         if (code) {
1066 #if !defined(AFS_SUN5_ENV) && !defined(AFS_OSF_ENV)
1067             setuerror(code);
1068 #endif
1069         }
1070 #endif /* !AFS_LINUX20_ENV */
1071     } else {
1072 #if defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_LINUX20_ENV)
1073         code = EINVAL;
1074 #else
1075         setuerror(EINVAL);
1076 #endif  /* AFS_SUN5_ENV */
1077     }
1078 out:
1079 #ifdef AFS_LINUX20_ENV
1080     code = -code;
1081     unlock_kernel();
1082 #endif
1083     return code;
1084 }
1085 #endif /* AFS_SGI_ENV */
1086 #endif  /* !AFS_AIX32_ENV       */
1087
1088 /*
1089  * Initstate in the range 0 < x < 100 are early initialization states.
1090  * Initstate of 100 means a AFSOP_START operation has been done.  After this,
1091  *  the cache may be initialized.
1092  * Initstate of 101 means a AFSOP_GO operation has been done.  This operation
1093  *  is done after all the cache initialization has been done.
1094  * Initstate of 200 means that the volume has been looked up once, possibly
1095  *  incorrectly.
1096  * Initstate of 300 means that the volume has been *successfully* looked up.
1097  */
1098 afs_CheckInit() {
1099     register int code = 0;
1100
1101     AFS_STATCNT(afs_CheckInit);
1102     if (afs_initState <= 100)
1103         code =  ENXIO;   /* never finished init phase */
1104     else if (afs_initState == 101) {    /* init done, wait for afs_daemon */
1105         while (afs_initState < 200) afs_osi_Sleep(&afs_initState);
1106     } else  if (afs_initState == 200) 
1107         code =  ETIMEDOUT; /* didn't find root volume */
1108     return code;
1109 }
1110
1111 int afs_shuttingdown = 0; 
1112 void
1113 afs_shutdown()
1114 {
1115     extern short afs_brsDaemons;
1116     extern afs_int32 afs_CheckServerDaemonStarted;
1117     extern struct afs_osi_WaitHandle AFS_WaitHandler, AFS_CSWaitHandler;
1118     extern struct osi_file *afs_cacheInodep;
1119
1120     AFS_STATCNT(afs_shutdown);
1121     if (afs_shuttingdown) return;
1122     afs_shuttingdown = 1;
1123     if (afs_cold_shutdown) afs_warn("COLD ");
1124     else afs_warn("WARM ");
1125     afs_warn("shutting down of: CB... "); 
1126
1127     afs_termState = AFSOP_STOP_RXCALLBACK;
1128     rx_WakeupServerProcs();
1129     /* shutdown_rxkernel(); */
1130     while (afs_termState == AFSOP_STOP_RXCALLBACK)
1131         afs_osi_Sleep(&afs_termState);
1132
1133     afs_warn("afs... ");
1134     while (afs_termState == AFSOP_STOP_AFS) {
1135         afs_osi_CancelWait(&AFS_WaitHandler);
1136         afs_osi_Sleep(&afs_termState);
1137     }
1138     if (afs_CheckServerDaemonStarted) {
1139         while (afs_termState == AFSOP_STOP_CS) {
1140             afs_osi_CancelWait(&AFS_CSWaitHandler);
1141             afs_osi_Sleep(&afs_termState);
1142         }
1143     }
1144     afs_warn("BkG... ");
1145     /* Wake-up afs_brsDaemons so that we don't have to wait for a bkg job! */
1146     while (afs_termState == AFSOP_STOP_BKG) {
1147         afs_osi_Wakeup(&afs_brsDaemons);
1148         afs_osi_Sleep(&afs_termState);
1149     }
1150     afs_warn("CTrunc... ");
1151     /* Cancel cache truncate daemon. */
1152     while (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
1153         afs_osi_Wakeup((char*)&afs_CacheTruncateDaemon);
1154         afs_osi_Sleep(&afs_termState);
1155     }
1156 #if     defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
1157     afs_warn("RxEvent... ");
1158     /* cancel rx event deamon */
1159     while (afs_termState == AFSOP_STOP_RXEVENT) 
1160         afs_osi_Sleep(&afs_termState);
1161 #if defined(RXK_LISTENER_ENV)
1162     afs_warn("RxListener... ");
1163     /* cancel rx listener */
1164     osi_StopListener(); /* This closes rx_socket. */
1165     while (afs_termState == AFSOP_STOP_RXK_LISTENER) 
1166         afs_osi_Sleep(&afs_termState);
1167 #endif
1168 #else
1169     afs_termState =  AFSOP_STOP_COMPLETE;
1170 #endif
1171     afs_warn("\n");
1172
1173     /* Close file only after daemons which can write to it are stopped. */
1174     if (afs_cacheInodep)        /* memcache won't set this */
1175     {
1176         osi_UFSClose(afs_cacheInodep);    /* Since we always leave it open */
1177         afs_cacheInodep = 0;
1178     }
1179     return;     /* Just kill daemons for now */
1180 #ifdef notdef
1181     shutdown_CB();  
1182     shutdown_AFS();
1183     shutdown_rxkernel();
1184     shutdown_rxevent(); 
1185     shutdown_rx();
1186     afs_shutdown_BKG(); 
1187     shutdown_bufferpackage();
1188     shutdown_daemons();
1189     shutdown_cache();
1190     shutdown_osi();
1191     shutdown_osinet();
1192     shutdown_osifile();
1193     shutdown_vnodeops();
1194     shutdown_vfsops();
1195     shutdown_exporter();
1196     shutdown_memcache();
1197 #if !defined(AFS_NONFSTRANS) || defined(AFS_AIX_IAUTH_ENV)
1198 #if !defined(AFS_DEC_ENV) && !defined(AFS_OSF_ENV)
1199     /* this routine does not exist in Ultrix systems... 93.01.19 */
1200     shutdown_nfsclnt();
1201 #endif /* AFS_DEC_ENV */
1202 #endif
1203     shutdown_afstest();
1204     /* The following hold the cm stats */
1205 /*
1206     bzero(&afs_cmstats, sizeof(struct afs_CMStats));
1207     bzero(&afs_stats_cmperf, sizeof(struct afs_stats_CMPerf));
1208     bzero(&afs_stats_cmfullperf, sizeof(struct afs_stats_CMFullPerf));
1209 */
1210     afs_warn(" ALL allocated tables\n");
1211     afs_shuttingdown = 0;
1212 #endif
1213 }
1214
1215 shutdown_afstest()
1216 {
1217     AFS_STATCNT(shutdown_afstest);
1218     afs_initState = afs_termState = afs_setTime = 0;
1219     AFS_Running = afs_CB_Running = 0;
1220     afs_CacheInit_Done = afs_Go_Done = 0;
1221     if (afs_cold_shutdown) {
1222       *afs_rootVolumeName = 0;
1223     }
1224 }
1225
1226
1227 /* In case there is a bunch of dynamically build bkg daemons to free */
1228 afs_shutdown_BKG()
1229 { AFS_STATCNT(shutdown_BKG); }
1230
1231
1232 #if defined(AFS_ALPHA_ENV) || defined(AFS_SGI61_ENV)
1233 /* For SGI 6.2, this can is changed to 1 if it's a 32 bit kernel. */
1234 #if defined(AFS_SGI62_ENV) && defined(KERNEL) && !defined(_K64U64)
1235 int afs_icl_sizeofLong = 1;
1236 #else
1237 int afs_icl_sizeofLong = 2;
1238 #endif /* SGI62 */
1239 #else
1240 int afs_icl_sizeofLong = 1;
1241 #endif
1242
1243 int afs_icl_inited = 0;
1244
1245 /* init function, called once, under afs_icl_lock */
1246 afs_icl_Init()
1247 {
1248     afs_icl_inited = 1;
1249     return 0;
1250 }
1251
1252 extern struct afs_icl_log *afs_icl_FindLog();
1253 extern struct afs_icl_set *afs_icl_FindSet();
1254
1255
1256 static int
1257 Afscall_icl(long opcode, long p1, long p2, long p3, long p4, long *retval)
1258 {
1259     register int i;
1260     afs_int32 *lp, elts, flags;
1261     register afs_int32 code;
1262     struct afs_icl_log *logp;
1263     struct afs_icl_set *setp;
1264 #if defined(AFS_SGI61_ENV) || defined(AFS_SUN57_ENV)
1265     size_t temp;
1266 #else /* AFS_SGI61_ENV */
1267     afs_uint32 temp;
1268 #endif /* AFS_SGI61_ENV */
1269     char tname[65];
1270     afs_int32 startCookie;
1271     afs_int32 allocated;
1272     struct afs_icl_log *tlp;
1273
1274 #ifdef  AFS_SUN5_ENV
1275     if (!afs_suser(CRED())) {   /* only root can run this code */
1276         return (EACCES);
1277     }
1278 #else
1279     if (!afs_suser()) { /* only root can run this code */
1280 #if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_LINUX20_ENV)
1281         setuerror(EACCES);
1282         return EACCES;
1283 #else
1284         return EPERM;
1285 #endif
1286     }
1287 #endif
1288     switch (opcode) {
1289     case ICL_OP_COPYOUTCLR:     /* copy out data then clear */
1290     case ICL_OP_COPYOUT:        /* copy ouy data */
1291         /* copyout: p1=logname, p2=&buffer, p3=size(words), p4=&cookie
1292          * return flags<<24 + nwords.
1293          * updates cookie to updated start (not end) if we had to
1294          * skip some records.
1295          */
1296         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1297         if (code) return code;
1298         AFS_COPYIN((char *)p4, (char *)&startCookie, sizeof(afs_int32), code);
1299         if (code) return code;
1300         logp = afs_icl_FindLog(tname);
1301         if (!logp) return ENOENT;
1302 #define BUFFERSIZE      AFS_LRALLOCSIZ
1303         lp = (afs_int32 *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
1304         elts = BUFFERSIZE / sizeof(afs_int32);
1305         if (p3 < elts) elts = p3;
1306         flags = (opcode == ICL_OP_COPYOUT) ? 0 : ICL_COPYOUTF_CLRAFTERREAD;
1307         code = afs_icl_CopyOut(logp, lp, &elts, (afs_uint32 *) &startCookie,
1308                            &flags);
1309         if (code) {
1310             osi_FreeLargeSpace((struct osi_buffer *) lp);
1311             break;
1312         }
1313         AFS_COPYOUT((char *)lp, (char *)p2, elts * sizeof(afs_int32), code);
1314         if (code) goto done;
1315         AFS_COPYOUT((char *) &startCookie, (char *)p4, sizeof(afs_int32), code);
1316         if (code) goto done;
1317         *retval = (flags<<24) | (elts & 0xffffff);
1318       done:
1319         afs_icl_LogRele(logp);
1320         osi_FreeLargeSpace((struct osi_buffer *) lp);
1321         break;
1322
1323     case ICL_OP_ENUMLOGS:       /* enumerate logs */
1324         /* enumerate logs: p1=index, p2=&name, p3=sizeof(name), p4=&size.
1325          * return 0 for success, otherwise error.
1326          */
1327         for(tlp = afs_icl_allLogs; tlp; tlp=tlp->nextp) {
1328             if (p1-- == 0) break;
1329         }
1330         if (!tlp) return ENOENT;     /* past the end of file */
1331         temp = strlen(tlp->name)+1;
1332         if (temp > p3) return EINVAL;
1333         AFS_COPYOUT(tlp->name, (char *) p2, temp, code);
1334         if (!code)      /* copy out size of log */
1335             AFS_COPYOUT((char *)&tlp->logSize, (char *)p4, sizeof (afs_int32), code);
1336         break;
1337
1338     case ICL_OP_ENUMLOGSBYSET:  /* enumerate logs by set name */
1339         /* enumerate logs: p1=setname, p2=index, p3=&name, p4=sizeof(name).
1340          * return 0 for success, otherwise error.
1341          */
1342         AFS_COPYINSTR((char *)p1, tname, sizeof (tname), &temp, code);
1343         if (code) return code;
1344         setp = afs_icl_FindSet(tname);
1345         if (!setp) return ENOENT;
1346         if (p2 > ICL_LOGSPERSET)
1347             return EINVAL;
1348         if (!(tlp = setp->logs[p2]))
1349             return EBADF;
1350         temp = strlen(tlp->name)+1;
1351         if (temp > p4) return EINVAL;
1352         AFS_COPYOUT(tlp->name, (char *)p3, temp, code);
1353         break;
1354
1355     case ICL_OP_CLRLOG:         /* clear specified log */
1356         /* zero out the specified log: p1=logname */
1357         AFS_COPYINSTR((char *)p1, tname, sizeof (tname), &temp, code);
1358         if (code) return code;
1359         logp = afs_icl_FindLog(tname);
1360         if (!logp) return ENOENT;
1361         code = afs_icl_ZeroLog(logp);
1362         afs_icl_LogRele(logp);
1363         break;
1364
1365     case ICL_OP_CLRSET:         /* clear specified set */
1366         /* zero out the specified set: p1=setname */
1367         AFS_COPYINSTR((char *)p1, tname, sizeof (tname), &temp, code);
1368         if (code) return code;
1369         setp = afs_icl_FindSet(tname);
1370         if (!setp) return ENOENT;
1371         code = afs_icl_ZeroSet(setp);
1372         afs_icl_SetRele(setp);
1373         break;
1374
1375     case ICL_OP_CLRALL:         /* clear all logs */
1376         /* zero out all logs -- no args */
1377         code = 0;
1378         ObtainWriteLock(&afs_icl_lock,178);
1379         for(tlp = afs_icl_allLogs; tlp; tlp=tlp->nextp) {
1380             tlp->refCount++;    /* hold this guy */
1381             ReleaseWriteLock(&afs_icl_lock);
1382             /* don't clear persistent logs */
1383             if ((tlp->states & ICL_LOGF_PERSISTENT) == 0)
1384                 code = afs_icl_ZeroLog(tlp);
1385             ObtainWriteLock(&afs_icl_lock,179);
1386             if (--tlp->refCount == 0)
1387                 afs_icl_ZapLog(tlp);
1388             if (code) break;
1389         }
1390         ReleaseWriteLock(&afs_icl_lock);
1391         break;
1392
1393     case ICL_OP_ENUMSETS:       /* enumerate all sets */
1394         /* enumerate sets: p1=index, p2=&name, p3=sizeof(name), p4=&states.
1395          * return 0 for success, otherwise error.
1396          */
1397         for(setp = afs_icl_allSets; setp; setp = setp->nextp) {
1398             if (p1-- == 0) break;
1399         }
1400         if (!setp) return ENOENT;       /* past the end of file */
1401         temp = strlen(setp->name)+1;
1402         if (temp > p3) return EINVAL;
1403         AFS_COPYOUT(setp->name, (char *)p2, temp, code);
1404         if (!code)      /* copy out size of log */
1405             AFS_COPYOUT((char *)&setp->states,(char *)p4, sizeof (afs_int32), code);
1406         break;
1407
1408     case ICL_OP_SETSTAT:        /* set status on a set */
1409         /* activate the specified set: p1=setname, p2=op */
1410         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1411         if (code) return code;
1412         setp = afs_icl_FindSet(tname);
1413         if (!setp) return ENOENT;
1414         code = afs_icl_SetSetStat(setp, p2);
1415         afs_icl_SetRele(setp);
1416         break;
1417
1418     case ICL_OP_SETSTATALL:     /* set status on all sets */
1419         /* activate the specified set: p1=op */
1420         code = 0;
1421         ObtainWriteLock(&afs_icl_lock,180);
1422         for(setp = afs_icl_allSets; setp; setp=setp->nextp) {
1423             setp->refCount++;   /* hold this guy */
1424             ReleaseWriteLock(&afs_icl_lock);
1425             /* don't set states on persistent sets */
1426             if ((setp->states & ICL_SETF_PERSISTENT) == 0)
1427                 code = afs_icl_SetSetStat(setp, p1);
1428             ObtainWriteLock(&afs_icl_lock,181);
1429             if (--setp->refCount == 0)
1430                 afs_icl_ZapSet(setp);
1431             if (code) break;
1432         }
1433         ReleaseWriteLock(&afs_icl_lock);
1434         break;
1435
1436     case ICL_OP_SETLOGSIZE:             /* set size of log */
1437         /* set the size of the specified log: p1=logname, p2=size (in words) */
1438         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1439         if (code) return code;
1440         logp = afs_icl_FindLog(tname);
1441         if (!logp) return ENOENT;
1442         code = afs_icl_LogSetSize(logp, p2);
1443         afs_icl_LogRele(logp);
1444         break;
1445
1446     case ICL_OP_GETLOGINFO:             /* get size of log */
1447         /* zero out the specified log: p1=logname, p2=&logSize, p3=&allocated */
1448         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1449         if (code) return code;
1450         logp = afs_icl_FindLog(tname);
1451         if (!logp) return ENOENT;
1452         allocated = !!logp->datap;
1453         AFS_COPYOUT((char *)&logp->logSize, (char *) p2, sizeof(afs_int32), code);
1454         if (!code)
1455             AFS_COPYOUT((char *)&allocated, (char *) p3, sizeof(afs_int32), code);
1456         afs_icl_LogRele(logp);
1457         break;
1458
1459     case ICL_OP_GETSETINFO:             /* get state of set */
1460         /* zero out the specified set: p1=setname, p2=&state */
1461         AFS_COPYINSTR((char *)p1, tname, sizeof(tname), &temp, code);
1462         if (code) return code;
1463         setp = afs_icl_FindSet(tname);
1464         if (!setp) return ENOENT;
1465         AFS_COPYOUT((char *)&setp->states, (char *) p2, sizeof(afs_int32), code);
1466         afs_icl_SetRele(setp);
1467         break;
1468
1469     default:
1470         code = EINVAL;
1471     }
1472
1473     return code;
1474 }
1475
1476
1477 afs_lock_t afs_icl_lock;
1478
1479 /* exported routine: a 4 parameter event */
1480 afs_icl_Event4(setp, eventID, lAndT, p1, p2, p3, p4)
1481   register struct afs_icl_set *setp;
1482   afs_int32 eventID;
1483   afs_int32 lAndT;
1484   long p1, p2, p3, p4;
1485 {
1486     register struct afs_icl_log *logp;
1487     afs_int32 mask;
1488     register int i;
1489     register afs_int32 tmask;
1490     int ix;
1491
1492     /* If things aren't init'ed yet (or the set is inactive), don't panic */
1493     if (!ICL_SETACTIVE(setp)) return;
1494
1495     AFS_ASSERT_GLOCK();
1496     mask = lAndT>>24 & 0xff;    /* mask of which logs to log to */
1497     ix = ICL_EVENTBYTE(eventID);
1498     ObtainReadLock(&setp->lock);
1499     if (setp->eventFlags[ix] & ICL_EVENTMASK(eventID)) {
1500         for(i=0, tmask = 1; i<ICL_LOGSPERSET; i++, tmask <<= 1) {
1501             if (mask & tmask) {
1502                 afs_icl_AppendRecord(setp->logs[i], eventID, lAndT & 0xffffff,
1503                               p1, p2, p3, p4);
1504             }
1505             mask &= ~tmask;
1506             if (mask == 0) break;       /* break early */
1507         }
1508     }
1509     ReleaseReadLock(&setp->lock);
1510 }
1511
1512 /* Next 4 routines should be implemented via var-args or something.
1513  * Whole purpose is to avoid compiler warnings about parameter # mismatches.
1514  * Otherwise, could call afs_icl_Event4 directly.
1515  */
1516 afs_icl_Event3(setp, eventID, lAndT, p1, p2, p3)
1517   register struct afs_icl_set *setp;
1518   afs_int32 eventID;
1519   afs_int32 lAndT;
1520   long p1, p2, p3;
1521 {
1522     return afs_icl_Event4(setp, eventID, lAndT, p1, p2, p3, (long)0);
1523 }
1524
1525 afs_icl_Event2(setp, eventID, lAndT, p1, p2)
1526   register struct afs_icl_set *setp;
1527   afs_int32 eventID;
1528   afs_int32 lAndT;
1529   long p1, p2;
1530 {
1531     return afs_icl_Event4(setp, eventID, lAndT, p1, p2, (long)0, (long)0);
1532 }
1533
1534 afs_icl_Event1(setp, eventID, lAndT, p1)
1535   register struct afs_icl_set *setp;
1536   afs_int32 eventID;
1537   afs_int32 lAndT;
1538   long p1;
1539 {
1540     return afs_icl_Event4(setp, eventID, lAndT, p1, (long)0, (long)0, (long)0);
1541 }
1542
1543 afs_icl_Event0(setp, eventID, lAndT)
1544   register struct afs_icl_set *setp;
1545   afs_int32 eventID;
1546   afs_int32 lAndT;
1547 {
1548     return afs_icl_Event4(setp, eventID, lAndT, (long)0, (long)0, (long)0, (long)0);
1549 }
1550
1551 struct afs_icl_log *afs_icl_allLogs = 0;
1552
1553 /* function to purge records from the start of the log, until there
1554  * is at least minSpace long's worth of space available without
1555  * making the head and the tail point to the same word.
1556  *
1557  * Log must be write-locked.
1558  */
1559 static afs_icl_GetLogSpace(logp, minSpace)
1560   register struct afs_icl_log *logp;
1561   afs_int32 minSpace;
1562 {
1563     register unsigned int tsize;
1564
1565     while (logp->logSize - logp->logElements <= minSpace) {
1566         /* eat a record */
1567         tsize = ((logp->datap[logp->firstUsed]) >> 24) & 0xff;
1568         logp->logElements -= tsize;
1569         logp->firstUsed += tsize;
1570         if (logp->firstUsed >= logp->logSize)
1571             logp->firstUsed -= logp->logSize;
1572         logp->baseCookie += tsize;
1573     }
1574 }
1575
1576 /* append string astr to buffer, including terminating null char.
1577  *
1578  * log must be write-locked.
1579  */
1580 #define ICL_CHARSPERLONG        4
1581 static afs_int32 afs_icl_AppendString(logp, astr)
1582   struct afs_icl_log *logp;
1583   char *astr;
1584 {
1585     char *op;           /* ptr to char to write */
1586     int tc;
1587     register int bib;   /* bytes in buffer */
1588
1589     bib = 0;
1590     op = (char *) &(logp->datap[logp->firstFree]);
1591     while (1) {
1592         tc = *astr++;
1593         *op++ = tc;
1594         if (++bib >= ICL_CHARSPERLONG) {
1595             /* new word */
1596             bib = 0;
1597             if (++(logp->firstFree) >= logp->logSize) {
1598                 logp->firstFree = 0;
1599                 op = (char *) &(logp->datap[0]);
1600             }
1601             logp->logElements++;
1602         }
1603         if (tc == 0) break;
1604     }
1605     if (bib > 0) {
1606         /* if we've used this word at all, allocate it */
1607         if (++(logp->firstFree) >= logp->logSize) {
1608             logp->firstFree = 0;
1609         }
1610         logp->logElements++;
1611     }
1612 }
1613
1614 /* add a long to the log, ignoring overflow (checked already) */
1615 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
1616 #define ICL_APPENDINT32(lp, x) \
1617     MACRO_BEGIN \
1618         (lp)->datap[(lp)->firstFree] = (x); \
1619         if (++((lp)->firstFree) >= (lp)->logSize) { \
1620                 (lp)->firstFree = 0; \
1621         } \
1622         (lp)->logElements++; \
1623     MACRO_END
1624
1625 #define ICL_APPENDLONG(lp, x) \
1626     MACRO_BEGIN \
1627         ICL_APPENDINT32((lp), ((x) >> 32) & 0xffffffffL); \
1628         ICL_APPENDINT32((lp), (x) & 0xffffffffL); \
1629     MACRO_END
1630
1631 #else /* AFS_ALPHA_ENV */
1632 #define ICL_APPENDLONG(lp, x) \
1633     MACRO_BEGIN \
1634         (lp)->datap[(lp)->firstFree] = (x); \
1635         if (++((lp)->firstFree) >= (lp)->logSize) { \
1636                 (lp)->firstFree = 0; \
1637         } \
1638         (lp)->logElements++; \
1639     MACRO_END
1640 #define ICL_APPENDINT32(lp, x) ICL_APPENDLONG((lp), (x))
1641 #endif /* AFS_ALPHA_ENV */
1642
1643 /* routine to tell whether we're dealing with the address or the
1644  * object itself
1645  */
1646 afs_icl_UseAddr(type)
1647   int type;
1648 {
1649     if (type == ICL_TYPE_HYPER || type == ICL_TYPE_STRING
1650         || type == ICL_TYPE_FID)
1651         return 1;
1652     else
1653         return 0;
1654 }
1655
1656 /* Function to append a record to the log.  Written for speed
1657  * since we know that we're going to have to make this work fast
1658  * pretty soon, anyway.  The log must be unlocked.
1659  */
1660
1661 afs_icl_AppendRecord(logp, op, types, p1, p2, p3, p4)
1662   register struct afs_icl_log *logp;
1663   afs_int32 op;
1664   afs_int32 types;
1665   long p1, p2, p3, p4;
1666 {
1667     int rsize;                  /* record size in longs */
1668     register int tsize;         /* temp size */
1669     osi_timeval_t tv;
1670     int t1, t2, t3, t4;
1671
1672     t4 = types & 0x3f;          /* decode types */
1673     types >>= 6;
1674     t3 = types & 0x3f;
1675     types >>= 6;
1676     t2 = types & 0x3f;
1677     types >>= 6;
1678     t1 = types & 0x3f;
1679
1680     osi_GetTime(&tv);           /* It panics for solaris if inside */
1681     ObtainWriteLock(&logp->lock,182);
1682     if (!logp->datap) {
1683         ReleaseWriteLock(&logp->lock);
1684         return;
1685     }
1686
1687     /* get timestamp as # of microseconds since some time that doesn't
1688      * change that often.  This algorithm ticks over every 20 minutes
1689      * or so (1000 seconds).  Write a timestamp record if it has.
1690      */
1691     if (tv.tv_sec - logp->lastTS > 1024)
1692     {
1693         /* the timer has wrapped -- write a timestamp record */
1694         if (logp->logSize - logp->logElements <= 5)
1695             afs_icl_GetLogSpace(logp, 5);
1696
1697         ICL_APPENDINT32(logp, (afs_int32)(5<<24) + (ICL_TYPE_UNIXDATE<<18));
1698         ICL_APPENDINT32(logp, (afs_int32)ICL_INFO_TIMESTAMP);
1699         ICL_APPENDINT32(logp, (afs_int32)0); /* use thread ID zero for clocks */
1700         ICL_APPENDINT32(logp,
1701                         (afs_int32)(tv.tv_sec & 0x3ff) * 1000000 + tv.tv_usec);
1702         ICL_APPENDINT32(logp, (afs_int32)tv.tv_sec);
1703
1704         logp->lastTS = tv.tv_sec;
1705     }
1706
1707     rsize = 4;                          /* base case */
1708     if (t1) {
1709         /* compute size of parameter p1.  Only tricky case is string.
1710          * In that case, we have to call strlen to get the string length.
1711          */
1712         ICL_SIZEHACK(t1, p1);
1713     }
1714     if (t2) {
1715         /* compute size of parameter p2.  Only tricky case is string.
1716          * In that case, we have to call strlen to get the string length.
1717          */
1718         ICL_SIZEHACK(t2, p2);
1719     }
1720     if (t3) {
1721         /* compute size of parameter p3.  Only tricky case is string.
1722          * In that case, we have to call strlen to get the string length.
1723          */
1724         ICL_SIZEHACK(t3, p3);
1725     }
1726     if (t4) {
1727         /* compute size of parameter p4.  Only tricky case is string.
1728          * In that case, we have to call strlen to get the string length.
1729          */
1730         ICL_SIZEHACK(t4, p4);
1731     }
1732
1733     /* At this point, we've computed all of the parameter sizes, and
1734      * have in rsize the size of the entire record we want to append.
1735      * Next, we check that we actually have room in the log to do this
1736      * work, and then we do the append.
1737      */
1738     if (rsize > 255) {
1739         ReleaseWriteLock(&logp->lock);
1740         return;         /* log record too big to express */
1741     }
1742
1743     if (logp->logSize - logp->logElements <= rsize)
1744         afs_icl_GetLogSpace(logp, rsize);
1745
1746     ICL_APPENDINT32(logp,
1747                     (afs_int32)(rsize<<24) + (t1<<18) + (t2<<12) + (t3<<6) + t4);
1748     ICL_APPENDINT32(logp, (afs_int32)op);
1749     ICL_APPENDINT32(logp, (afs_int32)osi_ThreadUnique());
1750     ICL_APPENDINT32(logp, (afs_int32)(tv.tv_sec & 0x3ff) * 1000000 + tv.tv_usec);
1751
1752     if (t1) {
1753         /* marshall parameter 1 now */
1754         if (t1 == ICL_TYPE_STRING) {
1755             afs_icl_AppendString(logp, (char *) p1);
1756         }
1757         else if (t1 == ICL_TYPE_HYPER) {
1758             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p1)->high);
1759             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p1)->low);
1760         }
1761         else if (t1 == ICL_TYPE_FID) {
1762             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[0]);
1763             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[1]);
1764             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[2]);
1765             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p1)[3]);
1766         }
1767 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
1768         else if (t1 == ICL_TYPE_INT32)
1769             ICL_APPENDINT32(logp, (afs_int32)p1);
1770 #endif /* AFS_ALPHA_ENV */
1771         else ICL_APPENDLONG(logp, p1); 
1772     }
1773     if (t2) {
1774         /* marshall parameter 2 now */
1775         if (t2 == ICL_TYPE_STRING) afs_icl_AppendString(logp, (char *) p2);
1776         else if (t2 == ICL_TYPE_HYPER) {
1777             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p2)->high);
1778             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p2)->low);
1779         }
1780         else if (t2 == ICL_TYPE_FID) {
1781             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[0]);
1782             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[1]);
1783             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[2]);
1784             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p2)[3]);
1785         }
1786 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
1787         else if (t2 == ICL_TYPE_INT32)
1788             ICL_APPENDINT32(logp, (afs_int32)p2);
1789 #endif /* AFS_ALPHA_ENV */
1790         else ICL_APPENDLONG(logp, p2);
1791     }
1792     if (t3) {
1793         /* marshall parameter 3 now */
1794         if (t3 == ICL_TYPE_STRING) afs_icl_AppendString(logp, (char *) p3);
1795         else if (t3 == ICL_TYPE_HYPER) {
1796             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p3)->high);
1797             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p3)->low);
1798         }
1799         else if (t3 == ICL_TYPE_FID) {
1800             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[0]);
1801             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[1]);
1802             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[2]);
1803             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p3)[3]);
1804         }
1805 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
1806         else if (t3 == ICL_TYPE_INT32)
1807             ICL_APPENDINT32(logp, (afs_int32)p3);
1808 #endif /* AFS_ALPHA_ENV */
1809         else ICL_APPENDLONG(logp, p3);
1810     }
1811     if (t4) {
1812         /* marshall parameter 4 now */
1813         if (t4 == ICL_TYPE_STRING) afs_icl_AppendString(logp, (char *) p4);
1814         else if (t4 == ICL_TYPE_HYPER) {
1815             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p4)->high);
1816             ICL_APPENDINT32(logp, (afs_int32)((struct afs_hyper_t *)p4)->low);
1817         }
1818         else if (t4 == ICL_TYPE_FID) {
1819             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[0]);
1820             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[1]);
1821             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[2]);
1822             ICL_APPENDINT32(logp, (afs_int32)((afs_int32 *)p4)[3]);
1823         }
1824 #if defined(AFS_ALPHA_ENV) || (defined(AFS_SGI61_ENV) && (_MIPS_SZLONG==64))
1825         else if (t4 == ICL_TYPE_INT32)
1826             ICL_APPENDINT32(logp, (afs_int32)p4);
1827 #endif /* AFS_ALPHA_ENV */
1828         else ICL_APPENDLONG(logp, p4);
1829     }
1830     ReleaseWriteLock(&logp->lock);
1831 }
1832
1833 /* create a log with size logSize; return it in *outLogpp and tag
1834  * it with name "name."
1835  */
1836 afs_icl_CreateLog(name, logSize, outLogpp)
1837   char *name;
1838   afs_int32 logSize;
1839   struct afs_icl_log **outLogpp;
1840 {
1841     return afs_icl_CreateLogWithFlags(name, logSize, /*flags*/0, outLogpp);
1842 }
1843
1844 /* create a log with size logSize; return it in *outLogpp and tag
1845  * it with name "name."  'flags' can be set to make the log unclearable.
1846  */
1847 afs_icl_CreateLogWithFlags(name, logSize, flags, outLogpp)
1848   char *name;
1849   afs_int32 logSize;
1850   afs_uint32 flags;
1851   struct afs_icl_log **outLogpp;
1852 {
1853     register struct afs_icl_log *logp;
1854
1855     /* add into global list under lock */
1856     ObtainWriteLock(&afs_icl_lock,183);
1857     if (!afs_icl_inited) afs_icl_Init();
1858
1859     for (logp = afs_icl_allLogs; logp; logp=logp->nextp) {
1860         if (strcmp(logp->name, name) == 0) {
1861             /* found it already created, just return it */
1862             logp->refCount++;
1863             *outLogpp = logp;
1864             if (flags & ICL_CRLOG_FLAG_PERSISTENT)
1865             {
1866                 ObtainWriteLock(&logp->lock,184);
1867                 logp->states |= ICL_LOGF_PERSISTENT;
1868                 ReleaseWriteLock(&logp->lock);
1869             }
1870             ReleaseWriteLock(&afs_icl_lock);
1871             return 0;
1872         }
1873     }
1874     
1875     logp = (struct afs_icl_log *)
1876         osi_AllocSmallSpace(sizeof(struct afs_icl_log));
1877     bzero((caddr_t)logp, sizeof(*logp));
1878
1879     logp->refCount = 1;
1880     logp->name = osi_AllocSmallSpace(strlen(name)+1);
1881     strcpy(logp->name, name);
1882     LOCK_INIT(&logp->lock, "logp lock");
1883     logp->logSize = logSize;
1884     logp->datap = (afs_int32 *)0;       /* don't allocate it until we need it */
1885
1886     if (flags & ICL_CRLOG_FLAG_PERSISTENT)
1887         logp->states |= ICL_LOGF_PERSISTENT;
1888
1889     logp->nextp = afs_icl_allLogs;
1890     afs_icl_allLogs = logp;
1891     ReleaseWriteLock(&afs_icl_lock);
1892
1893     *outLogpp = logp;
1894     return 0;
1895 }
1896
1897 /* called with a log, a pointer to a buffer, the size of the buffer
1898  * (in *bufSizep), the starting cookie (in *cookiep, use 0 at the start)
1899  * and returns data in the provided buffer, and returns output flags
1900  * in *flagsp.  The flag ICL_COPYOUTF_MISSEDSOME is set if we can't
1901  * find the record with cookie value cookie.
1902  */
1903 afs_icl_CopyOut(logp, bufferp, bufSizep, cookiep, flagsp)
1904   register struct afs_icl_log *logp;
1905   afs_int32 *bufferp;
1906   afs_int32 *bufSizep;
1907   afs_uint32 *cookiep;
1908   afs_int32 *flagsp;
1909 {
1910     afs_int32 nwords;           /* number of words to copy out */
1911     afs_uint32 startCookie;     /* first cookie to use */
1912     register afs_int32 i;
1913     afs_int32 outWords;         /* words we've copied out */
1914     afs_int32 inWords;          /* max words to copy out */
1915     afs_int32 code;                     /* return code */
1916     afs_int32 ix;                       /* index we're copying from */
1917     afs_int32 outFlags;         /* return flags */
1918     afs_int32 inFlags;          /* flags passed in */
1919     afs_int32 end;
1920
1921     inWords = *bufSizep;        /* max to copy out */
1922     outWords = 0;               /* amount copied out */
1923     startCookie = *cookiep;
1924     outFlags = 0;
1925     inFlags = *flagsp;
1926     code = 0;
1927
1928     ObtainWriteLock(&logp->lock,185);
1929     if (!logp->datap) {
1930         ReleaseWriteLock(&logp->lock);
1931         goto done;
1932     }
1933
1934     /* first, compute the index of the start cookie we've been passed */
1935     while (1) {
1936         /* (re-)compute where we should start */
1937         if (startCookie < logp->baseCookie) {
1938             if (startCookie)  /* missed some output */
1939                 outFlags |= ICL_COPYOUTF_MISSEDSOME;
1940             /* skip to the first available record */
1941             startCookie = logp->baseCookie;
1942             *cookiep = startCookie;
1943         }
1944
1945         /* compute where we find the first element to copy out */
1946         ix = logp->firstUsed + startCookie - logp->baseCookie;
1947         if (ix >= logp->logSize) ix -= logp->logSize;
1948
1949         /* if have some data now, break out and process it */
1950         if (startCookie - logp->baseCookie < logp->logElements) break;
1951
1952         /* At end of log, so clear it if we need to */
1953         if (inFlags & ICL_COPYOUTF_CLRAFTERREAD)
1954         {
1955             logp->firstUsed = logp->firstFree = 0;
1956             logp->logElements = 0;
1957         }
1958         /* otherwise, either wait for the data to arrive, or return */
1959         if (!(inFlags & ICL_COPYOUTF_WAITIO)) {
1960             ReleaseWriteLock(&logp->lock);
1961             code = 0;
1962             goto done;
1963         }
1964         logp->states |= ICL_LOGF_WAITING;
1965         ReleaseWriteLock(&logp->lock);
1966         afs_osi_Sleep(&logp->lock);
1967         ObtainWriteLock(&logp->lock,186);
1968     }
1969     /* copy out data from ix to logSize or firstFree, depending
1970      * upon whether firstUsed <= firstFree (no wrap) or otherwise.
1971      * be careful not to copy out more than nwords.
1972      */
1973     if (ix >= logp->firstUsed) {
1974         if (logp->firstUsed <= logp->firstFree)
1975             /* no wrapping */
1976             end = logp->firstFree;      /* first element not to copy */
1977         else
1978             end = logp->logSize;
1979         nwords = inWords;       /* don't copy more than this */
1980         if (end - ix < nwords)
1981             nwords = end - ix;
1982         if (nwords > 0) {
1983             bcopy((char *) &logp->datap[ix], (char *) bufferp,
1984                   sizeof(afs_int32) * nwords);
1985             outWords += nwords;
1986             inWords -= nwords;
1987             bufferp += nwords;
1988         }
1989         /* if we're going to copy more out below, we'll start here */
1990         ix = 0;
1991     }
1992     /* now, if active part of the log has wrapped, there's more stuff
1993      * starting at the head of the log.  Copy out more from there.
1994      */
1995     if (logp->firstUsed > logp->firstFree
1996         && ix < logp->firstFree && inWords > 0) {
1997         /* (more to) copy out from the wrapped section at the
1998          * start of the log.  May get here even if didn't copy any
1999          * above, if the cookie points directly into the wrapped section.
2000          */
2001         nwords = inWords;
2002         if (logp->firstFree - ix < nwords)
2003             nwords = logp->firstFree - ix;
2004         bcopy((char *) &logp->datap[ix], (char *) bufferp,
2005               sizeof(afs_int32) * nwords);
2006         outWords += nwords;
2007         inWords -= nwords;
2008         bufferp += nwords;
2009     }
2010
2011     ReleaseWriteLock(&logp->lock);
2012
2013   done:
2014     if (code == 0) {
2015         *bufSizep = outWords;
2016         *flagsp = outFlags;
2017     }
2018     return code;
2019 }
2020
2021 /* return basic parameter information about a log */
2022 afs_icl_GetLogParms(logp, maxSizep, curSizep)
2023   struct afs_icl_log *logp;
2024   afs_int32 *maxSizep;
2025   afs_int32 *curSizep;
2026 {
2027     ObtainReadLock(&logp->lock);
2028     *maxSizep = logp->logSize;
2029     *curSizep = logp->logElements;
2030     ReleaseReadLock(&logp->lock);
2031     return 0;
2032 }
2033
2034
2035 /* hold and release logs */
2036 afs_icl_LogHold(logp)
2037   register struct afs_icl_log *logp;
2038 {
2039     ObtainWriteLock(&afs_icl_lock,187);
2040     logp->refCount++;
2041     ReleaseWriteLock(&afs_icl_lock);
2042     return 0;
2043 }
2044
2045 /* hold and release logs, called with lock already held */
2046 afs_icl_LogHoldNL(logp)
2047   register struct afs_icl_log *logp;
2048 {
2049     logp->refCount++;
2050     return 0;
2051 }
2052
2053 /* keep track of how many sets believe the log itself is allocated */
2054 afs_icl_LogUse(logp)
2055   register struct afs_icl_log *logp;
2056 {
2057     ObtainWriteLock(&logp->lock,188);
2058     if (logp->setCount == 0) {
2059         /* this is the first set actually using the log -- allocate it */
2060         if (logp->logSize == 0) {
2061             /* we weren't passed in a hint and it wasn't set */
2062             logp->logSize = ICL_DEFAULT_LOGSIZE;
2063         }
2064         logp->datap = (afs_int32 *) afs_osi_Alloc(sizeof(afs_int32) * logp->logSize);
2065 #ifdef  AFS_AIX32_ENV
2066         pin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
2067 #endif
2068     }
2069     logp->setCount++;
2070     ReleaseWriteLock(&logp->lock);
2071     return 0;
2072 }
2073
2074 /* decrement the number of real users of the log, free if possible */
2075 afs_icl_LogFreeUse(logp)
2076   register struct afs_icl_log *logp;
2077 {
2078     ObtainWriteLock(&logp->lock,189);
2079     if (--logp->setCount == 0) {
2080         /* no more users -- free it (but keep log structure around)*/
2081         afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
2082 #ifdef  AFS_AIX32_ENV
2083         unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
2084 #endif
2085         logp->firstUsed = logp->firstFree = 0;
2086         logp->logElements = 0;
2087         logp->datap = (afs_int32 *)0;
2088     }
2089     ReleaseWriteLock(&logp->lock);
2090     return 0;
2091 }
2092
2093 /* set the size of the log to 'logSize' */
2094 afs_icl_LogSetSize(logp, logSize)
2095   register struct afs_icl_log *logp;
2096   afs_int32 logSize;
2097 {  
2098     ObtainWriteLock(&logp->lock,190);
2099     if (!logp->datap) {
2100         /* nothing to worry about since it's not allocated */
2101         logp->logSize = logSize;
2102     }
2103     else {
2104         /* reset log */
2105         logp->firstUsed = logp->firstFree = 0;
2106         logp->logElements = 0;
2107
2108         /* free and allocate a new one */
2109         afs_osi_Free(logp->datap, sizeof(afs_int32) * logp->logSize);
2110 #ifdef  AFS_AIX32_ENV
2111         unpin((char *)logp->datap, sizeof(afs_int32) * logp->logSize);
2112 #endif
2113         logp->datap = (afs_int32 *) afs_osi_Alloc(sizeof(afs_int32) * logSize);
2114 #ifdef  AFS_AIX32_ENV
2115         pin((char *)logp->datap, sizeof(afs_int32) * logSize);
2116 #endif
2117         logp->logSize = logSize;
2118     }
2119     ReleaseWriteLock(&logp->lock);
2120
2121     return 0;
2122 }
2123
2124 /* free a log.  Called with afs_icl_lock locked. */
2125 afs_icl_ZapLog(logp)
2126   register struct afs_icl_log *logp;
2127 {
2128     register struct afs_icl_log **lpp, *tp;
2129
2130     for(lpp = &afs_icl_allLogs, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
2131         if (tp == logp) {
2132             /* found the dude we want to remove */
2133             *lpp = logp->nextp;
2134             osi_FreeSmallSpace(logp->name);
2135             osi_FreeSmallSpace(logp->datap);
2136             osi_FreeSmallSpace(logp);
2137             break;      /* won't find it twice */
2138         }
2139     }
2140     return 0;
2141 }
2142
2143 /* do the release, watching for deleted entries */
2144 afs_icl_LogRele(logp)
2145   register struct afs_icl_log *logp;
2146 {
2147     ObtainWriteLock(&afs_icl_lock,191);
2148     if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
2149         afs_icl_ZapLog(logp);   /* destroys logp's lock! */
2150     }
2151     ReleaseWriteLock(&afs_icl_lock);
2152     return 0;
2153 }
2154
2155 /* do the release, watching for deleted entries, log already held */
2156 afs_icl_LogReleNL(logp)
2157   register struct afs_icl_log *logp;
2158 {
2159     if (--logp->refCount == 0 && (logp->states & ICL_LOGF_DELETED)) {
2160         afs_icl_ZapLog(logp);   /* destroys logp's lock! */
2161     }
2162     return 0;
2163 }
2164
2165 /* zero out the log */
2166 afs_icl_ZeroLog(logp)
2167   register struct afs_icl_log *logp;
2168 {
2169     ObtainWriteLock(&logp->lock,192);
2170     logp->firstUsed = logp->firstFree = 0;
2171     logp->logElements = 0;
2172     logp->baseCookie = 0;
2173     ReleaseWriteLock(&logp->lock);
2174     return 0;
2175 }
2176
2177 /* free a log entry, and drop its reference count */
2178 afs_icl_LogFree(logp)
2179   register struct afs_icl_log *logp;
2180 {
2181     ObtainWriteLock(&logp->lock,193);
2182     logp->states |= ICL_LOGF_DELETED;
2183     ReleaseWriteLock(&logp->lock);
2184     afs_icl_LogRele(logp);
2185     return 0;
2186 }
2187
2188 /* find a log by name, returning it held */
2189 struct afs_icl_log *afs_icl_FindLog(name)
2190   char *name;
2191 {
2192     register struct afs_icl_log *tp;
2193     ObtainWriteLock(&afs_icl_lock,194);
2194     for(tp = afs_icl_allLogs; tp; tp=tp->nextp) {
2195         if (strcmp(tp->name, name) == 0) {
2196             /* this is the dude we want */
2197             tp->refCount++;
2198             break;
2199         }
2200     }
2201     ReleaseWriteLock(&afs_icl_lock);
2202     return tp;
2203 }
2204
2205 afs_icl_EnumerateLogs(aproc, arock)
2206   int (*aproc)();
2207   char *arock;
2208 {
2209     register struct afs_icl_log *tp;
2210     register afs_int32 code;
2211
2212     code = 0;
2213     ObtainWriteLock(&afs_icl_lock,195);
2214     for(tp = afs_icl_allLogs; tp; tp=tp->nextp) {
2215         tp->refCount++; /* hold this guy */
2216         ReleaseWriteLock(&afs_icl_lock);
2217         ObtainReadLock(&tp->lock);
2218         code = (*aproc)(tp->name, arock, tp);
2219         ReleaseReadLock(&tp->lock);
2220         ObtainWriteLock(&afs_icl_lock,196);
2221         if (--tp->refCount == 0)
2222             afs_icl_ZapLog(tp);
2223         if (code) break;
2224     }
2225     ReleaseWriteLock(&afs_icl_lock);
2226     return code;
2227 }
2228
2229 struct afs_icl_set *afs_icl_allSets = 0;
2230
2231 afs_icl_CreateSet(name, baseLogp, fatalLogp, outSetpp)
2232   char *name;
2233   struct afs_icl_log *baseLogp;
2234   struct afs_icl_log *fatalLogp;
2235   struct afs_icl_set **outSetpp;
2236 {
2237     return afs_icl_CreateSetWithFlags(name, baseLogp, fatalLogp,
2238                                       /*flags*/0, outSetpp);
2239 }
2240
2241 /* create a set, given pointers to base and fatal logs, if any.
2242  * Logs are unlocked, but referenced, and *outSetpp is returned
2243  * referenced.  Function bumps reference count on logs, since it
2244  * addds references from the new afs_icl_set.  When the set is destroyed,
2245  * those references will be released.
2246  */
2247 afs_icl_CreateSetWithFlags(name, baseLogp, fatalLogp, flags, outSetpp)
2248   char *name;
2249   struct afs_icl_log *baseLogp;
2250   struct afs_icl_log *fatalLogp;
2251   afs_uint32  flags;
2252   struct afs_icl_set **outSetpp;
2253 {
2254     register struct afs_icl_set *setp;
2255     register int i;
2256     afs_int32 states = ICL_DEFAULT_SET_STATES;
2257
2258     ObtainWriteLock(&afs_icl_lock,197);
2259     if (!afs_icl_inited) afs_icl_Init();
2260
2261     for (setp = afs_icl_allSets; setp; setp = setp->nextp) {
2262         if (strcmp(setp->name, name) == 0) {
2263             setp->refCount++;
2264             *outSetpp = setp;
2265             if (flags & ICL_CRSET_FLAG_PERSISTENT)
2266             {
2267                 ObtainWriteLock(&setp->lock,198);
2268                 setp->states |= ICL_SETF_PERSISTENT;
2269                 ReleaseWriteLock(&setp->lock);
2270             }
2271             ReleaseWriteLock(&afs_icl_lock);
2272             return 0;
2273         }
2274     }
2275     
2276     /* determine initial state */
2277     if (flags & ICL_CRSET_FLAG_DEFAULT_ON)
2278         states = ICL_SETF_ACTIVE;
2279     else if (flags & ICL_CRSET_FLAG_DEFAULT_OFF)
2280         states = ICL_SETF_FREED;
2281     if (flags & ICL_CRSET_FLAG_PERSISTENT)
2282         states |= ICL_SETF_PERSISTENT;
2283
2284     setp = (struct afs_icl_set *) afs_osi_Alloc(sizeof(struct afs_icl_set));
2285     bzero((caddr_t)setp, sizeof(*setp));
2286     setp->refCount = 1;
2287     if (states & ICL_SETF_FREED)
2288         states &= ~ICL_SETF_ACTIVE;     /* if freed, can't be active */
2289     setp->states = states;
2290
2291     LOCK_INIT(&setp->lock, "setp lock");
2292     /* next lock is obtained in wrong order, hierarchy-wise, but
2293      * it doesn't matter, since no one can find this lock yet, since
2294      * the afs_icl_lock is still held, and thus the obtain can't block.
2295      */
2296     ObtainWriteLock(&setp->lock,199);
2297     setp->name = osi_AllocSmallSpace(strlen(name)+1);
2298     strcpy(setp->name, name);
2299     setp->nevents = ICL_DEFAULTEVENTS;
2300     setp->eventFlags = afs_osi_Alloc(ICL_DEFAULTEVENTS);
2301 #ifdef  AFS_AIX32_ENV
2302     pin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
2303 #endif
2304     for(i=0; i<ICL_DEFAULTEVENTS; i++)
2305         setp->eventFlags[i] = 0xff;     /* default to enabled */
2306
2307     /* update this global info under the afs_icl_lock */
2308     setp->nextp = afs_icl_allSets;
2309     afs_icl_allSets = setp;
2310     ReleaseWriteLock(&afs_icl_lock);
2311
2312     /* set's basic lock is still held, so we can finish init */
2313     if (baseLogp) {
2314         setp->logs[0] = baseLogp;
2315         afs_icl_LogHold(baseLogp);
2316         if (!(setp->states & ICL_SETF_FREED))
2317             afs_icl_LogUse(baseLogp);   /* log is actually being used */
2318     }
2319     if (fatalLogp) {
2320         setp->logs[1] = fatalLogp;
2321         afs_icl_LogHold(fatalLogp);
2322         if (!(setp->states & ICL_SETF_FREED))
2323             afs_icl_LogUse(fatalLogp);  /* log is actually being used */
2324     }
2325     ReleaseWriteLock(&setp->lock);
2326
2327     *outSetpp = setp;
2328     return 0;
2329 }
2330
2331 /* function to change event enabling information for a particular set */
2332 afs_icl_SetEnable(setp, eventID, setValue)
2333   struct afs_icl_set *setp;
2334   afs_int32 eventID;
2335   int setValue;
2336 {
2337     char *tp;
2338
2339     ObtainWriteLock(&setp->lock,200);
2340     if (!ICL_EVENTOK(setp, eventID)) {
2341         ReleaseWriteLock(&setp->lock);
2342         return -1;
2343     }
2344     tp = &setp->eventFlags[ICL_EVENTBYTE(eventID)];
2345     if (setValue)
2346         *tp |= ICL_EVENTMASK(eventID);
2347     else
2348         *tp &= ~(ICL_EVENTMASK(eventID));
2349     ReleaseWriteLock(&setp->lock);
2350     return 0;
2351 }
2352
2353 /* return indication of whether a particular event ID is enabled
2354  * for tracing.  If *getValuep is set to 0, the event is disabled,
2355  * otherwise it is enabled.  All events start out enabled by default.
2356  */
2357 afs_icl_GetEnable(setp, eventID, getValuep)
2358   struct afs_icl_set *setp;
2359   afs_int32 eventID;
2360   int *getValuep;
2361 {
2362     ObtainReadLock(&setp->lock);
2363     if (!ICL_EVENTOK(setp, eventID)) {
2364         ReleaseWriteLock(&setp->lock);
2365         return -1;
2366     }
2367     if (setp->eventFlags[ICL_EVENTBYTE(eventID)] & ICL_EVENTMASK(eventID))
2368         *getValuep = 1;
2369     else
2370         *getValuep = 0;
2371     ReleaseReadLock(&setp->lock);
2372     return 0;
2373 }
2374
2375 /* hold and release event sets */
2376 afs_icl_SetHold(setp)
2377   register struct afs_icl_set *setp;
2378 {
2379     ObtainWriteLock(&afs_icl_lock,201);
2380     setp->refCount++;
2381     ReleaseWriteLock(&afs_icl_lock);
2382     return 0;
2383 }
2384
2385 /* free a set.  Called with afs_icl_lock locked */
2386 afs_icl_ZapSet(setp)
2387   register struct afs_icl_set *setp;
2388 {
2389     register struct afs_icl_set **lpp, *tp;
2390     int i;
2391     register struct afs_icl_log *tlp;
2392
2393     for(lpp = &afs_icl_allSets, tp = *lpp; tp; lpp = &tp->nextp, tp = *lpp) {
2394         if (tp == setp) {
2395             /* found the dude we want to remove */
2396             *lpp = setp->nextp;
2397             osi_FreeSmallSpace(setp->name);
2398             afs_osi_Free(setp->eventFlags, ICL_DEFAULTEVENTS);
2399 #ifdef  AFS_AIX32_ENV
2400             unpin((char *)setp->eventFlags, ICL_DEFAULTEVENTS);
2401 #endif
2402             for(i=0; i < ICL_LOGSPERSET; i++) {
2403                 if (tlp = setp->logs[i])
2404                     afs_icl_LogReleNL(tlp);
2405             }
2406             osi_FreeSmallSpace(setp);
2407             break;      /* won't find it twice */
2408         }
2409     }
2410     return 0;
2411 }
2412
2413 /* do the release, watching for deleted entries */
2414 afs_icl_SetRele(setp)
2415   register struct afs_icl_set *setp;
2416 {
2417     ObtainWriteLock(&afs_icl_lock,202);
2418     if (--setp->refCount == 0 && (setp->states & ICL_SETF_DELETED)) {
2419         afs_icl_ZapSet(setp);   /* destroys setp's lock! */
2420     }
2421     ReleaseWriteLock(&afs_icl_lock);
2422     return 0;
2423 }
2424
2425 /* free a set entry, dropping its reference count */
2426 afs_icl_SetFree(setp)
2427   register struct afs_icl_set *setp;
2428 {
2429     ObtainWriteLock(&setp->lock,203);
2430     setp->states |= ICL_SETF_DELETED;
2431     ReleaseWriteLock(&setp->lock);
2432     afs_icl_SetRele(setp);
2433     return 0;
2434 }
2435
2436 /* find a set by name, returning it held */
2437 struct afs_icl_set *afs_icl_FindSet(name)
2438   char *name;
2439 {
2440     register struct afs_icl_set *tp;
2441     ObtainWriteLock(&afs_icl_lock,204);
2442     for(tp = afs_icl_allSets; tp; tp=tp->nextp) {
2443         if (strcmp(tp->name, name) == 0) {
2444             /* this is the dude we want */
2445             tp->refCount++;
2446             break;
2447         }
2448     }
2449     ReleaseWriteLock(&afs_icl_lock);
2450     return tp;
2451 }
2452
2453 /* zero out all the logs in the set */
2454 afs_icl_ZeroSet(setp)
2455    struct afs_icl_set *setp;
2456 {
2457     register int i;
2458     int code = 0;
2459     int tcode;
2460     struct afs_icl_log *logp;
2461     
2462     ObtainReadLock(&setp->lock);
2463     for(i = 0; i < ICL_LOGSPERSET; i++) {
2464         logp = setp->logs[i];
2465         if (logp) {
2466             afs_icl_LogHold(logp);
2467             tcode = afs_icl_ZeroLog(logp);
2468             if (tcode != 0) code = tcode;       /* save the last bad one */
2469             afs_icl_LogRele(logp);
2470         }
2471     }
2472     ReleaseReadLock(&setp->lock);
2473     return code;
2474 }
2475
2476 afs_icl_EnumerateSets(aproc, arock)
2477   int (*aproc)();
2478   char *arock;
2479 {
2480     register struct afs_icl_set *tp, *np;
2481     register afs_int32 code;
2482
2483     code = 0;
2484     ObtainWriteLock(&afs_icl_lock,205);
2485     for(tp = afs_icl_allSets; tp; tp=np) {
2486         tp->refCount++; /* hold this guy */
2487         ReleaseWriteLock(&afs_icl_lock);
2488         code = (*aproc)(tp->name, arock, tp);
2489         ObtainWriteLock(&afs_icl_lock,206);
2490         np = tp->nextp; /* tp may disappear next, but not np */
2491         if (--tp->refCount == 0 && (tp->states & ICL_SETF_DELETED))
2492             afs_icl_ZapSet(tp);
2493         if (code) break;
2494     }
2495     ReleaseWriteLock(&afs_icl_lock);
2496     return code;
2497 }
2498
2499 afs_icl_AddLogToSet(setp, newlogp)
2500   struct afs_icl_set *setp;
2501   struct afs_icl_log *newlogp;
2502 {
2503     register int i;
2504     int code = -1;
2505     struct afs_icl_log *logp;
2506     
2507     ObtainWriteLock(&setp->lock,207);
2508     for(i = 0; i < ICL_LOGSPERSET; i++) {
2509         if (!setp->logs[i]) {
2510             setp->logs[i] = newlogp;
2511             code = i;
2512             afs_icl_LogHold(newlogp);
2513             if (!(setp->states & ICL_SETF_FREED)) {
2514                 /* bump up the number of sets using the log */
2515                 afs_icl_LogUse(newlogp);
2516             }
2517             break;
2518         }
2519     }
2520     ReleaseWriteLock(&setp->lock);
2521     return code;
2522 }
2523
2524 afs_icl_SetSetStat(setp, op)
2525   struct afs_icl_set *setp;
2526   int op;
2527 {
2528     int i;
2529     afs_int32 code;
2530     struct afs_icl_log *logp;
2531
2532     ObtainWriteLock(&setp->lock,208);
2533     switch(op) {
2534     case ICL_OP_SS_ACTIVATE:    /* activate a log */
2535         /*
2536          * If we are not already active, see if we have released
2537          * our demand that the log be allocated (FREED set).  If
2538          * we have, reassert our desire.
2539          */
2540         if (!(setp->states & ICL_SETF_ACTIVE)) {
2541             if (setp->states & ICL_SETF_FREED) {
2542                 /* have to reassert desire for logs */
2543                 for(i = 0; i < ICL_LOGSPERSET; i++) {
2544                     logp = setp->logs[i];
2545                     if (logp) {
2546                         afs_icl_LogHold(logp);
2547                         afs_icl_LogUse(logp);
2548                         afs_icl_LogRele(logp);
2549                     }
2550                 }
2551                 setp->states &= ~ICL_SETF_FREED;
2552             }
2553             setp->states |= ICL_SETF_ACTIVE;
2554         }
2555         code = 0;
2556         break;
2557
2558     case ICL_OP_SS_DEACTIVATE:  /* deactivate a log */
2559         /* this doesn't require anything beyond clearing the ACTIVE flag */
2560         setp->states &= ~ICL_SETF_ACTIVE;
2561         code = 0;
2562         break;
2563
2564     case ICL_OP_SS_FREE:        /* deassert design for log */
2565         /* 
2566          * if we are already in this state, do nothing; otherwise
2567          * deassert desire for log
2568          */
2569         if (setp->states & ICL_SETF_ACTIVE)
2570             code = EINVAL;
2571         else {
2572             if (!(setp->states & ICL_SETF_FREED)) {
2573                 for(i = 0; i < ICL_LOGSPERSET; i++) {
2574                     logp = setp->logs[i];
2575                     if (logp) {
2576                         afs_icl_LogHold(logp);
2577                         afs_icl_LogFreeUse(logp);
2578                         afs_icl_LogRele(logp);
2579                     }
2580                 }
2581                 setp->states |= ICL_SETF_FREED;
2582             }
2583             code = 0;
2584         }
2585         break;
2586
2587     default:
2588         code = EINVAL;
2589     }
2590     ReleaseWriteLock(&setp->lock);
2591     return code;
2592 }
2593