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