cachemanager additional cleanup
[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 static void
468 wait_for_cachedefs(void) {
469 #ifdef AFS_CACHE_VNODE_PATH
470     if (cacheDiskType != AFS_FCACHE_TYPE_MEM) 
471         while ((afs_numcachefiles < 1) || (afs_numfilesperdir < 1) ||
472                (afs_cachebasedir[0] != '/')) {
473             printf("afs: waiting for cache parameter definitions\n");
474             afs_osi_Sleep(&afs_initState);
475         }
476 #endif
477 }
478
479 #ifdef AFS_DARWIN100_ENV
480 #define AFSKPTR(X) k ## X
481 int
482 afs_syscall_call(long parm, long parm2, long parm3,
483                  long parm4, long parm5, long parm6)
484 {
485     return afs_syscall64_call(CAST_USER_ADDR_T((parm)),
486                               CAST_USER_ADDR_T((parm2)),
487                               CAST_USER_ADDR_T((parm3)),
488                               CAST_USER_ADDR_T((parm4)),
489                               CAST_USER_ADDR_T((parm5)),
490                               CAST_USER_ADDR_T((parm6)));
491 }
492 #else
493 #define AFSKPTR(X) ((caddr_t)X)
494 #endif
495 int
496 #ifdef AFS_DARWIN100_ENV
497 afs_syscall64_call(user_addr_t kparm, user_addr_t kparm2, user_addr_t kparm3,
498                  user_addr_t kparm4, user_addr_t kparm5, user_addr_t kparm6)
499 #else
500 afs_syscall_call(long parm, long parm2, long parm3,
501                  long parm4, long parm5, long parm6)
502 #endif
503 {
504     afs_int32 code = 0;
505 #if defined(AFS_SGI61_ENV) || defined(AFS_SUN57_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
506     size_t bufferSize;
507 #else /* AFS_SGI61_ENV */
508     u_int bufferSize;
509 #endif /* AFS_SGI61_ENV */
510 #ifdef AFS_DARWIN100_ENV
511     /* AFSKPTR macro relies on this name format/mapping */
512     afs_uint32 parm = (afs_uint32)kparm;
513     afs_uint32 parm2 = (afs_uint32)kparm2;
514     afs_uint32 parm3 = (afs_uint32)kparm3;
515     afs_uint32 parm4 = (afs_uint32)kparm4;
516     afs_uint32 parm5 = (afs_uint32)kparm5;
517     afs_uint32 parm6 = (afs_uint32)kparm6;
518 #endif
519
520     AFS_STATCNT(afs_syscall_call);
521     if (
522 #ifdef  AFS_SUN5_ENV
523         !afs_suser(CRED())
524 #else
525         !afs_suser(NULL)
526 #endif
527                     && (parm != AFSOP_GETMTU) && (parm != AFSOP_GETMASK)) {
528         /* only root can run this code */
529 #if defined(AFS_SUN5_ENV) || defined(KERNEL_HAVE_UERROR)
530 #if defined(KERNEL_HAVE_UERROR)
531         setuerror(EACCES);
532 #endif
533         code = EACCES;
534 #else
535         code = EPERM;
536 #endif
537         AFS_GLOCK();
538 #ifdef AFS_DARWIN80_ENV
539         put_vfs_context();
540 #endif
541         goto out;
542     }
543     AFS_GLOCK();
544 #ifdef AFS_DARWIN80_ENV
545     put_vfs_context();
546 #endif
547 #if ((defined(AFS_LINUX24_ENV) && defined(COMPLETION_H_EXISTS)) || defined(AFS_DARWIN80_ENV)) && !defined(UKERNEL)
548     if (parm < AFSOP_ADDCELL || parm == AFSOP_RXEVENT_DAEMON
549         || parm == AFSOP_RXLISTENER_DAEMON) {
550         afs_DaemonOp(parm, parm2, parm3, parm4, parm5, parm6);
551     }
552 #else /* !(AFS_LINUX24_ENV && !UKERNEL) */
553     if (parm == AFSOP_START_RXCALLBACK) {
554         if (afs_CB_Running)
555             goto out;
556         afs_CB_Running = 1;
557 #ifndef RXK_LISTENER_ENV
558         code = afs_InitSetup(parm2);
559         if (!code)
560 #endif /* !RXK_LISTENER_ENV */
561         {
562 #ifdef RXK_LISTENER_ENV
563             while (afs_RX_Running != 2)
564                 afs_osi_Sleep(&afs_RX_Running);
565 #else /* !RXK_LISTENER_ENV */
566             afs_initState = AFSOP_START_AFS;
567             afs_osi_Wakeup(&afs_initState);
568 #endif /* RXK_LISTENER_ENV */
569             afs_osi_Invisible();
570             afs_RXCallBackServer();
571             afs_osi_Visible();
572         }
573 #ifdef AFS_SGI_ENV
574         AFS_GUNLOCK();
575         exit(CLD_EXITED, code);
576 #endif /* AFS_SGI_ENV */
577     }
578 #ifdef RXK_LISTENER_ENV
579     else if (parm == AFSOP_RXLISTENER_DAEMON) {
580         if (afs_RX_Running)
581             goto out;
582         afs_RX_Running = 1;
583         code = afs_InitSetup(parm2);
584         if (parm3) {
585             rx_enablePeerRPCStats();
586         }
587         if (parm4) {
588             rx_enableProcessRPCStats();
589         }
590         if (!code) {
591             afs_initState = AFSOP_START_AFS;
592             afs_osi_Wakeup(&afs_initState);
593             afs_osi_Invisible();
594             afs_RX_Running = 2;
595             afs_osi_Wakeup(&afs_RX_Running);
596 #ifndef UKERNEL
597             afs_osi_RxkRegister();
598 #endif /* !UKERNEL */
599             rxk_Listener();
600             afs_osi_Visible();
601         }
602 #ifdef  AFS_SGI_ENV
603         AFS_GUNLOCK();
604         exit(CLD_EXITED, code);
605 #endif /* AFS_SGI_ENV */
606     }
607 #endif /* RXK_LISTENER_ENV */
608     else if (parm == AFSOP_START_AFS) {
609         /* afs daemon */
610         if (AFS_Running)
611             goto out;
612         AFS_Running = 1;
613         while (afs_initState < AFSOP_START_AFS)
614             afs_osi_Sleep(&afs_initState);
615
616         afs_initState = AFSOP_START_BKG;
617         afs_osi_Wakeup(&afs_initState);
618         afs_osi_Invisible();
619         afs_Daemon();
620         afs_osi_Visible();
621 #ifdef AFS_SGI_ENV
622         AFS_GUNLOCK();
623         exit(CLD_EXITED, 0);
624 #endif /* AFS_SGI_ENV */
625     } else if (parm == AFSOP_START_CS) {
626         afs_osi_Invisible();
627         afs_CheckServerDaemon();
628         afs_osi_Visible();
629 #ifdef AFS_SGI_ENV
630         AFS_GUNLOCK();
631         exit(CLD_EXITED, 0);
632 #endif /* AFS_SGI_ENV */
633     } else if (parm == AFSOP_START_BKG) {
634         while (afs_initState < AFSOP_START_BKG)
635             afs_osi_Sleep(&afs_initState);
636         if (afs_initState < AFSOP_GO) {
637             afs_initState = AFSOP_GO;
638             afs_osi_Wakeup(&afs_initState);
639         }
640         /* start the bkg daemon */
641         afs_osi_Invisible();
642 #ifdef AFS_AIX32_ENV
643         if (parm2)
644             afs_BioDaemon(parm2);
645         else
646 #endif /* AFS_AIX32_ENV */
647             afs_BackgroundDaemon();
648         afs_osi_Visible();
649 #ifdef AFS_SGI_ENV
650         AFS_GUNLOCK();
651         exit(CLD_EXITED, 0);
652 #endif /* AFS_SGI_ENV */
653     } else if (parm == AFSOP_START_TRUNCDAEMON) {
654         while (afs_initState < AFSOP_GO)
655             afs_osi_Sleep(&afs_initState);
656         /* start the bkg daemon */
657         afs_osi_Invisible();
658         afs_CacheTruncateDaemon();
659         afs_osi_Visible();
660 #ifdef  AFS_SGI_ENV
661         AFS_GUNLOCK();
662         exit(CLD_EXITED, 0);
663 #endif /* AFS_SGI_ENV */
664     }
665 #if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
666     else if (parm == AFSOP_RXEVENT_DAEMON) {
667         while (afs_initState < AFSOP_START_BKG)
668             afs_osi_Sleep(&afs_initState);
669         afs_osi_Invisible();
670         afs_rxevent_daemon();
671         afs_osi_Visible();
672 #ifdef AFS_SGI_ENV
673         AFS_GUNLOCK();
674         exit(CLD_EXITED, 0);
675 #endif /* AFS_SGI_ENV */
676     }
677 #endif /* AFS_SUN5_ENV || RXK_LISTENER_ENV */
678 #endif /* AFS_LINUX24_ENV && !UKERNEL */
679     else if (parm == AFSOP_BASIC_INIT) {
680         afs_int32 temp;
681
682         while (!afs_InitSetup_done)
683             afs_osi_Sleep(&afs_InitSetup_done);
684
685 #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)
686         temp = AFS_MINBUFFERS;  /* Should fix this soon */
687 #else
688         /* number of 2k buffers we could get from all of the buffer space */
689         temp = ((afs_bufferpages * NBPG) >> 11);
690         temp = temp >> 2;       /* don't take more than 25% (our magic parameter) */
691         if (temp < AFS_MINBUFFERS)
692             temp = AFS_MINBUFFERS;      /* though we really should have this many */
693 #endif
694         DInit(temp);
695         afs_rootFid.Fid.Volume = 0;
696         code = 0;
697     } else if (parm == AFSOP_BUCKETPCT) {
698         /* need to enable this now, will disable again before GO
699            if we don't have 100% */
700         splitdcache = 1;
701         switch (parm2) {
702         case 1:
703             afs_tpct1 = parm3;
704             break;
705         case 2:
706             afs_tpct2 = parm3;
707             break;
708         }           
709     } else if (parm == AFSOP_ADDCELL) {
710         /* add a cell.  Parameter 2 is 8 hosts (in net order),  parm 3 is the null-terminated
711          * name.  Parameter 4 is the length of the name, including the null.  Parm 5 is the
712          * home cell flag (0x1 bit) and the nosuid flag (0x2 bit) */
713         struct afsop_cell *tcell = afs_osi_Alloc(sizeof(struct afsop_cell));
714
715         code = afs_InitDynroot();
716         if (!code) {
717             AFS_COPYIN(AFSKPTR(parm2), (caddr_t)tcell->hosts, sizeof(tcell->hosts),
718                        code);
719         }
720         if (!code) {
721             if (parm4 > sizeof(tcell->cellName))
722                 code = EFAULT;
723             else {
724               AFS_COPYIN(AFSKPTR(parm3), (caddr_t)tcell->cellName, parm4, code);
725                 if (!code)
726                     afs_NewCell(tcell->cellName, tcell->hosts, parm5, NULL, 0,
727                                 0, 0);
728             }
729         }
730         afs_osi_Free(tcell, sizeof(struct afsop_cell));
731     } else if (parm == AFSOP_ADDCELL2) {
732         struct afsop_cell *tcell = afs_osi_Alloc(sizeof(struct afsop_cell));
733         char *tbuffer = osi_AllocSmallSpace(AFS_SMALLOCSIZ), *lcnamep = 0;
734         char *tbuffer1 = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
735         int cflags = parm4;
736
737         code = afs_InitDynroot();
738         if (!code) {
739 #if 0
740             /* wait for basic init - XXX can't find any reason we need this? */
741             while (afs_initState < AFSOP_START_BKG)
742                 afs_osi_Sleep(&afs_initState);
743 #endif
744
745             AFS_COPYIN(AFSKPTR(parm2), (caddr_t)tcell->hosts, sizeof(tcell->hosts),
746                        code);
747         }
748         if (!code) {
749             AFS_COPYINSTR(AFSKPTR(parm3), tbuffer1, AFS_SMALLOCSIZ,
750                           &bufferSize, code);
751             if (!code) {
752                 if (parm4 & 4) {
753                     AFS_COPYINSTR(AFSKPTR(parm5), tbuffer, AFS_SMALLOCSIZ,
754                                   &bufferSize, code);
755                     if (!code) {
756                         lcnamep = tbuffer;
757                         cflags |= CLinkedCell;
758                     }
759                 }
760                 if (!code)
761                     code =
762                         afs_NewCell(tbuffer1, tcell->hosts, cflags, lcnamep,
763                                     0, 0, 0);
764             }
765         }
766         afs_osi_Free(tcell, sizeof(struct afsop_cell));
767         osi_FreeSmallSpace(tbuffer);
768         osi_FreeSmallSpace(tbuffer1);
769     } else if (parm == AFSOP_ADDCELLALIAS) {
770         /*
771          * Call arguments:
772          * parm2 is the alias name
773          * parm3 is the real cell name
774          */
775         char *aliasName = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
776         char *cellName = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
777
778         code = afs_InitDynroot();
779         if (!code) {
780             AFS_COPYINSTR(AFSKPTR(parm2), aliasName, AFS_SMALLOCSIZ, &bufferSize,
781                           code);
782         }
783         if (!code)
784             AFS_COPYINSTR(AFSKPTR(parm3), cellName, AFS_SMALLOCSIZ,
785                           &bufferSize, code);
786         if (!code)
787             afs_NewCellAlias(aliasName, cellName);
788         osi_FreeSmallSpace(aliasName);
789         osi_FreeSmallSpace(cellName);
790     } else if (parm == AFSOP_SET_THISCELL) {
791         /*
792          * Call arguments:
793          * parm2 is the primary cell name
794          */
795         char *cell = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
796
797         code = afs_InitDynroot();
798         if (!code) {
799             AFS_COPYINSTR(AFSKPTR(parm2), cell, AFS_SMALLOCSIZ, &bufferSize, code);
800         }
801         if (!code)
802             afs_SetPrimaryCell(cell);
803         osi_FreeSmallSpace(cell);
804     } else if (parm == AFSOP_CACHEINIT) {
805         struct afs_cacheParams cparms;
806
807         if (afs_CacheInit_Done)
808             goto out;
809
810         AFS_COPYIN(AFSKPTR(parm2), (caddr_t) & cparms, sizeof(cparms), code);
811         if (code) {
812 #if defined(KERNEL_HAVE_UERROR)
813             setuerror(code);
814             code = -1;
815 #endif
816             goto out;
817         }
818         afs_CacheInit_Done = 1;
819         code = afs_icl_InitLogs();
820         afs_setTime = cparms.setTimeFlag;
821
822         code =
823             afs_CacheInit(cparms.cacheScaches, cparms.cacheFiles,
824                           cparms.cacheBlocks, cparms.cacheDcaches,
825                           cparms.cacheVolumes, cparms.chunkSize,
826                           cparms.memCacheFlag, cparms.inodes, cparms.users, 
827                           cparms.dynamic_vcaches);
828
829     } else if (parm == AFSOP_CACHEINODE) {
830         ino_t ainode = parm2;
831         /* wait for basic init */
832         while (afs_initState < AFSOP_START_BKG)
833             afs_osi_Sleep(&afs_initState);
834
835 #ifdef AFS_DARWIN80_ENV
836         get_vfs_context();
837 #endif
838         /* do it by inode */
839 #ifdef AFS_SGI62_ENV
840         ainode = (ainode << 32) | (parm3 & 0xffffffff);
841 #endif
842         code = afs_InitCacheFile(NULL, ainode);
843 #ifdef AFS_DARWIN80_ENV
844         put_vfs_context();
845 #endif
846     } else if (parm == AFSOP_CACHEDIRS) {
847         afs_numfilesperdir = parm2;
848         afs_osi_Wakeup(&afs_initState);
849     } else if (parm == AFSOP_CACHEFILES) {
850         afs_numcachefiles = parm2;
851         afs_osi_Wakeup(&afs_initState);
852     } else if (parm == AFSOP_ROOTVOLUME) {
853         /* wait for basic init */
854         while (afs_initState < AFSOP_START_BKG)
855             afs_osi_Sleep(&afs_initState);
856
857         if (parm2) {
858             AFS_COPYINSTR(AFSKPTR(parm2), afs_rootVolumeName,
859                           sizeof(afs_rootVolumeName), &bufferSize, code);
860             afs_rootVolumeName[sizeof(afs_rootVolumeName) - 1] = 0;
861         } else
862             code = 0;
863     } else if (parm == AFSOP_CACHEFILE || parm == AFSOP_CACHEINFO
864                || parm == AFSOP_VOLUMEINFO || parm == AFSOP_AFSLOG
865                || parm == AFSOP_CELLINFO || parm == AFSOP_CACHEBASEDIR) {
866         char *tbuffer = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
867
868         code = 0;
869         AFS_COPYINSTR(AFSKPTR(parm2), tbuffer, AFS_SMALLOCSIZ, &bufferSize,
870                       code);
871         if (code) {
872             osi_FreeSmallSpace(tbuffer);
873             goto out;
874         }
875         if (!code) {
876             tbuffer[AFS_SMALLOCSIZ - 1] = '\0'; /* null-terminate the name */
877             /* We have the cache dir copied in.  Call the cache init routine */
878 #ifdef AFS_DARWIN80_ENV
879             get_vfs_context();
880 #endif
881             if (parm == AFSOP_CACHEBASEDIR) {
882                 strncpy(afs_cachebasedir, tbuffer, 1024);
883                 afs_cachebasedir[1023] = '\0';
884                 afs_osi_Wakeup(&afs_initState);
885             } else if (parm == AFSOP_CACHEFILE) {
886                 wait_for_cachedefs();
887                 code = afs_InitCacheFile(tbuffer, 0);
888             } else if (parm == AFSOP_CACHEINFO) {
889                 wait_for_cachedefs();
890                 code = afs_InitCacheInfo(tbuffer);
891             } else if (parm == AFSOP_VOLUMEINFO) {
892                 wait_for_cachedefs();
893                 code = afs_InitVolumeInfo(tbuffer);
894             } else if (parm == AFSOP_CELLINFO) {
895                 wait_for_cachedefs();
896                 code = afs_InitCellInfo(tbuffer);
897             }
898 #ifdef AFS_DARWIN80_ENV
899             put_vfs_context();
900 #endif
901         }
902         osi_FreeSmallSpace(tbuffer);
903     } else if (parm == AFSOP_GO) {
904 #ifdef AFS_CACHE_VNODE_PATH
905         if (cacheDiskType != AFS_FCACHE_TYPE_MEM) {
906             afs_int32 dummy;
907             
908             wait_for_cachedefs();
909             
910 #ifdef AFS_DARWIN80_ENV
911             get_vfs_context();
912 #endif
913             if ((afs_numcachefiles > 0) && (afs_numfilesperdir > 0) && 
914                 (afs_cachebasedir[0] == '/')) {
915                 for (dummy = 0; dummy < afs_numcachefiles; dummy++) {
916                     code = afs_InitCacheFile(NULL, dummy);
917                 }
918             }
919 #ifdef AFS_DARWIN80_ENV
920             put_vfs_context();
921 #endif
922         }
923 #endif
924         /* the generic initialization calls come here.  One parameter: should we do the
925          * set-time operation on this workstation */
926         if (afs_Go_Done)
927             goto out;
928         afs_Go_Done = 1;
929         while (afs_initState < AFSOP_GO)
930             afs_osi_Sleep(&afs_initState);
931         afs_initState = 101;
932         afs_setTime = parm2;
933         if (afs_tpct1 + afs_tpct2 != 100) {
934             afs_tpct1 = 0;
935             afs_tpct2 = 0;
936             splitdcache = 0;
937         } else {        
938             splitdcache = 1;
939         }
940         afs_osi_Wakeup(&afs_initState);
941 #if     (!defined(AFS_NONFSTRANS)) || defined(AFS_AIX_IAUTH_ENV)
942         afs_nfsclient_init();
943 #endif
944         afs_uuid_create(&afs_cb_interface.uuid);
945         printf("found %d non-empty cache files (%d%%).\n",
946                afs_stats_cmperf.cacheFilesReused,
947                (100 * afs_stats_cmperf.cacheFilesReused) /
948                (afs_stats_cmperf.cacheNumEntries ? afs_stats_cmperf.
949                 cacheNumEntries : 1));
950     } else if (parm == AFSOP_ADVISEADDR) {
951         /* pass in the host address to the rx package */
952         int rxbind = 0;
953         int refresh = 0;
954
955         afs_int32 count = parm2;
956         afs_int32 *buffer =
957             afs_osi_Alloc(sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
958         afs_int32 *maskbuffer =
959             afs_osi_Alloc(sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
960         afs_int32 *mtubuffer =
961             afs_osi_Alloc(sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
962         int i;
963
964         /* This is a refresh */
965         if (count & 0x40000000) {
966             count &= ~0x40000000;
967             /* Can't bind after we start. Fix? */
968             count &= ~0x80000000;
969             refresh = 1;
970         }
971
972         /* Bind, but only if there's only one address configured */ 
973         if ( count & 0x80000000) {
974             count &= ~0x80000000;
975             if (count == 1)
976                 rxbind=1;
977         }
978
979         if (count > AFS_MAX_INTERFACE_ADDR) {
980             code = ENOMEM;
981             count = AFS_MAX_INTERFACE_ADDR;
982         }
983
984         AFS_COPYIN(AFSKPTR(parm3), (caddr_t)buffer, count * sizeof(afs_int32),
985                    code);
986         if (parm4 && !code)
987             AFS_COPYIN(AFSKPTR(parm4), (caddr_t)maskbuffer,
988                        count * sizeof(afs_int32), code);
989         if (parm5 && !code)
990             AFS_COPYIN(AFSKPTR(parm5), (caddr_t)mtubuffer,
991                        count * sizeof(afs_int32), code);
992
993         if (!code) {
994             afs_cb_interface.numberOfInterfaces = count;
995             for (i = 0; i < count; i++) {
996                 afs_cb_interface.addr_in[i] = buffer[i];
997 #ifdef AFS_USERSPACE_IP_ADDR
998                 /* AFS_USERSPACE_IP_ADDR means we have no way of finding the
999                  * machines IP addresses when in the kernel (the in_ifaddr
1000                  * struct is not available), so we pass the info in at
1001                  * startup. We also pass in the subnetmask and mtu size. The
1002                  * subnetmask is used when setting the rank:
1003                  * afsi_SetServerIPRank(); and the mtu size is used when
1004                  * finding the best mtu size. rxi_FindIfnet() is replaced
1005                  * with rxi_Findcbi().
1006                  */
1007                 afs_cb_interface.subnetmask[i] =
1008                     (parm4 ? maskbuffer[i] : 0xffffffff);
1009                 afs_cb_interface.mtu[i] = (parm5 ? mtubuffer[i] : htonl(1500));
1010 #endif
1011             }
1012             rxi_setaddr(buffer[0]);
1013             if (!refresh) {
1014                 if (rxbind)
1015                     rx_bindhost = buffer[0];
1016                 else
1017                     rx_bindhost = htonl(INADDR_ANY);
1018             }
1019         } else {
1020             refresh = 0;
1021         }
1022
1023         afs_osi_Free(buffer, sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
1024         afs_osi_Free(maskbuffer, sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
1025         afs_osi_Free(mtubuffer, sizeof(afs_int32) * AFS_MAX_INTERFACE_ADDR);
1026
1027         if (refresh) {
1028             afs_CheckServers(1, NULL);     /* check down servers */
1029             afs_CheckServers(0, NULL);     /* check down servers */
1030         }
1031     }
1032 #ifdef  AFS_SGI53_ENV
1033     else if (parm == AFSOP_NFSSTATICADDR) {
1034         extern int (*nfs_rfsdisptab_v2) ();
1035         nfs_rfsdisptab_v2 = (int (*)())parm2;
1036     } else if (parm == AFSOP_NFSSTATICADDR2) {
1037         extern int (*nfs_rfsdisptab_v2) ();
1038 #ifdef _K64U64
1039         nfs_rfsdisptab_v2 = (int (*)())((parm2 << 32) | (parm3 & 0xffffffff));
1040 #else /* _K64U64 */
1041         nfs_rfsdisptab_v2 = (int (*)())(parm3 & 0xffffffff);
1042 #endif /* _K64U64 */
1043     }
1044 #if defined(AFS_SGI62_ENV) && !defined(AFS_SGI65_ENV)
1045     else if (parm == AFSOP_SBLOCKSTATICADDR2) {
1046         extern int (*afs_sblockp) ();
1047         extern void (*afs_sbunlockp) ();
1048 #ifdef _K64U64
1049         afs_sblockp = (int (*)())((parm2 << 32) | (parm3 & 0xffffffff));
1050         afs_sbunlockp = (void (*)())((parm4 << 32) | (parm5 & 0xffffffff));
1051 #else
1052         afs_sblockp = (int (*)())(parm3 & 0xffffffff);
1053         afs_sbunlockp = (void (*)())(parm5 & 0xffffffff);
1054 #endif /* _K64U64 */
1055     }
1056 #endif /* AFS_SGI62_ENV && !AFS_SGI65_ENV */
1057 #endif /* AFS_SGI53_ENV */
1058     else if (parm == AFSOP_SHUTDOWN) {
1059         afs_cold_shutdown = 0;
1060         if (parm2 == 1)
1061             afs_cold_shutdown = 1;
1062 #ifndef AFS_DARWIN_ENV
1063         if (afs_globalVFS != 0) {
1064             afs_warn("AFS isn't unmounted yet! Call aborted\n");
1065             code = EACCES;
1066         } else
1067 #endif
1068             afs_shutdown();
1069     } else if (parm == AFSOP_AFS_VFSMOUNT) {
1070 #ifdef  AFS_HPUX_ENV
1071         vfsmount(parm2, parm3, parm4, parm5);
1072 #else /* defined(AFS_HPUX_ENV) */
1073 #if defined(KERNEL_HAVE_UERROR)
1074         setuerror(EINVAL);
1075 #else
1076         code = EINVAL;
1077 #endif
1078 #endif /* defined(AFS_HPUX_ENV) */
1079     } else if (parm == AFSOP_CLOSEWAIT) {
1080         afs_SynchronousCloses = 'S';
1081     } else if (parm == AFSOP_GETMTU) {
1082         afs_uint32 mtu = 0;
1083 #if     !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
1084 #ifdef AFS_USERSPACE_IP_ADDR
1085         afs_int32 i;
1086         i = rxi_Findcbi(parm2);
1087         mtu = ((i == -1) ? htonl(1500) : afs_cb_interface.mtu[i]);
1088 #else /* AFS_USERSPACE_IP_ADDR */
1089         AFS_IFNET_T tifnp;
1090
1091         tifnp = rxi_FindIfnet(parm2, NULL);     /*  make iterative */
1092         mtu = (tifnp ? ifnet_mtu(tifnp) : htonl(1500));
1093 #endif /* else AFS_USERSPACE_IP_ADDR */
1094 #endif /* !AFS_SUN5_ENV */
1095         if (!code)
1096             AFS_COPYOUT((caddr_t) & mtu, AFSKPTR(parm3),
1097                         sizeof(afs_int32), code);
1098 #ifdef AFS_AIX32_ENV
1099 /* this is disabled for now because I can't figure out how to get access
1100  * to these kernel variables.  It's only for supporting user-mode rx
1101  * programs -- it makes a huge difference on the 220's in my testbed,
1102  * though I don't know why. The bosserver does this with /etc/no, so it's
1103  * being handled a different way for the servers right now.  */
1104 /*      {
1105         static adjusted = 0;
1106         extern u_long sb_max_dflt;
1107         if (!adjusted) {
1108           adjusted = 1;
1109           if (sb_max_dflt < 131072) sb_max_dflt = 131072; 
1110           if (sb_max < 131072) sb_max = 131072; 
1111         }
1112       } */
1113 #endif /* AFS_AIX32_ENV */
1114     } else if (parm == AFSOP_GETMASK) { /* parm2 == addr in net order */
1115         afs_uint32 mask = 0;
1116 #if     !defined(AFS_SUN5_ENV)
1117 #ifdef AFS_USERSPACE_IP_ADDR
1118         afs_int32 i;
1119         i = rxi_Findcbi(parm2);
1120         if (i != -1) {
1121             mask = afs_cb_interface.subnetmask[i];
1122         } else {
1123             code = -1;
1124         }
1125 #else /* AFS_USERSPACE_IP_ADDR */
1126         AFS_IFNET_T tifnp;
1127
1128         tifnp = rxi_FindIfnet(parm2, &mask);    /* make iterative */
1129         if (!tifnp)
1130             code = -1;
1131 #endif /* else AFS_USERSPACE_IP_ADDR */
1132 #endif /* !AFS_SUN5_ENV */
1133         if (!code)
1134             AFS_COPYOUT((caddr_t) & mask, AFSKPTR(parm3),
1135                         sizeof(afs_int32), code);
1136     }
1137     else if (parm == AFSOP_AFSDB_HANDLER) {
1138         int sizeArg = (int)parm4;
1139         int kmsgLen = sizeArg & 0xffff;
1140         int cellLen = (sizeArg & 0xffff0000) >> 16;
1141         afs_int32 *kmsg = afs_osi_Alloc(kmsgLen);
1142         char *cellname = afs_osi_Alloc(cellLen);
1143
1144 #ifndef UKERNEL
1145         afs_osi_MaskUserLoop();
1146 #endif
1147         AFS_COPYIN(AFSKPTR(parm2), cellname, cellLen, code);
1148         AFS_COPYIN(AFSKPTR(parm3), kmsg, kmsgLen, code);
1149         if (!code) {
1150             code = afs_AFSDBHandler(cellname, cellLen, kmsg);
1151             if (*cellname == 1)
1152                 *cellname = 0;
1153             if (code == -2) {   /* Shutting down? */
1154                 *cellname = 1;
1155                 code = 0;
1156             }
1157         }
1158         if (!code)
1159             AFS_COPYOUT(cellname, AFSKPTR(parm2), cellLen, code);
1160         afs_osi_Free(kmsg, kmsgLen);
1161         afs_osi_Free(cellname, cellLen);
1162     }
1163     else if (parm == AFSOP_SET_DYNROOT) {
1164         code = afs_SetDynrootEnable(parm2);
1165     } else if (parm == AFSOP_SET_FAKESTAT) {
1166         afs_fakestat_enable = parm2;
1167         code = 0;
1168     } else if (parm == AFSOP_SET_BACKUPTREE) {
1169         afs_bkvolpref = parm2;
1170     } else if (parm == AFSOP_SET_RXPCK) {
1171         rx_extraPackets = parm2;
1172         afscall_set_rxpck_received = 1;
1173     } else if (parm == AFSOP_SET_RXMAXMTU) {
1174         rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = parm2;
1175     } else {
1176         code = EINVAL;
1177     }
1178
1179   out:
1180     AFS_GUNLOCK();
1181 #ifdef AFS_LINUX20_ENV
1182     return -code;
1183 #else
1184     return code;
1185 #endif
1186 }
1187
1188 /*
1189  * Initstate in the range 0 < x < 100 are early initialization states.
1190  * Initstate of 100 means a AFSOP_START operation has been done.  After this,
1191  *  the cache may be initialized.
1192  * Initstate of 101 means a AFSOP_GO operation has been done.  This operation
1193  *  is done after all the cache initialization has been done.
1194  * Initstate of 200 means that the volume has been looked up once, possibly
1195  *  incorrectly.
1196  * Initstate of 300 means that the volume has been *successfully* looked up.
1197  */
1198 int
1199 afs_CheckInit(void)
1200 {
1201     register int code = 0;
1202
1203     AFS_STATCNT(afs_CheckInit);
1204     if (afs_initState <= 100)
1205         code = ENXIO;           /* never finished init phase */
1206     else if (afs_initState == 101) {    /* init done, wait for afs_daemon */
1207         while (afs_initState < 200)
1208             afs_osi_Sleep(&afs_initState);
1209     } else if (afs_initState == 200)
1210         code = ETIMEDOUT;       /* didn't find root volume */
1211     return code;
1212 }
1213
1214 int afs_shuttingdown = 0;
1215 void
1216 afs_shutdown(void)
1217 {
1218     extern short afs_brsDaemons;
1219     extern afs_int32 afs_CheckServerDaemonStarted;
1220     extern struct afs_osi_WaitHandle AFS_WaitHandler, AFS_CSWaitHandler;
1221     extern struct osi_file *afs_cacheInodep;
1222
1223     AFS_STATCNT(afs_shutdown);
1224     if (afs_initState == 0) {
1225         afs_warn("AFS not initialized - not shutting down\n");
1226       return;
1227     }
1228
1229     if (afs_shuttingdown)
1230         return;
1231     afs_shuttingdown = 1;
1232     if (afs_cold_shutdown)
1233         afs_warn("afs: COLD ");
1234     else
1235         afs_warn("afs: WARM ");
1236     afs_warn("shutting down of: CB... ");
1237
1238     afs_termState = AFSOP_STOP_RXCALLBACK;
1239     rx_WakeupServerProcs();
1240 #ifdef AFS_AIX51_ENV
1241     shutdown_rxkernel();
1242 #endif
1243     /* close rx server connections here? */
1244     while (afs_termState == AFSOP_STOP_RXCALLBACK)
1245         afs_osi_Sleep(&afs_termState);
1246
1247     afs_warn("afs... ");
1248     while (afs_termState == AFSOP_STOP_AFS) {
1249         afs_osi_CancelWait(&AFS_WaitHandler);
1250         afs_osi_Sleep(&afs_termState);
1251     }
1252     if (afs_CheckServerDaemonStarted) {
1253         while (afs_termState == AFSOP_STOP_CS) {
1254             afs_osi_CancelWait(&AFS_CSWaitHandler);
1255             afs_osi_Sleep(&afs_termState);
1256         }
1257     }
1258     afs_warn("BkG... ");
1259     /* Wake-up afs_brsDaemons so that we don't have to wait for a bkg job! */
1260     while (afs_termState == AFSOP_STOP_BKG) {
1261         afs_osi_Wakeup(&afs_brsDaemons);
1262         afs_osi_Sleep(&afs_termState);
1263     }
1264     afs_warn("CTrunc... ");
1265     /* Cancel cache truncate daemon. */
1266     while (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
1267         afs_osi_Wakeup((char *)&afs_CacheTruncateDaemon);
1268         afs_osi_Sleep(&afs_termState);
1269     }
1270     afs_warn("AFSDB... ");
1271     afs_StopAFSDB();
1272     while (afs_termState == AFSOP_STOP_AFSDB)
1273         afs_osi_Sleep(&afs_termState);
1274 #if     defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
1275     afs_warn("RxEvent... ");
1276     /* cancel rx event daemon */
1277     while (afs_termState == AFSOP_STOP_RXEVENT)
1278         afs_osi_Sleep(&afs_termState);
1279 #if defined(RXK_LISTENER_ENV)
1280 #ifndef UKERNEL
1281     afs_warn("UnmaskRxkSignals... ");
1282     afs_osi_UnmaskRxkSignals();
1283 #endif
1284     /* cancel rx listener */
1285     afs_warn("RxListener... ");
1286     osi_StopListener();         /* This closes rx_socket. */
1287     while (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1288         afs_warn("Sleep... ");
1289         afs_osi_Sleep(&afs_termState);
1290     }
1291 #endif
1292 #else
1293     afs_termState = AFSOP_STOP_COMPLETE;
1294 #endif
1295
1296 #ifdef AFS_AIX51_ENV
1297     shutdown_daemons();
1298 #endif
1299     shutdown_CB();
1300     shutdown_bufferpackage();
1301     shutdown_cache();
1302     shutdown_osi();
1303     /*
1304      * Close file only after daemons which can write to it are stopped.
1305      * Need to close before the osinet shutdown to avoid failing check
1306      * for dangling memory allocations.
1307      */
1308     if (afs_cacheInodep) {      /* memcache won't set this */
1309         osi_UFSClose(afs_cacheInodep);  /* Since we always leave it open */
1310         afs_cacheInodep = 0;
1311     }
1312     /*
1313      * Shutdown the ICL logs - needed to free allocated memory space and avoid
1314      * warnings from shutdown_osinet
1315      */
1316     shutdown_icl();
1317     shutdown_osinet();
1318     shutdown_osifile();
1319     shutdown_vnodeops();
1320     shutdown_memcache();
1321     shutdown_xscache();
1322 #if (!defined(AFS_NONFSTRANS) || defined(AFS_AIX_IAUTH_ENV))
1323     shutdown_exporter();
1324     shutdown_nfsclnt();
1325 #endif
1326     shutdown_afstest();
1327     shutdown_AFS();
1328     /* The following hold the cm stats */
1329     memset(&afs_cmstats, 0, sizeof(struct afs_CMStats));
1330     memset(&afs_stats_cmperf, 0, sizeof(struct afs_stats_CMPerf));
1331     memset(&afs_stats_cmfullperf, 0, sizeof(struct afs_stats_CMFullPerf));
1332     afs_warn(" ALL allocated tables... ");
1333
1334     afs_shuttingdown = 0;
1335     afs_warn("done\n");
1336
1337     return;                     /* Just kill daemons for now */
1338 }
1339
1340 void
1341 shutdown_afstest(void)
1342 {
1343     AFS_STATCNT(shutdown_afstest);
1344     afs_initState = afs_termState = afs_setTime = 0;
1345     AFS_Running = afs_CB_Running = 0;
1346     afs_CacheInit_Done = afs_Go_Done = 0;
1347     if (afs_cold_shutdown) {
1348         *afs_rootVolumeName = 0;
1349     }
1350 }