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