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