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