ubik recovery and remote use correct file number
[openafs.git] / src / ubik / remote.c
index ff0b30b..4069cbd 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
 #include <sys/types.h>
+#include <string.h>
+#include <stdarg.h>
+
 #ifdef AFS_NT40_ENV
 #include <winsock2.h>
+#include <fcntl.h>
 #else
 #include <sys/file.h>
 #include <netinet/in.h>
 #endif
-#include <string.h>
+
 #include <lock.h>
 #include <rx/xdr.h>
 #include <rx/rx.h>
+#include <errno.h>
 #include <afs/afsutil.h>
 
 #define UBIK_INTERNALS
 #include "ubik.h"
 #include "ubik_int.h"
-int (*ubik_CheckRXSecurityProc) ();
-char *ubik_CheckRXSecurityRock;
-void printServerInfo();
 
-/* routines for handling requests remotely-submitted by the sync site.  These are
-    only write transactions (we don't propagate read trans), and there is at most one
-    write transaction extant at any one time.
-*/
+int (*ubik_CheckRXSecurityProc) (void *, struct rx_call *);
+void *ubik_CheckRXSecurityRock;
 
-struct ubik_trans *ubik_currentTrans = 0;
+static void printServerInfo(void);
 
+/*! \file
+ * routines for handling requests remotely-submitted by the sync site.  These are
+ * only write transactions (we don't propagate read trans), and there is at most one
+ * write transaction extant at any one time.
+ */
 
-ubik_CheckAuth(acall)
-     register struct rx_call *acall;
+struct ubik_trans *ubik_currentTrans = 0;
+
+int
+ubik_CheckAuth(struct rx_call *acall)
 {
-    register afs_int32 code;
+    afs_int32 code;
     if (ubik_CheckRXSecurityProc) {
        code = (*ubik_CheckRXSecurityProc) (ubik_CheckRXSecurityRock, acall);
        return code;
@@ -57,28 +62,15 @@ ubik_CheckAuth(acall)
  * sync site is executing a write transaction.
  */
 afs_int32
-SDISK_Begin(rxcall, atid)
-     register struct rx_call *rxcall;
-     struct ubik_tid *atid;
+SDISK_Begin(struct rx_call *rxcall, struct ubik_tid *atid)
 {
-    register afs_int32 code;
+    afs_int32 code;
 
     if ((code = ubik_CheckAuth(rxcall))) {
        return code;
     }
     DBHOLD(ubik_dbase);
-    urecovery_CheckTid(atid);
-    if (ubik_currentTrans) {
-       /* If the thread is not waiting for lock - ok to end it */
-#if !defined(UBIK_PAUSE)
-       if (ubik_currentTrans->locktype != LOCKWAIT) {
-#endif /* UBIK_PAUSE */
-           udisk_end(ubik_currentTrans);
-#if !defined(UBIK_PAUSE)
-       }
-#endif /* UBIK_PAUSE */
-       ubik_currentTrans = (struct ubik_trans *)0;
-    }
+    urecovery_CheckTid(atid, 1);
     code = udisk_begin(ubik_dbase, UBIK_WRITETRANS, &ubik_currentTrans);
     if (!code && ubik_currentTrans) {
        /* label this trans with the right trans id */
@@ -91,12 +83,10 @@ SDISK_Begin(rxcall, atid)
 
 
 afs_int32
-SDISK_Commit(rxcall, atid)
-     register struct rx_call *rxcall;
-     struct ubik_tid *atid;
+SDISK_Commit(struct rx_call *rxcall, struct ubik_tid *atid)
 {
-    register afs_int32 code;
-    register struct ubik_dbase *dbase;
+    afs_int32 code;
+    struct ubik_dbase *dbase;
 
     if ((code = ubik_CheckAuth(rxcall))) {
        return code;
@@ -113,10 +103,15 @@ SDISK_Commit(rxcall, atid)
     }
 
     dbase = ubik_currentTrans->dbase;
+
+    ObtainWriteLock(&dbase->cache_lock);
+
     DBHOLD(dbase);
-    urecovery_CheckTid(atid);
+
+    urecovery_CheckTid(atid, 0);
     if (!ubik_currentTrans) {
        DBRELE(dbase);
+       ReleaseWriteLock(&dbase->cache_lock);
        return USYNC;
     }
 
@@ -126,16 +121,15 @@ SDISK_Commit(rxcall, atid)
        ubik_dbVersion = ubik_dbase->version;
     }
     DBRELE(dbase);
+    ReleaseWriteLock(&dbase->cache_lock);
     return code;
 }
 
 afs_int32
-SDISK_ReleaseLocks(rxcall, atid)
-     register struct rx_call *rxcall;
-     struct ubik_tid *atid;
+SDISK_ReleaseLocks(struct rx_call *rxcall, struct ubik_tid *atid)
 {
-    register struct ubik_dbase *dbase;
-    register afs_int32 code;
+    struct ubik_dbase *dbase;
+    afs_int32 code;
 
     if ((code = ubik_CheckAuth(rxcall))) {
        return code;
@@ -151,7 +145,7 @@ SDISK_ReleaseLocks(rxcall, atid)
 
     dbase = ubik_currentTrans->dbase;
     DBHOLD(dbase);
-    urecovery_CheckTid(atid);
+    urecovery_CheckTid(atid, 0);
     if (!ubik_currentTrans) {
        DBRELE(dbase);
        return USYNC;
@@ -171,12 +165,10 @@ SDISK_ReleaseLocks(rxcall, atid)
 }
 
 afs_int32
-SDISK_Abort(rxcall, atid)
-     register struct rx_call *rxcall;
-     struct ubik_tid *atid;
+SDISK_Abort(struct rx_call *rxcall, struct ubik_tid *atid)
 {
-    register afs_int32 code;
-    register struct ubik_dbase *dbase;
+    afs_int32 code;
+    struct ubik_dbase *dbase;
 
     if ((code = ubik_CheckAuth(rxcall))) {
        return code;
@@ -192,7 +184,7 @@ SDISK_Abort(rxcall, atid)
 
     dbase = ubik_currentTrans->dbase;
     DBHOLD(dbase);
-    urecovery_CheckTid(atid);
+    urecovery_CheckTid(atid, 0);
     if (!ubik_currentTrans) {
        DBRELE(dbase);
        return USYNC;
@@ -212,14 +204,13 @@ SDISK_Abort(rxcall, atid)
     return code;
 }
 
+/* apos and alen are not used */
 afs_int32
-SDISK_Lock(rxcall, atid, afile, apos, alen, atype)
-     register struct rx_call *rxcall;
-     struct ubik_tid *atid;
-     afs_int32 afile, apos, alen, atype;       /* apos and alen are not used */
+SDISK_Lock(struct rx_call *rxcall, struct ubik_tid *atid,
+          afs_int32 afile, afs_int32 apos, afs_int32 alen, afs_int32 atype)
 {
-    register afs_int32 code;
-    register struct ubik_dbase *dbase;
+    afs_int32 code;
+    struct ubik_dbase *dbase;
     struct ubik_trans *ubik_thisTrans;
 
     if ((code = ubik_CheckAuth(rxcall))) {
@@ -237,7 +228,7 @@ SDISK_Lock(rxcall, atid, afile, apos, alen, atype)
     }
     dbase = ubik_currentTrans->dbase;
     DBHOLD(dbase);
-    urecovery_CheckTid(atid);
+    urecovery_CheckTid(atid, 0);
     if (!ubik_currentTrans) {
        DBRELE(dbase);
        return USYNC;
@@ -259,13 +250,12 @@ SDISK_Lock(rxcall, atid, afile, apos, alen, atype)
     return code;
 }
 
-/* Write a vector of data */
+/*!
+ * \brief Write a vector of data
+ */
 afs_int32
-SDISK_WriteV(rxcall, atid, io_vector, io_buffer)
-     register struct rx_call *rxcall;
-     struct ubik_tid *atid;
-     iovec_wrt *io_vector;
-     iovec_buf *io_buffer;
+SDISK_WriteV(struct rx_call *rxcall, struct ubik_tid *atid,
+            iovec_wrt *io_vector, iovec_buf *io_buffer)
 {
     afs_int32 code, i, offset;
     struct ubik_dbase *dbase;
@@ -285,7 +275,7 @@ SDISK_WriteV(rxcall, atid, io_vector, io_buffer)
 
     dbase = ubik_currentTrans->dbase;
     DBHOLD(dbase);
-    urecovery_CheckTid(atid);
+    urecovery_CheckTid(atid, 0);
     if (!ubik_currentTrans) {
        DBRELE(dbase);
        return USYNC;
@@ -313,14 +303,11 @@ SDISK_WriteV(rxcall, atid, io_vector, io_buffer)
 }
 
 afs_int32
-SDISK_Write(rxcall, atid, afile, apos, adata)
-     register struct rx_call *rxcall;
-     struct ubik_tid *atid;
-     afs_int32 afile, apos;
-     register bulkdata *adata;
+SDISK_Write(struct rx_call *rxcall, struct ubik_tid *atid,
+           afs_int32 afile, afs_int32 apos, bulkdata *adata)
 {
-    register afs_int32 code;
-    register struct ubik_dbase *dbase;
+    afs_int32 code;
+    struct ubik_dbase *dbase;
 
     if ((code = ubik_CheckAuth(rxcall))) {
        return code;
@@ -335,7 +322,7 @@ SDISK_Write(rxcall, atid, afile, apos, adata)
 
     dbase = ubik_currentTrans->dbase;
     DBHOLD(dbase);
-    urecovery_CheckTid(atid);
+    urecovery_CheckTid(atid, 0);
     if (!ubik_currentTrans) {
        DBRELE(dbase);
        return USYNC;
@@ -348,14 +335,11 @@ SDISK_Write(rxcall, atid, afile, apos, adata)
 }
 
 afs_int32
-SDISK_Truncate(rxcall, atid, afile, alen)
-     register struct rx_call *rxcall;
-     struct ubik_tid *atid;
-     afs_int32 afile;
-     afs_int32 alen;
+SDISK_Truncate(struct rx_call *rxcall, struct ubik_tid *atid,
+              afs_int32 afile, afs_int32 alen)
 {
-    register afs_int32 code;
-    register struct ubik_dbase *dbase;
+    afs_int32 code;
+    struct ubik_dbase *dbase;
 
     if ((code = ubik_CheckAuth(rxcall))) {
        return code;
@@ -370,7 +354,7 @@ SDISK_Truncate(rxcall, atid, afile, alen)
 
     dbase = ubik_currentTrans->dbase;
     DBHOLD(dbase);
-    urecovery_CheckTid(atid);
+    urecovery_CheckTid(atid, 0);
     if (!ubik_currentTrans) {
        DBRELE(dbase);
        return USYNC;
@@ -381,11 +365,10 @@ SDISK_Truncate(rxcall, atid, afile, alen)
 }
 
 afs_int32
-SDISK_GetVersion(rxcall, aversion)
-     register struct rx_call *rxcall;
-     register struct ubik_version *aversion;
+SDISK_GetVersion(struct rx_call *rxcall,
+                struct ubik_version *aversion)
 {
-    register afs_int32 code;
+    afs_int32 code;
 
     if ((code = ubik_CheckAuth(rxcall))) {
        return code;
@@ -419,14 +402,12 @@ SDISK_GetVersion(rxcall, aversion)
 }
 
 afs_int32
-SDISK_GetFile(rxcall, file, version)
-     register struct rx_call *rxcall;
-     register afs_int32 file;
-     struct ubik_version *version;
+SDISK_GetFile(struct rx_call *rxcall, afs_int32 file,
+             struct ubik_version *version)
 {
-    register afs_int32 code;
-    register struct ubik_dbase *dbase;
-    register afs_int32 offset;
+    afs_int32 code;
+    struct ubik_dbase *dbase;
+    afs_int32 offset;
     struct ubik_stat ubikstat;
     char tbuffer[256];
     afs_int32 tlen;
@@ -436,7 +417,7 @@ SDISK_GetFile(rxcall, file, version)
        return code;
     }
 /* temporarily disabled because it causes problems for migration tool.  Hey, it's just
- * a sanity check, anyway. 
+ * a sanity check, anyway.
     if (ubeacon_AmSyncSite()) {
       return UDEADLOCK;
     }
@@ -480,21 +461,25 @@ SDISK_GetFile(rxcall, file, version)
 }
 
 afs_int32
-SDISK_SendFile(rxcall, file, length, avers)
-     register struct rx_call *rxcall;
-     afs_int32 file;
-     afs_int32 length;
-     struct ubik_version *avers;
+SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
+              afs_int32 length, struct ubik_version *avers)
 {
-    register afs_int32 code;
-    register struct ubik_dbase *dbase;
-    char tbuffer[256];
+    afs_int32 code;
+    struct ubik_dbase *dbase = NULL;
+    char tbuffer[1024];
     afs_int32 offset;
     struct ubik_version tversion;
-    register int tlen;
+    int tlen;
     struct rx_peer *tpeer;
     struct rx_connection *tconn;
-    afs_uint32 otherHost;
+    afs_uint32 otherHost = 0;
+    char hoststr[16];
+#ifndef OLD_URECOVERY
+    char pbuffer[1028];
+    int flen, fd = -1;
+    afs_int32 epoch = 0;
+    afs_int32 pass;
+#endif
 
     /* send the file back to the requester */
 
@@ -529,47 +514,116 @@ SDISK_SendFile(rxcall, file, length, avers)
     urecovery_AbortAll(dbase);
 
     ubik_print("Ubik: Synchronize database with server %s\n",
-              afs_inet_ntoa(otherHost));
+              afs_inet_ntoa_r(otherHost, hoststr));
 
     offset = 0;
+#ifdef OLD_URECOVERY
     (*dbase->truncate) (dbase, file, 0);       /* truncate first */
-    tversion.epoch = 0;                /* start off by labelling in-transit db as invalid */
     tversion.counter = 0;
+#else
+    epoch =
+#endif
+    tversion.epoch = 0;                /* start off by labelling in-transit db as invalid */
     (*dbase->setlabel) (dbase, file, &tversion);       /* setlabel does sync */
+#ifndef OLD_URECOVERY
+    flen = length;
+    afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.TMP", ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
+    fd = open(pbuffer, O_CREAT | O_RDWR | O_TRUNC, 0600);
+    if (fd < 0) {
+       code = errno;
+       goto failed;
+    }
+    code = lseek(fd, HDRSIZE, 0);
+    if (code != HDRSIZE) {
+       close(fd);
+       goto failed;
+    }
+    pass = 0;
+#endif
     memcpy(&ubik_dbase->version, &tversion, sizeof(struct ubik_version));
     while (length > 0) {
        tlen = (length > sizeof(tbuffer) ? sizeof(tbuffer) : length);
+#if !defined(OLD_URECOVERY) && !defined(AFS_PTHREAD_ENV)
+       if (pass % 4 == 0)
+           IOMGR_Poll();
+#endif
        code = rx_Read(rxcall, tbuffer, tlen);
        if (code != tlen) {
            DBRELE(dbase);
            ubik_dprint("Rx-read length error=%d\n", code);
            code = BULK_ERROR;
+           close(fd);
            goto failed;
        }
+#ifdef OLD_URECOVERY
        code = (*dbase->write) (dbase, file, tbuffer, offset, tlen);
+#else
+       code = write(fd, tbuffer, tlen);
+       pass++;
+#endif
        if (code != tlen) {
            DBRELE(dbase);
            ubik_dprint("write failed error=%d\n", code);
            code = UIOERROR;
+           close(fd);
            goto failed;
        }
        offset += tlen;
        length -= tlen;
     }
+#ifndef OLD_URECOVERY
+    code = close(fd);
+    if (code)
+       goto failed;
+#endif
 
     /* sync data first, then write label and resync (resync done by setlabel call).
      * This way, good label is only on good database. */
+#ifdef OLD_URECOVERY
     (*ubik_dbase->sync) (dbase, file);
-    code = (*ubik_dbase->setlabel) (dbase, file, avers);
+#else
+    afs_snprintf(tbuffer, sizeof(tbuffer), "%s.DB%s%d", ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
+#ifdef AFS_NT40_ENV
+    afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.OLD", ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
+    code = unlink(pbuffer);
+    if (!code)
+       code = rename(tbuffer, pbuffer);
+    afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.TMP", ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
+#endif
+    if (!code)
+       code = rename(pbuffer, tbuffer);
+    if (!code) {
+       (*ubik_dbase->open) (ubik_dbase, file);
+#endif
+       code = (*ubik_dbase->setlabel) (dbase, file, avers);
+#ifndef OLD_URECOVERY
+    }
+#ifdef AFS_NT40_ENV
+    afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.OLD", ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
+    unlink(pbuffer);
+#endif
+#endif
     memcpy(&ubik_dbase->version, avers, sizeof(struct ubik_version));
     udisk_Invalidate(dbase, file);     /* new dbase, flush disk buffers */
+#ifdef AFS_PTHREAD_ENV
+    assert(pthread_cond_broadcast(&dbase->version_cond) == 0);
+#else
     LWP_NoYieldSignal(&dbase->version);
+#endif
     DBRELE(dbase);
   failed:
     if (code) {
+#ifndef OLD_URECOVERY
+       unlink(pbuffer);
+       /* Failed to sync. Allow reads again for now. */
+       if (dbase != NULL) {
+           tversion.epoch = epoch;
+           (*dbase->setlabel) (dbase, file, &tversion);
+       }
+#endif
        ubik_print
            ("Ubik: Synchronize database with server %s failed (error = %d)\n",
-            afs_inet_ntoa(otherHost), code);
+            afs_inet_ntoa_r(otherHost, hoststr), code);
     } else {
        ubik_print("Ubik: Synchronize database completed\n");
     }
@@ -578,25 +632,26 @@ SDISK_SendFile(rxcall, file, length, avers)
 
 
 afs_int32
-SDISK_Probe(rxcall)
-     register struct rx_call *rxcall;
+SDISK_Probe(struct rx_call *rxcall)
 {
     return 0;
 }
 
-/*
-* Update remote machines addresses in my server list
-* Send back my addresses to caller of this RPC
-* Returns zero on success, else 1.
-*/
+/*!
+ * \brief Update remote machines addresses in my server list
+ *
+ * Send back my addresses to caller of this RPC
+ * \return zero on success, else 1.
+ */
 afs_int32
-SDISK_UpdateInterfaceAddr(rxcall, inAddr, outAddr)
-     register struct rx_call *rxcall;
-     UbikInterfaceAddr *inAddr, *outAddr;
+SDISK_UpdateInterfaceAddr(struct rx_call *rxcall,
+                         UbikInterfaceAddr *inAddr,
+                         UbikInterfaceAddr *outAddr)
 {
     struct ubik_server *ts, *tmp;
     afs_uint32 remoteAddr;     /* in net byte order */
     int i, j, found = 0, probableMatch = 0;
+    char hoststr[16];
 
     /* copy the output parameters */
     for (i = 0; i < UBIK_MAX_INTERFACE_ADDR; i++)
@@ -634,7 +689,7 @@ SDISK_UpdateInterfaceAddr(rxcall, inAddr, outAddr)
     if (!probableMatch || found) {
        ubik_print("Inconsistent Cell Info from server: ");
        for (i = 0; i < UBIK_MAX_INTERFACE_ADDR && inAddr->hostAddr[i]; i++)
-           ubik_print("%s ", afs_inet_ntoa(htonl(inAddr->hostAddr[i])));
+           ubik_print("%s ", afs_inet_ntoa_r(htonl(inAddr->hostAddr[i]), hoststr));
        ubik_print("\n");
        fflush(stdout);
        fflush(stderr);
@@ -648,33 +703,32 @@ SDISK_UpdateInterfaceAddr(rxcall, inAddr, outAddr)
 
     ubik_print("ubik: A Remote Server has addresses: ");
     for (i = 0; i < UBIK_MAX_INTERFACE_ADDR && ts->addr[i]; i++)
-       ubik_print("%s ", afs_inet_ntoa(ts->addr[i]));
+       ubik_print("%s ", afs_inet_ntoa_r(ts->addr[i], hoststr));
     ubik_print("\n");
 
     return 0;
 }
 
-void
-printServerInfo()
+static void
+printServerInfo(void)
 {
     struct ubik_server *ts;
     int i, j = 1;
+    char hoststr[16];
 
     ubik_print("Local CellServDB:");
     for (ts = ubik_servers; ts; ts = ts->next, j++) {
        ubik_print("Server %d: ", j);
        for (i = 0; (i < UBIK_MAX_INTERFACE_ADDR) && ts->addr[i]; i++)
-           ubik_print("%s ", afs_inet_ntoa(ts->addr[i]));
+           ubik_print("%s ", afs_inet_ntoa_r(ts->addr[i], hoststr));
     }
     ubik_print("\n");
 }
 
 afs_int32
-SDISK_SetVersion(rxcall, atid, oldversionp, newversionp)
-     struct rx_call *rxcall;
-     struct ubik_tid *atid;
-     struct ubik_version *oldversionp;
-     struct ubik_version *newversionp;
+SDISK_SetVersion(struct rx_call *rxcall, struct ubik_tid *atid,
+                struct ubik_version *oldversionp,
+                struct ubik_version *newversionp)
 {
     afs_int32 code = 0;
     struct ubik_dbase *dbase;
@@ -698,7 +752,7 @@ SDISK_SetVersion(rxcall, atid, oldversionp, newversionp)
 
     dbase = ubik_currentTrans->dbase;
     DBHOLD(dbase);
-    urecovery_CheckTid(atid);
+    urecovery_CheckTid(atid, 0);
     if (!ubik_currentTrans) {
        DBRELE(dbase);
        return USYNC;