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