Windows: Build Demand Attach File Service
[openafs.git] / src / viced / viced.c
index 6e9bfea..d4ad4fa 100644 (file)
@@ -30,6 +30,9 @@
 #include <afs/procmgmt.h>      /* signal(), kill(), wait(), etc. */
 #include <sys/stat.h>
 #include <fcntl.h>
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
 #ifdef AFS_NT40_ENV
 #include <io.h>
 #include <windows.h>
@@ -119,7 +122,6 @@ static afs_int32 Do_VLRegisterRPC(void);
 
 int eventlog = 0, rxlog = 0;
 FILE *debugFile;
-FILE *console = NULL;
 
 #ifdef AFS_PTHREAD_ENV
 pthread_mutex_t fsync_glock_mutex;
@@ -129,7 +131,6 @@ char fsync_wait[1];
 #endif /* AFS_PTHREAD_ENV */
 
 #ifdef AFS_NT40_ENV
-#define AFS_QUIETFS_ENV 1
 #define NT_OPEN_MAX    1024    /* This is an arbitrary no. we came up with for 
                                 * now. We hope this will be replaced by a more
                                 * intelligent estimate later. */
@@ -200,6 +201,7 @@ int abort_threshold = 10;
 int udpBufSize = 0;            /* UDP buffer size for receive */
 int sendBufSize = 16384;       /* send buffer size */
 int saneacls = 0;              /* Sane ACLs Flag */
+static int unsafe_attach = 0;   /* avoid inUse check on vol attach? */
 
 struct timeval tp;
 
@@ -240,6 +242,7 @@ static void FlagMsg(void);
 #define PTHREAD_RWLOCK_INITIALIZER {0x2DA8B3B4, {0}}
 #endif
 
+#ifndef AFS_NT40_ENV
 struct fs_state fs_state = 
     { FS_MODE_NORMAL, 
       0, 
@@ -250,6 +253,26 @@ struct fs_state fs_state =
       PTHREAD_COND_INITIALIZER,
       PTHREAD_RWLOCK_INITIALIZER
     };
+#else /* AFS_NT40_ENV */
+struct fs_state fs_state;
+
+static int fs_stateInit(void)
+{
+    fs_state.mode = FS_MODE_NORMAL;
+    fs_state.FiveMinuteLWP_tranquil = 0;
+    fs_state.HostCheckLWP_tranquil = 0;
+    fs_state.FsyncCheckLWP_tranquil = 0;
+    fs_state.salvsync_fatal_error = 0;
+
+    fs_state.options.fs_state_save = 1;
+    fs_state.options.fs_state_restore = 1;
+    fs_state.options.fs_state_verify_before_save = 1;
+    fs_state.options.fs_state_verify_after_restore = 1;
+
+    assert(pthread_cond_init(&fs_state.worker_done_cv, NULL) == 0);
+    assert(pthread_rwlock_init(&fs_state.state_lock, NULL) == 0);
+}
+#endif /* AFS_NT40_ENV */
 #endif /* AFS_DEMAND_ATTACH_FS */
 
 /*
@@ -423,7 +446,7 @@ setThreadId(char *s)
     pthread_setspecific(rx_thread_id_key, (void *)(intptr_t)rxi_pthread_hinum);
     MUTEX_EXIT(&rx_stats_mutex);
     ViceLog(0,
-           ("Set thread id %d for '%s'\n",
+           ("Set thread id %p for '%s'\n",
             pthread_getspecific(rx_thread_id_key), s));
 #endif
 }
@@ -477,15 +500,9 @@ FiveMinuteCheckLWP(void *unused)
 #endif
        if (printBanner && (++msg & 1)) {       /* Every 10 minutes */
            time_t now = FT_ApproxTime();
-           if (console != NULL) {
-#ifndef AFS_QUIETFS_ENV
-               fprintf(console, "File server is running at %s\r",
-                       afs_ctime(&now, tbuffer, sizeof(tbuffer)));
-#endif /* AFS_QUIETFS_ENV */
-               ViceLog(2,
-                       ("File server is running at %s\n",
-                        afs_ctime(&now, tbuffer, sizeof(tbuffer))));
-           }
+           ViceLog(2,
+                   ("File server is running at %s\n",
+                    afs_ctime(&now, tbuffer, sizeof(tbuffer))));
        }
 #ifdef AFS_DEMAND_ATTACH_FS
        FS_STATE_WRLOCK;
@@ -799,6 +816,15 @@ ShutDownAndCore(int dopanic)
 
 #ifdef AFS_DEMAND_ATTACH_FS
     FS_STATE_WRLOCK;
+    if (fs_state.mode == FS_MODE_SHUTDOWN) {
+       /* it is possible for at least fs_stateSave() (called below) to call
+        * ShutDownAndCore if there's host list corruption; prevent
+        * deinitializing some stuff twice */
+       ViceLog(0, ("ShutDownAndCore called during shutdown? Skipping volume "
+                   "and host package shutdown\n"));
+       FS_STATE_UNLOCK;
+       goto done_vol_host;
+    }
     fs_state.mode = FS_MODE_SHUTDOWN;
     FS_STATE_UNLOCK;
 #endif
@@ -808,12 +834,6 @@ ShutDownAndCore(int dopanic)
             afs_ctime(&now, tbuffer, sizeof(tbuffer))));
     if (dopanic)
        ViceLog(0, ("ABNORMAL SHUTDOWN, see core file.\n"));
-#ifndef AFS_QUIETFS_ENV
-    if (console != NULL) {
-       fprintf(console, "File server restart/shutdown received at %s\r",
-               afs_ctime(&now, tbuffer, sizeof(tbuffer)));
-    }
-#endif
     DFlush();
     if (!dopanic)
        PrintCounters();
@@ -844,36 +864,29 @@ ShutDownAndCore(int dopanic)
                FS_UNLOCK;
                FS_STATE_RDLOCK;
            }
+           FS_STATE_UNLOCK;
 
            /* ok. it should now be fairly safe. let's do the state dump */
            fs_stateSave();
        }
     }
+ done_vol_host:
+
 #endif /* AFS_DEMAND_ATTACH_FS */
 
     if (debugFile) {
        rx_PrintStats(debugFile);
        fflush(debugFile);
     }
-    if (console != NULL) {
-       now = time(0);
-       if (dopanic) {
-#ifndef AFS_QUIETFS_ENV
-           fprintf(console, "File server has terminated abnormally at %s\r",
-                   afs_ctime(&now, tbuffer, sizeof(tbuffer)));
-#endif
-           ViceLog(0,
-                   ("File server has terminated abnormally at %s\n",
-                    afs_ctime(&now, tbuffer, sizeof(tbuffer))));
-       } else {
-#ifndef AFS_QUIETFS_ENV
-           fprintf(console, "File server has terminated normally at %s\r",
-                   afs_ctime(&now, tbuffer, sizeof(tbuffer)));
-#endif
-           ViceLog(0,
-                   ("File server has terminated normally at %s\n",
-                    afs_ctime(&now, tbuffer, sizeof(tbuffer))));
-       }
+    now = time(0);
+    if (dopanic) {
+      ViceLog(0,
+             ("File server has terminated abnormally at %s\n",
+              afs_ctime(&now, tbuffer, sizeof(tbuffer))));
+    } else {
+      ViceLog(0,
+             ("File server has terminated normally at %s\n",
+              afs_ctime(&now, tbuffer, sizeof(tbuffer))));
     }
 
     if (dopanic)
@@ -934,6 +947,7 @@ FlagMsg(void)
     fputs("[-vlruthresh <minutes before unused volumes become eligible for soft detach> (default is 2 hours)] ", stdout);
     fputs("[-vlruinterval <seconds between VLRU scans> (default is 2 minutes)] ", stdout);
     fputs("[-vlrumax <max volumes to soft detach in one VLRU scan> (default is 8)] ", stdout);
+    fputs("[-unsafe-nosalvage (bypass volume inUse safety check on attach, bypassing salvage)] ", stdout);
 #elif AFS_PTHREAD_ENV
     fputs("[-vattachpar <number of volume attach threads> (default is 1)] ", stdout);
 #endif
@@ -1214,6 +1228,8 @@ ParseArgs(int argc, char *argv[])
                return -1; 
            }
            VLRU_SetOptions(VLRU_SET_MAX, atoi(argv[++i]));
+       } else if (!strcmp(argv[i], "-unsafe-nosalvage")) {
+           unsafe_attach = 1;
 #endif /* AFS_DEMAND_ATTACH_FS */
        } else if (!strcmp(argv[i], "-s")) {
            Sawsmall = 1;
@@ -1499,8 +1515,8 @@ NewParms(int initializing)
                ViceLog(0, ("Read on parms failed with errno = %d\n", errno));
            } else {
                ViceLog(0,
-                       ("Read on parms failed; expected %d bytes but read %d\n",
-                        sbuf.st_size, i));
+                       ("Read on parms failed; expected %ld bytes but read %d\n",
+                        (long) sbuf.st_size, i));
            }
            free(parms);
            return;
@@ -1608,7 +1624,8 @@ struct ubik_client *cstruct;
 afs_int32
 vl_Initialize(const char *confDir)
 {
-    afs_int32 code, scIndex = 0, i;
+    afs_int32 code, i;
+    afs_int32 scIndex = RX_SECIDX_NULL;
     struct afsconf_dir *tdir;
     struct rx_securityClass *sc;
     struct afsconf_cell info;
@@ -1950,9 +1967,6 @@ main(int argc, char *argv[])
                argv[0]);
        exit(2);
     }
-#ifndef AFS_QUIETFS_ENV
-    console = afs_fopen("/dev/console", "w");
-#endif
     /* set ihandle package defaults prior to parsing args */
     ih_PkgDefaults();
 
@@ -1991,11 +2005,7 @@ main(int argc, char *argv[])
        ViceLog(0, ("Both -spare and -pctspare specified, exiting.\n"));
        exit(-1);
     }
-#ifdef AFS_SGI_XFS_IOPS_ENV
-    ViceLog(0, ("XFS/EFS File server starting\n"));
-#else
-    ViceLog(0, ("File server starting\n"));
-#endif
+    LogCommandLine(argc, argv, "starting", "", "File server", FSLog);
 
 #if defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
     /* initialize the pthread soft signal handler thread */
@@ -2237,6 +2247,7 @@ main(int argc, char *argv[])
     opts.nLargeVnodes = large;
     opts.nSmallVnodes = nSmallVns;
     opts.volcache = volcache;
+    opts.unsafe_attach = unsafe_attach;
 
     if (VInitVolumePackage2(fileServer, &opts)) {
        ViceLog(0,
@@ -2306,13 +2317,8 @@ main(int argc, char *argv[])
 
     FT_GetTimeOfDay(&tp, 0);
 
-#ifndef AFS_QUIETFS_ENV
-    if (console != NULL) { 
-        time_t t = tp.tv_sec;
-       fprintf(console, "File server has started at %s\r",
-               afs_ctime(&t, tbuffer, sizeof(tbuffer)));
-    }
-#endif
+    ViceLog(0, ("File server has started at %s",
+               afs_ctime((time_t *)&(tp.tv_sec), tbuffer, sizeof(tbuffer))));
 
     /*
      * Figure out the FileServer's name and primary address.