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