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