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