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