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