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