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