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