src/afsd/afsd.c: Fix misleading indentation
[openafs.git] / src / afsd / afsd.c
index 9050ef5..0976099 100644 (file)
   *    -blocks     The number of blocks available in the workstation cache.
   *    -files      The target number of files in the workstation cache (Default:
   *                1000).
-  *    -rootvol            The name of the root volume to use.
+  *    -rootvol    The name of the root volume to use.
   *    -stat       The number of stat cache entries.
-  *    -hosts      List of servers to check for volume location info FOR THE
+  *    -hosts      [OBSOLETE] List of servers to check for volume location info FOR THE
   *                HOME CELL.
   *     -memcache   Use an in-memory cache rather than disk.
-  *    -cachedir    The base directory for the workstation cache.
+  *    -cachedir   The base directory for the workstation cache.
   *    -mountdir   The directory on which the AFS is to be mounted.
-  *    -confdir    The configuration directory .
+  *    -confdir    The configuration directory.
   *    -nosettime  Don't keep checking the time to avoid drift (default).
-  *     -settime    Keep checking the time to avoid drift.
+  *     -settime    [IGNORED] Keep checking the time to avoid drift.
   *    -rxmaxmtu   Set the max mtu to help with VPN issues.
-  *    -verbose     Be chatty.
+  *    -verbose    Be chatty.
   *    -disable-dynamic-vcaches     Disable the use of -stat value as the starting size of
   *                          the size of the vcache/stat cache pool,
   *                          but increase that pool dynamically as needed.
   *                support daemon
   *     -chunksize [n]   2^n is the chunksize to be used.  0 is default.
   *     -dcache    The number of data cache entries.
+  *     -volumes    The number of volume entries.
   *     -biods     Number of bkg I/O daemons (AIX3.1 only)
   *    -prealloc  Number of preallocated "small" memory blocks
-  *    -logfile   [OBSOLETE] Place where to put the logfile (default in
+  *    -logfile    [IGNORED] Place where to put the logfile (default in
   *                <cache>/etc/AFSLog.
   *    -waitclose make close calls always synchronous (slows em down, tho)
   *    -files_per_subdir [n]   number of files per cache subdir. (def=2048)
   *    -shutdown  Shutdown afs daemons
+  *    -enable_peer_stats      Collect RPC statistics by peer.
+  *    -enable_process_stats   Collect RPC statistics for this process.
+  *    -mem_alloc_sleep [IGNORED] Sleep when allocating memory.
+  *    -afsdb      Enable AFSDB support.
+  *    -dynroot        Enable dynroot support.
+  *    -dynroot-sparse Enable dynroot support with minimal cell list.
+  *    -fakestat       Enable fake stat() for cross-cell mounts.
+  *    -fakestat-all   Enable fake stat() for all mounts.
+  *    -nomount    Do not mount /afs.
+  *    -backuptree Prefer backup volumes for mountpoints in backup volumes.
+  *    -rxbind     Bind the rx socket.
+  *    -rxpck      Value for rx_extraPackets.
+  *    -splitcache RW/RO ratio for cache.
+  *    -rxmaxfrags Max number of UDP fragments per rx packet.
+  *    -inumcalc  inode number calculation method; 0=compat, 1=MD5 digest
   *---------------------------------------------------------------------------*/
 
 #include <afsconfig.h>
 #include <afs/param.h>
-#include <roken.h>
+/* darwin dirent.h doesn't give us the prototypes we want if KERNEL is
+ * defined, and roken includes dirent */
+#if defined(UKERNEL) && defined(AFS_USR_DARWIN_ENV)
+# undef KERNEL
+# include <roken.h>
+# define KERNEL
+#else
+# include <roken.h>
+#endif
 
 #define VFS 1
 
@@ -71,6 +95,7 @@
 
 #include <sys/file.h>
 #include <sys/wait.h>
+#include <hcrypto/rand.h>
 
 /* darwin dirent.h doesn't give us the prototypes we want if KERNEL is
  * defined */
 #endif
 #include <CoreFoundation/CoreFoundation.h>
 
-#include <SystemConfiguration/SystemConfiguration.h>
-#include <SystemConfiguration/SCDynamicStore.h>
-
+static int event_pid;
 #ifndef AFS_ARM_DARWIN_ENV
-#include <IOKit/pwr_mgt/IOPMLib.h>
-#include <IOKit/IOMessage.h>
-
-static io_connect_t root_port;
-static IONotificationPortRef notify;
-static io_object_t iterator;
+#define MACOS_EVENT_HANDLING 1
 #endif
-
-static CFRunLoopSourceRef source;
-
-static int event_pid;
-
 #endif /* AFS_DARWIN_ENV */
 
 #if AFS_HAVE_STATVFS || defined(HAVE_SYS_STATVFS_H)
@@ -264,6 +277,7 @@ static int enable_fakestat = 0;     /* enable fakestat support */
 static int enable_backuptree = 0;      /* enable backup tree support */
 static int enable_nomount = 0; /* do not mount */
 static int enable_splitcache = 0;
+static char *inumcalc = NULL;        /* inode number calculation method */
 static int afsd_dynamic_vcaches = 0;   /* Enable dynamic-vcache support */
 int afsd_verbose = 0;          /*Are we being chatty? */
 int afsd_debug = 0;            /*Are we printing debugging info? */
@@ -347,9 +361,21 @@ enum optionsList {
     OPT_rxmaxmtu,
     OPT_dynrootsparse,
     OPT_rxmaxfrags,
+    OPT_inumcalc,
 };
 
-#if defined(AFS_DARWIN_ENV) && !defined(AFS_ARM_DARWIN_ENV)
+#ifdef MACOS_EVENT_HANDLING
+#include <SystemConfiguration/SystemConfiguration.h>
+#include <SystemConfiguration/SCDynamicStore.h>
+
+#include <IOKit/pwr_mgt/IOPMLib.h>
+#include <IOKit/IOMessage.h>
+
+static io_connect_t root_port;
+static IONotificationPortRef notify;
+static io_object_t iterator;
+static CFRunLoopSourceRef source;
+
 static void
 afsd_sleep_callback(void * refCon, io_service_t service,
                    natural_t messageType, void * messageArgument )
@@ -1327,7 +1353,7 @@ CheckCacheBaseDir(char *dir)
 
                    if (cp = hasmntopt(&mnt, "dev="))
                        rdev =
-                           (int)strtol(cp + strlen("dev="), (char **)NULL,
+                           (int)strtol(cp + strlen("dev="), NULL,
                                        16);
 
                    if ((rdev == 0) && (stat(mnt.mnt_mountp, &statmnt) == 0))
@@ -1459,7 +1485,7 @@ AfsdbLookupHandler(void)
     kernelMsg[1] = 0;
     acellName[0] = '\0';
 
-#if defined(AFS_DARWIN_ENV) && !defined(AFS_ARM_DARWIN_ENV)
+#ifdef MACOS_EVENT_HANDLING
     /* Fork the event handler also. */
     code = fork();
     if (code == 0) {
@@ -1658,8 +1684,16 @@ rmtsysd_thread(void *rock)
 }
 #endif /* !UKERNEL */
 
-int
-mainproc(struct cmd_syndesc *as, void *arock)
+/**
+ * Check the command line and cacheinfo options.
+ *
+ * @param[in] as  parsed command line arguments
+ *
+ * @note Invokes the shutdown syscall and exits with 0 when
+ *       -shutdown is given.
+ */
+static int
+CheckOptions(struct cmd_syndesc *as)
 {
     afs_int32 code;            /*Result of fork() */
 #ifdef AFS_SUN5_ENV
@@ -1868,6 +1902,9 @@ mainproc(struct cmd_syndesc *as, void *arock)
     }
 
     cmd_OptionAsInt(as, OPT_rxmaxfrags, &rxmaxfrags);
+    if (cmd_OptionPresent(as, OPT_inumcalc)) {
+       cmd_OptionAsString(as, OPT_inumcalc, &inumcalc);
+    }
 
     /* parse cacheinfo file if this is a diskcache */
     if (ParseCacheInfoFile()) {
@@ -2079,8 +2116,9 @@ afsd_run(void)
        exit(1);
     }
     if (afsd_debug)
-       printf("%s: %d inode_for_V entries at 0x%x, %lu bytes\n", rn,
-              cacheFiles, inode_for_V, (cacheFiles * sizeof(AFSD_INO_T)));
+       printf("%s: %d inode_for_V entries at %p, %lu bytes\n", rn,
+              cacheFiles, inode_for_V,
+              (unsigned long)cacheFiles * sizeof(AFSD_INO_T));
 #endif
 
     if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) {
@@ -2120,6 +2158,13 @@ afsd_run(void)
 
     /* initialize the rx random number generator from user space */
     {
+       /* rand-fortuna wants at least 128 bytes of seed; be generous. */
+       unsigned char seedbuf[256];
+       if (RAND_bytes(seedbuf, sizeof(seedbuf)) != 1) {
+           printf("SEED_ENTROPY: Error retrieving seed entropy\n");
+       }
+       afsd_syscall(AFSOP_SEED_ENTROPY, seedbuf, sizeof(seedbuf));
+       memset(seedbuf, 0, sizeof(seedbuf));
        /* parse multihomed address files */
        afs_uint32 addrbuf[MAXIPADDRS], maskbuf[MAXIPADDRS],
            mtubuf[MAXIPADDRS];
@@ -2130,7 +2175,7 @@ afsd_run(void)
        if (code > 0) {
            if (enable_rxbind)
                code = code | 0x80000000;
-               afsd_syscall(AFSOP_ADVISEADDR, code, addrbuf, maskbuf, mtubuf);
+           afsd_syscall(AFSOP_ADVISEADDR, code, addrbuf, maskbuf, mtubuf);
        } else
            printf("ADVISEADDR: Error in specifying interface addresses:%s\n",
                   reason);
@@ -2280,6 +2325,33 @@ afsd_run(void)
             printf("%s: Error seting rxmaxmtu\n", rn);
     }
 
+    if (inumcalc != NULL) {
+       if (strcmp(inumcalc, "compat") == 0) {
+           if (afsd_verbose) {
+               printf("%s: Setting original inode number calculation method in kernel.\n",
+                      rn);
+           }
+           code = afsd_syscall(AFSOP_SET_INUMCALC, AFS_INUMCALC_COMPAT);
+           if (code) {
+               printf("%s: Error setting inode calculation method: code=%d.\n",
+                      rn, code);
+           }
+       } else if (strcmp(inumcalc, "md5") == 0) {
+           if (afsd_verbose) {
+               printf("%s: Setting md5 digest inode number calculation in kernel.\n",
+                      rn);
+           }
+           code = afsd_syscall(AFSOP_SET_INUMCALC, AFS_INUMCALC_MD5);
+           if (code) {
+               printf("%s: Error setting inode calculation method: code=%d.\n",
+                      rn, code);
+           }
+       } else {
+           printf("%s: Unknown value for -inumcalc: %s."
+                  "Using default inode calculation method.\n", rn, inumcalc);
+       }
+    }
+
     if (enable_dynroot) {
        if (afsd_verbose)
            printf("%s: Enabling dynroot support in kernel%s.\n", rn,
@@ -2415,7 +2487,7 @@ afsd_run(void)
     if (afsd_debug)
        printf("%s: Calling AFSOP_GO with cacheSetTime = %d\n", rn,
               0);
-       afsd_syscall(AFSOP_GO, 0);
+    afsd_syscall(AFSOP_GO, 0);
 
     /*
      * At this point, we have finished passing the kernel all the info
@@ -2436,6 +2508,9 @@ afsd_run(void)
        if (afsd_verbose)
            printf("%s: Forking 'rmtsys' daemon.\n", rn);
        afsd_fork(0, rmtsysd_thread, NULL);
+       code = afsd_syscall(AFSOP_SET_RMTSYS_FLAG, 1);
+       if (code)
+           printf("%s: Error enabling rmtsys support.\n", rn);
     }
 #endif /* !UKERNEL */
     /*
@@ -2444,14 +2519,16 @@ afsd_run(void)
     return 0;
 }
 
+#ifndef UKERNEL
 #include "AFS_component_version_number.c"
+#endif
 
 void
 afsd_init(void)
 {
     struct cmd_syndesc *ts;
 
-    ts = cmd_CreateSyntax(NULL, mainproc, NULL, "start AFS");
+    ts = cmd_CreateSyntax(NULL, NULL, NULL, 0, "start AFS");
 
     /* 0 - 10 */
     cmd_AddParmAtOffset(ts, OPT_blocks, "-blocks", CMD_SINGLE,
@@ -2497,11 +2574,9 @@ afsd_init(void)
     cmd_AddParmAtOffset(ts, OPT_shutdown, "-shutdown", CMD_FLAG,
                        CMD_OPTIONAL, "Shutdown all afs state");
     cmd_AddParmAtOffset(ts, OPT_peerstats, "-enable_peer_stats", CMD_FLAG,
-                       CMD_OPTIONAL | CMD_HIDE,
-                       "Collect rpc statistics by peer");
+                       CMD_OPTIONAL, "Collect rpc statistics by peer");
     cmd_AddParmAtOffset(ts, OPT_processstats, "-enable_process_stats",
-                       CMD_FLAG, CMD_OPTIONAL | CMD_HIDE,
-                       "Collect rpc statistics for this process");
+                       CMD_FLAG, CMD_OPTIONAL, "Collect rpc statistics for this process");
     cmd_AddParmAtOffset(ts, OPT_memallocsleep, "-mem_alloc_sleep",
                        CMD_FLAG, CMD_OPTIONAL | CMD_HIDE,
                        "Allow sleeps when allocating memory cache");
@@ -2523,7 +2598,7 @@ afsd_init(void)
                        CMD_OPTIONAL, "Do not mount AFS");
     cmd_AddParmAtOffset(ts, OPT_backuptree, "-backuptree", CMD_FLAG,
                        CMD_OPTIONAL,
-                       "Prefer backup volumes for mointpoints in backup "
+                       "Prefer backup volumes for mountpoints in backup "
                        "volumes");
     cmd_AddParmAtOffset(ts, OPT_rxbind, "-rxbind", CMD_FLAG,
                        CMD_OPTIONAL,
@@ -2547,12 +2622,28 @@ afsd_init(void)
                        CMD_OPTIONAL,
                        "Set the maximum number of UDP fragments Rx should "
                        "send/receive per Rx packet");
+    cmd_AddParmAtOffset(ts, OPT_inumcalc, "-inumcalc", CMD_SINGLE, CMD_OPTIONAL,
+                       "Set inode number calculation method");
 }
 
+/**
+ * Parse and check the command line options.
+ *
+ * @note The -shutdown command is handled in CheckOptions().
+ */
 int
 afsd_parse(int argc, char **argv)
 {
-    return cmd_Dispatch(argc, argv);
+    struct cmd_syndesc *ts = NULL;
+    int code;
+
+    code = cmd_Parse(argc, argv, &ts);
+    if (code) {
+       return code;
+    }
+    code = CheckOptions(ts);
+    cmd_FreeOptions(&ts);
+    return code;
 }
 
 /**
@@ -2611,6 +2702,8 @@ afsd_syscall_populate(struct afsd_syscall_args *args, int syscall, va_list ap)
     case AFSOP_SET_BACKUPTREE:
     case AFSOP_BUCKETPCT:
     case AFSOP_GO:
+    case AFSOP_SET_RMTSYS_FLAG:
+    case AFSOP_SET_INUMCALC:
        params[0] = CAST_SYSCALL_PARAM((va_arg(ap, int)));
        break;
     case AFSOP_SET_THISCELL:
@@ -2665,6 +2758,10 @@ afsd_syscall_populate(struct afsd_syscall_args *args, int syscall, va_list ap)
        params[0] = CAST_SYSCALL_PARAM((va_arg(ap, afs_uint32)));
 #endif
        break;
+    case AFSOP_SEED_ENTROPY:
+       params[0] = CAST_SYSCALL_PARAM((va_arg(ap, void *)));
+       params[1] = CAST_SYSCALL_PARAM((va_arg(ap, afs_uint32)));
+       break;
     default:
        printf("Unknown syscall enountered: %d\n", syscall);
        opr_Assert(0);