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