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