ubik: Make udisk_Log* functions static
[openafs.git] / src / ubik / disk.c
index fecfcd8..a091e7f 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 <roken.h>
+#include <afs/opr.h>
 
-#include <sys/types.h>
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
+#ifdef AFS_PTHREAD_ENV
+# include <opr/lock.h>
 #else
-#include <sys/file.h>
-#include <netinet/in.h>
+# include <opr/lockstub.h>
 #endif
-#include <errno.h>
-#include <string.h>
-#include <lock.h>
-#include <rx/xdr.h>
-
-
 
 #define UBIK_INTERNALS
 #include "ubik.h"
@@ -33,36 +25,37 @@ RCSID
 
 #define        PHSIZE  128
 static struct buffer {
-    struct ubik_dbase *dbase;  /* dbase within which the buffer resides */
-    afs_int32 file;            /* Unique cache key */
-    afs_int32 page;            /* page number */
+    struct ubik_dbase *dbase;  /*!< dbase within which the buffer resides */
+    afs_int32 file;            /*!< Unique cache key */
+    afs_int32 page;            /*!< page number */
     struct buffer *lru_next;
     struct buffer *lru_prev;
-    struct buffer *hashNext;   /* next dude in hash table */
-    char *data;                        /* ptr to the data */
-    char lockers;              /* usage ref count */
-    char dirty;                        /* is buffer modified */
-    char hashIndex;            /* back ptr to hash table */
+    struct buffer *hashNext;   /*!< next dude in hash table */
+    char *data;                        /*!< ptr to the data */
+    char lockers;              /*!< usage ref count */
+    char dirty;                        /*!< is buffer modified */
+    char hashIndex;            /*!< back ptr to hash table */
 } *Buffers;
 
 #define pHash(page) ((page) & (PHSIZE-1))
 
 afs_int32 ubik_nBuffers = NBUFFERS;
-static struct buffer *phTable[PHSIZE]; /* page hash table */
+static struct buffer *phTable[PHSIZE]; /*!< page hash table */
 static struct buffer *LruBuffer;
 static int nbuffers;
 static int calls = 0, ios = 0, lastb = 0;
 static char *BufferData;
 static struct buffer *newslot(struct ubik_dbase *adbase, afs_int32 afid,
                              afs_int32 apage);
-static initd = 0;
 #define        BADFID      0xffffffff
 
-static DTrunc(struct ubik_dbase *dbase, afs_int32 fid, afs_int32 length);
+static int DTrunc(struct ubik_trans *atrans, afs_int32 fid, afs_int32 length);
 
 static struct ubik_trunc *freeTruncList = 0;
 
-/* remove a transaction from the database's active transaction list.  Don't free it */
+/*!
+ * \brief Remove a transaction from the database's active transaction list.  Don't free it.
+ */
 static int
 unthread(struct ubik_trans *atrans)
 {
@@ -78,8 +71,10 @@ unthread(struct ubik_trans *atrans)
     return 2;                  /* no entry */
 }
 
-/* some debugging assistance */
-int
+/*!
+ * \brief some debugging assistance
+ */
+void
 udisk_Debug(struct ubik_debug *aparm)
 {
     struct buffer *tb;
@@ -97,24 +92,24 @@ udisk_Debug(struct ubik_debug *aparm)
                aparm->writeLockedPages++;
        }
     }
-    return 0;
 }
 
-/* log format is defined here, and implicitly in recovery.c
+/*!
+ * \brief Write an opcode to the log.
+ *
+ * log format is defined here, and implicitly in recovery.c
  *
  * 4 byte opcode, followed by parameters, each 4 bytes long.  All integers
  * are in logged in network standard byte order, in case we want to move logs
  * from machine-to-machine someday.
  *
- * Begin transaction: opcode
- * Commit transaction: opcode, version (8 bytes)
- * Truncate file: opcode, file number, length
- * Abort transaction: opcode
- * Write data: opcode, file, position, length, <length> data bytes
+ * Begin transaction: opcode \n
+ * Commit transaction: opcode, version (8 bytes) \n
+ * Truncate file: opcode, file number, length \n
+ * Abort transaction: opcode \n
+ * Write data: opcode, file, position, length, <length> data bytes \n
  */
-
-/* write an opcode to the log */
-int
+static int
 udisk_LogOpcode(struct ubik_dbase *adbase, afs_int32 aopcode, int async)
 {
     struct ubik_stat ustat;
@@ -141,8 +136,10 @@ udisk_LogOpcode(struct ubik_dbase *adbase, afs_int32 aopcode, int async)
     return code;
 }
 
-/* log a commit, never syncing */
-int
+/*!
+ * \brief Log a commit, never syncing.
+ */
+static int
 udisk_LogEnd(struct ubik_dbase *adbase, struct ubik_version *aversion)
 {
     afs_int32 code;
@@ -171,8 +168,10 @@ udisk_LogEnd(struct ubik_dbase *adbase, struct ubik_version *aversion)
     return code;
 }
 
-/* log a truncate operation, never syncing */
-int
+/*!
+ * \brief Log a truncate operation, never syncing.
+ */
+static int
 udisk_LogTruncate(struct ubik_dbase *adbase, afs_int32 afile,
                  afs_int32 alength)
 {
@@ -199,9 +198,11 @@ udisk_LogTruncate(struct ubik_dbase *adbase, afs_int32 afile,
     return 0;
 }
 
-/* write some data to the log, never syncing */
-int
-udisk_LogWriteData(struct ubik_dbase *adbase, afs_int32 afile, char *abuffer,
+/*!
+ * \brief Write some data to the log, never syncing.
+ */
+static int
+udisk_LogWriteData(struct ubik_dbase *adbase, afs_int32 afile, void *abuffer,
                   afs_int32 apos, afs_int32 alen)
 {
     struct ubik_stat ustat;
@@ -235,15 +236,14 @@ udisk_LogWriteData(struct ubik_dbase *adbase, afs_int32 afile, char *abuffer,
     return 0;
 }
 
-static int
-DInit(int abuffers)
+int
+udisk_Init(int abuffers)
 {
     /* Initialize the venus buffer system. */
     int i;
     struct buffer *tb;
-    Buffers = (struct buffer *)malloc(abuffers * sizeof(struct buffer));
-    memset(Buffers, 0, abuffers * sizeof(struct buffer));
-    BufferData = (char *)malloc(abuffers * UBIK_PAGESIZE);
+    Buffers = calloc(abuffers, sizeof(struct buffer));
+    BufferData = malloc(abuffers * UBIK_PAGESIZE);
     nbuffers = abuffers;
     for (i = 0; i < PHSIZE; i++)
        phTable[i] = 0;
@@ -261,7 +261,9 @@ DInit(int abuffers)
     return 0;
 }
 
-/* Take a buffer and mark it as the least recently used buffer */
+/*!
+ * \brief Take a buffer and mark it as the least recently used buffer.
+ */
 static void
 Dlru(struct buffer *abuf)
 {
@@ -281,7 +283,9 @@ Dlru(struct buffer *abuf)
     LruBuffer = abuf;
 }
 
-/* Take a buffer and mark it as the most recently used buffer */
+/*!
+ * \brief Take a buffer and mark it as the most recently used buffer.
+ */
 static void
 Dmru(struct buffer *abuf)
 {
@@ -301,31 +305,67 @@ Dmru(struct buffer *abuf)
     LruBuffer->lru_prev = abuf;
 }
 
-/* get a pointer to a particular buffer */
+static_inline int
+MatchBuffer(struct buffer *buf, int page, afs_int32 fid,
+            struct ubik_trans *atrans)
+{
+    if (buf->page != page) {
+       return 0;
+    }
+    if (buf->file != fid) {
+       return 0;
+    }
+    if (atrans->type == UBIK_READTRANS && buf->dirty) {
+       /* if 'buf' is dirty, it has uncommitted changes; we do not want to
+        * see uncommitted changes if we are a read transaction, so skip over
+        * it. */
+       return 0;
+    }
+    if (buf->dbase != atrans->dbase) {
+       return 0;
+    }
+    return 1;
+}
+
+/*!
+ * \brief Get a pointer to a particular buffer.
+ */
 static char *
-DRead(struct ubik_dbase *dbase, afs_int32 fid, int page)
+DRead(struct ubik_trans *atrans, afs_int32 fid, int page)
 {
     /* Read a page from the disk. */
-    struct buffer *tb, *lastbuffer;
+    struct buffer *tb, *lastbuffer, *found_tb = NULL;
     afs_int32 code;
+    struct ubik_dbase *dbase = atrans->dbase;
 
     calls++;
     lastbuffer = LruBuffer->lru_prev;
 
-    if ((lastbuffer->page == page) && (lastbuffer->file == fid)
-       && (lastbuffer->dbase == dbase)) {
+    /* Skip for write transactions for a clean page - this may not be the right page to use */
+    if (MatchBuffer(lastbuffer, page, fid, atrans)
+               && (atrans->type == UBIK_READTRANS || lastbuffer->dirty)) {
        tb = lastbuffer;
        tb->lockers++;
        lastb++;
        return tb->data;
     }
     for (tb = phTable[pHash(page)]; tb; tb = tb->hashNext) {
-       if (tb->page == page && tb->file == fid && tb->dbase == dbase) {
-           Dmru(tb);
-           tb->lockers++;
-           return tb->data;
+       if (MatchBuffer(tb, page, fid, atrans)) {
+           if (tb->dirty || atrans->type == UBIK_READTRANS) {
+               found_tb = tb;
+               break;
+           }
+           /* Remember this clean page - we might use it */
+           found_tb = tb;
        }
     }
+    /* For a write transaction, use a matching clean page if no dirty one was found */
+    if (found_tb) {
+       Dmru(found_tb);
+       found_tb->lockers++;
+       return found_tb->data;
+    }
+
     /* can't find it */
     tb = newslot(dbase, fid, page);
     if (!tb)
@@ -351,13 +391,16 @@ DRead(struct ubik_dbase *dbase, afs_int32 fid, int page)
     return tb->data;
 }
 
-/* zap truncated pages */
+/*!
+ * \brief Zap truncated pages.
+ */
 static int
-DTrunc(struct ubik_dbase *dbase, afs_int32 fid, afs_int32 length)
+DTrunc(struct ubik_trans *atrans, afs_int32 fid, afs_int32 length)
 {
     afs_int32 maxPage;
     struct buffer *tb;
     int i;
+    struct ubik_dbase *dbase = atrans->dbase;
 
     maxPage = (length + UBIK_PAGESIZE - 1) >> UBIK_LOGPAGESIZE;        /* first invalid page now in file */
     for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
@@ -369,17 +412,19 @@ DTrunc(struct ubik_dbase *dbase, afs_int32 fid, afs_int32 length)
     return 0;
 }
 
-/* allocate a truncation entry.  We allocate special entries representing truncations, rather than
-    performing them immediately, so that we can abort a transaction easily by simply purging
-    the in-core memory buffers and discarding these truncation entries.
-*/
+/*!
+ * \brief Allocate a truncation entry.
+ *
+ * We allocate special entries representing truncations, rather than
+ * performing them immediately, so that we can abort a transaction easily by simply purging
+ * the in-core memory buffers and discarding these truncation entries.
+ */
 static struct ubik_trunc *
 GetTrunc(void)
 {
     struct ubik_trunc *tt;
     if (!freeTruncList) {
-       freeTruncList =
-           (struct ubik_trunc *)malloc(sizeof(struct ubik_trunc));
+       freeTruncList = malloc(sizeof(struct ubik_trunc));
        freeTruncList->next = (struct ubik_trunc *)0;
     }
     tt = freeTruncList;
@@ -387,7 +432,9 @@ GetTrunc(void)
     return tt;
 }
 
-/* free a truncation entry */
+/*!
+ * \brief Free a truncation entry.
+ */
 static int
 PutTrunc(struct ubik_trunc *at)
 {
@@ -396,7 +443,9 @@ PutTrunc(struct ubik_trunc *at)
     return 0;
 }
 
-/* find a truncation entry for a file, if any */
+/*!
+ * \brief Find a truncation entry for a file, if any.
+ */
 static struct ubik_trunc *
 FindTrunc(struct ubik_trans *atrans, afs_int32 afile)
 {
@@ -408,18 +457,20 @@ FindTrunc(struct ubik_trans *atrans, afs_int32 afile)
     return (struct ubik_trunc *)0;
 }
 
-/* do truncates associated with trans, and free them */
+/*!
+ * \brief Do truncates associated with \p atrans, and free them.
+ */
 static int
 DoTruncs(struct ubik_trans *atrans)
 {
     struct ubik_trunc *tt, *nt;
-    int (*tproc) ();
+    int (*tproc) (struct ubik_dbase *, afs_int32, afs_int32);
     afs_int32 rcode = 0, code;
 
     tproc = atrans->dbase->truncate;
     for (tt = atrans->activeTruncs; tt; tt = nt) {
        nt = tt->next;
-       DTrunc(atrans->dbase, tt->file, tt->length);    /* zap pages from buffer cache */
+       DTrunc(atrans, tt->file, tt->length);   /* zap pages from buffer cache */
        code = (*tproc) (atrans->dbase, tt->file, tt->length);
        if (code)
            rcode = code;
@@ -430,7 +481,9 @@ DoTruncs(struct ubik_trans *atrans)
     return (rcode);
 }
 
-/* mark a fid as invalid */
+/*!
+ * \brief Mark an \p fid as invalid.
+ */
 int
 udisk_Invalidate(struct ubik_dbase *adbase, afs_int32 afid)
 {
@@ -446,7 +499,9 @@ udisk_Invalidate(struct ubik_dbase *adbase, afs_int32 afid)
     return 0;
 }
 
-/* move this page into the correct hash bucket */
+/*!
+ * \brief Move this page into the correct hash bucket.
+ */
 static int
 FixupBucket(struct buffer *ap)
 {
@@ -470,7 +525,9 @@ FixupBucket(struct buffer *ap)
     return 0;
 }
 
-/* create a new slot for a particular dbase page */
+/*!
+ * \brief Create a new slot for a particular dbase page.
+ */
 static struct buffer *
 newslot(struct ubik_dbase *adbase, afs_int32 afid, afs_int32 apage)
 {
@@ -503,7 +560,9 @@ newslot(struct ubik_dbase *adbase, afs_int32 afid, afs_int32 apage)
     return pp;
 }
 
-/* Release a buffer, specifying whether or not the buffer has been modified by the locker. */
+/*!
+ * \brief Release a buffer, specifying whether or not the buffer has been modified by the locker.
+ */
 static void
 DRelease(char *ap, int flag)
 {
@@ -520,18 +579,22 @@ DRelease(char *ap, int flag)
     return;
 }
 
-/* flush all modified buffers, leaves dirty bits set (they're cleared
- * by DSync).  Note interaction with DSync: you call this thing first,
- * writing the buffers to the disk.  Then you call DSync to sync all the
+/*!
+ * \brief Flush all modified buffers, leaves dirty bits set (they're cleared
+ * by DSync()).
+ *
+ * \note Note interaction with DSync(): you call this thing first,
+ * writing the buffers to the disk.  Then you call DSync() to sync all the
  * files that were written, and to clear the dirty bits.  You should
  * always call DFlush/DSync as a pair.
  */
 static int
-DFlush(struct ubik_dbase *adbase)
+DFlush(struct ubik_trans *atrans)
 {
     int i;
     afs_int32 code;
     struct buffer *tb;
+    struct ubik_dbase *adbase = atrans->dbase;
 
     tb = Buffers;
     for (i = 0; i < nbuffers; i++, tb++) {
@@ -547,9 +610,11 @@ DFlush(struct ubik_dbase *adbase)
     return 0;
 }
 
-/* flush all modified buffers */
+/*!
+ * \brief Flush all modified buffers.
+ */
 static int
-DAbort(struct ubik_dbase *adbase)
+DAbort(struct ubik_trans *atrans)
 {
     int i;
     struct buffer *tb;
@@ -565,15 +630,39 @@ DAbort(struct ubik_dbase *adbase)
     return 0;
 }
 
-/* must only be called after DFlush, due to its interpretation of dirty flag */
+/**
+ * Invalidate any buffers that are duplicates of abuf. Duplicate buffers
+ * can appear if a read transaction reads a page that is dirty, then that
+ * dirty page is synced. The read transaction will skip over the dirty page,
+ * and create a new buffer, and when the dirty page is synced, it will be
+ * identical (except for contents) to the read-transaction buffer.
+ */
+static void
+DedupBuffer(struct buffer *abuf)
+{
+    struct buffer *tb;
+    for (tb = phTable[pHash(abuf->page)]; tb; tb = tb->hashNext) {
+       if (tb->page == abuf->page && tb != abuf && tb->file == abuf->file
+           && tb->dbase == abuf->dbase) {
+
+           tb->file = BADFID;
+           Dlru(tb);
+       }
+    }
+}
+
+/*!
+ * \attention DSync() must only be called after DFlush(), due to its interpretation of dirty flag.
+ */
 static int
-DSync(struct ubik_dbase *adbase)
+DSync(struct ubik_trans *atrans)
 {
     int i;
     afs_int32 code;
     struct buffer *tb;
     afs_int32 file;
     afs_int32 rCode;
+    struct ubik_dbase *adbase = atrans->dbase;
 
     rCode = 0;
     while (1) {
@@ -582,8 +671,10 @@ DSync(struct ubik_dbase *adbase)
            if (tb->dirty == 1) {
                if (file == BADFID)
                    file = tb->file;
-               if (file != BADFID && tb->file == file)
+               if (file != BADFID && tb->file == file) {
                    tb->dirty = 0;
+                   DedupBuffer(tb);
+               }
            }
        }
        if (file == BADFID)
@@ -596,11 +687,14 @@ DSync(struct ubik_dbase *adbase)
     return rCode;
 }
 
-/* Same as read, only do not even try to read the page */
+/*!
+ * \brief Same as DRead(), only do not even try to read the page.
+ */
 static char *
-DNew(struct ubik_dbase *dbase, afs_int32 fid, int page)
+DNew(struct ubik_trans *atrans, afs_int32 fid, int page)
 {
     struct buffer *tb;
+    struct ubik_dbase *dbase = atrans->dbase;
 
     if ((tb = newslot(dbase, fid, page)) == 0)
        return NULL;
@@ -609,21 +703,21 @@ DNew(struct ubik_dbase *dbase, afs_int32 fid, int page)
     return tb->data;
 }
 
-/* read data from database */
+/*!
+ * \brief Read data from database.
+ */
 int
-udisk_read(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
+udisk_read(struct ubik_trans *atrans, afs_int32 afile, void *abuffer,
           afs_int32 apos, afs_int32 alen)
 {
     char *bp;
     afs_int32 offset, len, totalLen;
-    struct ubik_dbase *dbase;
 
     if (atrans->flags & TRDONE)
        return UDONE;
     totalLen = 0;
-    dbase = atrans->dbase;
     while (alen > 0) {
-       bp = DRead(dbase, afile, apos >> UBIK_LOGPAGESIZE);
+       bp = DRead(atrans, afile, apos >> UBIK_LOGPAGESIZE);
        if (!bp)
            return UEOF;
        /* otherwise, min of remaining bytes and end of buffer to user mode */
@@ -632,7 +726,7 @@ udisk_read(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
        if (len > alen)
            len = alen;
        memcpy(abuffer, bp + offset, len);
-       abuffer += len;
+       abuffer = (char *)abuffer + len;
        apos += len;
        alen -= len;
        totalLen += len;
@@ -641,7 +735,9 @@ udisk_read(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
     return 0;
 }
 
-/* truncate file */
+/*!
+ * \brief Truncate file.
+ */
 int
 udisk_truncate(struct ubik_trans *atrans, afs_int32 afile, afs_int32 alength)
 {
@@ -673,14 +769,15 @@ udisk_truncate(struct ubik_trans *atrans, afs_int32 afile, afs_int32 alength)
     return code;
 }
 
-/* write data to database, using logs */
+/*!
+ * \brief Write data to database, using logs.
+ */
 int
-udisk_write(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
+udisk_write(struct ubik_trans *atrans, afs_int32 afile, void *abuffer,
            afs_int32 apos, afs_int32 alen)
 {
     char *bp;
     afs_int32 offset, len, totalLen;
-    struct ubik_dbase *dbase;
     struct ubik_trunc *tt;
     afs_int32 code;
 
@@ -689,9 +786,8 @@ udisk_write(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
     if (atrans->type != UBIK_WRITETRANS)
        return UBADTYPE;
 
-    dbase = atrans->dbase;
     /* first write the data to the log */
-    code = udisk_LogWriteData(dbase, afile, abuffer, apos, alen);
+    code = udisk_LogWriteData(atrans->dbase, afile, abuffer, apos, alen);
     if (code)
        return code;
 
@@ -706,9 +802,9 @@ udisk_write(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
     /* now update vm */
     totalLen = 0;
     while (alen > 0) {
-       bp = DRead(dbase, afile, apos >> UBIK_LOGPAGESIZE);
+       bp = DRead(atrans, afile, apos >> UBIK_LOGPAGESIZE);
        if (!bp) {
-           bp = DNew(dbase, afile, apos >> UBIK_LOGPAGESIZE);
+           bp = DNew(atrans, afile, apos >> UBIK_LOGPAGESIZE);
            if (!bp)
                return UIOERROR;
            memset(bp, 0, UBIK_PAGESIZE);
@@ -719,7 +815,7 @@ udisk_write(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
        if (len > alen)
            len = alen;
        memcpy(bp + offset, abuffer, len);
-       abuffer += len;
+       abuffer = (char *)abuffer + len;
        apos += len;
        alen -= len;
        totalLen += len;
@@ -728,19 +824,16 @@ udisk_write(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
     return 0;
 }
 
-/* begin a new local transaction */
+/*!
+ * \brief Begin a new local transaction.
+ */
 int
 udisk_begin(struct ubik_dbase *adbase, int atype, struct ubik_trans **atrans)
 {
     afs_int32 code;
     struct ubik_trans *tt;
 
-    *atrans = (struct ubik_trans *)NULL;
-    /* Make sure system is initialized before doing anything */
-    if (!initd) {
-       initd = 1;
-       DInit(ubik_nBuffers);
-    }
+    *atrans = NULL;
     if (atype == UBIK_WRITETRANS) {
        if (adbase->flags & DBWRITING)
            return USYNC;
@@ -748,27 +841,32 @@ udisk_begin(struct ubik_dbase *adbase, int atype, struct ubik_trans **atrans)
        if (code)
            return code;
     }
-    tt = (struct ubik_trans *)malloc(sizeof(struct ubik_trans));
-    memset(tt, 0, sizeof(struct ubik_trans));
+    tt = calloc(1, sizeof(struct ubik_trans));
     tt->dbase = adbase;
     tt->next = adbase->activeTrans;
     adbase->activeTrans = tt;
     tt->type = atype;
     if (atype == UBIK_READTRANS)
        adbase->readers++;
-    else if (atype == UBIK_WRITETRANS)
+    else if (atype == UBIK_WRITETRANS) {
+       UBIK_VERSION_LOCK;
        adbase->flags |= DBWRITING;
+       UBIK_VERSION_UNLOCK;
+    }
     *atrans = tt;
     return 0;
 }
 
-/* commit transaction */
+/*!
+ * \brief Commit transaction.
+ */
 int
 udisk_commit(struct ubik_trans *atrans)
 {
     struct ubik_dbase *dbase;
     afs_int32 code = 0;
     struct ubik_version oldversion, newversion;
+    afs_int32 now = FT_ApproxTime();
 
     if (atrans->flags & TRDONE)
        return (UTWOENDS);
@@ -778,44 +876,59 @@ udisk_commit(struct ubik_trans *atrans)
 
        /* On the first write to the database. We update the versions */
        if (ubeacon_AmSyncSite() && !(urecovery_state & UBIK_RECLABELDB)) {
+           UBIK_VERSION_LOCK;
+           if (version_globals.ubik_epochTime < UBIK_MILESTONE
+               || version_globals.ubik_epochTime > now) {
+               ubik_print
+                   ("Ubik: New database label %d is out of the valid range (%d - %d)\n",
+                    version_globals.ubik_epochTime, UBIK_MILESTONE, now);
+               panic("Writing Ubik DB label\n");
+           }
            oldversion = dbase->version;
-           newversion.epoch = FT_ApproxTime();;
+           newversion.epoch = version_globals.ubik_epochTime;
            newversion.counter = 1;
 
            code = (*dbase->setlabel) (dbase, 0, &newversion);
-           if (code)
-               return (code);
-           ubik_epochTime = newversion.epoch;
+           if (code) {
+               UBIK_VERSION_UNLOCK;
+               return code;
+           }
+
            dbase->version = newversion;
+           UBIK_VERSION_UNLOCK;
+
+           urecovery_state |= UBIK_RECLABELDB;
 
            /* Ignore the error here. If the call fails, the site is
-            * marked down and when we detect it is up again, we will 
+            * marked down and when we detect it is up again, we will
             * send the entire database to it.
             */
-           ContactQuorum(DISK_SetVersion, atrans, 1 /*CStampVersion */ ,
-                         &oldversion, &newversion);
-           urecovery_state |= UBIK_RECLABELDB;
+           ContactQuorum_DISK_SetVersion( atrans, 1 /*CStampVersion */ ,
+                                          &oldversion, &newversion);
        }
 
+       UBIK_VERSION_LOCK;
        dbase->version.counter++;       /* bump commit count */
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
-       assert(pthread_cond_broadcast(&dbase->version_cond) == 0);
+#ifdef AFS_PTHREAD_ENV
+       opr_cv_broadcast(&dbase->version_cond);
 #else
        LWP_NoYieldSignal(&dbase->version);
 #endif
        code = udisk_LogEnd(dbase, &dbase->version);
        if (code) {
            dbase->version.counter--;
-           return (code);
+           UBIK_VERSION_UNLOCK;
+           return code;
        }
+       UBIK_VERSION_UNLOCK;
 
        /* If we fail anytime after this, then panic and let the
-        * recovery replay the log. 
+        * recovery replay the log.
         */
-       code = DFlush(dbase);   /* write dirty pages to respective files */
+       code = DFlush(atrans);  /* write dirty pages to respective files */
        if (code)
            panic("Writing Ubik DB modifications\n");
-       code = DSync(dbase);    /* sync the files and mark pages not dirty */
+       code = DSync(atrans);   /* sync the files and mark pages not dirty */
        if (code)
            panic("Synchronizing Ubik DB modifications\n");
 
@@ -841,7 +954,9 @@ udisk_commit(struct ubik_trans *atrans)
     return code;
 }
 
-/* abort transaction */
+/*!
+ * \brief Abort transaction.
+ */
 int
 udisk_abort(struct ubik_trans *atrans)
 {
@@ -852,9 +967,9 @@ udisk_abort(struct ubik_trans *atrans)
        return UTWOENDS;
 
     /* Check if we are the write trans before logging abort, lest we
-     * abort a good write trans in progress. 
-     * We don't really care if the LOGABORT gets to the log because we 
-     * truncate the log next. If the truncate fails, we panic; for 
+     * abort a good write trans in progress.
+     * We don't really care if the LOGABORT gets to the log because we
+     * truncate the log next. If the truncate fails, we panic; for
      * otherwise, the log entries remain. On restart, replay of the log
      * will do nothing because the abort is there or no LogEnd opcode.
      */
@@ -864,7 +979,7 @@ udisk_abort(struct ubik_trans *atrans)
        code = (*dbase->truncate) (dbase, LOGFILE, 0);
        if (code)
            panic("Truncating Ubik logfile during an abort\n");
-       DAbort(dbase);          /* remove all dirty pages */
+       DAbort(atrans);         /* remove all dirty pages */
     }
 
     /* When the transaction is marked done, it also means the logfile
@@ -874,8 +989,10 @@ udisk_abort(struct ubik_trans *atrans)
     return 0;
 }
 
-/* destroy a transaction after it has been committed or aborted.  if
- * it hasn't committed before you call this routine, we'll abort the
+/*!
+ * \brief Destroy a transaction after it has been committed or aborted.
+ *
+ * If it hasn't committed before you call this routine, we'll abort the
  * transaction for you.
  */
 int
@@ -883,19 +1000,6 @@ udisk_end(struct ubik_trans *atrans)
 {
     struct ubik_dbase *dbase;
 
-#if defined(UBIK_PAUSE)
-    /* Another thread is trying to lock this transaction.
-     * That can only be an RPC doing SDISK_Lock.
-     * Unlock the transaction, 'cause otherwise the other
-     * thread will never wake up.  Don't free it because
-     * the caller will do that already.
-     */
-    if (atrans->flags & TRSETLOCK) {
-       atrans->flags |= TRSTALE;
-       ulock_relLock(atrans);
-       return;
-    }
-#endif /* UBIK_PAUSE */
     if (!(atrans->flags & TRDONE))
        udisk_abort(atrans);
     dbase = atrans->dbase;
@@ -907,7 +1011,9 @@ udisk_end(struct ubik_trans *atrans)
      * we could be unsetting someone else's bit.
      */
     if (atrans->type == UBIK_WRITETRANS && dbase->flags & DBWRITING) {
+       UBIK_VERSION_LOCK;
        dbase->flags &= ~DBWRITING;
+       UBIK_VERSION_UNLOCK;
     } else {
        dbase->readers--;
     }
@@ -918,8 +1024,8 @@ udisk_end(struct ubik_trans *atrans)
     free(atrans);
 
     /* Wakeup any writers waiting in BeginTrans() */
-#if defined(AFS_PTHREAD_ENV) && defined(UBIK_PTHREAD_ENV)
-       assert(pthread_cond_broadcast(&dbase->flags_cond) == 0);
+#ifdef AFS_PTHREAD_ENV
+    opr_cv_broadcast(&dbase->flags_cond);
 #else
     LWP_NoYieldSignal(&dbase->flags);
 #endif