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