afs: Move init_hckernel_init to osi_Init
[openafs.git] / src / afs / afs_call.c
index 3da9264..43d7778 100644 (file)
 #include <afsconfig.h>
 #include "afs/param.h"
 
+#if defined(HAVE_LINUX_KTHREAD_RUN) && !defined(UKERNEL)
+#  include "h/kthread.h"
+#endif
 
 #include "afs/sysincludes.h"   /* Standard vendor system headers */
 #include "afsincludes.h"       /* Afs-based standard headers */
 #include "afs/afs_stats.h"
 #include "rx/rx_globals.h"
-#if !defined(UKERNEL) && !defined(AFS_LINUX20_ENV)
-#include "net/if.h"
-#ifdef AFS_SGI62_ENV
-#include "h/hashing.h"
-#endif
-#if !defined(AFS_HPUX110_ENV) && !defined(AFS_DARWIN_ENV)
-#include "netinet/in_var.h"
-#endif
+#if !defined(UKERNEL)
+# if !defined(AFS_LINUX20_ENV)
+#  include "net/if.h"
+#  ifdef AFS_SGI62_ENV
+#   include "h/hashing.h"
+#  endif
+#  if !defined(AFS_HPUX110_ENV) && !defined(AFS_DARWIN_ENV)
+#   include "netinet/in_var.h"
+#  endif
+# endif
 #endif /* !defined(UKERNEL) */
-#ifdef AFS_LINUX22_ENV
-#include "h/smp_lock.h"
-#endif
 #ifdef AFS_SUN510_ENV
 #include "h/ksynch.h"
 #include "h/sunddi.h"
 #endif
+#include <hcrypto/rand.h>
 
 #if defined(AFS_SUN5_ENV) || defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_HPUX_ENV)
 #define        AFS_MINBUFFERS  100
@@ -38,7 +41,7 @@
 #define        AFS_MINBUFFERS  50
 #endif
 
-#if ((defined(AFS_LINUX24_ENV) && defined(HAVE_LINUX_COMPLETION_H)) || defined(AFS_DARWIN80_ENV)) && !defined(UKERNEL)
+#if (defined(AFS_SUN5_ENV) || (defined(AFS_LINUX24_ENV) && defined(HAVE_LINUX_COMPLETION_H)) || defined(AFS_DARWIN80_ENV)) && !defined(UKERNEL)
 /* If AFS_DAEMONOP_ENV is defined, it indicates we run "daemon" AFS syscalls by
  * spawning a kernel thread to do the work, instead of running them in the
  * calling process. */
@@ -61,7 +64,6 @@ krwlock_t afsifinfo_lock;
 
 afs_int32 afs_initState = 0;
 afs_int32 afs_termState = 0;
-afs_int32 afs_setTime = 0;
 int afs_cold_shutdown = 0;
 char afs_SynchronousCloses = '\0';
 static int afs_CB_Running = 0;
@@ -69,15 +71,19 @@ static int AFS_Running = 0;
 static int afs_CacheInit_Done = 0;
 static int afs_Go_Done = 0;
 extern struct interfaceAddr afs_cb_interface;
+#ifdef RXK_LISTENER_ENV
 static int afs_RX_Running = 0;
+#endif
 static int afs_InitSetup_done = 0;
 afs_int32 afs_numcachefiles = -1;
 afs_int32 afs_numfilesperdir = -1;
 char afs_cachebasedir[1024];
+afs_int32 afs_rmtsys_enable = 0;
 
 afs_int32 afs_rx_deadtime = AFS_RXDEADTIME;
 afs_int32 afs_rx_harddead = AFS_HARDDEADTIME;
 afs_int32 afs_rx_idledead = AFS_IDLEDEADTIME;
+afs_int32 afs_rx_idledead_rep = AFS_IDLEDEADTIME_REP;
 
 static int afscall_set_rxpck_received = 0;
 
@@ -117,7 +123,7 @@ afs_InitSetup(int preallocs)
 
     memset(afs_zeros, 0, AFS_ZEROS);
 
-    rx_SetBusyChannelError(RX_CALL_TIMEOUT);
+    rx_SetBusyChannelError(1);  /* turn on busy call error reporting */
 
     /* start RX */
     if(!afscall_set_rxpck_received)
@@ -293,11 +299,13 @@ afsd_thread(void *rock)
 # ifdef SYS_SETPRIORITY_EXPORTED
     int (*sys_setpriority) (int, int, int) = sys_call_table[__NR_setpriority];
 # endif
-# if defined(AFS_LINUX26_ENV)
+# if !defined(HAVE_LINUX_KTHREAD_RUN)
+#  if defined(AFS_LINUX26_ENV)
     daemonize("afsd");
-# else
+#  else
     daemonize();
-# endif
+#  endif
+# endif /* !HAVE_LINUX_KTHREAD_RUN */
                                /* doesn't do much, since we were forked from keventd, but
                                 * does call mm_release, which wakes up our parent (since it
                                 * used CLONE_VFORK) */
@@ -338,6 +346,9 @@ afsd_thread(void *rock)
        complete_and_exit(0, 0);
        break;
     case AFSOP_START_BKG:
+#ifdef AFS_NEW_BKG
+       afs_warn("Install matching afsd! Old background daemons not supported.\n");
+#else
        sprintf(current->comm, "afs_bkgstart");
        AFS_GLOCK();
        complete(arg->complete);
@@ -350,6 +361,7 @@ afsd_thread(void *rock)
        sprintf(current->comm, "afs_background");
        afs_BackgroundDaemon();
        AFS_GUNLOCK();
+#endif
        complete_and_exit(0, 0);
        break;
     case AFSOP_START_TRUNCDAEMON:
@@ -430,8 +442,14 @@ afsd_launcher(void *rock)
     struct afsd_thread_info *rock = container_of(work, struct afsd_thread_info, tq);
 # endif
 
+# if defined(HAVE_LINUX_KTHREAD_RUN)
+    if (IS_ERR(kthread_run(afsd_thread, (void *)rock, "afsd"))) {
+       afs_warn("kthread_run failed; afs startup will not complete\n");
+    }
+# else /* !HAVE_LINUX_KTHREAD_RUN */
     if (!kernel_thread(afsd_thread, (void *)rock, CLONE_VFORK | SIGCHLD))
        afs_warn("kernel_thread failed. afs startup will not complete\n");
+# endif /* !HAVE_LINUX_KTHREAD_RUN */
 }
 
 void
@@ -474,6 +492,115 @@ afs_DaemonOp(long parm, long parm2, long parm3, long parm4, long parm5,
 }
 #endif
 
+#ifdef AFS_SUN5_ENV
+struct afs_daemonop_args {
+    kcondvar_t cv;
+    long parm;
+};
+
+static void
+afsd_thread(struct afs_daemonop_args *args)
+{
+    long parm = args->parm;
+
+    AFS_GLOCK();
+    cv_signal(&args->cv);
+
+    switch (parm) {
+    case AFSOP_START_RXCALLBACK:
+       if (afs_CB_Running)
+           goto out;
+       afs_CB_Running = 1;
+       while (afs_RX_Running != 2)
+           afs_osi_Sleep(&afs_RX_Running);
+       afs_RXCallBackServer();
+       AFS_GUNLOCK();
+       return;
+    case AFSOP_START_AFS:
+       if (AFS_Running)
+           goto out;
+       AFS_Running = 1;
+       while (afs_initState < AFSOP_START_AFS)
+           afs_osi_Sleep(&afs_initState);
+       afs_initState = AFSOP_START_BKG;
+       afs_osi_Wakeup(&afs_initState);
+       afs_Daemon();
+       AFS_GUNLOCK();
+       return;
+    case AFSOP_START_BKG:
+       while (afs_initState < AFSOP_START_BKG)
+           afs_osi_Sleep(&afs_initState);
+       if (afs_initState < AFSOP_GO) {
+           afs_initState = AFSOP_GO;
+           afs_osi_Wakeup(&afs_initState);
+       }
+       afs_BackgroundDaemon();
+       AFS_GUNLOCK();
+       return;
+    case AFSOP_START_TRUNCDAEMON:
+       while (afs_initState < AFSOP_GO)
+           afs_osi_Sleep(&afs_initState);
+       afs_CacheTruncateDaemon();
+       AFS_GUNLOCK();
+       return;
+    case AFSOP_START_CS:
+       afs_CheckServerDaemon();
+       AFS_GUNLOCK();
+       return;
+    case AFSOP_RXEVENT_DAEMON:
+       while (afs_initState < AFSOP_START_BKG)
+           afs_osi_Sleep(&afs_initState);
+       afs_rxevent_daemon();
+       AFS_GUNLOCK();
+       return;
+    case AFSOP_RXLISTENER_DAEMON:
+       afs_initState = AFSOP_START_AFS;
+       afs_osi_Wakeup(&afs_initState);
+       afs_RX_Running = 2;
+       afs_osi_Wakeup(&afs_RX_Running);
+       afs_osi_RxkRegister();
+       rxk_Listener();
+       AFS_GUNLOCK();
+       return;
+    default:
+       AFS_GUNLOCK();
+       afs_warn("Unknown op %ld in afsd_thread()\n", parm);
+       return;
+    }
+ out:
+    AFS_GUNLOCK();
+    return;
+}
+
+static void
+afs_DaemonOp(long parm, long parm2, long parm3, long parm4, long parm5,
+            long parm6)
+{
+    struct afs_daemonop_args args;
+
+    if (daemonOp_common(parm, parm2, parm3, parm4, parm5, parm6)) {
+       return;
+    }
+
+    args.parm = parm;
+
+    cv_init(&args.cv, "AFS DaemonOp cond var", CV_DEFAULT, NULL);
+
+    if (thread_create(NULL, 0, afsd_thread, &args, 0, &p0, TS_RUN,
+        minclsyspri) == NULL) {
+
+       afs_warn("thread_create failed: AFS startup will not complete\n");
+    }
+
+    /* we passed &args to the new thread, which is on the stack. wait until
+     * it has read the arguments so it doesn't try to read the args after we
+     * have returned */
+    cv_wait(&args.cv, &afs_global_lock);
+
+    cv_destroy(&args.cv);
+}
+#endif /* AFS_SUN5_ENV */
+
 #ifdef AFS_DARWIN100_ENV
 # define AFSKPTR(X) k ## X
 int
@@ -500,7 +627,7 @@ afs_syscall_call(long parm, long parm2, long parm3,
 #endif
 {
     afs_int32 code = 0;
-#if defined(AFS_SGI61_ENV) || defined(AFS_SUN57_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+#if defined(AFS_SGI61_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
     size_t bufferSize;
 #else /* AFS_SGI61_ENV */
     u_int bufferSize;
@@ -543,7 +670,7 @@ afs_syscall_call(long parm, long parm2, long parm3,
     put_vfs_context();
 #endif
 #ifdef AFS_DAEMONOP_ENV
-# if defined(AFS_DARWIN80_ENV)
+# if defined(AFS_NEW_BKG)
     if (parm == AFSOP_BKG_HANDLER) {
        /* if afs_uspc_param grows this should be checked */
        struct afs_uspc_param *mvParam = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
@@ -586,7 +713,7 @@ afs_syscall_call(long parm, long parm2, long parm3,
        afs_osi_Free(param2, namebufsz);
        osi_FreeSmallSpace(mvParam);
     } else
-# endif /* DARWIN80 */
+# endif /* AFS_NEW_BKG */
     if (parm < AFSOP_ADDCELL || parm == AFSOP_RXEVENT_DAEMON
        || parm == AFSOP_RXLISTENER_DAEMON) {
        afs_DaemonOp(parm, parm2, parm3, parm4, parm5, parm6);
@@ -678,7 +805,7 @@ afs_syscall_call(long parm, long parm2, long parm3,
        AFS_GUNLOCK();
        exit(CLD_EXITED, 0);
 # endif /* AFS_SGI_ENV */
-# ifndef AFS_DARWIN80_ENV
+# ifndef AFS_NEW_BKG
     } else if (parm == AFSOP_START_BKG) {
        while (afs_initState < AFSOP_START_BKG)
            afs_osi_Sleep(&afs_initState);
@@ -699,7 +826,7 @@ afs_syscall_call(long parm, long parm2, long parm3,
        AFS_GUNLOCK();
        exit(CLD_EXITED, 0);
 #  endif /* AFS_SGI_ENV */
-# endif /* ! AFS_DARWIN80_ENV */
+# endif /* ! AFS_NEW_BKG */
     } else if (parm == AFSOP_START_TRUNCDAEMON) {
        while (afs_initState < AFSOP_GO)
            afs_osi_Sleep(&afs_initState);
@@ -712,7 +839,7 @@ afs_syscall_call(long parm, long parm2, long parm3,
        exit(CLD_EXITED, 0);
 # endif /* AFS_SGI_ENV */
     }
-# if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
+# if defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV) || defined(RXK_UPCALL_ENV)
     else if (parm == AFSOP_RXEVENT_DAEMON) {
        while (afs_initState < AFSOP_START_BKG)
            afs_osi_Sleep(&afs_initState);
@@ -724,7 +851,7 @@ afs_syscall_call(long parm, long parm2, long parm3,
        exit(CLD_EXITED, 0);
 #  endif /* AFS_SGI_ENV */
     }
-# endif /* AFS_SUN5_ENV || RXK_LISTENER_ENV */
+# endif /* AFS_SUN5_ENV || RXK_LISTENER_ENV || RXK_UPCALL_ENV */
 #endif /* AFS_DAEMONOP_ENV */
     else if (parm == AFSOP_BASIC_INIT) {
        afs_int32 temp;
@@ -875,7 +1002,10 @@ afs_syscall_call(long parm, long parm2, long parm3,
        }
        afs_CacheInit_Done = 1;
         code = afs_icl_InitLogs();
-       afs_setTime = cparms.setTimeFlag;
+       if (cparms.setTimeFlag) {
+           afs_warn("afs: AFSOP_CACHEINIT setTimeFlag ignored; are you "
+                    "running an old afsd?\n");
+       }
 
        code =
            afs_CacheInit(cparms.cacheScaches, cparms.cacheFiles,
@@ -925,10 +1055,6 @@ afs_syscall_call(long parm, long parm2, long parm3,
        code = 0;
        AFS_COPYINSTR(AFSKPTR(parm2), tbuffer, AFS_SMALLOCSIZ, &bufferSize,
                      code);
-       if (code) {
-           osi_FreeSmallSpace(tbuffer);
-           goto out;
-       }
        if (!code) {
            tbuffer[AFS_SMALLOCSIZ - 1] = '\0'; /* null-terminate the name */
            /* We have the cache dir copied in.  Call the cache init routine */
@@ -958,7 +1084,11 @@ afs_syscall_call(long parm, long parm2, long parm3,
        while (afs_initState < AFSOP_GO)
            afs_osi_Sleep(&afs_initState);
        afs_initState = 101;
-       afs_setTime = parm2;
+       if (parm2) {
+           /* parm2 used to set afs_setTime */
+           afs_warn("afs: AFSOP_GO setTime flag ignored; are you running an "
+                    "old afsd?\n");
+       }
        if (afs_tpct1 + afs_tpct2 != 100) {
            afs_tpct1 = 0;
            afs_tpct2 = 0;
@@ -970,7 +1100,9 @@ afs_syscall_call(long parm, long parm2, long parm3,
 #if    (!defined(AFS_NONFSTRANS)) || defined(AFS_AIX_IAUTH_ENV)
        afs_nfsclient_init();
 #endif
-       afs_uuid_create(&afs_cb_interface.uuid);
+       if (afs_uuid_create(&afs_cb_interface.uuid) != 0)
+           memset(&afs_cb_interface.uuid, 0, sizeof(afsUUID));
+
        printf("found %d non-empty cache files (%d%%).\n",
               afs_stats_cmperf.cacheFilesReused,
               (100 * afs_stats_cmperf.cacheFilesReused) /
@@ -1058,35 +1190,9 @@ afs_syscall_call(long parm, long parm2, long parm3,
 
        if (refresh) {
            afs_CheckServers(1, NULL);     /* check down servers */
-           afs_CheckServers(0, NULL);     /* check down servers */
+           afs_CheckServers(0, NULL);     /* check up servers */
        }
     }
-#ifdef AFS_SGI53_ENV
-    else if (parm == AFSOP_NFSSTATICADDR) {
-       extern int (*nfs_rfsdisptab_v2) ();
-       nfs_rfsdisptab_v2 = (int (*)())parm2;
-    } else if (parm == AFSOP_NFSSTATICADDR2) {
-       extern int (*nfs_rfsdisptab_v2) ();
-# ifdef _K64U64
-       nfs_rfsdisptab_v2 = (int (*)())((parm2 << 32) | (parm3 & 0xffffffff));
-# else /* _K64U64 */
-       nfs_rfsdisptab_v2 = (int (*)())(parm3 & 0xffffffff);
-# endif /* _K64U64 */
-    }
-# if defined(AFS_SGI62_ENV) && !defined(AFS_SGI65_ENV)
-    else if (parm == AFSOP_SBLOCKSTATICADDR2) {
-       extern int (*afs_sblockp) ();
-       extern void (*afs_sbunlockp) ();
-#  ifdef _K64U64
-       afs_sblockp = (int (*)())((parm2 << 32) | (parm3 & 0xffffffff));
-       afs_sbunlockp = (void (*)())((parm4 << 32) | (parm5 & 0xffffffff));
-#  else
-       afs_sblockp = (int (*)())(parm3 & 0xffffffff);
-       afs_sbunlockp = (void (*)())(parm5 & 0xffffffff);
-#  endif /* _K64U64 */
-    }
-# endif /* AFS_SGI62_ENV && !AFS_SGI65_ENV */
-#endif /* AFS_SGI53_ENV */
     else if (parm == AFSOP_SHUTDOWN) {
        afs_cold_shutdown = 0;
        if (parm2 == 1)
@@ -1204,6 +1310,25 @@ afs_syscall_call(long parm, long parm2, long parm3,
        afscall_set_rxpck_received = 1;
     } else if (parm == AFSOP_SET_RXMAXMTU) {
        rx_MyMaxSendSize = rx_maxReceiveSizeUser = rx_maxReceiveSize = parm2;
+    } else if (parm == AFSOP_SET_RXMAXFRAGS) {
+       rxi_nSendFrags = rxi_nRecvFrags = parm2;
+    } else if (parm == AFSOP_SET_RMTSYS_FLAG) {
+       afs_rmtsys_enable = parm2;
+       code = 0;
+#ifndef UKERNEL
+    } else if (parm == AFSOP_SEED_ENTROPY) {
+       unsigned char *seedbuf;
+
+       if (parm3 > 4096) {
+           code = EFAULT;
+       } else {
+           seedbuf = afs_osi_Alloc(parm3);
+           AFS_COPYIN(AFSKPTR(parm2), seedbuf, parm3, code);
+           RAND_seed(seedbuf, parm3);
+           memset(seedbuf, 0, parm3);
+           afs_osi_Free(seedbuf, parm3);
+       }
+#endif
     } else {
        code = EINVAL;
     }
@@ -1260,14 +1385,37 @@ afs_shutdown(void)
 
     if (afs_shuttingdown)
        return;
-    afs_FlushVCBs(2);       /* Reasonable effort to free dynamically allocated callback returns */
+
+    /* Give up all of our callbacks if we can. This must be done before setting
+     * afs_shuttingdown, since it calls afs_InitReq, which will fail if
+     * afs_shuttingdown is set. */
+    afs_FlushVCBs(2);
 
     afs_shuttingdown = 1;
+
     if (afs_cold_shutdown)
        afs_warn("afs: COLD ");
     else
        afs_warn("afs: WARM ");
-    afs_warn("shutting down of: CB... ");
+    afs_warn("shutting down of: vcaches... ");
+
+#if !defined(AFS_FBSD_ENV)
+    /* The FBSD afs_unmount() calls vflush(), which reclaims all vnodes
+     * on the mountpoint, flushing them in the process.  In the presence
+     * of bugs, flushing again here can cause panics. */
+    afs_FlushAllVCaches();
+#endif
+
+    afs_termState = AFSOP_STOP_BKG;
+
+    afs_warn("BkG... ");
+    /* Wake-up afs_brsDaemons so that we don't have to wait for a bkg job! */
+    while (afs_termState == AFSOP_STOP_BKG) {
+       afs_osi_Wakeup(&afs_brsDaemons);
+       afs_osi_Sleep(&afs_termState);
+    }
+
+    afs_warn("CB... ");
 
     afs_termState = AFSOP_STOP_RXCALLBACK;
     rx_WakeupServerProcs();
@@ -1289,12 +1437,6 @@ afs_shutdown(void)
            afs_osi_Sleep(&afs_termState);
        }
     }
-    afs_warn("BkG... ");
-    /* Wake-up afs_brsDaemons so that we don't have to wait for a bkg job! */
-    while (afs_termState == AFSOP_STOP_BKG) {
-       afs_osi_Wakeup(&afs_brsDaemons);
-       afs_osi_Sleep(&afs_termState);
-    }
     afs_warn("CTrunc... ");
     /* Cancel cache truncate daemon. */
     while (afs_termState == AFSOP_STOP_TRUNCDAEMON) {
@@ -1305,7 +1447,7 @@ afs_shutdown(void)
     afs_StopAFSDB();
     while (afs_termState == AFSOP_STOP_AFSDB)
        afs_osi_Sleep(&afs_termState);
-#if    defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV)
+#if    defined(AFS_SUN5_ENV) || defined(RXK_LISTENER_ENV) || defined(RXK_UPCALL_ENV)
     afs_warn("RxEvent... ");
     /* cancel rx event daemon */
     while (afs_termState == AFSOP_STOP_RXEVENT)
@@ -1380,7 +1522,7 @@ void
 shutdown_afstest(void)
 {
     AFS_STATCNT(shutdown_afstest);
-    afs_initState = afs_termState = afs_setTime = 0;
+    afs_initState = afs_termState = 0;
     AFS_Running = afs_CB_Running = 0;
     afs_CacheInit_Done = afs_Go_Done = 0;
     if (afs_cold_shutdown) {