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