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