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