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