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