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