Protect ubik cache accesses
[openafs.git] / src / ubik / ubik.c
index 843abb7..b86a17b 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
 #include <sys/types.h>
+#include <string.h>
+#include <stdarg.h>
+#include <time.h>
+
 #ifdef AFS_NT40_ENV
 #include <winsock2.h>
 #else
@@ -21,9 +23,8 @@ RCSID
 #include <netinet/in.h>
 #include <sys/param.h>
 #endif
-#include <time.h>
+
 #include <lock.h>
-#include <string.h>
 #include <rx/xdr.h>
 #include <rx/rx.h>
 #include <afs/cellconfig.h>
@@ -36,42 +37,41 @@ RCSID
 
 #define ERROR_EXIT(code) {error=(code); goto error_exit;}
 
-/*  This system is organized in a hierarchical set of related modules.  Modules
-    at one level can only call modules at the same level or below.
-    
-    At the bottom level (0) we have R, RFTP, LWP and IOMGR, i.e. the basic
-    operating system primitives.
-    
-    At the next level (1) we have
-
-       VOTER--The module responsible for casting votes when asked.  It is also
-       responsible for determining whether this server should try to become
-       a synchronization site.
-       
-       BEACONER--The module responsible for sending keep-alives out when a
-       server is actually the sync site, or trying to become a sync site.
-       
-       DISK--The module responsible for representing atomic transactions
-       on the local disk.  It maintains a new-value only log.
-       
-       LOCK--The module responsible for locking byte ranges in the database file.
-       
-    At the next level (2) we have
-         
-       RECOVERY--The module responsible for ensuring that all members of a quorum
-       have the same up-to-date database after a new synchronization site is
-       elected.  This module runs only on the synchronization site.
-       
-    At the next level (3) we have
-    
-       REMOTE--The module responsible for interpreting requests from the sync
-       site and applying them to the database, after obtaining the appropriate
-       locks.
-       
-    At the next level (4) we have
-    
-       UBIK--The module users call to perform operations on the database.
-*/
+/*!
+ * \file
+ * This system is organized in a hierarchical set of related modules.  Modules
+ * at one level can only call modules at the same level or below.
+ * 
+ * At the bottom level (0) we have R, RFTP, LWP and IOMGR, i.e. the basic
+ * operating system primitives.
+ *
+ * At the next level (1) we have
+ *
+ * \li VOTER--The module responsible for casting votes when asked.  It is also
+ * responsible for determining whether this server should try to become
+ * a synchronization site.
+ * \li BEACONER--The module responsible for sending keep-alives out when a
+ * server is actually the sync site, or trying to become a sync site.
+ * \li DISK--The module responsible for representing atomic transactions
+ * on the local disk.  It maintains a new-value only log.
+ * \li LOCK--The module responsible for locking byte ranges in the database file.
+ *     
+ * At the next level (2) we have
+ *  
+ * \li RECOVERY--The module responsible for ensuring that all members of a quorum
+ * have the same up-to-date database after a new synchronization site is
+ * elected.  This module runs only on the synchronization site.
+ *     
+ * At the next level (3) we have
+ *
+ * \li REMOTE--The module responsible for interpreting requests from the sync
+ * site and applying them to the database, after obtaining the appropriate
+ * locks.
+ *
+ * At the next level (4) we have
+ *
+ * \li UBIK--The module users call to perform operations on the database.
+ */
 
 
 /* some globals */
@@ -81,34 +81,35 @@ struct ubik_stats ubik_stats;
 afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR];
 afs_int32 ubik_epochTime = 0;
 afs_int32 urecovery_state = 0;
-int (*ubik_SRXSecurityProc) ();
-char *ubik_SRXSecurityRock;
+int (*ubik_SRXSecurityProc) (void *, struct rx_securityClass **, afs_int32 *);
+void *ubik_SRXSecurityRock;
 struct ubik_server *ubik_servers;
 short ubik_callPortal;
 
-static int BeginTrans();
+static int BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
+                     struct ubik_trans **transPtr, int readAny);
 
 struct rx_securityClass *ubik_sc[3];
 
-/* perform an operation at a quorum, handling error conditions.  return 0 if
-    all worked, otherwise mark failing server as down and return UERROR
-
-    Note that if any server misses an update, we must wait BIGTIME seconds before
-    allowing the transaction to commit, to ensure that the missing and possibly still
-    functioning server times out and stop handing out old data.  This is done in the commit
-    code, where we wait for a server marked down to have stayed down for BIGTIME seconds
-    before we allow a transaction to commit.  A server that fails but comes back up won't give
-    out old data because it is sent the sync count along with the beacon message that
-    marks it as *really* up (beaconSinceDown).
-*/
 #define        CStampVersion       1   /* meaning set ts->version */
+
+/*! 
+ * \brief Perform an operation at a quorum, handling error conditions.
+ * \return 0 if all worked and a quorum was contacted successfully
+ * \return otherwise mark failing server as down and return #UERROR
+ *
+ * \note If any server misses an update, we must wait #BIGTIME seconds before
+ * allowing the transaction to commit, to ensure that the missing and 
+ * possibly still functioning server times out and stops handing out old 
+ * data.  This is done in the commit code, where we wait for a server marked 
+ * down to have stayed down for #BIGTIME seconds before we allow a transaction 
+ * to commit.  A server that fails but comes back up won't give out old data 
+ * because it is sent the sync count along with the beacon message that
+ * marks it as \b really up (\p beaconSinceDown).
+ */
 afs_int32
-ContactQuorum(aproc, atrans, aflags, aparm0, aparm1, aparm2, aparm3, aparm4,
-             aparm5)
-     int (*aproc) ();
-     int aflags;
-     register struct ubik_trans *atrans;
-     long aparm0, aparm1, aparm2, aparm3, aparm4, aparm5;
+ContactQuorum_NoArguments(afs_int32 (*proc)(struct rx_connection *, ubik_tid *),
+                         register struct ubik_trans *atrans, int aflags)
 {
     register struct ubik_server *ts;
     register afs_int32 code;
@@ -122,25 +123,176 @@ ContactQuorum(aproc, atrans, aflags, aparm0, aparm1, aparm2, aparm3, aparm4,
            ts->currentDB = 0;  /* db is no longer current; we just missed an update */
            continue;           /* not up-to-date, don't bother */
        }
-       code =
-           (*aproc) (ts->disk_rxcid, &atrans->tid, aparm0, aparm1, aparm2,
-                     aparm3, aparm4, aparm5);
-       if ((aproc == DISK_WriteV) && (code <= -450) && (code > -500)) {
+       code = (*proc)(ts->disk_rxcid, &atrans->tid);
+       if (code) {             /* failure */
+           rcode = code;
+           ts->up = 0;         /* mark as down now; beacons will no longer be sent */
+           ts->currentDB = 0;
+           ts->beaconSinceDown = 0;
+           urecovery_LostServer();     /* tell recovery to try to resend dbase later */
+       } else {                /* success */
+           if (!ts->isClone)
+               okcalls++;      /* count up how many worked */
+           if (aflags & CStampVersion) {
+               ts->version = atrans->dbase->version;
+           }
+       }
+    }
+    /* return 0 if we successfully contacted a quorum, otherwise return error code.  We don't have to contact ourselves (that was done locally) */
+    if (okcalls + 1 >= ubik_quorum)
+       return 0;
+    else
+       return rcode;
+}
+
+afs_int32
+ContactQuorum_DISK_Lock(register struct ubik_trans *atrans, int aflags,afs_int32 file,
+                       afs_int32 position, afs_int32 length, afs_int32 type)
+{
+    register struct ubik_server *ts;
+    register afs_int32 code;
+    afs_int32 rcode, okcalls;
+
+    rcode = 0;
+    okcalls = 0;
+    for (ts = ubik_servers; ts; ts = ts->next) {
+       /* for each server */
+       if (!ts->up || !ts->currentDB) {
+           ts->currentDB = 0;  /* db is no longer current; we just missed an update */
+           continue;           /* not up-to-date, don't bother */
+       }
+       code = DISK_Lock(ts->disk_rxcid, &atrans->tid, file, position, length,
+                          type);
+       if (code) {             /* failure */
+           rcode = code;
+           ts->up = 0;         /* mark as down now; beacons will no longer be sent */
+           ts->currentDB = 0;
+           ts->beaconSinceDown = 0;
+           urecovery_LostServer();     /* tell recovery to try to resend dbase later */
+       } else {                /* success */
+           if (!ts->isClone)
+               okcalls++;      /* count up how many worked */
+           if (aflags & CStampVersion) {
+               ts->version = atrans->dbase->version;
+           }
+       }
+    }
+    /* return 0 if we successfully contacted a quorum, otherwise return error code.  We don't have to contact ourselves (that was done locally) */
+    if (okcalls + 1 >= ubik_quorum)
+       return 0;
+    else
+       return rcode;
+}
+
+afs_int32
+ContactQuorum_DISK_Write(register struct ubik_trans *atrans, int aflags,
+                        afs_int32 file, afs_int32 position, bulkdata *data)
+{
+    register struct ubik_server *ts;
+    register afs_int32 code;
+    afs_int32 rcode, okcalls;
+
+    rcode = 0;
+    okcalls = 0;
+    for (ts = ubik_servers; ts; ts = ts->next) {
+       /* for each server */
+       if (!ts->up || !ts->currentDB) {
+           ts->currentDB = 0;  /* db is no longer current; we just missed an update */
+           continue;           /* not up-to-date, don't bother */
+       }
+       code = DISK_Write(ts->disk_rxcid, &atrans->tid, file, position, data);
+       if (code) {             /* failure */
+           rcode = code;
+           ts->up = 0;         /* mark as down now; beacons will no longer be sent */
+           ts->currentDB = 0;
+           ts->beaconSinceDown = 0;
+           urecovery_LostServer();     /* tell recovery to try to resend dbase later */
+       } else {                /* success */
+           if (!ts->isClone)
+               okcalls++;      /* count up how many worked */
+           if (aflags & CStampVersion) {
+               ts->version = atrans->dbase->version;
+           }
+       }
+    }
+    /* return 0 if we successfully contacted a quorum, otherwise return error code.  We don't have to contact ourselves (that was done locally) */
+    if (okcalls + 1 >= ubik_quorum)
+       return 0;
+    else
+       return rcode;
+}
+
+afs_int32
+ContactQuorum_DISK_Truncate(register struct ubik_trans *atrans, int aflags,
+                           afs_int32 file, afs_int32 length)
+{
+    register struct ubik_server *ts;
+    register afs_int32 code;
+    afs_int32 rcode, okcalls;
+
+    rcode = 0;
+    okcalls = 0;
+    for (ts = ubik_servers; ts; ts = ts->next) {
+       /* for each server */
+       if (!ts->up || !ts->currentDB) {
+           ts->currentDB = 0;  /* db is no longer current; we just missed an update */
+           continue;           /* not up-to-date, don't bother */
+       }
+       code = DISK_Truncate(ts->disk_rxcid, &atrans->tid, file, length);
+       if (code) {             /* failure */
+           rcode = code;
+           ts->up = 0;         /* mark as down now; beacons will no longer be sent */
+           ts->currentDB = 0;
+           ts->beaconSinceDown = 0;
+           urecovery_LostServer();     /* tell recovery to try to resend dbase later */
+       } else {                /* success */
+           if (!ts->isClone)
+               okcalls++;      /* count up how many worked */
+           if (aflags & CStampVersion) {
+               ts->version = atrans->dbase->version;
+           }
+       }
+    }
+    /* return 0 if we successfully contacted a quorum, otherwise return error code.  We don't have to contact ourselves (that was done locally) */
+    if (okcalls + 1 >= ubik_quorum)
+       return 0;
+    else
+       return rcode;
+}
+
+afs_int32
+ContactQuorum_DISK_WriteV(register struct ubik_trans *atrans, int aflags,
+                         iovec_wrt * io_vector, iovec_buf *io_buffer)
+{
+    register struct ubik_server *ts;
+    register afs_int32 code;
+    afs_int32 rcode, okcalls;
+
+    rcode = 0;
+    okcalls = 0;
+    for (ts = ubik_servers; ts; ts = ts->next) {
+       /* for each server */
+       if (!ts->up || !ts->currentDB) {
+           ts->currentDB = 0;  /* db is no longer current; we just missed an update */
+           continue;           /* not up-to-date, don't bother */
+       }
+
+       code = DISK_WriteV(ts->disk_rxcid, &atrans->tid, io_vector, io_buffer);
+
+       if ((code <= -450) && (code > -500)) {
            /* An RPC interface mismatch (as defined in comerr/error_msg.c).
             * Un-bulk the entries and do individual DISK_Write calls
             * instead of DISK_WriteV.
             */
-           iovec_wrt *iovec_infoP = (iovec_wrt *) aparm0;
-           iovec_buf *iovec_dataP = (iovec_buf *) aparm1;
            struct ubik_iovec *iovec =
-               (struct ubik_iovec *)iovec_infoP->iovec_wrt_val;
-           char *iobuf = (char *)iovec_dataP->iovec_buf_val;
+               (struct ubik_iovec *)io_vector->iovec_wrt_val;
+           char *iobuf = (char *)io_buffer->iovec_buf_val;
            bulkdata tcbs;
            afs_int32 i, offset;
 
-           for (i = 0, offset = 0; i < iovec_infoP->iovec_wrt_len; i++) {
+           for (i = 0, offset = 0; i < io_vector->iovec_wrt_len; i++) {
                /* Sanity check for going off end of buffer */
-               if ((offset + iovec[i].length) > iovec_dataP->iovec_buf_len) {
+               if ((offset + iovec[i].length) > io_buffer->iovec_buf_len) {
                    code = UINTERNAL;
                    break;
                }
@@ -155,6 +307,7 @@ ContactQuorum(aproc, atrans, aflags, aparm0, aparm1, aparm2, aparm3, aparm4,
                offset += iovec[i].length;
            }
        }
+
        if (code) {             /* failure */
            rcode = code;
            ts->up = 0;         /* mark as down now; beacons will no longer be sent */
@@ -176,20 +329,68 @@ ContactQuorum(aproc, atrans, aflags, aparm0, aparm1, aparm2, aparm3, aparm4,
        return rcode;
 }
 
-/* This routine initializes the ubik system for a set of servers.  It returns 0 for success, or an error code on failure.  The set of servers is specified by serverList; nServers gives the number of entries in this array.  Finally, dbase is the returned structure representing this instance of a ubik; it is passed to various calls below.  The variable pathName provides an initial prefix used for naming storage files used by this system.  It should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
+afs_int32
+ContactQuorum_DISK_SetVersion(register struct ubik_trans *atrans, int aflags, 
+                             ubik_version *OldVersion,
+                             ubik_version *NewVersion)
+{
+    register struct ubik_server *ts;
+    register afs_int32 code;
+    afs_int32 rcode, okcalls;
 
-    Note that the host named by myHost should not also be listed in serverList.
-*/
+    rcode = 0;
+    okcalls = 0;
+    for (ts = ubik_servers; ts; ts = ts->next) {
+       /* for each server */
+       if (!ts->up || !ts->currentDB) {
+           ts->currentDB = 0;  /* db is no longer current; we just missed an update */
+           continue;           /* not up-to-date, don't bother */
+       }
+       code = DISK_SetVersion(ts->disk_rxcid, &atrans->tid, OldVersion, 
+                              NewVersion);
+       if (code) {             /* failure */
+           rcode = code;
+           ts->up = 0;         /* mark as down now; beacons will no longer be sent */
+           ts->currentDB = 0;
+           ts->beaconSinceDown = 0;
+           urecovery_LostServer();     /* tell recovery to try to resend dbase later */
+       } else {                /* success */
+           if (!ts->isClone)
+               okcalls++;      /* count up how many worked */
+           if (aflags & CStampVersion) {
+               ts->version = atrans->dbase->version;
+           }
+       }
+    }
+    /* return 0 if we successfully contacted a quorum, otherwise return error code.  We don't have to contact ourselves (that was done locally) */
+    if (okcalls + 1 >= ubik_quorum)
+       return 0;
+    else
+       return rcode;
+}
 
+/*! 
+ * \brief This routine initializes the ubik system for a set of servers.
+ * \return 0 for success, or an error code on failure.
+ * \param serverList set of servers specified; nServers gives the number of entries in this array.
+ * \param pathName provides an initial prefix used for naming storage files used by this system.  
+ * \param dbase the returned structure representing this instance of an ubik; it is passed to various calls below.  
+ *
+ * \todo This routine should perhaps be generalized to a low-level disk interface providing read, write, file enumeration and sync operations.
+ *
+ * \warning The host named by myHost should not also be listed in serverList.
+ *
+ * \see ubik_ServerInit(), ubik_ServerInitByInfo()
+ */
 int
 ubik_ServerInitCommon(afs_int32 myHost, short myPort,
                      struct afsconf_cell *info, char clones[],
-                     afs_int32 serverList[], char *pathName,
+                     afs_int32 serverList[], const char *pathName,
                      struct ubik_dbase **dbase)
 {
     register struct ubik_dbase *tdb;
     register afs_int32 code;
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
+#ifdef AFS_PTHREAD_ENV
     pthread_t rxServerThread;        /* pthread variables */
     pthread_t ubeacon_InteractThread;
     pthread_t urecovery_InteractThread;
@@ -198,14 +399,13 @@ ubik_ServerInitCommon(afs_int32 myHost, short myPort,
     pthread_attr_t urecovery_Interact_tattr;
 #else
     PROCESS junk;
+    extern int rx_stackSize;
 #endif
 
     afs_int32 secIndex;
     struct rx_securityClass *secClass;
 
     struct rx_service *tservice;
-    extern int VOTE_ExecuteRequest(), DISK_ExecuteRequest();
-    extern int rx_stackSize;
 
     initialize_U_error_table();
 
@@ -215,12 +415,17 @@ ubik_ServerInitCommon(afs_int32 myHost, short myPort,
     tdb->activeTrans = (struct ubik_trans *)0;
     memset(&tdb->version, 0, sizeof(struct ubik_version));
     memset(&tdb->cachedVersion, 0, sizeof(struct ubik_version));
+#ifdef AFS_PTHREAD_ENV
+    assert(pthread_mutex_init(&tdb->versionLock, NULL) == 0);
+#else
     Lock_Init(&tdb->versionLock);
+#endif
+    Lock_Init(&tdb->cache_lock);
     tdb->flags = 0;
     tdb->read = uphys_read;
     tdb->write = uphys_write;
     tdb->truncate = uphys_truncate;
-    tdb->open = 0;             /* this function isn't used any more */
+    tdb->open = uphys_invalidate;      /* this function isn't used any more */
     tdb->sync = uphys_sync;
     tdb->stat = uphys_stat;
     tdb->getlabel = uphys_getlabel;
@@ -231,6 +436,11 @@ ubik_ServerInitCommon(afs_int32 myHost, short myPort,
     *dbase = tdb;
     ubik_dbase = tdb;          /* for now, only one db per server; can fix later when we have names for the other dbases */
 
+#ifdef AFS_PTHREAD_ENV
+    assert(pthread_cond_init(&tdb->version_cond, NULL) == 0);
+    assert(pthread_cond_init(&tdb->flags_cond, NULL) == 0);
+#endif /* AFS_PTHREAD_ENV */
+
     /* initialize RX */
 
     /* the following call is idempotent so when/if it got called earlier,
@@ -290,7 +500,7 @@ ubik_ServerInitCommon(afs_int32 myHost, short myPort,
      * UpdateInterfaceAddr RPC that occurs in ubeacon_InitServerList. This avoids
      * the "steplock" problem in ubik initialization. Defect 11037.
      */
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
+#ifdef AFS_PTHREAD_ENV
 /* do assert stuff */
     assert(pthread_attr_init(&rxServer_tattr) == 0);
     assert(pthread_attr_setdetachstate(&rxServer_tattr, PTHREAD_CREATE_DETACHED) == 0);
@@ -299,7 +509,7 @@ ubik_ServerInitCommon(afs_int32 myHost, short myPort,
     assert(pthread_create(&rxServerThread, &rxServer_tattr, (void *)rx_ServerProc, NULL) == 0);
 #else
     LWP_CreateProcess(rx_ServerProc, rx_stackSize, RX_PROCESS_PRIORITY,
-              (void *)0, "rx_ServerProc", &junk);
+              NULL, "rx_ServerProc", &junk);
 #endif
 
     /* do basic initialization */
@@ -317,7 +527,7 @@ ubik_ServerInitCommon(afs_int32 myHost, short myPort,
        return code;
 
     /* now start up async processes */
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
+#ifdef AFS_PTHREAD_ENV
 /* do assert stuff */
     assert(pthread_attr_init(&ubeacon_Interact_tattr) == 0);
     assert(pthread_attr_setdetachstate(&ubeacon_Interact_tattr, PTHREAD_CREATE_DETACHED) == 0);
@@ -334,7 +544,7 @@ ubik_ServerInitCommon(afs_int32 myHost, short myPort,
        return code;
 #endif
 
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
+#ifdef AFS_PTHREAD_ENV
 /* do assert stuff */
     assert(pthread_attr_init(&urecovery_Interact_tattr) == 0);
     assert(pthread_attr_setdetachstate(&urecovery_Interact_tattr, PTHREAD_CREATE_DETACHED) == 0);
@@ -354,10 +564,13 @@ ubik_ServerInitCommon(afs_int32 myHost, short myPort,
 
 }
 
+/*!
+ * \see ubik_ServerInitCommon()
+ */
 int
 ubik_ServerInitByInfo(afs_int32 myHost, short myPort,
                      struct afsconf_cell *info, char clones[],
-                     char *pathName, struct ubik_dbase **dbase)
+                     const char *pathName, struct ubik_dbase **dbase)
 {
     afs_int32 code;
 
@@ -367,9 +580,12 @@ ubik_ServerInitByInfo(afs_int32 myHost, short myPort,
     return code;
 }
 
+/*!
+ * \see ubik_ServerInitCommon()
+ */
 int
 ubik_ServerInit(afs_int32 myHost, short myPort, afs_int32 serverList[],
-               char *pathName, struct ubik_dbase **dbase)
+               const char *pathName, struct ubik_dbase **dbase)
 {
     afs_int32 code;
 
@@ -379,17 +595,18 @@ ubik_ServerInit(afs_int32 myHost, short myPort, afs_int32 serverList[],
     return code;
 }
 
-/*  This routine begins a read or write transaction on the transaction
-    identified by transPtr, in the dbase named by dbase.  An open mode of
-    ubik_READTRANS identifies this as a read transaction, while a mode of
-    ubik_WRITETRANS identifies this as a write transaction.  transPtr 
-    is set to the returned transaction control block. The readAny flag is
-    set to 0 or 1 by the wrapper functions ubik_BeginTrans() or 
-    ubik_BeginTransReadAny() below.
-
-    We can only begin transaction when we have an up-to-date database.
-*/
-
+/*!
+ * \brief This routine begins a read or write transaction on the transaction
+ * identified by transPtr, in the dbase named by dbase.
+ *
+ * An open mode of ubik_READTRANS identifies this as a read transaction, 
+ * while a mode of ubik_WRITETRANS identifies this as a write transaction.
+ * transPtr is set to the returned transaction control block. 
+ * The readAny flag is set to 0 or 1 by the wrapper functions ubik_BeginTrans() or 
+ * ubik_BeginTransReadAny() below.
+ *
+ * \note We can only begin transaction when we have an up-to-date database.
+ */
 static int
 BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
           struct ubik_trans **transPtr, int readAny)
@@ -427,7 +644,7 @@ BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
 #endif
                return UNOQUORUM;       /* a white lie */
            }
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
+#ifdef AFS_PTHREAD_ENV
            sleep(2);
 #else
            IOMGR_Sleep(2);
@@ -448,16 +665,15 @@ BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
     if (transMode == UBIK_WRITETRANS) {
        /* if we're writing already, wait */
        while (dbase->flags & DBWRITING) {
-           DBRELE(dbase);
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
-           assert(pthread_mutex_lock(&dbase->flags_mutex) == 0);
-           assert(pthread_cond_wait(&dbase->flags_cond, &dbase->flags_mutex) == 0);
-           assert(pthread_mutex_unlock(&dbase->flags_mutex) == 0);
+#ifdef AFS_PTHREAD_ENV
+           assert(pthread_cond_wait(&dbase->flags_cond, &dbase->versionLock) == 0);
 #else
+           DBRELE(dbase);
            LWP_WaitProcess(&dbase->flags);
-#endif
            DBHOLD(dbase);
+#endif
        }
+
        if (!ubeacon_AmSyncSite()) {
            DBRELE(dbase);
            return UNOTSYNC;
@@ -487,11 +703,11 @@ BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
 #endif /* UBIK_PAUSE */
 
        /* next try to start transaction on appropriate number of machines */
-       code = ContactQuorum(DISK_Begin, tt, 0);
+       code = ContactQuorum_NoArguments(DISK_Begin, tt, 0);
        if (code) {
            /* we must abort the operation */
            udisk_abort(tt);
-           ContactQuorum(DISK_Abort, tt, 0);   /* force aborts to the others */
+           ContactQuorum_NoArguments(DISK_Abort, tt, 0); /* force aborts to the others */
            udisk_end(tt);
            DBRELE(dbase);
            return code;
@@ -503,6 +719,9 @@ BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
     return 0;
 }
 
+/*!
+ * \see BeginTrans()
+ */
 int
 ubik_BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
                struct ubik_trans **transPtr)
@@ -510,6 +729,9 @@ ubik_BeginTrans(register struct ubik_dbase *dbase, afs_int32 transMode,
     return BeginTrans(dbase, transMode, transPtr, 0);
 }
 
+/*!
+ * \see BeginTrans()
+ */
 int
 ubik_BeginTransReadAny(register struct ubik_dbase *dbase, afs_int32 transMode,
                       struct ubik_trans **transPtr)
@@ -517,7 +739,9 @@ ubik_BeginTransReadAny(register struct ubik_dbase *dbase, afs_int32 transMode,
     return BeginTrans(dbase, transMode, transPtr, 1);
 }
 
-/* this routine ends a read or write transaction by aborting it */
+/*!
+ * \brief This routine ends a read or write transaction by aborting it.
+ */
 int
 ubik_AbortTrans(register struct ubik_trans *transPtr)
 {
@@ -526,8 +750,19 @@ ubik_AbortTrans(register struct ubik_trans *transPtr)
     register struct ubik_dbase *dbase;
 
     dbase = transPtr->dbase;
+
+    if (transPtr->flags & TRCACHELOCKED) {
+       ReleaseReadLock(&dbase->cache_lock);
+       transPtr->flags &= ~TRCACHELOCKED;
+    }
+
+    ObtainWriteLock(&dbase->cache_lock);
+
     DBHOLD(dbase);
     memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
+
+    ReleaseWriteLock(&dbase->cache_lock);
+
     /* see if we're still up-to-date */
     if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
        udisk_abort(transPtr);
@@ -552,14 +787,17 @@ ubik_AbortTrans(register struct ubik_trans *transPtr)
     }
 
     /* now it is safe to try remote abort */
-    code = ContactQuorum(DISK_Abort, transPtr, 0);
+    code = ContactQuorum_NoArguments(DISK_Abort, transPtr, 0);
     code2 = udisk_abort(transPtr);
     udisk_end(transPtr);
     DBRELE(dbase);
     return (code ? code : code2);
 }
 
-/* This routine ends a read or write transaction on the open transaction identified by transPtr.  It returns an error code. */
+/*!
+ * \brief This routine ends a read or write transaction on the open transaction identified by transPtr.
+ * \return an error code.
+ */
 int
 ubik_EndTrans(register struct ubik_trans *transPtr)
 {
@@ -579,15 +817,20 @@ ubik_EndTrans(register struct ubik_trans *transPtr)
     }
 
     dbase = transPtr->dbase;
+
+    if (transPtr->flags & TRCACHELOCKED) {
+       ReleaseReadLock(&dbase->cache_lock);
+       transPtr->flags &= ~TRCACHELOCKED;
+    }
     DBHOLD(dbase);
-    memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
 
     /* give up if no longer current */
     if (!urecovery_AllBetter(dbase, transPtr->flags & TRREADANY)) {
        udisk_abort(transPtr);
        udisk_end(transPtr);
        DBRELE(dbase);
-       return UNOQUORUM;
+       code = UNOQUORUM;
+       goto error;
     }
 
     if (transPtr->type == UBIK_READTRANS) {    /* reads are easy */
@@ -596,20 +839,21 @@ ubik_EndTrans(register struct ubik_trans *transPtr)
            goto success;       /* update cachedVersion correctly */
        udisk_end(transPtr);
        DBRELE(dbase);
-       return code;
+       goto error;
     }
 
     if (!ubeacon_AmSyncSite()) {       /* no longer sync site */
        udisk_abort(transPtr);
        udisk_end(transPtr);
        DBRELE(dbase);
-       return UNOTSYNC;
+       code = UNOTSYNC;
+       goto error;
     }
 
     /* now it is safe to do commit */
     code = udisk_commit(transPtr);
     if (code == 0)
-       code = ContactQuorum(DISK_Commit, transPtr, CStampVersion);
+       code = ContactQuorum_NoArguments(DISK_Commit, transPtr, CStampVersion);
     if (code) {
        /* failed to commit, so must return failure.  Try to clear locks first, just for fun
         * Note that we don't know if this transaction will eventually commit at this point.
@@ -617,10 +861,10 @@ ubik_EndTrans(register struct ubik_trans *transPtr)
         * we lose.  If we contact a majority of sites, then we won't be here: contacting
         * a majority guarantees commit, since it guarantees that one dude will be a
         * member of the next quorum. */
-       ContactQuorum(DISK_ReleaseLocks, transPtr, 0);
+       ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
        udisk_end(transPtr);
        DBRELE(dbase);
-       return code;
+       goto error;
     }
     /* before we can start sending unlock messages, we must wait until all servers
      * that are possibly still functioning on the other side of a network partition
@@ -644,7 +888,7 @@ ubik_EndTrans(register struct ubik_trans *transPtr)
                code = 1;
                tv.tv_sec = 1;  /* try again after a while (ha ha) */
                tv.tv_usec = 0;
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
+#ifdef AFS_PTHREAD_ENV
                select(0, 0, 0, 0, &tv);
 #else
                IOMGR_Select(0, 0, 0, 0, &tv);  /* poll, should we wait on something? */
@@ -656,27 +900,42 @@ ubik_EndTrans(register struct ubik_trans *transPtr)
            break;              /* no down ones still pseudo-active */
     }
 
+    /* the commit bumped the dbase version, and since the write was local
+     * our cache should still be up to date, so make sure to update
+     * cachedVersion, too */
+    memcpy(&dbase->cachedVersion, &dbase->version,
+           sizeof(dbase->cachedVersion));
+
     /* finally, unlock all the dudes.  We can return success independent of the number of servers
      * that really unlock the dbase; the others will do it if/when they elect a new sync site.
      * The transaction is committed anyway, since we succeeded in contacting a quorum
      * at the start (when invoking the DiskCommit function).
      */
-    ContactQuorum(DISK_ReleaseLocks, transPtr, 0);
+    ContactQuorum_NoArguments(DISK_ReleaseLocks, transPtr, 0);
 
   success:
     udisk_end(transPtr);
-    /* update version on successful EndTrans */
-    memcpy(&dbase->cachedVersion, &dbase->version,
-          sizeof(struct ubik_version));
-
+    /* don't update cachedVersion here; it should have been updated way back
+     * in ubik_CheckCache, and earlier in this function for writes */
     DBRELE(dbase);
     return 0;
-}
 
-/* This routine reads length bytes into buffer from the current position in the database.  The file pointer is updated appropriately (by adding the number of bytes actually transferred), and the length actually transferred is stored in the long integer pointed to by length.  Note that *length is an INOUT parameter: at the start it represents the size of the buffer, and when done, it contains the number of bytes actually transferred.  A short read returns zero for an error code. */
+  error:
+    ObtainWriteLock(&dbase->cache_lock);
+    memset(&dbase->cachedVersion, 0, sizeof(struct ubik_version));
+    ReleaseWriteLock(&dbase->cache_lock);
+    return code;
+}
 
+/*!
+ * \brief This routine reads length bytes into buffer from the current position in the database.
+ * 
+ * The file pointer is updated appropriately (by adding the number of bytes actually transferred), and the length actually transferred is stored in the long integer pointed to by length.  A short read returns zero for an error code.
+ *
+ * \note *length is an INOUT parameter: at the start it represents the size of the buffer, and when done, it contains the number of bytes actually transferred.
+ */
 int
-ubik_Read(register struct ubik_trans *transPtr, char *buffer,
+ubik_Read(register struct ubik_trans *transPtr, void *buffer,
          afs_int32 length)
 {
     register afs_int32 code;
@@ -698,9 +957,11 @@ ubik_Read(register struct ubik_trans *transPtr, char *buffer,
     return code;
 }
 
-/* This routine will flush the io data in the iovec structures. It first
- * flushes to the local disk and then uses ContactQuorum to write it to 
- * the other servers.
+/*!
+ * \brief This routine will flush the io data in the iovec structures. 
+ *
+ * It first flushes to the local disk and then uses ContactQuorum to write it
+ * to the other servers.
  */
 int
 ubik_Flush(struct ubik_trans *transPtr)
@@ -721,11 +982,11 @@ ubik_Flush(struct ubik_trans *transPtr)
 
     /* Update the rest of the servers in the quorum */
     code =
-       ContactQuorum(DISK_WriteV, transPtr, 0, &transPtr->iovec_info,
-                     &transPtr->iovec_data);
+       ContactQuorum_DISK_WriteV(transPtr, 0, &transPtr->iovec_info,
+                                 &transPtr->iovec_data);
     if (code) {
        udisk_abort(transPtr);
-       ContactQuorum(DISK_Abort, transPtr, 0); /* force aborts to the others */
+       ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
        transPtr->iovec_info.iovec_wrt_len = 0;
        transPtr->iovec_data.iovec_buf_len = 0;
        ERROR_EXIT(code);
@@ -741,12 +1002,13 @@ ubik_Flush(struct ubik_trans *transPtr)
 }
 
 int
-ubik_Write(register struct ubik_trans *transPtr, char *buffer,
+ubik_Write(register struct ubik_trans *transPtr, void *vbuffer,
           afs_int32 length)
 {
     struct ubik_iovec *iovec;
     afs_int32 code, error = 0;
     afs_int32 pos, len, size;
+    char * buffer = (char *)vbuffer;
 
     if (transPtr->type != UBIK_WRITETRANS)
        return UBADTYPE;
@@ -756,7 +1018,7 @@ ubik_Write(register struct ubik_trans *transPtr, char *buffer,
     if (length > IOVEC_MAXBUF) {
        for (pos = 0, len = length; len > 0; len -= size, pos += size) {
            size = ((len < IOVEC_MAXBUF) ? len : IOVEC_MAXBUF);
-           code = ubik_Write(transPtr, &buffer[pos], size);
+           code = ubik_Write(transPtr, buffer+pos, size);
            if (code)
                return (code);
        }
@@ -826,8 +1088,13 @@ ubik_Write(register struct ubik_trans *transPtr, char *buffer,
     return error;
 }
 
-/* This sets the file pointer associated with the current transaction to the appropriate file and byte position.  Unlike Unix files, a transaction is labelled by both a file number (fileid) and a byte position relative to the specified file (position). */
-
+/*!
+ * \brief This sets the file pointer associated with the current transaction
+ * to the appropriate file and byte position.
+ *
+ * Unlike Unix files, a transaction is labelled by both a file number \p fileid
+ * and a byte position relative to the specified file \p position.
+ */
 int
 ubik_Seek(register struct ubik_trans *transPtr, afs_int32 fileid,
          afs_int32 position)
@@ -846,8 +1113,10 @@ ubik_Seek(register struct ubik_trans *transPtr, afs_int32 fileid,
     return code;
 }
 
-/* This call returns the file pointer associated with the specified transaction in fileid and position. */
-
+/*!
+ * \brief This call returns the file pointer associated with the specified
+ * transaction in \p fileid and \p position.
+ */
 int
 ubik_Tell(register struct ubik_trans *transPtr, afs_int32 * fileid,
          afs_int32 * position)
@@ -859,8 +1128,10 @@ ubik_Tell(register struct ubik_trans *transPtr, afs_int32 * fileid,
     return 0;
 }
 
-/* This sets the file size for the currently-selected file to length bytes, if length is less than the file's current size. */
-
+/*!
+ * \brief This sets the file size for the currently-selected file to \p length
+ * bytes, if length is less than the file's current size.
+ */
 int
 ubik_Truncate(register struct ubik_trans *transPtr, afs_int32 length)
 {
@@ -882,13 +1153,13 @@ ubik_Truncate(register struct ubik_trans *transPtr, afs_int32 length)
     code = udisk_truncate(transPtr, transPtr->seekFile, length);
     if (!code) {
        code =
-           ContactQuorum(DISK_Truncate, transPtr, 0, transPtr->seekFile,
-                         length);
+           ContactQuorum_DISK_Truncate(transPtr, 0, transPtr->seekFile,
+                                       length);
     }
     if (code) {
        /* we must abort the operation */
        udisk_abort(transPtr);
-       ContactQuorum(DISK_Abort, transPtr, 0); /* force aborts to the others */
+       ContactQuorum_NoArguments(DISK_Abort, transPtr, 0); /* force aborts to the others */
        ERROR_EXIT(code);
     }
 
@@ -897,7 +1168,9 @@ ubik_Truncate(register struct ubik_trans *transPtr, afs_int32 length)
     return error;
 }
 
-/* set a lock; all locks are released on transaction end (commit/abort) */
+/*!
+ * \brief set a lock; all locks are released on transaction end (commit/abort)
+ */
 int
 ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
             int atype)
@@ -927,13 +1200,13 @@ ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
        /* now do the operation locally, and propagate it out */
        code = ulock_getLock(atrans, atype, 1);
        if (code == 0) {
-           code = ContactQuorum(DISK_Lock, atrans, 0, 0, 1 /*unused */ ,
-                                1 /*unused */ , LOCKWRITE);
+           code = ContactQuorum_DISK_Lock(atrans, 0, 0, 1 /*unused */ ,
+                                          1 /*unused */ , LOCKWRITE);
        }
        if (code) {
            /* we must abort the operation */
            udisk_abort(atrans);
-           ContactQuorum(DISK_Abort, atrans, 0);       /* force aborts to the others */
+           ContactQuorum_NoArguments(DISK_Abort, atrans, 0); /* force aborts to the others */
            ERROR_EXIT(code);
        }
     }
@@ -943,26 +1216,33 @@ ubik_SetLock(struct ubik_trans *atrans, afs_int32 apos, afs_int32 alen,
     return error;
 }
 
-/* utility to wait for a version # to change */
+/*!
+ * \brief utility to wait for a version # to change
+ */
 int
 ubik_WaitVersion(register struct ubik_dbase *adatabase,
                 register struct ubik_version *aversion)
 {
+    DBHOLD(adatabase);
     while (1) {
        /* wait until version # changes, and then return */
-       if (vcmp(*aversion, adatabase->version) != 0)
+       if (vcmp(*aversion, adatabase->version) != 0) {
+           DBRELE(adatabase);
            return 0;
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
-       assert(pthread_mutex_lock(&adatabase->version_mutex) == 0);
-       assert(pthread_cond_wait(&adatabase->version_cond,&adatabase->version_mutex) == 0);
-       assert(pthread_mutex_unlock(&adatabase->version_mutex) == 0);
+       }
+#ifdef AFS_PTHREAD_ENV
+       assert(pthread_cond_wait(&adatabase->version_cond, &adatabase->versionLock) == 0);
 #else
+       DBRELE(adatabase);
        LWP_WaitProcess(&adatabase->version);   /* same vers, just wait */
+       DBHOLD(adatabase);
 #endif
     }
 }
 
-/* utility to get the version of the dbase a transaction is dealing with */
+/*!
+ * \brief utility to get the version of the dbase a transaction is dealing with
+ */
 int
 ubik_GetVersion(register struct ubik_trans *atrans,
                register struct ubik_version *avers)
@@ -971,12 +1251,15 @@ ubik_GetVersion(register struct ubik_trans *atrans,
     return 0;
 }
 
-/* Facility to simplify database caching.  Returns zero if last trans was done
-   on the local server and was successful.  If return value is non-zero and the
-   caller is a server caching part of the Ubik database, it should invalidate
-   that cache.  A return value of -1 means bad (NULL) argument. */
-
-int
+/*!
+ * \brief Facility to simplify database caching.  
+ * \return zero if last trans was done on the local server and was successful.
+ * \return -1 means bad (NULL) argument.
+ * 
+ * If return value is non-zero and the caller is a server caching part of the 
+ * Ubik database, it should invalidate that cache.
+ */
+static int
 ubik_CacheUpdate(register struct ubik_trans *atrans)
 {
     if (!(atrans && atrans->dbase))
@@ -984,20 +1267,100 @@ ubik_CacheUpdate(register struct ubik_trans *atrans)
     return vcmp(atrans->dbase->cachedVersion, atrans->dbase->version) != 0;
 }
 
+/**
+ * check and possibly update cache of ubik db.
+ *
+ * If the version of the cached db data is out of date, this calls (*check) to
+ * update the cache. If (*check) returns success, we update the version of the
+ * cached db data.
+ *
+ * Checking the version of the cached db data is done under a read lock;
+ * updating the cache (and thus calling (*check)) is done under a write lock
+ * so is guaranteed not to interfere with another thread's (*check). On
+ * successful return, a read lock on the cached db data is obtained, which
+ * will be released by ubik_EndTrans or ubik_AbortTrans.
+ *
+ * @param[in] atrans ubik transaction
+ * @param[in] check  function to call to check/update cache
+ * @param[in] rock   rock to pass to *check
+ *
+ * @return operation status
+ *   @retval 0       success
+ *   @retval nonzero error; cachedVersion not updated
+ *
+ * @post On success, application cache is read-locked, and cache data is
+ *       up-to-date
+ */
 int
-panic(char *a, char *b, char *c, char *d)
+ubik_CheckCache(struct ubik_trans *atrans, ubik_updatecache_func cbf, void *rock)
+{
+    int ret = 0;
+
+    if (!(atrans && atrans->dbase))
+       return -1;
+
+    ObtainReadLock(&atrans->dbase->cache_lock);
+
+    while (ubik_CacheUpdate(atrans) != 0) {
+
+       ReleaseReadLock(&atrans->dbase->cache_lock);
+       ObtainSharedLock(&atrans->dbase->cache_lock);
+
+       if (ubik_CacheUpdate(atrans) != 0) {
+
+           BoostSharedLock(&atrans->dbase->cache_lock);
+
+           ret = (*cbf) (atrans, rock);
+           if (ret == 0) {
+               memcpy(&atrans->dbase->cachedVersion, &atrans->dbase->version,
+                      sizeof(atrans->dbase->cachedVersion));
+           }
+       }
+
+       /* It would be nice if we could convert from a shared lock to a read
+        * lock... instead, just release the shared and acquire the read */
+       ReleaseSharedLock(&atrans->dbase->cache_lock);
+
+       if (ret) {
+           /* if we have an error, don't retry, and don't hold any locks */
+           return ret;
+       }
+
+       ObtainReadLock(&atrans->dbase->cache_lock);
+    }
+
+    atrans->flags |= TRCACHELOCKED;
+
+    return 0;
+}
+
+/*!
+ * "Who said anything about panicking?" snapped Arthur. 
+ * "This is still just the culture shock. You wait till I've settled down
+ * into the situation and found my bearings. \em Then I'll start panicking!"
+ * --Authur Dent
+ *
+ * \returns There is no return from panic.
+ */
+void
+panic(char *format, ...)
 {
+    va_list ap;
+
+    va_start(ap, format);
     ubik_print("Ubik PANIC: ");
-    ubik_print(a, b, c, d);
+    ubik_vprint(format, ap);
+    va_end(ap);
+
     abort();
     ubik_print("BACK FROM ABORT\n");   /* shouldn't come back */
     exit(1);                   /* never know, though  */
 }
 
-/*
-** This functions takes an IP addresses as its parameter. It returns the
-** the primary IP address that is on the host passed in.
-*/
+/*!
+ * This function takes an IP addresses as its parameter. It returns the
+ * the primary IP address that is on the host passed in, or 0 if not found.
+ */
 afs_uint32
 ubikGetPrimaryInterfaceAddr(afs_uint32 addr)
 {