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