afs: add afsd -inumcalc option
[openafs.git] / src / afs / afs_call.c
index 0e7feb2..4938e7b 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_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
@@ -35,7 +41,7 @@
 #define        AFS_MINBUFFERS  50
 #endif
 
-#if (defined(AFS_SUN5_ENV) || (defined(AFS_LINUX24_ENV) && defined(HAVE_LINUX_COMPLETION_H)) || defined(AFS_DARWIN80_ENV)) && !defined(UKERNEL)
+#if (defined(AFS_SUN5_ENV) || defined(AFS_LINUX26_ENV) || 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. */
@@ -65,11 +71,14 @@ 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;
@@ -78,9 +87,8 @@ afs_int32 afs_rx_idledead_rep = AFS_IDLEDEADTIME_REP;
 
 static int afscall_set_rxpck_received = 0;
 
-#if defined(AFS_HPUX_ENV)
-extern int afs_vfs_mount();
-#endif /* defined(AFS_HPUX_ENV) */
+/* From afs_util.c */
+extern afs_int32 afs_md5inum;
 
 /* This is code which needs to be called once when the first daemon enters
  * the client. A non-zero return means an error and AFS should not start.
@@ -114,8 +122,6 @@ afs_InitSetup(int preallocs)
 
     memset(afs_zeros, 0, AFS_ZEROS);
 
-    rx_SetBusyChannelError(1);  /* turn on busy call error reporting */
-
     /* start RX */
     if(!afscall_set_rxpck_received)
     rx_extraPackets = AFS_NRXPACKETS;  /* smaller # of packets */
@@ -273,9 +279,9 @@ afs_DaemonOp(long parm, long parm2, long parm3, long parm4, long parm5,
 #endif
 
 
-#if defined(AFS_LINUX24_ENV) && defined(HAVE_LINUX_COMPLETION_H)
+#if defined(AFS_LINUX26_ENV)
 struct afsd_thread_info {
-# if defined(AFS_LINUX26_ENV) && !defined(INIT_WORK_HAS_DATA)
+# if !defined(INIT_WORK_HAS_DATA)
     struct work_struct tq;
 # endif
     unsigned long parm;
@@ -290,11 +296,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) */
@@ -335,6 +343,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);
@@ -347,6 +358,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:
@@ -427,8 +439,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
@@ -649,7 +667,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);
@@ -692,7 +710,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);
@@ -784,7 +802,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);
@@ -805,7 +823,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);
@@ -896,12 +914,6 @@ afs_syscall_call(long parm, long parm2, long parm3,
        osi_Assert(tbuffer1 != NULL);
        code = afs_InitDynroot();
        if (!code) {
-#if 0
-           /* wait for basic init - XXX can't find any reason we need this? */
-           while (afs_initState < AFSOP_START_BKG)
-               afs_osi_Sleep(&afs_initState);
-#endif
-
            AFS_COPYIN(AFSKPTR(parm2), (caddr_t)tcell->hosts, sizeof(tcell->hosts),
                       code);
        }
@@ -1034,10 +1046,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 */
@@ -1214,22 +1222,6 @@ afs_syscall_call(long parm, long parm2, long parm3,
        if (!code)
            AFS_COPYOUT((caddr_t) & mtu, AFSKPTR(parm3),
                        sizeof(afs_int32), code);
-#ifdef AFS_AIX32_ENV
-/* this is disabled for now because I can't figure out how to get access
- * to these kernel variables.  It's only for supporting user-mode rx
- * programs -- it makes a huge difference on the 220's in my testbed,
- * though I don't know why. The bosserver does this with /etc/no, so it's
- * being handled a different way for the servers right now.  */
-/*      {
-       static adjusted = 0;
-       extern u_long sb_max_dflt;
-       if (!adjusted) {
-         adjusted = 1;
-         if (sb_max_dflt < 131072) sb_max_dflt = 131072;
-         if (sb_max < 131072) sb_max = 131072;
-       }
-      } */
-#endif /* AFS_AIX32_ENV */
     } else if (parm == AFSOP_GETMASK) {        /* parm2 == addr in net order */
        afs_uint32 mask = 0;
 #if    !defined(AFS_SUN5_ENV)
@@ -1295,6 +1287,36 @@ afs_syscall_call(long parm, long parm2, long parm3,
        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 if (parm == AFSOP_SET_INUMCALC) {
+       switch (parm2) {
+       case AFS_INUMCALC_COMPAT:
+           afs_md5inum = 0;
+           code = 0;
+           break;
+       case AFS_INUMCALC_MD5:
+           afs_md5inum = 1;
+           code = 0;
+           break;
+       default:
+           code = EINVAL;
+       }
     } else {
        code = EINVAL;
     }
@@ -1334,7 +1356,7 @@ afs_CheckInit(void)
     return code;
 }
 
-int afs_shuttingdown = 0;
+enum afs_shutdown_state afs_shuttingdown = AFS_RUNNING;
 void
 afs_shutdown(void)
 {
@@ -1349,15 +1371,15 @@ afs_shutdown(void)
       return;
     }
 
-    if (afs_shuttingdown)
+    if (afs_shuttingdown != AFS_RUNNING)
        return;
 
-    /* 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_shuttingdown = AFS_FLUSHING_CB;
+
+    /* Give up all of our callbacks if we can. */
     afs_FlushVCBs(2);
 
-    afs_shuttingdown = 1;
+    afs_shuttingdown = AFS_SHUTDOWN;
 
     if (afs_cold_shutdown)
        afs_warn("afs: COLD ");
@@ -1372,6 +1394,15 @@ afs_shutdown(void)
     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;
@@ -1394,12 +1425,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) {
@@ -1410,7 +1435,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)
@@ -1475,7 +1500,7 @@ afs_shutdown(void)
     memset(&afs_stats_cmfullperf, 0, sizeof(struct afs_stats_CMFullPerf));
     afs_warn(" ALL allocated tables... ");
 
-    afs_shuttingdown = 0;
+    afs_shuttingdown = AFS_RUNNING;
     afs_warn("done\n");
 
     return;                    /* Just kill daemons for now */