abstract rx if structure access
[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
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afsincludes.h"        /* Afs-based standard headers */
16 #include "afs/afs_stats.h"
17 #include "rx/rx_globals.h"
18 #if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
19 #include "net/if.h"
20 #ifdef AFS_SGI62_ENV
21 #include "h/hashing.h"
22 #endif
23 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_DARWIN60_ENV)
24 #include "netinet/in_var.h"
25 #endif
26 #endif /* !defined(UKERNEL) */
27 #ifdef AFS_LINUX22_ENV
28 #include "h/smp_lock.h"
29 #endif
30 #ifdef AFS_SUN510_ENV
31 #include "h/ksynch.h"
32 #include "h/sunddi.h"
33 #endif
34
35 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_HPUX_ENV)
36 #define AFS_MINBUFFERS  100
37 #else
38 #define AFS_MINBUFFERS  50
39 #endif
40
41 struct afsop_cell {
42     afs_int32 hosts[AFS_MAXCELLHOSTS];
43     char cellName[100];
44 };
45
46 char afs_zeros[AFS_ZEROS];
47 char afs_rootVolumeName[64] = "";
48 afs_uint32 rx_bindhost;
49
50 #ifdef AFS_SUN510_ENV
51 ddi_taskq_t *afs_taskq;
52 krwlock_t afsifinfo_lock;
53 #endif
54
55 afs_int32 afs_initState = 0;
56 afs_int32 afs_termState = 0;
57 afs_int32 afs_setTime = 0;
58 int afs_cold_shutdown = 0;
59 char afs_SynchronousCloses = '\0';
60 static int afs_CB_Running = 0;
61 static int AFS_Running = 0;
62 static int afs_CacheInit_Done = 0;
63 static int afs_Go_Done = 0;
64 extern struct interfaceAddr afs_cb_interface;
65 static int afs_RX_Running = 0;
66 static int afs_InitSetup_done = 0;
67 afs_int32 afs_numcachefiles = -1;
68 afs_int32 afs_numfilesperdir = -1;
69 char afs_cachebasedir[1024];
70
71 afs_int32 afs_rx_deadtime = AFS_RXDEADTIME;
72 afs_int32 afs_rx_harddead = AFS_HARDDEADTIME;
73 afs_int32 afs_rx_idledead = AFS_IDLEDEADTIME;
74
75 static int afscall_set_rxpck_received = 0;
76
77 #if defined(AFS_HPUX_ENV)
78 extern int afs_vfs_mount();
79 #endif /* defined(AFS_HPUX_ENV) */
80
81 /* This is code which needs to be called once when the first daemon enters
82  * the client. A non-zero return means an error and AFS should not start.
83  */
84 static int
85 afs_InitSetup(int preallocs)
86 {
87     int code;
88
89     if (afs_InitSetup_done)
90         return EAGAIN;
91
92 #ifdef AFS_SUN510_ENV
93     /* Initialize a RW lock for the ifinfo global array */
94     rw_init(&afsifinfo_lock, NULL, RW_DRIVER, NULL);
95
96     /* Create a taskq */
97     afs_taskq = ddi_taskq_create(NULL, "afs_taskq", 2, TASKQ_DEFAULTPRI, 0);
98
99     osi_StartNetIfPoller();
100 #endif
101
102 #ifndef AFS_NOSTATS
103     /*
104      * Set up all the AFS statistics variables.  This should be done
105      * exactly once, and it should be done here, the first resource-setting
106      * routine to be called by the CM/RX.
107      */
108     afs_InitStats();
109 #endif /* AFS_NOSTATS */
110
111     memset(afs_zeros, 0, AFS_ZEROS);
112
113     /* start RX */
114     if(!afscall_set_rxpck_received)
115     rx_extraPackets = AFS_NRXPACKETS;   /* smaller # of packets */
116     code = rx_InitHost(rx_bindhost, htons(7001));
117     if (code) {
118         printf("AFS: RX failed to initialize %d).\n", code);
119         return code;
120     }
121     rx_SetRxDeadTime(afs_rx_deadtime);
122     /* resource init creates the services */
123     afs_ResourceInit(preallocs);
124
125     afs_InitSetup_done = 1;
126     afs_osi_Wakeup(&afs_InitSetup_done);
127
128     return code;
129 }
130 #if defined(AFS_DARWIN80_ENV)
131 struct afsd_thread_info {
132     unsigned long parm;
133 };
134 static int
135 afsd_thread(int *rock)
136 {
137     struct afsd_thread_info *arg = (struct afsd_thread_info *)rock;
138     unsigned long parm = arg->parm;
139
140     switch (parm) {
141     case AFSOP_START_RXCALLBACK:
142         AFS_GLOCK();
143         wakeup(arg);
144         afs_CB_Running = 1;
145         while (afs_RX_Running != 2)
146             afs_osi_Sleep(&afs_RX_Running);
147         afs_RXCallBackServer();
148         AFS_GUNLOCK();
149         thread_terminate(current_thread());
150         break;
151     case AFSOP_START_AFS:
152         AFS_GLOCK();
153         wakeup(arg);
154         AFS_Running = 1;
155         while (afs_initState < AFSOP_START_AFS)
156             afs_osi_Sleep(&afs_initState);
157         afs_initState = AFSOP_START_BKG;
158         afs_osi_Wakeup(&afs_initState);
159         afs_Daemon();
160         AFS_GUNLOCK();
161         thread_terminate(current_thread());
162         break;
163     case AFSOP_START_BKG:
164         AFS_GLOCK();
165         wakeup(arg);
166         while (afs_initState < AFSOP_START_BKG)
167             afs_osi_Sleep(&afs_initState);
168         if (afs_initState < AFSOP_GO) {
169             afs_initState = AFSOP_GO;
170             afs_osi_Wakeup(&afs_initState);
171         }
172         afs_BackgroundDaemon();
173         AFS_GUNLOCK();
174         thread_terminate(current_thread());
175         break;
176     case AFSOP_START_TRUNCDAEMON:
177         AFS_GLOCK();
178         wakeup(arg);
179         while (afs_initState < AFSOP_GO)
180             afs_osi_Sleep(&afs_initState);
181         afs_CacheTruncateDaemon();
182         AFS_GUNLOCK();
183         thread_terminate(current_thread());
184         break;
185     case AFSOP_START_CS:
186         AFS_GLOCK();
187         wakeup(arg);
188         afs_CheckServerDaemon();
189         AFS_GUNLOCK();
190         thread_terminate(current_thread());
191         break;
192     case AFSOP_RXEVENT_DAEMON:
193         AFS_GLOCK();
194         wakeup(arg);
195         while (afs_initState < AFSOP_START_BKG)
196             afs_osi_Sleep(&afs_initState);
197         afs_rxevent_daemon();
198         AFS_GUNLOCK();
199         thread_terminate(current_thread());
200         break;
201     case AFSOP_RXLISTENER_DAEMON:
202         AFS_GLOCK();
203         wakeup(arg);
204         afs_initState = AFSOP_START_AFS;
205         afs_osi_Wakeup(&afs_initState);
206         afs_RX_Running = 2;
207         afs_osi_Wakeup(&afs_RX_Running);
208         afs_osi_RxkRegister();
209         rxk_Listener();
210         AFS_GUNLOCK();
211         thread_terminate(current_thread());
212         break;
213     default:
214         printf("Unknown op %ld in StartDaemon()\n", (long)parm);
215         break;
216     }
217 }
218
219 void
220 afs_DaemonOp(long parm, long parm2, long parm3, long parm4, long parm5,
221              long parm6)
222 {
223     int code;
224     struct afsd_thread_info info;
225     thread_t thread;
226
227     if (parm == AFSOP_START_RXCALLBACK) {
228         if (afs_CB_Running)
229             return;
230     } else if (parm == AFSOP_RXLISTENER_DAEMON) {
231         if (afs_RX_Running)
232             return;
233         afs_RX_Running = 1;
234         code = afs_InitSetup(parm2);
235         if (parm3) {
236             rx_enablePeerRPCStats();
237         }
238         if (parm4) {
239             rx_enableProcessRPCStats();
240         }
241         if (code)
242             return;
243     } else if (parm == AFSOP_START_AFS) {
244         if (AFS_Running)
245             return;
246     }                           /* other functions don't need setup in the parent */
247     info.parm = parm;
248     kernel_thread_start((thread_continue_t)afsd_thread, &info, &thread);
249     AFS_GUNLOCK();
250     /* we need to wait cause we passed stack pointers around.... */
251     msleep(&info, NULL, PVFS, "afs_DaemonOp", NULL);
252     AFS_GLOCK();
253     thread_deallocate(thread);
254 }
255 #endif
256
257
258 #if defined(AFS_LINUX24_ENV) && defined(COMPLETION_H_EXISTS)
259 struct afsd_thread_info {
260 #if defined(AFS_LINUX26_ENV) && !defined(INIT_WORK_HAS_DATA)
261     struct work_struct tq;
262 #endif
263     unsigned long parm;
264     struct completion *complete;
265 };
266
267 static int
268 afsd_thread(void *rock)
269 {
270     struct afsd_thread_info *arg = rock;
271     unsigned long parm = arg->parm;
272 #ifdef SYS_SETPRIORITY_EXPORTED
273     int (*sys_setpriority) (int, int, int) = sys_call_table[__NR_setpriority];
274 #endif
275 #if defined(AFS_LINUX26_ENV)
276     daemonize("afsd");
277 #else
278     daemonize();
279 #endif
280                                 /* doesn't do much, since we were forked from keventd, but
281                                  * does call mm_release, which wakes up our parent (since it
282                                  * used CLONE_VFORK) */
283 #if !defined(AFS_LINUX26_ENV)
284     reparent_to_init();
285 #endif
286     afs_osi_MaskSignals();
287     switch (parm) {
288     case AFSOP_START_RXCALLBACK:
289         sprintf(current->comm, "afs_cbstart");
290         AFS_GLOCK();
291         complete(arg->complete);
292         afs_CB_Running = 1;
293         while (afs_RX_Running != 2)
294             afs_osi_Sleep(&afs_RX_Running);
295         sprintf(current->comm, "afs_callback");
296         afs_RXCallBackServer();
297         AFS_GUNLOCK();
298         complete_and_exit(0, 0);
299         break;
300     case AFSOP_START_AFS:
301         sprintf(current->comm, "afs_afsstart");
302         AFS_GLOCK();
303         complete(arg->complete);
304         AFS_Running = 1;
305         while (afs_initState < AFSOP_START_AFS)
306             afs_osi_Sleep(&afs_initState);
307         afs_initState = AFSOP_START_BKG;
308         afs_osi_Wakeup(&afs_initState);
309         sprintf(current->comm, "afsd");
310         afs_Daemon();
311         AFS_GUNLOCK();
312         complete_and_exit(0, 0);
313         break;
314     case AFSOP_START_BKG:
315         sprintf(current->comm, "afs_bkgstart");
316         AFS_GLOCK();
317         complete(arg->complete);
318         while (afs_initState < AFSOP_START_BKG)
319             afs_osi_Sleep(&afs_initState);
320         if (afs_initState < AFSOP_GO) {
321             afs_initState = AFSOP_GO;
322             afs_osi_Wakeup(&afs_initState);
323         }
324         sprintf(current->comm, "afs_background");
325         afs_BackgroundDaemon();
326         AFS_GUNLOCK();
327         complete_and_exit(0, 0);
328         break;
329     case AFSOP_START_TRUNCDAEMON:
330         sprintf(current->comm, "afs_trimstart");
331         AFS_GLOCK();
332         complete(arg->complete);
333         while (afs_initState < AFSOP_GO)
334             afs_osi_Sleep(&afs_initState);
335         sprintf(current->comm, "afs_cachetrim");
336         afs_CacheTruncateDaemon();
337         AFS_GUNLOCK();
338         complete_and_exit(0, 0);
339         break;
340     case AFSOP_START_CS:
341         sprintf(current->comm, "afs_checkserver");
342         AFS_GLOCK();
343         complete(arg->complete);
344         afs_CheckServerDaemon();
345         AFS_GUNLOCK();
346         complete_and_exit(0, 0);
347         break;
348     case AFSOP_RXEVENT_DAEMON:
349         sprintf(current->comm, "afs_evtstart");
350 #ifdef SYS_SETPRIORITY_EXPORTED
351         sys_setpriority(PRIO_PROCESS, 0, -10);
352 #else
353 #ifdef CURRENT_INCLUDES_NICE
354         current->nice = -10;
355 #endif
356 #endif
357         AFS_GLOCK();
358         complete(arg->complete);
359         while (afs_initState < AFSOP_START_BKG)
360             afs_osi_Sleep(&afs_initState);
361         sprintf(current->comm, "afs_rxevent");
362         afs_rxevent_daemon();
363         AFS_GUNLOCK();
364         complete_and_exit(0, 0);
365         break;
366     case AFSOP_RXLISTENER_DAEMON:
367         sprintf(current->comm, "afs_lsnstart");
368 #ifdef SYS_SETPRIORITY_EXPORTED
369         sys_setpriority(PRIO_PROCESS, 0, -10);
370 #else
371 #ifdef CURRENT_INCLUDES_NICE
372         current->nice = -10;
373 #endif
374 #endif
375         AFS_GLOCK();
376         complete(arg->complete);
377         afs_initState = AFSOP_START_AFS;
378         afs_osi_Wakeup(&afs_initState);
379         afs_RX_Running = 2;
380         afs_osi_Wakeup(&afs_RX_Running);
381         afs_osi_RxkRegister();
382         sprintf(current->comm, "afs_rxlistener");
383         rxk_Listener();
384         AFS_GUNLOCK();
385         complete_and_exit(0, 0);
386         break;
387     default:
388         printf("Unknown op %ld in StartDaemon()\n", (long)parm);
389         break;
390     }
391     return 0;
392 }
393
394 void
395 #if defined(AFS_LINUX26_ENV) && !defined(INIT_WORK_HAS_DATA)
396 afsd_launcher(struct work_struct *work)
397 #else
398 afsd_launcher(void *rock)
399 #endif
400 {
401 #if defined(AFS_LINUX26_ENV) && !defined(INIT_WORK_HAS_DATA)
402     struct afsd_thread_info *rock = container_of(work, struct afsd_thread_info, tq);
403 #endif
404
405     if (!kernel_thread(afsd_thread, (void *)rock, CLONE_VFORK | SIGCHLD))
406         printf("kernel_thread failed. afs startup will not complete\n");
407 }
408
409 void
410 afs_DaemonOp(long parm, long parm2, long parm3, long parm4, long parm5,
411              long parm6)
412 {
413     int code;
414     DECLARE_COMPLETION(c);
415 #if defined(AFS_LINUX26_ENV) 
416 #if defined(INIT_WORK_HAS_DATA)
417     struct work_struct tq;
418 #endif
419 #else
420     struct tq_struct tq;
421 #endif
422     struct afsd_thread_info info;
423     if (parm == AFSOP_START_RXCALLBACK) {
424         if (afs_CB_Running)
425             return;
426     } else if (parm == AFSOP_RXLISTENER_DAEMON) {
427         if (afs_RX_Running)
428             return;
429         afs_RX_Running = 1;
430         code = afs_InitSetup(parm2);
431         if (parm3) {
432             rx_enablePeerRPCStats();
433         }
434         if (parm4) {
435             rx_enableProcessRPCStats();
436         }
437         if (code)
438             return;
439     } else if (parm == AFSOP_START_AFS) {
440         if (AFS_Running)
441             return;
442     }                           /* other functions don't need setup in the parent */
443     info.complete = &c;
444     info.parm = parm;
445 #if defined(AFS_LINUX26_ENV)
446 #if !defined(INIT_WORK_HAS_DATA)
447     INIT_WORK(&info.tq, afsd_launcher);
448     schedule_work(&info.tq);
449 #else
450     INIT_WORK(&tq, afsd_launcher, &info);
451     schedule_work(&tq);
452 #endif
453 #else
454     tq.sync = 0;
455     INIT_LIST_HEAD(&tq.list);
456     tq.routine = afsd_launcher;
457     tq.data = &info;
458     schedule_task(&tq);
459 #endif
460     AFS_GUNLOCK();
461     /* we need to wait cause we passed stack pointers around.... */
462     wait_for_completion(&c);
463     AFS_GLOCK();
464 }
465 #endif
466
467 #ifdef AFS_DARWIN100_ENV
468 #define AFSKPTR(X) k ## X
469 int
470 afs_syscall_call(long parm, long parm2, long parm3,
471                  long parm4, long parm5, long parm6)
472 {
473     return afs_syscall64_call(CAST_USER_ADDR_T((parm)),
474                               CAST_USER_ADDR_T((parm2)),
475                               CAST_USER_ADDR_T((parm3)),
476                               CAST_USER_ADDR_T((parm4)),
477                               CAST_USER_ADDR_T((parm5)),
478                               CAST_USER_ADDR_T((parm6)));
479 }
480 #else
481 #define AFSKPTR(X) ((caddr_t)X)
482 #endif
483 int
484 #ifdef AFS_DARWIN100_ENV
485 afs_syscall64_call(user_addr_t kparm, user_addr_t kparm2, user_addr_t kparm3,
486                  user_addr_t kparm4, user_addr_t kparm5, user_addr_t kparm6)
487 #else
488 afs_syscall_call(long parm, long parm2, long parm3,
489                  long parm4, long parm5, long parm6)
490 #endif
491 {
492     afs_int32 code = 0;
493 #if defined(AFS_SGI61_ENV) || defined(AFS_SUN57_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
494     size_t bufferSize;
495 #else /* AFS_SGI61_ENV */
496     u_int bufferSize;
497 #endif /* AFS_SGI61_ENV */
498 #ifdef AFS_DARWIN100_ENV
499     /* AFSKPTR macro relies on this name format/mapping */
500     afs_uint32 parm = (afs_uint32)kparm;
501     afs_uint32 parm2 = (afs_uint32)kparm2;
502     afs_uint32 parm3 = (afs_uint32)kparm3;
503     afs_uint32 parm4 = (afs_uint32)kparm4;
504     afs_uint32 parm5 = (afs_uint32)kparm5;
505     afs_uint32 parm6 = (afs_uint32)kparm6;
506 #endif
507
508     AFS_STATCNT(afs_syscall_call);
509     if (
510 #ifdef  AFS_SUN5_ENV
511         !afs_suser(CRED())
512 #else
513         !afs_suser(NULL)
514 #endif
515                     && (parm != AFSOP_GETMTU) && (parm != AFSOP_GETMASK)) {
516         /* only root can run this code */
517 #if defined(AFS_SUN5_ENV) || defined(KERNEL_HAVE_UERROR)
518 #if defined(KERNEL_HAVE_UERROR)
519         setuerror(EACCES);
520 #endif
521         code = EACCES;
522 #else
523         code = EPERM;
524 #endif
525         AFS_GLOCK();
526 #ifdef AFS_DARWIN80_ENV
527         put_vfs_context();
528 #endif
529         goto out;
530     }
531     AFS_GLOCK();
532 #ifdef AFS_DARWIN80_ENV
533     put_vfs_context();
534 #endif
535 #if ((defined(AFS_LINUX24_ENV) && defined(COMPLETION_H_EXISTS)) || defined(AFS_DARWIN80_ENV)) && !defined(UKERNEL)
536     if (parm < AFSOP_ADDCELL || parm == AFSOP_RXEVENT_DAEMON
537         || parm == AFSOP_RXLISTENER_DAEMON) {
538         afs_DaemonOp(parm, parm2, parm3, parm4, parm5, parm6);
539     }
540 #else /* !(AFS_LINUX24_ENV && !UKERNEL) */
541     if (parm == AFSOP_START_RXCALLBACK) {
542         if (afs_CB_Running)
543             goto out;
544         afs_CB_Running = 1;
545 #ifndef RXK_LISTENER_ENV
546         code = afs_InitSetup(parm2);
547         if (!code)
548 #endif /* !RXK_LISTENER_ENV */
549         {
550 #ifdef RXK_LISTENER_ENV
551             while (afs_RX_Running != 2)
552                 afs_osi_Sleep(&afs_RX_Running);
553 #else /* !RXK_LISTENER_ENV */
554             afs_initState = AFSOP_START_AFS;
555             afs_osi_Wakeup(&afs_initState);
556 #endif /* RXK_LISTENER_ENV */
557             afs_osi_Invisible();
558             afs_RXCallBackServer();
559             afs_osi_Visible();
560         }
561 #ifdef AFS_SGI_ENV
562         AFS_GUNLOCK();
563         exit(CLD_EXITED, code);
564 #endif /* AFS_SGI_ENV */
565     }
566 #ifdef RXK_LISTENER_ENV
567     else if (parm == AFSOP_RXLISTENER_DAEMON) {
568         if (afs_RX_Running)
569             goto out;
570         afs_RX_Running = 1;
571         code = afs_InitSetup(parm2);
572         if (parm3) {
573             rx_enablePeerRPCStats();
574         }
575         if (parm4) {
576             rx_enableProcessRPCStats();
577         }
578         if (!code) {
579             afs_initState = AFSOP_START_AFS;
580             afs_osi_Wakeup(&afs_initState);
581             afs_osi_Invisible();
582             afs_RX_Running = 2;
583             afs_osi_Wakeup(&afs_RX_Running);
584 #ifndef UKERNEL
585             afs_osi_RxkRegister();
586 #endif /* !UKERNEL */
587             rxk_Listener();
588             afs_osi_Visible();
589         }
590 #ifdef  AFS_SGI_ENV
591         AFS_GUNLOCK();
592         exit(CLD_EXITED, code);
593 #endif /* AFS_SGI_ENV */
594     }
595 #endif /* RXK_LISTENER_ENV */
596     else if (parm == AFSOP_START_AFS) {
597         /* afs daemon */
598         if (AFS_Running)
599             goto out;
600         AFS_Running = 1;
601         while (afs_initState < AFSOP_START_AFS)
602             afs_osi_Sleep(&afs_initState);
603
604         afs_initState = AFSOP_START_BKG;
605         afs_osi_Wakeup(&afs_initState);
606         afs_osi_Invisible();
607         afs_Daemon();
608         afs_osi_Visible();
609 #ifdef AFS_SGI_ENV
610         AFS_GUNLOCK();
611         exit(CLD_EXITED, 0);
612 #endif /* AFS_SGI_ENV */
613     } else if (parm == AFSOP_START_CS) {
614         afs_osi_Invisible();
615         afs_CheckServerDaemon();
616         afs_osi_Visible();
617 #ifdef AFS_SGI_ENV
618         AFS_GUNLOCK();
619         exit(CLD_EXITED, 0);
620 #endif /* AFS_SGI_ENV */
621     } else if (parm == AFSOP_START_BKG) {
622         while (afs_initState < AFSOP_START_BKG)
623             afs_osi_Sleep(&afs_initState);
624         if (afs_initState < AFSOP_GO) {
625             afs_initState = AFSOP_GO;
626             afs_osi_Wakeup(&afs_initState);
627         }
628         /* start the bkg daemon */
629         afs_osi_Invisible();
630 #ifdef AFS_AIX32_ENV
631         if (parm2)
632             afs_BioDaemon(parm2);
633         else
634 #endif /* AFS_AIX32_ENV */
635             afs_BackgroundDaemon();
636         afs_osi_Visible();
637 #ifdef AFS_SGI_ENV
638         AFS_GUNLOCK();
639         exit(CLD_EXITED, 0);
640 #endif /* AFS_SGI_ENV */
641     } else if (parm == AFSOP_START_TRUNCDAEMON) {
642         while (afs_initState < AFSOP_GO)
643             afs_osi_Sleep(&afs_initState);
644         /* start the bkg daemon */
645         afs_osi_Invisible();
646         afs_CacheTruncateDaemon();
647         afs_osi_Visible();
648 #ifdef  AFS_SGI_ENV
649         AFS_GUNLOCK();
650         exit(CLD_EXITED, 0);
651 #endif /* AFS_SGI_ENV */
652     }
653 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
654     else if (parm == AFSOP_RXEVENT_DAEMON) {
655         while (afs_initState < AFSOP_START_BKG)
656             afs_osi_Sleep(&afs_initState);
657         afs_osi_Invisible();
658         afs_rxevent_daemon();
659         afs_osi_Visible();
660 #ifdef AFS_SGI_ENV
661         AFS_GUNLOCK();
662         exit(CLD_EXITED, 0);
663 #endif /* AFS_SGI_ENV */
664     }
665 #endif /* AFS_SUN5_ENV || RXK_LISTENER_ENV */
666 #endif /* AFS_LINUX24_ENV && !UKERNEL */
667     else if (parm == AFSOP_BASIC_INIT) {
668         afs_int32 temp;
669
670         while (!afs_InitSetup_done)
671             afs_osi_Sleep(&afs_InitSetup_done);
672
673 #if defined(AFS_SGI_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV) || defined(AFS_SUN5_ENV)
674         temp = AFS_MINBUFFERS;  /* Should fix this soon */
675 #else
676         /* number of 2k buffers we could get from all of the buffer space */
677         temp = ((afs_bufferpages * NBPG) >> 11);
678         temp = temp >> 2;       /* don't take more than 25% (our magic parameter) */
679         if (temp < AFS_MINBUFFERS)
680             temp = AFS_MINBUFFERS;      /* though we really should have this many */
681 #endif
682         DInit(temp);
683         afs_rootFid.Fid.Volume = 0;
684         code = 0;
685     } else if (parm == AFSOP_BUCKETPCT) {
686         /* need to enable this now, will disable again before GO
687            if we don't have 100% */
688         splitdcache = 1;
689         switch (parm2) {
690         case 1:
691             afs_tpct1 = parm3;
692             break;
693         case 2:
694             afs_tpct2 = parm3;
695             break;
696         }           
697     } else if (parm == AFSOP_ADDCELL) {
698         /* add a cell.  Parameter 2 is 8 hosts (in net order),  parm 3 is the null-terminated
699          * name.  Parameter 4 is the length of the name, including the null.  Parm 5 is the
700          * home cell flag (0x1 bit) and the nosuid flag (0x2 bit) */
701         struct afsop_cell *tcell = afs_osi_Alloc(sizeof(struct afsop_cell));
702
703         code = afs_InitDynroot();
704         if (!code) {
705             AFS_COPYIN(AFSKPTR(parm2), (caddr_t)tcell->hosts, sizeof(tcell->hosts),
706                        code);
707         }
708         if (!code) {
709             if (parm4 > sizeof(tcell->cellName))
710                 code = EFAULT;
711             else {
712               AFS_COPYIN(AFSKPTR(parm3), (caddr_t)tcell->cellName, parm4, code);
713                 if (!code)
714                     afs_NewCell(tcell->cellName, tcell->hosts, parm5, NULL, 0,
715                                 0, 0);
716             }
717         }
718         afs_osi_Free(tcell, sizeof(struct afsop_cell));
719     } else if (parm == AFSOP_ADDCELL2) {
720         struct afsop_cell *tcell = afs_osi_Alloc(sizeof(struct afsop_cell));
721         char *tbuffer = osi_AllocSmallSpace(AFS_SMALLOCSIZ), *lcnamep = 0;
722         char *tbuffer1 = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
723         int cflags = parm4;
724
725         code = afs_InitDynroot();
726         if (!code) {
727 #if 0
728             /* wait for basic init - XXX can't find any reason we need this? */
729             while (afs_initState < AFSOP_START_BKG)
730                 afs_osi_Sleep(&afs_initState);
731 #endif
732
733             AFS_COPYIN(AFSKPTR(parm2), (caddr_t)tcell->hosts, sizeof(tcell->hosts),
734                        code);
735         }
736         if (!code) {
737             AFS_COPYINSTR(AFSKPTR(parm3), tbuffer1, AFS_SMALLOCSIZ,
738                           &bufferSize, code);
739             if (!code) {
740                 if (parm4 & 4) {
741                     AFS_COPYINSTR(AFSKPTR(parm5), tbuffer, AFS_SMALLOCSIZ,
742                                   &bufferSize, code);
743                     if (!code) {
744                         lcnamep = tbuffer;
745                         cflags |= CLinkedCell;
746                     }
747                 }
748                 if (!code)
749                     code =
750                         afs_NewCell(tbuffer1, tcell->hosts, cflags, lcnamep,
751                                     0, 0, 0);
752             }
753         }
754         afs_osi_Free(tcell, sizeof(struct afsop_cell));
755         osi_FreeSmallSpace(tbuffer);
756         osi_FreeSmallSpace(tbuffer1);
757     } else if (parm == AFSOP_ADDCELLALIAS) {
758         /*
759          * Call arguments:
760          * parm2 is the alias name
761          * parm3 is the real cell name
762          */
763         char *aliasName = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
764         char *cellName = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
765
766         code = afs_InitDynroot();
767         if (!code) {
768             AFS_COPYINSTR(AFSKPTR(parm2), aliasName, AFS_SMALLOCSIZ, &bufferSize,
769                           code);
770         }
771         if (!code)
772             AFS_COPYINSTR(AFSKPTR(parm3), cellName, AFS_SMALLOCSIZ,
773                           &bufferSize, code);
774         if (!code)
775             afs_NewCellAlias(aliasName, cellName);
776         osi_FreeSmallSpace(aliasName);
777         osi_FreeSmallSpace(cellName);
778     } else if (parm == AFSOP_SET_THISCELL) {
779         /*
780          * Call arguments:
781          * parm2 is the primary cell name
782          */
783         char *cell = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
784
785         code = afs_InitDynroot();
786         if (!code) {
787             AFS_COPYINSTR(AFSKPTR(parm2), cell, AFS_SMALLOCSIZ, &bufferSize, code);
788         }
789         if (!code)
790             afs_SetPrimaryCell(cell);
791         osi_FreeSmallSpace(cell);
792     } else if (parm == AFSOP_CACHEINIT) {
793         struct afs_cacheParams cparms;
794
795         if (afs_CacheInit_Done)
796             goto out;
797
798         AFS_COPYIN(AFSKPTR(parm2), (caddr_t) & cparms, sizeof(cparms), code);
799         if (code) {
800 #if defined(KERNEL_HAVE_UERROR)
801             setuerror(code);
802             code = -1;
803 #endif
804             goto out;
805         }
806         afs_CacheInit_Done = 1;
807         code = afs_icl_InitLogs();
808         afs_setTime = cparms.setTimeFlag;
809
810         code =
811             afs_CacheInit(cparms.cacheScaches, cparms.cacheFiles,
812                           cparms.cacheBlocks, cparms.cacheDcaches,
813                           cparms.cacheVolumes, cparms.chunkSize,
814                           cparms.memCacheFlag, cparms.inodes, cparms.users, 
815                           cparms.dynamic_vcaches);
816
817     } else if (parm == AFSOP_CACHEINODE) {
818         ino_t ainode = parm2;
819         /* wait for basic init */
820         while (afs_initState < AFSOP_START_BKG)
821             afs_osi_Sleep(&afs_initState);
822
823 #ifdef AFS_DARWIN80_ENV
824         get_vfs_context();
825 #endif
826         /* do it by inode */
827 #ifdef AFS_SGI62_ENV
828         ainode = (ainode << 32) | (parm3 & 0xffffffff);
829 #endif
830         code = afs_InitCacheFile(NULL, ainode);
831 #ifdef AFS_DARWIN80_ENV
832         put_vfs_context();
833 #endif
834     } else if (parm == AFSOP_CACHEDIRS) {
835         afs_numfilesperdir = parm2;
836         afs_osi_Wakeup(&afs_initState);
837     } else if (parm == AFSOP_CACHEFILES) {
838         afs_numcachefiles = parm2;
839         afs_osi_Wakeup(&afs_initState);
840     } else if (parm == AFSOP_ROOTVOLUME) {
841         /* wait for basic init */
842         while (afs_initState < AFSOP_START_BKG)
843             afs_osi_Sleep(&afs_initState);
844
845         if (parm2) {
846             AFS_COPYINSTR(AFSKPTR(parm2), afs_rootVolumeName,
847                           sizeof(afs_rootVolumeName), &bufferSize, code);
848             afs_rootVolumeName[sizeof(afs_rootVolumeName) - 1] = 0;
849         } else
850             code = 0;
851     } else if (parm == AFSOP_CACHEFILE || parm == AFSOP_CACHEINFO
852                || parm == AFSOP_VOLUMEINFO || parm == AFSOP_CELLINFO) {
853         char *tbuffer = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
854
855         code = 0;
856         AFS_COPYINSTR(AFSKPTR(parm2), tbuffer, AFS_SMALLOCSIZ, &bufferSize,
857                       code);
858         if (code) {
859             osi_FreeSmallSpace(tbuffer);
860             goto out;
861         }
862         if (!code) {
863             tbuffer[AFS_SMALLOCSIZ - 1] = '\0'; /* null-terminate the name */
864             /* We have the cache dir copied in.  Call the cache init routine */
865 #ifdef AFS_DARWIN80_ENV
866             get_vfs_context();
867 #endif
868             if (parm == AFSOP_CACHEFILE) {
869                 code = afs_InitCacheFile(tbuffer, 0);
870             } else if (parm == AFSOP_CACHEINFO) {
871                 code = afs_InitCacheInfo(tbuffer);
872             } else if (parm == AFSOP_VOLUMEINFO) {
873                 code = afs_InitVolumeInfo(tbuffer);
874             } else if (parm == AFSOP_CELLINFO) {
875                 code = afs_InitCellInfo(tbuffer);
876             }
877 #ifdef AFS_DARWIN80_ENV
878             put_vfs_context();
879 #endif
880         }
881         osi_FreeSmallSpace(tbuffer);
882     } else if (parm == AFSOP_GO) {
883         /* the generic initialization calls come here.  One parameter: should we do the
884          * set-time operation on this workstation */
885         if (afs_Go_Done)
886             goto out;
887         afs_Go_Done = 1;
888         while (afs_initState < AFSOP_GO)
889             afs_osi_Sleep(&afs_initState);
890         afs_initState = 101;
891         afs_setTime = parm2;
892         if (afs_tpct1 + afs_tpct2 != 100) {
893             afs_tpct1 = 0;
894             afs_tpct2 = 0;
895             splitdcache = 0;
896         } else {        
897             splitdcache = 1;
898         }
899         afs_osi_Wakeup(&afs_initState);
900 #if     (!defined(AFS_NONFSTRANS)) || defined(AFS_AIX_IAUTH_ENV)
901         afs_nfsclient_init();
902 #endif
903         afs_uuid_create(&afs_cb_interface.uuid);
904         printf("found %d non-empty cache files (%d%%).\n",
905                afs_stats_cmperf.cacheFilesReused,
906                (100 * afs_stats_cmperf.cacheFilesReused) /
907                (afs_stats_cmperf.cacheNumEntries ? afs_stats_cmperf.
908                 cacheNumEntries : 1));
909     } else if (parm == AFSOP_ADVISEADDR) {
910         /* pass in the host address to the rx package */
911         int rxbind = 0;
912         int refresh = 0;
913
914         afs_int32 count = parm2;
915         afs_int32 *buffer =
916             afs_osi_Alloc(sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
917         afs_int32 *maskbuffer =
918             afs_osi_Alloc(sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
919         afs_int32 *mtubuffer =
920             afs_osi_Alloc(sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
921         int i;
922
923         /* This is a refresh */
924         if (count & 0x40000000) {
925             count &= ~0x40000000;
926             /* Can't bind after we start. Fix? */
927             count &= ~0x80000000;
928             refresh = 1;
929         }
930
931         /* Bind, but only if there's only one address configured */ 
932         if ( count & 0x80000000) {
933             count &= ~0x80000000;
934             if (count == 1)
935                 rxbind=1;
936         }
937
938         if (count > AFS_MAX_INTERFACE_ADDR) {
939             code = ENOMEM;
940             count = AFS_MAX_INTERFACE_ADDR;
941         }
942
943         AFS_COPYIN(AFSKPTR(parm3), (caddr_t)buffer, count * sizeof(afs_int32),
944                    code);
945         if (parm4 && !code)
946             AFS_COPYIN(AFSKPTR(parm4), (caddr_t)maskbuffer,
947                        count * sizeof(afs_int32), code);
948         if (parm5 && !code)
949             AFS_COPYIN(AFSKPTR(parm5), (caddr_t)mtubuffer,
950                        count * sizeof(afs_int32), code);
951
952         if (!code) {
953             afs_cb_interface.numberOfInterfaces = count;
954             for (i = 0; i < count; i++) {
955                 afs_cb_interface.addr_in[i] = buffer[i];
956 #ifdef AFS_USERSPACE_IP_ADDR
957                 /* AFS_USERSPACE_IP_ADDR means we have no way of finding the
958                  * machines IP addresses when in the kernel (the in_ifaddr
959                  * struct is not available), so we pass the info in at
960                  * startup. We also pass in the subnetmask and mtu size. The
961                  * subnetmask is used when setting the rank:
962                  * afsi_SetServerIPRank(); and the mtu size is used when
963                  * finding the best mtu size. rxi_FindIfnet() is replaced
964                  * with rxi_Findcbi().
965                  */
966                 afs_cb_interface.subnetmask[i] =
967                     (parm4 ? maskbuffer[i] : 0xffffffff);
968                 afs_cb_interface.mtu[i] = (parm5 ? mtubuffer[i] : htonl(1500));
969 #endif
970             }
971             rxi_setaddr(buffer[0]);
972             if (!refresh) {
973                 if (rxbind)
974                     rx_bindhost = buffer[0];
975                 else
976                     rx_bindhost = htonl(INADDR_ANY);
977             }
978         } else {
979             refresh = 0;
980         }
981
982         afs_osi_Free(buffer, sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
983         afs_osi_Free(maskbuffer, sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
984         afs_osi_Free(mtubuffer, sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
985
986         if (refresh) {
987             afs_CheckServers(1, NULL);     /* check down servers */
988             afs_CheckServers(0, NULL);     /* check down servers */
989         }
990     }
991 #ifdef  AFS_SGI53_ENV
992     else if (parm == AFSOP_NFSSTATICADDR) {
993         extern int (*nfs_rfsdisptab_v2) ();
994         nfs_rfsdisptab_v2 = (int (*)())parm2;
995     } else if (parm == AFSOP_NFSSTATICADDR2) {
996         extern int (*nfs_rfsdisptab_v2) ();
997 #ifdef _K64U64
998         nfs_rfsdisptab_v2 = (int (*)())((parm2 << 32) | (parm3 & 0xffffffff));
999 #else /* _K64U64 */
1000         nfs_rfsdisptab_v2 = (int (*)())(parm3 & 0xffffffff);
1001 #endif /* _K64U64 */
1002     }
1003 #if defined(AFS_SGI62_ENV) && !defined(AFS_SGI65_ENV)
1004     else if (parm == AFSOP_SBLOCKSTATICADDR2) {
1005         extern int (*afs_sblockp) ();
1006         extern void (*afs_sbunlockp) ();
1007 #ifdef _K64U64
1008         afs_sblockp = (int (*)())((parm2 << 32) | (parm3 & 0xffffffff));
1009         afs_sbunlockp = (void (*)())((parm4 << 32) | (parm5 & 0xffffffff));
1010 #else
1011         afs_sblockp = (int (*)())(parm3 & 0xffffffff);
1012         afs_sbunlockp = (void (*)())(parm5 & 0xffffffff);
1013 #endif /* _K64U64 */
1014     }
1015 #endif /* AFS_SGI62_ENV && !AFS_SGI65_ENV */
1016 #endif /* AFS_SGI53_ENV */
1017     else if (parm == AFSOP_SHUTDOWN) {
1018         afs_cold_shutdown = 0;
1019         if (parm2 == 1)
1020             afs_cold_shutdown = 1;
1021 #ifndef AFS_DARWIN_ENV
1022         if (afs_globalVFS != 0) {
1023             afs_warn("AFS isn't unmounted yet! Call aborted\n");
1024             code = EACCES;
1025         } else
1026 #endif
1027             afs_shutdown();
1028     } else if (parm == AFSOP_AFS_VFSMOUNT) {
1029 #ifdef  AFS_HPUX_ENV
1030         vfsmount(parm2, parm3, parm4, parm5);
1031 #else /* defined(AFS_HPUX_ENV) */
1032 #if defined(KERNEL_HAVE_UERROR)
1033         setuerror(EINVAL);
1034 #else
1035         code = EINVAL;
1036 #endif
1037 #endif /* defined(AFS_HPUX_ENV) */
1038     } else if (parm == AFSOP_CLOSEWAIT) {
1039         afs_SynchronousCloses = 'S';
1040     } else if (parm == AFSOP_GETMTU) {
1041         afs_uint32 mtu = 0;
1042 #if     !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
1043 #ifdef AFS_USERSPACE_IP_ADDR
1044         afs_int32 i;
1045         i = rxi_Findcbi(parm2);
1046         mtu = ((i == -1) ? htonl(1500) : afs_cb_interface.mtu[i]);
1047 #else /* AFS_USERSPACE_IP_ADDR */
1048         rx_ifnet_t tifnp;
1049
1050         tifnp = rxi_FindIfnet(parm2, NULL);     /*  make iterative */
1051         mtu = (tifnp ? rx_ifnet_mtu(tifnp) : htonl(1500));
1052 #endif /* else AFS_USERSPACE_IP_ADDR */
1053 #endif /* !AFS_SUN5_ENV */
1054         if (!code)
1055             AFS_COPYOUT((caddr_t) & mtu, AFSKPTR(parm3),
1056                         sizeof(afs_int32), code);
1057 #ifdef AFS_AIX32_ENV
1058 /* this is disabled for now because I can't figure out how to get access
1059  * to these kernel variables.  It's only for supporting user-mode rx
1060  * programs -- it makes a huge difference on the 220's in my testbed,
1061  * though I don't know why. The bosserver does this with /etc/no, so it's
1062  * being handled a different way for the servers right now.  */
1063 /*      {
1064         static adjusted = 0;
1065         extern u_long sb_max_dflt;
1066         if (!adjusted) {
1067           adjusted = 1;
1068           if (sb_max_dflt < 131072) sb_max_dflt = 131072; 
1069           if (sb_max < 131072) sb_max = 131072; 
1070         }
1071       } */
1072 #endif /* AFS_AIX32_ENV */
1073     } else if (parm == AFSOP_GETMASK) { /* parm2 == addr in net order */
1074         afs_uint32 mask = 0;
1075 #if     !defined(AFS_SUN5_ENV)
1076 #ifdef AFS_USERSPACE_IP_ADDR
1077         afs_int32 i;
1078         i = rxi_Findcbi(parm2);
1079         if (i != -1) {
1080             mask = afs_cb_interface.subnetmask[i];
1081         } else {
1082             code = -1;
1083         }
1084 #else /* AFS_USERSPACE_IP_ADDR */
1085         rx_ifnet_t tifnp;
1086
1087         tifnp = rxi_FindIfnet(parm2, &mask);    /* make iterative */
1088         if (!tifnp)
1089             code = -1;
1090 #endif /* else AFS_USERSPACE_IP_ADDR */
1091 #endif /* !AFS_SUN5_ENV */
1092         if (!code)
1093             AFS_COPYOUT((caddr_t) & mask, AFSKPTR(parm3),
1094                         sizeof(afs_int32), code);
1095     }
1096     else if (parm == AFSOP_AFSDB_HANDLER) {
1097         int sizeArg = (int)parm4;
1098         int kmsgLen = sizeArg & 0xffff;
1099         int cellLen = (sizeArg & 0xffff0000) >> 16;
1100         afs_int32 *kmsg = afs_osi_Alloc(kmsgLen);
1101         char *cellname = afs_osi_Alloc(cellLen);
1102
1103 #ifndef UKERNEL
1104         afs_osi_MaskUserLoop();
1105 #endif
1106         AFS_COPYIN(AFSKPTR(parm2), cellname, cellLen, code);
1107         AFS_COPYIN(AFSKPTR(parm3), kmsg, kmsgLen, code);
1108         if (!code) {
1109             code = afs_AFSDBHandler(cellname, cellLen, kmsg);
1110             if (*cellname == 1)
1111                 *cellname = 0;
1112             if (code == -2) {   /* Shutting down? */
1113                 *cellname = 1;
1114                 code = 0;
1115             }
1116         }
1117         if (!code)
1118             AFS_COPYOUT(cellname, AFSKPTR(parm2), cellLen, code);
1119         afs_osi_Free(kmsg, kmsgLen);
1120         afs_osi_Free(cellname, cellLen);
1121     }
1122     else if (parm == AFSOP_SET_DYNROOT) {
1123         code = afs_SetDynrootEnable(parm2);
1124     } else if (parm == AFSOP_SET_FAKESTAT) {
1125         afs_fakestat_enable = parm2;
1126         code = 0;
1127     } else if (parm == AFSOP_SET_BACKUPTREE) {
1128         afs_bkvolpref = parm2;
1129     } else if (parm == AFSOP_SET_RXPCK) {
1130         rx_extraPackets = parm2;
1131         afscall_set_rxpck_received = 1;
1132     } else if (parm == AFSOP_SET_RXMAXMTU) {
1133         rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = parm2;
1134     } else {
1135         code = EINVAL;
1136     }
1137
1138   out:
1139     AFS_GUNLOCK();
1140 #ifdef AFS_LINUX20_ENV
1141     return -code;
1142 #else
1143     return code;
1144 #endif
1145 }
1146
1147 /*
1148  * Initstate in the range 0 < x < 100 are early initialization states.
1149  * Initstate of 100 means a AFSOP_START operation has been done.  After this,
1150  *  the cache may be initialized.
1151  * Initstate of 101 means a AFSOP_GO operation has been done.  This operation
1152  *  is done after all the cache initialization has been done.
1153  * Initstate of 200 means that the volume has been looked up once, possibly
1154  *  incorrectly.
1155  * Initstate of 300 means that the volume has been *successfully* looked up.
1156  */
1157 int
1158 afs_CheckInit(void)
1159 {
1160     register int code = 0;
1161
1162     AFS_STATCNT(afs_CheckInit);
1163     if (afs_initState <= 100)
1164         code = ENXIO;           /* never finished init phase */
1165     else if (afs_initState == 101) {    /* init done, wait for afs_daemon */
1166         while (afs_initState < 200)
1167             afs_osi_Sleep(&afs_initState);
1168     } else if (afs_initState == 200)
1169         code = ETIMEDOUT;       /* didn't find root volume */
1170     return code;
1171 }
1172
1173 int afs_shuttingdown = 0;
1174 void
1175 afs_shutdown(void)
1176 {
1177     extern short afs_brsDaemons;
1178     extern afs_int32 afs_CheckServerDaemonStarted;
1179     extern struct afs_osi_WaitHandle AFS_WaitHandler, AFS_CSWaitHandler;
1180     extern struct osi_file *afs_cacheInodep;
1181
1182     AFS_STATCNT(afs_shutdown);
1183     if (afs_initState == 0) {
1184         afs_warn("AFS not initialized - not shutting down\n");
1185       return;
1186     }
1187
1188     if (afs_shuttingdown)
1189         return;
1190     afs_shuttingdown = 1;
1191     if (afs_cold_shutdown)
1192         afs_warn("afs: COLD ");
1193     else
1194         afs_warn("afs: WARM ");
1195     afs_warn("shutting down of: CB... ");
1196
1197     afs_termState = AFSOP_STOP_RXCALLBACK;
1198     rx_WakeupServerProcs();
1199 #ifdef AFS_AIX51_ENV
1200     shutdown_rxkernel();
1201 #endif
1202     /* close rx server connections here? */
1203     while (afs_termState == AFSOP_STOP_RXCALLBACK)
1204         afs_osi_Sleep(&afs_termState);
1205
1206     afs_warn("afs... ");
1207     while (afs_termState == AFSOP_STOP_AFS) {
1208         afs_osi_CancelWait(&AFS_WaitHandler);
1209         afs_osi_Sleep(&afs_termState);
1210     }
1211     if (afs_CheckServerDaemonStarted) {
1212         while (afs_termState == AFSOP_STOP_CS) {
1213             afs_osi_CancelWait(&AFS_CSWaitHandler);
1214             afs_osi_Sleep(&afs_termState);
1215         }
1216     }
1217     afs_warn("BkG... ");
1218     /* Wake-up afs_brsDaemons so that we don't have to wait for a bkg job! */
1219     while (afs_termState == AFSOP_STOP_BKG) {
1220         afs_osi_Wakeup(&afs_brsDaemons);
1221         afs_osi_Sleep(&afs_termState);
1222     }
1223     afs_warn("CTrunc... ");
1224     /* Cancel cache truncate daemon. */
1225     while (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
1226         afs_osi_Wakeup((char *)&afs_CacheTruncateDaemon);
1227         afs_osi_Sleep(&afs_termState);
1228     }
1229     afs_warn("AFSDB... ");
1230     afs_StopAFSDB();
1231     while (afs_termState == AFSOP_STOP_AFSDB)
1232         afs_osi_Sleep(&afs_termState);
1233 #if     defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
1234     afs_warn("RxEvent... ");
1235     /* cancel rx event daemon */
1236     while (afs_termState == AFSOP_STOP_RXEVENT)
1237         afs_osi_Sleep(&afs_termState);
1238 #if defined(RXK_LISTENER_ENV)
1239 #ifndef UKERNEL
1240     afs_warn("UnmaskRxkSignals... ");
1241     afs_osi_UnmaskRxkSignals();
1242 #endif
1243     /* cancel rx listener */
1244     afs_warn("RxListener... ");
1245     osi_StopListener();         /* This closes rx_socket. */
1246     while (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1247         afs_warn("Sleep... ");
1248         afs_osi_Sleep(&afs_termState);
1249     }
1250 #endif
1251 #else
1252     afs_termState = AFSOP_STOP_COMPLETE;
1253 #endif
1254
1255 #ifdef AFS_AIX51_ENV
1256     shutdown_daemons();
1257 #endif
1258     shutdown_CB();
1259     shutdown_bufferpackage();
1260     shutdown_cache();
1261     shutdown_osi();
1262     /*
1263      * Close file only after daemons which can write to it are stopped.
1264      * Need to close before the osinet shutdown to avoid failing check
1265      * for dangling memory allocations.
1266      */
1267     if (afs_cacheInodep) {      /* memcache won't set this */
1268         osi_UFSClose(afs_cacheInodep);  /* Since we always leave it open */
1269         afs_cacheInodep = 0;
1270     }
1271     /*
1272      * Shutdown the ICL logs - needed to free allocated memory space and avoid
1273      * warnings from shutdown_osinet
1274      */
1275     shutdown_icl();
1276     shutdown_osinet();
1277     shutdown_osifile();
1278     shutdown_vnodeops();
1279     shutdown_memcache();
1280     shutdown_xscache();
1281 #if (!defined(AFS_NONFSTRANS) || defined(AFS_AIX_IAUTH_ENV))
1282     shutdown_exporter();
1283     shutdown_nfsclnt();
1284 #endif
1285     shutdown_afstest();
1286     shutdown_AFS();
1287     /* The following hold the cm stats */
1288     memset(&afs_cmstats, 0, sizeof(struct afs_CMStats));
1289     memset(&afs_stats_cmperf, 0, sizeof(struct afs_stats_CMPerf));
1290     memset(&afs_stats_cmfullperf, 0, sizeof(struct afs_stats_CMFullPerf));
1291     afs_warn(" ALL allocated tables... ");
1292
1293     afs_shuttingdown = 0;
1294     afs_warn("done\n");
1295
1296     return;                     /* Just kill daemons for now */
1297 }
1298
1299 void
1300 shutdown_afstest(void)
1301 {
1302     AFS_STATCNT(shutdown_afstest);
1303     afs_initState = afs_termState = afs_setTime = 0;
1304     AFS_Running = afs_CB_Running = 0;
1305     afs_CacheInit_Done = afs_Go_Done = 0;
1306     if (afs_cold_shutdown) {
1307         *afs_rootVolumeName = 0;
1308     }
1309 }