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