DAFS: large volume support - fileserver crash after "addled bitmap"
[openafs.git] / src / vol / volume.c
index c85b685..88e355f 100644 (file)
 #include <afs/param.h>
 
 #include <roken.h>
+#include <afs/opr.h>
 
-#include <rx/xdr.h>
-#include <afs/afsint.h>
 #include <ctype.h>
-#include <signal.h>
+#include <stddef.h>
+
+#ifdef HAVE_SYS_FILE_H
+#include <sys/file.h>
+#endif
+
+#ifdef AFS_PTHREAD_ENV
+# include <opr/lock.h>
+#else
+# include <opr/lockstub.h>
+#endif
+#include <opr/ffs.h>
+#include <opr/jhash.h>
+
+#include <afs/afsint.h>
+
+#include <rx/rx_queue.h>
+
 #ifndef AFS_NT40_ENV
-#include <sys/param.h>
 #if !defined(AFS_SGI_ENV)
 #ifdef AFS_OSF_ENV
 #include <ufs/fs.h>
 #endif
 #endif
 #else /* AFS_VFSINCL_ENV */
-#if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV)
+#if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_XBSD_ENV) && !defined(AFS_DARWIN_ENV)
 #include <sys/fs.h>
 #endif
 #endif /* AFS_VFSINCL_ENV */
 #endif /* AFS_OSF_ENV */
 #endif /* AFS_SGI_ENV */
-#endif /* AFS_NT40_ENV */
-#include <errno.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#ifdef AFS_NT40_ENV
-#include <fcntl.h>
-#else
-#include <sys/file.h>
-#endif
-#include <dirent.h>
+#endif /* !AFS_NT40_ENV */
+
 #ifdef AFS_AIX_ENV
 #include <sys/vfs.h>
-#include <fcntl.h>
 #else
 #ifdef AFS_HPUX_ENV
-#include <fcntl.h>
 #include <mntent.h>
 #else
 #if    defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
@@ -80,9 +85,7 @@
 #else
 #ifndef AFS_NT40_ENV
 #if defined(AFS_SGI_ENV)
-#include <fcntl.h>
 #include <mntent.h>
-
 #else
 #ifndef AFS_LINUX20_ENV
 #include <fstab.h>             /* Need to find in libc 5, present in libc 6 */
 #endif
 #endif /* AFS_HPUX_ENV */
 #endif
-#ifndef AFS_NT40_ENV
-#include <netdb.h>
-#include <netinet/in.h>
-#include <sys/wait.h>
-#include <setjmp.h>
-#ifndef ITIMER_REAL
-#include <sys/time.h>
-#endif /* ITIMER_REAL */
-#endif /* AFS_NT40_ENV */
-#if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
-#include <string.h>
-#else
-#include <strings.h>
-#endif
 
 #include "nfs.h"
 #include <afs/errors.h>
 #include <afs/afssyscalls.h>
 #include "ihandle.h"
 #include <afs/afsutil.h>
-#ifdef AFS_NT40_ENV
-#include <io.h>
-#endif
 #include "daemon_com.h"
 #include "fssync.h"
 #include "salvsync.h"
 #include "partition.h"
 #include "volume_inline.h"
 #include "common.h"
-#include "afs/afs_assert.h"
 #include "vutils.h"
-#ifndef AFS_NT40_ENV
 #include <afs/dir.h>
-#include <unistd.h>
-#endif
-
-#if !defined(offsetof)
-#include <stddef.h>
-#endif
-
-#ifdef O_LARGEFILE
-#define afs_stat       stat64
-#define afs_fstat      fstat64
-#define afs_open       open64
-#else /* !O_LARGEFILE */
-#define afs_stat       stat
-#define afs_fstat      fstat
-#define afs_open       open
-#endif /* !O_LARGEFILE */
 
 #ifdef AFS_PTHREAD_ENV
 pthread_mutex_t vol_glock_mutex;
@@ -179,7 +147,7 @@ extern void *calloc(), *realloc();
 #endif
 
 /* Forward declarations */
-static Volume *attach2(Error * ec, VolId volumeId, char *path,
+static Volume *attach2(Error * ec, VolumeId volumeId, char *path,
                       struct DiskPartition64 *partp, Volume * vp,
                       int isbusy, int mode, int *acheckedOut);
 static void ReallyFreeVolume(Volume * vp);
@@ -193,7 +161,7 @@ static void VInitVolumeHeaderCache(afs_uint32 howMany);
 static int GetVolumeHeader(Volume * vp);
 static void ReleaseVolumeHeader(struct volHeader *hd);
 static void FreeVolumeHeader(Volume * vp);
-static void AddVolumeToHashTable(Volume * vp, int hashid);
+static void AddVolumeToHashTable(Volume * vp, VolumeId hashid);
 static void DeleteVolumeFromHashTable(Volume * vp);
 #if 0
 static int VHold(Volume * vp);
@@ -205,7 +173,7 @@ static void VCloseVolumeHandles_r(Volume * vp);
 static void LoadVolumeHeader(Error * ec, Volume * vp);
 static int VCheckOffline(Volume * vp);
 static int VCheckDetach(Volume * vp);
-static Volume * GetVolume(Error * ec, Error * client_ec, VolId volumeId,
+static Volume * GetVolume(Error * ec, Error * client_ec, VolumeId volumeId,
                           Volume * hint, const struct timespec *ts);
 
 int LogLevel;                  /* Vice loglevel--not defined as extern so that it will be
@@ -221,9 +189,6 @@ pthread_t vol_glock_holder = 0;
 #endif
 
 
-#define VOLUME_BITMAP_GROWSIZE 16      /* bytes, => 128vnodes */
-                                       /* Must be a multiple of 4 (1 word) !! */
-
 /* this parameter needs to be tunable at runtime.
  * 128 was really inadequate for largish servers -- at 16384 volumes this
  * puts average chain length at 128, thus an average 65 deref's to find a volptr.
@@ -232,9 +197,11 @@ pthread_t vol_glock_holder = 0;
  * an AVL or splay tree might work a lot better, but we'll just increase
  * the default hash table size for now
  */
-#define DEFAULT_VOLUME_HASH_SIZE 256   /* Must be a power of 2!! */
-#define DEFAULT_VOLUME_HASH_MASK (DEFAULT_VOLUME_HASH_SIZE-1)
-#define VOLUME_HASH(volumeId) (volumeId&(VolumeHashTable.Mask))
+#define DEFAULT_VOLUME_HASH_BITS 10
+#define DEFAULT_VOLUME_HASH_SIZE opr_jhash_size(DEFAULT_VOLUME_HASH_BITS)
+#define DEFAULT_VOLUME_HASH_MASK opr_jhash_mask(DEFAULT_VOLUME_HASH_BITS)
+#define VOLUME_HASH(volumeId) \
+    (opr_jhash_int(volumeId, 0) & VolumeHashTable.Mask)
 
 /*
  * turn volume hash chains into partially ordered lists.
@@ -255,6 +222,12 @@ pthread_t vol_glock_holder = 0;
  */
 #define VOLUME_HASH_REORDER_CHAIN_THRESH (VOLUME_HASH_REORDER_THRESHOLD / 2)
 
+/*
+ * The per volume uniquifier is bumped by 200 and and written to disk
+ * every 200 file creates.
+ */
+#define VOLUME_UPDATE_UNIQUIFIER_BUMP 200
+
 #include "rx/rx_queue.h"
 
 
@@ -268,24 +241,6 @@ VolumeHashTable_t VolumeHashTable = {
 static void VInitVolumeHash(void);
 
 
-#ifndef AFS_HAVE_FFS
-/* This macro is used where an ffs() call does not exist. Was in util/ffs.c */
-ffs(x)
-{
-    afs_int32 ffs_i;
-    afs_int32 ffs_tmp = x;
-    if (ffs_tmp == 0)
-       return (-1);
-    else
-       for (ffs_i = 1;; ffs_i++) {
-           if (ffs_tmp & 1)
-               return (ffs_i);
-           else
-               ffs_tmp >>= 1;
-       }
-}
-#endif /* !AFS_HAVE_FFS */
-
 #ifdef AFS_PTHREAD_ENV
 /**
  * disk partition queue element
@@ -348,7 +303,7 @@ struct vinitvolumepackage_thread_param {
 
 static void *VInitVolumePackageThread(void *args);
 static struct DiskPartition64 *VInitNextPartition(struct partition_queue *pq);
-static VolId VInitNextVolumeId(DIR *dirp);
+static VolumeId VInitNextVolumeId(DIR *dirp);
 static int VInitPreAttachVolumes(int nthreads, struct volume_init_queue *vq);
 
 #endif /* !AFS_DEMAND_ATTACH_FS */
@@ -376,7 +331,7 @@ struct VLRU_DiskHeader {
 };
 
 struct VLRU_DiskEntry {
-    afs_uint32 vid;                       /* volume ID */
+    VolumeId vid;                       /* volume ID */
     afs_uint32 idx;                       /* generation */
     afs_uint32 last_get;                  /* timestamp of last get */
 };
@@ -418,6 +373,13 @@ static void VVByPListEndExclusive_r(struct DiskPartition64 * dp);
 static void VVByPListWait_r(struct DiskPartition64 * dp);
 
 /* online salvager */
+typedef enum {
+    VCHECK_SALVAGE_OK = 0,         /**< no pending salvage */
+    VCHECK_SALVAGE_SCHEDULED = 1,  /**< salvage has been scheduled */
+    VCHECK_SALVAGE_ASYNC = 2,      /**< salvage being scheduled */
+    VCHECK_SALVAGE_DENIED = 3,     /**< salvage not scheduled; denied */
+    VCHECK_SALVAGE_FAIL = 4        /**< salvage not scheduled; failed */
+} vsalvage_check;
 static int VCheckSalvage(Volume * vp);
 #if defined(SALVSYNC_BUILD_CLIENT) || defined(FSSYNC_BUILD_CLIENT)
 static int VScheduleSalvage_r(Volume * vp);
@@ -520,6 +482,8 @@ VOptDefaults(ProgramType pt, VolumePackageOptions *opts)
     opts->interrupt_rxcall = NULL;
     opts->offline_timeout = -1;
     opts->offline_shutdown_timeout = -1;
+    opts->usage_threshold = 128;
+    opts->usage_rate_limit = 5;
 
 #ifdef FAST_RESTART
     opts->unsafe_attach = 1;
@@ -562,7 +526,7 @@ static void
 VSetVInit_r(int value)
 {
     VInit = value;
-    CV_BROADCAST(&vol_vinit_cond);
+    opr_cv_broadcast(&vol_vinit_cond);
 }
 
 static_inline void
@@ -611,15 +575,15 @@ VInitVolumePackage2(ProgramType pt, VolumePackageOptions * opts)
     } else {
        VLRU_SetOptions(VLRU_SET_ENABLED, 0);
     }
-    osi_Assert(pthread_key_create(&VThread_key, NULL) == 0);
+    opr_Verify(pthread_key_create(&VThread_key, NULL) == 0);
 #endif
 
-    MUTEX_INIT(&vol_glock_mutex, "vol glock", MUTEX_DEFAULT, 0);
-    MUTEX_INIT(&vol_trans_mutex, "vol trans", MUTEX_DEFAULT, 0);
-    CV_INIT(&vol_put_volume_cond, "vol put", CV_DEFAULT, 0);
-    CV_INIT(&vol_sleep_cond, "vol sleep", CV_DEFAULT, 0);
-    CV_INIT(&vol_init_attach_cond, "vol init attach", CV_DEFAULT, 0);
-    CV_INIT(&vol_vinit_cond, "vol init", CV_DEFAULT, 0);
+    opr_mutex_init(&vol_glock_mutex);
+    opr_mutex_init(&vol_trans_mutex);
+    opr_cv_init(&vol_put_volume_cond);
+    opr_cv_init(&vol_sleep_cond);
+    opr_cv_init(&vol_init_attach_cond);
+    opr_cv_init(&vol_vinit_cond);
 #ifndef AFS_PTHREAD_ENV
     IOMGR_Initialize();
 #endif /* AFS_PTHREAD_ENV */
@@ -628,7 +592,7 @@ VInitVolumePackage2(ProgramType pt, VolumePackageOptions * opts)
     srandom(time(0));          /* For VGetVolumeInfo */
 
 #ifdef AFS_DEMAND_ATTACH_FS
-    MUTEX_INIT(&vol_salvsync_mutex, "salvsync", MUTEX_DEFAULT, 0);
+    opr_mutex_init(&vol_salvsync_mutex);
 #endif /* AFS_DEMAND_ATTACH_FS */
 
     /* Ok, we have done enough initialization that fileserver can
@@ -650,7 +614,7 @@ VInitVolumePackage2(ProgramType pt, VolumePackageOptions * opts)
 #if defined(AFS_DEMAND_ATTACH_FS) && defined(SALVSYNC_BUILD_CLIENT)
     if (VCanUseSALVSYNC()) {
        /* establish a connection to the salvager at this point */
-       osi_Assert(VConnectSALV() != 0);
+       opr_Verify(VConnectSALV() != 0);
     }
 #endif /* AFS_DEMAND_ATTACH_FS */
 
@@ -704,13 +668,15 @@ VInitVolumePackage2(ProgramType pt, VolumePackageOptions * opts)
 int
 VInitAttachVolumes(ProgramType pt)
 {
-    osi_Assert(VInit==1);
+    opr_Assert(VInit==1);
     if (pt == fileServer) {
        struct DiskPartition64 *diskP;
        /* Attach all the volumes in this partition */
        for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
            int nAttached = 0, nUnattached = 0;
-           osi_Assert(VAttachVolumesByPartition(diskP, &nAttached, &nUnattached) == 0);
+           opr_Verify(VAttachVolumesByPartition(diskP,
+                                                &nAttached, &nUnattached)
+                           == 0);
        }
     }
     VOL_LOCK;
@@ -735,7 +701,7 @@ VInitAttachVolumes(ProgramType pt)
 int
 VInitAttachVolumes(ProgramType pt)
 {
-    osi_Assert(VInit==1);
+    opr_Assert(VInit==1);
     if (pt == fileServer) {
        struct DiskPartition64 *diskP;
        struct vinitvolumepackage_thread_t params;
@@ -744,24 +710,26 @@ VInitAttachVolumes(ProgramType pt)
        pthread_t tid;
        pthread_attr_t attrs;
 
-       CV_INIT(&params.thread_done_cv, "thread done", CV_DEFAULT, 0);
+       opr_cv_init(&params.thread_done_cv);
        queue_Init(&params);
        params.n_threads_complete = 0;
 
        /* create partition work queue */
        for (parts=0, diskP = DiskPartitionList; diskP; diskP = diskP->next, parts++) {
-           dpq = (diskpartition_queue_t *) malloc(sizeof(struct diskpartition_queue_t));
-           osi_Assert(dpq != NULL);
+           dpq = malloc(sizeof(struct diskpartition_queue_t));
+           opr_Assert(dpq != NULL);
            dpq->diskP = diskP;
            queue_Append(&params,dpq);
        }
 
-       threads = MIN(parts, vol_attach_threads);
+       threads = min(parts, vol_attach_threads);
 
        if (threads > 1) {
            /* spawn off a bunch of initialization threads */
-           osi_Assert(pthread_attr_init(&attrs) == 0);
-           osi_Assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
+           opr_Verify(pthread_attr_init(&attrs) == 0);
+           opr_Verify(pthread_attr_setdetachstate(&attrs,
+                                                  PTHREAD_CREATE_DETACHED)
+                           == 0);
 
            Log("VInitVolumePackage: beginning parallel fileserver startup\n");
            Log("VInitVolumePackage: using %d threads to attach volumes on %d partitions\n",
@@ -771,9 +739,9 @@ VInitAttachVolumes(ProgramType pt)
            for (i=0; i < threads; i++) {
                 AFS_SIGSET_DECL;
                 AFS_SIGSET_CLEAR();
-               osi_Assert(pthread_create
-                      (&tid, &attrs, &VInitVolumePackageThread,
-                       &params) == 0);
+               opr_Verify(pthread_create(&tid, &attrs,
+                                         &VInitVolumePackageThread,
+                                         &params) == 0);
                 AFS_SIGSET_RESTORE();
            }
 
@@ -782,7 +750,7 @@ VInitAttachVolumes(ProgramType pt)
            }
            VOL_UNLOCK;
 
-           osi_Assert(pthread_attr_destroy(&attrs) == 0);
+           opr_Verify(pthread_attr_destroy(&attrs) == 0);
        } else {
            /* if we're only going to run one init thread, don't bother creating
             * another LWP */
@@ -793,11 +761,11 @@ VInitAttachVolumes(ProgramType pt)
            VInitVolumePackageThread(&params);
        }
 
-       CV_DESTROY(&params.thread_done_cv);
+       opr_cv_destroy(&params.thread_done_cv);
     }
     VOL_LOCK;
     VSetVInit_r(2);                    /* Initialized, and all volumes have been attached */
-    CV_BROADCAST(&vol_init_attach_cond);
+    opr_cv_broadcast(&vol_init_attach_cond);
     VOL_UNLOCK;
     return 0;
 }
@@ -828,14 +796,15 @@ VInitVolumePackageThread(void * args) {
        diskP = dpq->diskP;
        free(dpq);
 
-       osi_Assert(VAttachVolumesByPartition(diskP, &nAttached, &nUnattached) == 0);
+       opr_Verify(VAttachVolumesByPartition(diskP, &nAttached,
+                                            &nUnattached) == 0);
 
        VOL_LOCK;
     }
 
 done:
     params->n_threads_complete++;
-    CV_SIGNAL(&params->thread_done_cv);
+    opr_cv_signal(&params->thread_done_cv);
     VOL_UNLOCK;
     return NULL;
 }
@@ -855,7 +824,7 @@ done:
 int
 VInitAttachVolumes(ProgramType pt)
 {
-    osi_Assert(VInit==1);
+    opr_Assert(VInit==1);
     if (pt == fileServer) {
 
        struct DiskPartition64 *diskP;
@@ -868,26 +837,27 @@ VInitAttachVolumes(ProgramType pt)
 
        /* create partition work queue */
         queue_Init(&pq);
-       CV_INIT(&(pq.cv), "partq", CV_DEFAULT, 0);
-       MUTEX_INIT(&(pq.mutex), "partq", MUTEX_DEFAULT, 0);
+       opr_cv_init(&pq.cv);
+       opr_mutex_init(&pq.mutex);
        for (parts = 0, diskP = DiskPartitionList; diskP; diskP = diskP->next, parts++) {
            struct diskpartition_queue_t *dp;
-           dp = (struct diskpartition_queue_t*)malloc(sizeof(struct diskpartition_queue_t));
-           osi_Assert(dp != NULL);
+           dp = malloc(sizeof(struct diskpartition_queue_t));
+           opr_Assert(dp != NULL);
            dp->diskP = diskP;
            queue_Append(&pq, dp);
        }
 
         /* number of worker threads; at least one, not to exceed the number of partitions */
-       threads = MIN(parts, vol_attach_threads);
+       threads = min(parts, vol_attach_threads);
 
         /* create volume work queue */
         queue_Init(&vq);
-       CV_INIT(&(vq.cv), "volq", CV_DEFAULT, 0);
-       MUTEX_INIT(&(vq.mutex), "volq", MUTEX_DEFAULT, 0);
+       opr_cv_init(&vq.cv);
+       opr_mutex_init(&vq.mutex);
 
-        osi_Assert(pthread_attr_init(&attrs) == 0);
-        osi_Assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
+        opr_Verify(pthread_attr_init(&attrs) == 0);
+        opr_Verify(pthread_attr_setdetachstate(&attrs,
+                                              PTHREAD_CREATE_DETACHED) == 0);
 
         Log("VInitVolumePackage: beginning parallel fileserver startup\n");
         Log("VInitVolumePackage: using %d threads to pre-attach volumes on %d partitions\n",
@@ -898,30 +868,32 @@ VInitAttachVolumes(ProgramType pt)
            struct vinitvolumepackage_thread_param *params;
             AFS_SIGSET_DECL;
 
-            params = (struct vinitvolumepackage_thread_param *)malloc(sizeof(struct vinitvolumepackage_thread_param));
-            osi_Assert(params);
+            params = malloc(sizeof(struct vinitvolumepackage_thread_param));
+            opr_Assert(params);
             params->pq = &pq;
             params->vq = &vq;
             params->nthreads = threads;
             params->thread = i+1;
 
             AFS_SIGSET_CLEAR();
-           osi_Assert(pthread_create (&tid, &attrs, &VInitVolumePackageThread, (void*)params) == 0);
+           opr_Verify(pthread_create(&tid, &attrs,
+                                     &VInitVolumePackageThread,
+                                     (void*)params) == 0);
             AFS_SIGSET_RESTORE();
        }
 
         VInitPreAttachVolumes(threads, &vq);
 
-        osi_Assert(pthread_attr_destroy(&attrs) == 0);
-       CV_DESTROY(&pq.cv);
-       MUTEX_DESTROY(&pq.mutex);
-       CV_DESTROY(&vq.cv);
-       MUTEX_DESTROY(&vq.mutex);
+        opr_Verify(pthread_attr_destroy(&attrs) == 0);
+       opr_cv_destroy(&pq.cv);
+       opr_mutex_destroy(&pq.mutex);
+       opr_cv_destroy(&vq.cv);
+       opr_mutex_destroy(&vq.mutex);
     }
 
     VOL_LOCK;
     VSetVInit_r(2);                    /* Initialized, and all volumes have been attached */
-    CV_BROADCAST(&vol_init_attach_cond);
+    opr_cv_broadcast(&vol_init_attach_cond);
     VOL_UNLOCK;
 
     return 0;
@@ -942,15 +914,15 @@ VInitVolumePackageThread(void *args)
     struct volume_init_queue *vq;
     struct volume_init_batch *vb;
 
-    osi_Assert(args);
+    opr_Assert(args);
     params = (struct vinitvolumepackage_thread_param *)args;
     pq = params->pq;
     vq = params->vq;
-    osi_Assert(pq);
-    osi_Assert(vq);
+    opr_Assert(pq);
+    opr_Assert(vq);
 
-    vb = (struct volume_init_batch*)malloc(sizeof(struct volume_init_batch));
-    osi_Assert(vb);
+    vb = malloc(sizeof(struct volume_init_batch));
+    opr_Assert(vb);
     vb->thread = params->thread;
     vb->last = 0;
     vb->size = 0;
@@ -958,7 +930,7 @@ VInitVolumePackageThread(void *args)
     Log("Scanning partitions on thread %d of %d\n", params->thread, params->nthreads);
     while((partition = VInitNextPartition(pq))) {
         DIR *dirp;
-        VolId vid;
+        VolumeId vid;
 
         Log("Partition %s: pre-attaching volumes\n", partition->name);
         dirp = opendir(VPartitionPath(partition));
@@ -967,25 +939,24 @@ VInitVolumePackageThread(void *args)
             continue;
         }
         while ((vid = VInitNextVolumeId(dirp))) {
-            Volume *vp = (Volume*)malloc(sizeof(Volume));
-            osi_Assert(vp);
-            memset(vp, 0, sizeof(Volume));
+            Volume *vp = calloc(1, sizeof(Volume));
+            opr_Assert(vp);
             vp->device = partition->device;
             vp->partition = partition;
             vp->hashid = vid;
             queue_Init(&vp->vnode_list);
             queue_Init(&vp->rx_call_list);
-           CV_INIT(&V_attachCV(vp), "partattach", CV_DEFAULT, 0);
+           opr_cv_init(&V_attachCV(vp));
 
             vb->batch[vb->size++] = vp;
             if (vb->size == VINIT_BATCH_MAX_SIZE) {
-               MUTEX_ENTER(&vq->mutex);
+               opr_mutex_enter(&vq->mutex);
                 queue_Append(vq, vb);
-               CV_BROADCAST(&vq->cv);
-               MUTEX_EXIT(&vq->mutex);
+               opr_cv_broadcast(&vq->cv);
+               opr_mutex_exit(&vq->mutex);
 
-                vb = (struct volume_init_batch*)malloc(sizeof(struct volume_init_batch));
-                osi_Assert(vb);
+                vb = malloc(sizeof(struct volume_init_batch));
+                opr_Assert(vb);
                 vb->thread = params->thread;
                 vb->size = 0;
                 vb->last = 0;
@@ -995,10 +966,10 @@ VInitVolumePackageThread(void *args)
     }
 
     vb->last = 1;
-    MUTEX_ENTER(&vq->mutex);
+    opr_mutex_enter(&vq->mutex);
     queue_Append(vq, vb);
-    CV_BROADCAST(&vq->cv);
-    MUTEX_EXIT(&vq->mutex);
+    opr_cv_broadcast(&vq->cv);
+    opr_mutex_exit(&vq->mutex);
 
     Log("Partition scan thread %d of %d ended\n", params->thread, params->nthreads);
     free(params);
@@ -1020,17 +991,17 @@ VInitNextPartition(struct partition_queue *pq)
     }
 
     /* get next partition to scan */
-    MUTEX_ENTER(&pq->mutex);
+    opr_mutex_enter(&pq->mutex);
     if (queue_IsEmpty(pq)) {
-       MUTEX_EXIT(&pq->mutex);
+       opr_mutex_exit(&pq->mutex);
         return NULL;
     }
     dp = queue_First(pq, diskpartition_queue_t);
     queue_Remove(dp);
-    MUTEX_EXIT(&pq->mutex);
+    opr_mutex_exit(&pq->mutex);
 
-    osi_Assert(dp);
-    osi_Assert(dp->diskP);
+    opr_Assert(dp);
+    opr_Assert(dp->diskP);
 
     partition = dp->diskP;
     free(dp);
@@ -1040,11 +1011,11 @@ VInitNextPartition(struct partition_queue *pq)
 /**
  * Find next volume id on the partition.
  */
-static VolId
+static VolumeId
 VInitNextVolumeId(DIR *dirp)
 {
     struct dirent *d;
-    VolId vid = 0;
+    VolumeId vid = 0;
     char *ext;
 
     while((d = readdir(dirp))) {
@@ -1075,13 +1046,13 @@ VInitPreAttachVolumes(int nthreads, struct volume_init_queue *vq)
 
     while (nthreads) {
         /* dequeue next volume */
-       MUTEX_ENTER(&vq->mutex);
+       opr_mutex_enter(&vq->mutex);
         if (queue_IsEmpty(vq)) {
-           CV_WAIT(&vq->cv, &vq->mutex);
+           opr_cv_wait(&vq->cv, &vq->mutex);
         }
         vb = queue_First(vq, volume_init_batch);
         queue_Remove(vb);
-       MUTEX_EXIT(&vq->mutex);
+       opr_mutex_exit(&vq->mutex);
 
         if (vb->size) {
             VOL_LOCK;
@@ -1096,7 +1067,7 @@ VInitPreAttachVolumes(int nthreads, struct volume_init_queue *vq)
                     Log("Error looking up volume, code=%d\n", ec);
                 }
                 else if (dup) {
-                    Log("Warning: Duplicate volume id %d detected.\n", vp->hashid);
+                    Log("Warning: Duplicate volume id %" AFS_VOLID_FMT " detected.\n", afs_printable_VolumeId_lu(vp->hashid));
                 }
                 else {
                     /* put pre-attached volume onto the hash table
@@ -1260,11 +1231,12 @@ VShutdown_r(void)
     if (vol_attach_threads > 1) {
        /* prepare for parallel shutdown */
        params.n_threads = vol_attach_threads;
-       MUTEX_INIT(&params.lock, "params", MUTEX_DEFAULT, 0);
-       CV_INIT(&params.cv, "params", CV_DEFAULT, 0);
-       CV_INIT(&params.master_cv, "params master", CV_DEFAULT, 0);
-       osi_Assert(pthread_attr_init(&attrs) == 0);
-       osi_Assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
+       opr_mutex_init(&params.lock);
+       opr_cv_init(&params.cv);
+       opr_cv_init(&params.master_cv);
+       opr_Verify(pthread_attr_init(&attrs) == 0);
+       opr_Verify(pthread_attr_setdetachstate(&attrs,
+                                              PTHREAD_CREATE_DETACHED) == 0);
        queue_Init(&params);
 
        /* setup the basic partition information structures for
@@ -1289,8 +1261,8 @@ VShutdown_r(void)
 
 
            /* build up the pass 0 shutdown work queue */
-           dpq = (struct diskpartition_queue_t *) malloc(sizeof(struct diskpartition_queue_t));
-           osi_Assert(dpq != NULL);
+           dpq = malloc(sizeof(struct diskpartition_queue_t));
+           opr_Assert(dpq != NULL);
            dpq->diskP = diskP;
            queue_Prepend(&params, dpq);
 
@@ -1302,11 +1274,10 @@ VShutdown_r(void)
            vol_attach_threads, params.n_parts, params.n_parts > 1 ? "s" : "" );
 
        /* do pass 0 shutdown */
-       MUTEX_ENTER(&params.lock);
+       opr_mutex_enter(&params.lock);
        for (i=0; i < params.n_threads; i++) {
-           osi_Assert(pthread_create
-                  (&tid, &attrs, &VShutdownThread,
-                   &params) == 0);
+           opr_Verify(pthread_create(&tid, &attrs, &VShutdownThread,
+                                     &params) == 0);
        }
 
        /* wait for all the pass 0 shutdowns to complete */
@@ -1315,8 +1286,8 @@ VShutdown_r(void)
        }
        params.n_threads_complete = 0;
        params.pass = 1;
-       CV_BROADCAST(&params.cv);
-       MUTEX_EXIT(&params.lock);
+       opr_cv_broadcast(&params.cv);
+       opr_mutex_exit(&params.lock);
 
        Log("VShutdown:  pass 0 completed using the 1 thread per partition algorithm\n");
        Log("VShutdown:  starting passes 1 through 3 using finely-granular mp-fast algorithm\n");
@@ -1329,10 +1300,10 @@ VShutdown_r(void)
            VOL_CV_WAIT(&params.cv);
        }
 
-       osi_Assert(pthread_attr_destroy(&attrs) == 0);
-       CV_DESTROY(&params.cv);
-       CV_DESTROY(&params.master_cv);
-       MUTEX_DESTROY(&params.lock);
+       opr_Verify(pthread_attr_destroy(&attrs) == 0);
+       opr_cv_destroy(&params.cv);
+       opr_cv_destroy(&params.master_cv);
+       opr_mutex_destroy(&params.lock);
 
        /* drop the VByPList exclusive reservations */
        for (diskP = DiskPartitionList; diskP; diskP = diskP->next) {
@@ -1386,8 +1357,8 @@ VShutdown_r(void)
            code = VHold_r(vp);
            if (code == 0) {
                if (LogLevel >= 5)
-                   Log("VShutdown:  Attempting to take volume %u offline.\n",
-                       vp->hashid);
+                   Log("VShutdown:  Attempting to take volume %" AFS_VOLID_FMT " offline.\n",
+                       afs_printable_VolumeId_lu(vp->hashid));
 
                /* next, take the volume offline (drops reference count) */
                VOffline_r(vp, "File server was shut down");
@@ -1402,7 +1373,7 @@ VShutdown_r(void)
 void
 VShutdown(void)
 {
-    osi_Assert(VInit>0);
+    opr_Assert(VInit>0);
     VOL_LOCK;
     VShutdown_r();
     VOL_UNLOCK;
@@ -1609,14 +1580,14 @@ VShutdownThread(void * args)
     params = (vshutdown_thread_t *) args;
 
     /* acquire the shutdown pass 0 lock */
-    MUTEX_ENTER(&params->lock);
+    opr_mutex_enter(&params->lock);
 
     /* if there's still pass 0 work to be done,
      * get a work entry, and do a pass 0 shutdown */
     if (queue_IsNotEmpty(params)) {
        dpq = queue_First(params, diskpartition_queue_t);
        queue_Remove(dpq);
-       MUTEX_EXIT(&params->lock);
+       opr_mutex_exit(&params->lock);
        diskP = dpq->diskP;
        free(dpq);
        id = diskP->index;
@@ -1625,24 +1596,24 @@ VShutdownThread(void * args)
        while (ShutdownVolumeWalk_r(diskP, 0, &params->part_pass_head[id]))
            count++;
        params->stats[0][diskP->index] = count;
-       MUTEX_ENTER(&params->lock);
+       opr_mutex_enter(&params->lock);
     }
 
     params->n_threads_complete++;
     if (params->n_threads_complete == params->n_threads) {
        /* notify control thread that all workers have completed pass 0 */
-       CV_SIGNAL(&params->master_cv);
+       opr_cv_signal(&params->master_cv);
     }
     while (params->pass == 0) {
-       CV_WAIT(&params->cv, &params->lock);
+       opr_cv_wait(&params->cv, &params->lock);
     }
 
     /* switch locks */
-    MUTEX_EXIT(&params->lock);
+    opr_mutex_exit(&params->lock);
     VOL_LOCK;
 
     pass = params->pass;
-    osi_Assert(pass > 0);
+    opr_Assert(pass > 0);
 
     /* now escalate through the more complicated shutdowns */
     while (pass <= 3) {
@@ -1722,7 +1693,7 @@ VShutdownThread(void * args)
                    ShutdownCreateSchedule(params);
 
                    /* wake up all the workers */
-                   CV_BROADCAST(&params->cv);
+                   opr_cv_broadcast(&params->cv);
 
                    VOL_UNLOCK;
                    Log("VShutdown:  pass %d completed using %d threads on %d partitions\n",
@@ -1806,9 +1777,14 @@ ShutdownVByPForPass_r(struct DiskPartition64 * dp, int pass)
 {
     struct rx_queue * q = queue_First(&dp->vol_list, rx_queue);
     int i = 0;
+    const char *pass_strs[4] = {"{un/pre}attached vols", "vols w/ vol header loaded", "vols w/o vol header loaded", "vols with exclusive state"};
 
-    while (ShutdownVolumeWalk_r(dp, pass, &q))
+    while (ShutdownVolumeWalk_r(dp, pass, &q)) {
        i++;
+       if (0 == i%100) {
+           Log("VShutdownByPartition:  ... shut down %d volumes on %s in pass %d (%s)\n", i, VPartitionPath(dp), pass, pass_strs[pass]);
+       }
+    }
 
     return i;
 }
@@ -1869,14 +1845,15 @@ VShutdownVolume_r(Volume * vp)
     VCreateReservation_r(vp);
 
     if (LogLevel >= 5) {
-       Log("VShutdownVolume_r:  vid=%u, device=%d, state=%hu\n",
-           vp->hashid, vp->partition->device, V_attachState(vp));
+       Log("VShutdownVolume_r:  vid=%" AFS_VOLID_FMT ", device=%d, state=%u\n",
+           afs_printable_VolumeId_lu(vp->hashid), vp->partition->device,
+           (unsigned int) V_attachState(vp));
     }
 
     /* wait for other blocking ops to finish */
     VWaitExclusiveState_r(vp);
 
-    osi_Assert(VIsValidState(V_attachState(vp)));
+    opr_Assert(VIsValidState(V_attachState(vp)));
 
     switch(V_attachState(vp)) {
     case VOL_STATE_SALVAGING:
@@ -1896,8 +1873,8 @@ VShutdownVolume_r(Volume * vp)
        code = VHold_r(vp);
        if (!code) {
            if (LogLevel >= 5)
-               Log("VShutdown:  Attempting to take volume %u offline.\n",
-                   vp->hashid);
+               Log("VShutdown:  Attempting to take volume %" AFS_VOLID_FMT " offline.\n",
+                   afs_printable_VolumeId_lu(vp->hashid));
 
            /* take the volume offline (drops reference count) */
            VOffline_r(vp, "File server was shut down");
@@ -1918,6 +1895,22 @@ VShutdownVolume_r(Volume * vp)
 /* Header I/O routines                             */
 /***************************************************/
 
+static const char *
+HeaderName(bit32 magic)
+{
+    switch (magic) {
+    case VOLUMEINFOMAGIC:
+       return "volume info";
+    case SMALLINDEXMAGIC:
+       return "small index";
+    case LARGEINDEXMAGIC:
+       return "large index";
+    case LINKTABLEMAGIC:
+       return "link table";
+    }
+    return "unknown";
+}
+
 /* open a descriptor for the inode (h),
  * read in an on-disk structure into buffer (to) of size (size),
  * verify versionstamp in structure has magic (magic) and
@@ -1929,29 +1922,63 @@ ReadHeader(Error * ec, IHandle_t * h, char *to, int size, bit32 magic,
 {
     struct versionStamp *vsn;
     FdHandle_t *fdP;
+    afs_sfsize_t nbytes;
+    afs_ino_str_t stmp;
 
     *ec = 0;
     if (h == NULL) {
+       Log("ReadHeader: Null inode handle argument for %s header file.\n",
+           HeaderName(magic));
        *ec = VSALVAGE;
        return;
     }
 
     fdP = IH_OPEN(h);
     if (fdP == NULL) {
+       Log("ReadHeader: Failed to open %s header file "
+           "(volume=%" AFS_VOLID_FMT ", inode=%s); errno=%d\n", HeaderName(magic), afs_printable_VolumeId_lu(h->ih_vid),
+           PrintInode(stmp, h->ih_ino), errno);
        *ec = VSALVAGE;
        return;
     }
 
     vsn = (struct versionStamp *)to;
-    if (FDH_PREAD(fdP, to, size, 0) != size || vsn->magic != magic) {
+    nbytes = FDH_PREAD(fdP, to, size, 0);
+    if (nbytes < 0) {
+       Log("ReadHeader: Failed to read %s header file "
+           "(volume=%" AFS_VOLID_FMT ", inode=%s); errno=%d\n", HeaderName(magic), afs_printable_VolumeId_lu(h->ih_vid),
+           PrintInode(stmp, h->ih_ino), errno);
+       *ec = VSALVAGE;
+       FDH_REALLYCLOSE(fdP);
+       return;
+    }
+    if (nbytes != size) {
+       Log("ReadHeader: Incorrect number of bytes read from %s header file "
+           "(volume=%" AFS_VOLID_FMT ", inode=%s); expected=%d, read=%d\n",
+           HeaderName(magic), afs_printable_VolumeId_lu(h->ih_vid), 
+           PrintInode(stmp, h->ih_ino), size, (int)nbytes);
+       *ec = VSALVAGE;
+       FDH_REALLYCLOSE(fdP);
+       return;
+    }
+    if (vsn->magic != magic) {
+       Log("ReadHeader: Incorrect magic for %s header file "
+           "(volume=%" AFS_VOLID_FMT ", inode=%s); expected=0x%x, read=0x%x\n",
+           HeaderName(magic), afs_printable_VolumeId_lu(h->ih_vid),
+           PrintInode(stmp, h->ih_ino), magic, vsn->magic);
        *ec = VSALVAGE;
        FDH_REALLYCLOSE(fdP);
        return;
     }
+
     FDH_CLOSE(fdP);
 
     /* Check is conditional, in case caller wants to inspect version himself */
     if (version && vsn->version != version) {
+       Log("ReadHeader: Incorrect version for %s header file "
+           "(volume=%" AFS_VOLID_FMT ", inode=%s); expected=%x, read=%x\n",
+           HeaderName(magic), afs_printable_VolumeId_lu(h->ih_vid), PrintInode(stmp, h->ih_ino),
+           version, vsn->version);
        *ec = VSALVAGE;
     }
 }
@@ -2116,14 +2143,14 @@ VPreAttachVolumeByName_r(Error * ec, char *partition, char *name)
 Volume *
 VPreAttachVolumeById_r(Error * ec,
                       char * partition,
-                      VolId volumeId)
+                      VolumeId volumeId)
 {
     Volume *vp;
     struct DiskPartition64 *partp;
 
     *ec = 0;
 
-    osi_Assert(programType == fileServer);
+    opr_Assert(programType == fileServer);
 
     if (!(partp = VGetPartition_r(partition, 0))) {
        *ec = VNOVOL;
@@ -2131,11 +2158,26 @@ VPreAttachVolumeById_r(Error * ec,
        return NULL;
     }
 
+    /* ensure that any vp we pass to VPreAttachVolumeByVp_r
+     * is NOT in exclusive state.
+     */
+ retry:
     vp = VLookupVolume_r(ec, volumeId, NULL);
+
     if (*ec) {
        return NULL;
     }
 
+    if (vp && VIsExclusiveState(V_attachState(vp))) {
+       VCreateReservation_r(vp);
+       VWaitExclusiveState_r(vp);
+       VCancelReservation_r(vp);
+       vp = NULL;
+       goto retry;    /* look up volume again */
+    }
+
+    /* vp == NULL or vp not exclusive both OK */
+
     return VPreAttachVolumeByVp_r(ec, partp, vp, volumeId);
 }
 
@@ -2151,6 +2193,8 @@ VPreAttachVolumeById_r(Error * ec,
  *
  * @pre VOL_LOCK is held.
  *
+ * @pre vp (if specified) must not be in exclusive state.
+ *
  * @warning Returned volume object pointer does not have to
  *          equal the pointer passed in as argument vp.  There
  *          are potential race conditions which can result in
@@ -2169,12 +2213,17 @@ Volume *
 VPreAttachVolumeByVp_r(Error * ec,
                       struct DiskPartition64 * partp,
                       Volume * vp,
-                      VolId vid)
+                      VolumeId vid)
 {
     Volume *nvp = NULL;
 
     *ec = 0;
 
+    /* don't proceed unless it's safe */
+    if (vp) {
+       opr_Assert(!VIsExclusiveState(V_attachState(vp)));
+    }
+
     /* check to see if pre-attach already happened */
     if (vp &&
        (V_attachState(vp) != VOL_STATE_UNATTACHED) &&
@@ -2188,7 +2237,9 @@ VPreAttachVolumeByVp_r(Error * ec,
         *   - volume is in an error state
         *   - volume is pre-attached
         */
-       Log("VPreattachVolumeByVp_r: volume %u not in quiescent state\n", vid);
+       Log("VPreattachVolumeByVp_r: volume %" AFS_VOLID_FMT " not in quiescent state (state %u flags 0x%x)\n",
+           afs_printable_VolumeId_lu(vid), V_attachState(vp),
+           V_attachFlags(vp));
        goto done;
     } else if (vp) {
        /* we're re-attaching a volume; clear out some old state */
@@ -2206,12 +2257,11 @@ VPreAttachVolumeByVp_r(Error * ec,
        VOL_UNLOCK;
 
        /* allocate the volume structure */
-       vp = nvp = (Volume *) malloc(sizeof(Volume));
-       osi_Assert(vp != NULL);
-       memset(vp, 0, sizeof(Volume));
+       vp = nvp = calloc(1, sizeof(Volume));
+       opr_Assert(vp != NULL);
        queue_Init(&vp->vnode_list);
        queue_Init(&vp->rx_call_list);
-       CV_INIT(&V_attachCV(vp), "vp attach", CV_DEFAULT, 0);
+       opr_cv_init(&V_attachCV(vp));
     }
 
     /* link the volume with its associated vice partition */
@@ -2250,7 +2300,7 @@ VPreAttachVolumeByVp_r(Error * ec,
     VChangeState_r(vp, VOL_STATE_PREATTACHED);
 
     if (LogLevel >= 5)
-       Log("VPreAttachVolumeByVp_r:  volume %u pre-attached\n", vp->hashid);
+       Log("VPreAttachVolumeByVp_r:  volume %" AFS_VOLID_FMT " pre-attached\n", afs_printable_VolumeId_lu(vp->hashid));
 
   done:
     if (*ec)
@@ -2281,7 +2331,7 @@ VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
     struct DiskPartition64 *partp;
     char path[64];
     int isbusy = 0;
-    VolId volumeId;
+    VolumeId volumeId;
     int checkedOut;
 #ifdef AFS_DEMAND_ATTACH_FS
     VolumeStats stats_save;
@@ -2299,7 +2349,7 @@ VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
     }
 
     if (VRequiresPartLock()) {
-       osi_Assert(VInit == 3);
+       opr_Assert(VInit == 3);
        VLockPartition_r(partition);
     } else if (programType == fileServer) {
 #ifdef AFS_DEMAND_ATTACH_FS
@@ -2374,7 +2424,7 @@ VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
            }
        }
 
-       osi_Assert(vp != NULL);
+       opr_Assert(vp != NULL);
 
        /* handle pre-attach races
         *
@@ -2430,19 +2480,19 @@ VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
 
     VOL_UNLOCK;
 
-    strcat(path, "/");
+    strcat(path, OS_DIRSEP);
     strcat(path, name);
 
     if (!vp) {
       vp = (Volume *) calloc(1, sizeof(Volume));
-      osi_Assert(vp != NULL);
+      opr_Assert(vp != NULL);
       vp->hashid = volumeId;
       vp->device = partp->device;
       vp->partition = partp;
       queue_Init(&vp->vnode_list);
       queue_Init(&vp->rx_call_list);
 #ifdef AFS_DEMAND_ATTACH_FS
-      CV_INIT(&V_attachCV(vp), "vp attach", CV_DEFAULT, 0);
+      opr_cv_init(&V_attachCV(vp));
 #endif /* AFS_DEMAND_ATTACH_FS */
     }
 
@@ -2529,7 +2579,7 @@ VAttachVolumeByName_r(Error * ec, char *partition, char *name, int mode)
            }
        }
        if (LogLevel)
-           Log("VOnline:  volume %u (%s) attached and online\n", V_id(vp),
+         Log("VOnline:  volume %" AFS_VOLID_FMT " (%s) attached and online\n", afs_printable_VolumeId_lu(V_id(vp)),
                V_name(vp));
     }
 
@@ -2565,14 +2615,14 @@ VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode)
     struct DiskPartition64 *partp;
     char path[64];
     int isbusy = 0;
-    VolId volumeId;
+    VolumeId volumeId;
     Volume * nvp = NULL;
     VolumeStats stats_save;
     int checkedOut;
     *ec = 0;
 
     /* volume utility should never call AttachByVp */
-    osi_Assert(programType == fileServer);
+    opr_Assert(programType == fileServer);
 
     volumeId = vp->hashid;
     partp = vp->partition;
@@ -2616,7 +2666,7 @@ VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode)
        }
     }
 
-    osi_Assert(vp != NULL);
+    opr_Assert(vp != NULL);
     VChangeState_r(vp, VOL_STATE_ATTACHING);
 
     /* restore monotonically increasing stats */
@@ -2629,7 +2679,7 @@ VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode)
 
     VOL_UNLOCK;
 
-    strcat(path, "/");
+    strcat(path, OS_DIRSEP);
     strcat(path, name);
 
     /* do volume attach
@@ -2652,7 +2702,8 @@ VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode)
 
     VUpdateVolume_r(ec, vp, 0);
     if (*ec) {
-       Log("VAttachVolume: Error updating volume %u\n", vp->hashid);
+       Log("VAttachVolume: Error updating volume %" AFS_VOLID_FMT "\n",
+           afs_printable_VolumeId_lu(vp->hashid));
        VPutVolume_r(vp);
        goto done;
     }
@@ -2670,15 +2721,16 @@ VAttachVolumeByVp_r(Error * ec, Volume * vp, int mode)
 #endif /* !AFS_DEMAND_ATTACH_FS */
        VAddToVolumeUpdateList_r(ec, vp);
        if (*ec) {
-           Log("VAttachVolume: Error adding volume %u to update list\n", vp->hashid);
+           Log("VAttachVolume: Error adding volume %" AFS_VOLID_FMT " to update list\n",
+               afs_printable_VolumeId_lu(vp->hashid));
            if (vp)
                VPutVolume_r(vp);
            goto done;
        }
     }
     if (LogLevel)
-       Log("VOnline:  volume %u (%s) attached and online\n", V_id(vp),
-           V_name(vp));
+       Log("VOnline:  volume %" AFS_VOLID_FMT " (%s) attached and online\n",
+           afs_printable_VolumeId_lu(V_id(vp)), V_name(vp));
   done:
     if (reserve) {
        VCancelReservation_r(nvp);
@@ -2714,8 +2766,9 @@ VLockVolumeNB(Volume *vp, int locktype)
 {
     int code;
 
-    osi_Assert(programType != fileServer || VIsExclusiveState(V_attachState(vp)));
-    osi_Assert(!(V_attachFlags(vp) & VOL_LOCKED));
+    opr_Assert(programType != fileServer
+              || VIsExclusiveState(V_attachState(vp)));
+    opr_Assert(!(V_attachFlags(vp) & VOL_LOCKED));
 
     code = VLockVolumeByIdNB(vp->hashid, vp->partition, locktype);
     if (code == 0) {
@@ -2737,8 +2790,9 @@ VLockVolumeNB(Volume *vp, int locktype)
 static void
 VUnlockVolume(Volume *vp)
 {
-    osi_Assert(programType != fileServer || VIsExclusiveState(V_attachState(vp)));
-    osi_Assert((V_attachFlags(vp) & VOL_LOCKED));
+    opr_Assert(programType != fileServer
+              || VIsExclusiveState(V_attachState(vp)));
+    opr_Assert((V_attachFlags(vp) & VOL_LOCKED));
 
     VUnlockVolumeById(vp->hashid, vp->partition);
 
@@ -2822,7 +2876,7 @@ attach_volume_header(Error *ec, Volume *vp, struct DiskPartition64 *partp,
         SYNC_response res;
         memset(&res, 0, sizeof(res));
 
-       if (FSYNC_VolOp(volid, VPartitionPath(partp), FSYNC_VOL_NEEDVOLUME, mode, &res)
+       if (FSYNC_VolOp(volid, partp->name, FSYNC_VOL_NEEDVOLUME, mode, &res)
            != SYNC_OK) {
 
             if (res.hdr.reason == FSYNC_SALVAGE) {
@@ -2909,7 +2963,7 @@ attach_volume_header(Error *ec, Volume *vp, struct DiskPartition64 *partp,
     if (VCanUseFSSYNC() && (mode == V_PEEK || peek)) {
        SYNC_response res;
        res.payload.len = sizeof(VolumeDiskData);
-       res.payload.buf = &vp->header->diskstuff;
+       res.payload.buf = &(V_disk(vp));
 
        if (FSYNC_VolOp(vp->hashid,
                        partp->name,
@@ -2958,7 +3012,7 @@ attach_volume_header(Error *ec, Volume *vp, struct DiskPartition64 *partp,
 #if defined(AFS_DEMAND_ATTACH_FS) && defined(FSSYNC_BUILD_CLIENT)
     if (!peek && *ec == 0 && retry == 0 && VMustCheckoutVolume(mode)) {
 
-       code = FSYNC_VerifyCheckout(volid, VPartitionPath(partp), FSYNC_VOL_NEEDVOLUME, mode);
+       code = FSYNC_VerifyCheckout(volid, partp->name, FSYNC_VOL_NEEDVOLUME, mode);
 
        if (code == SYNC_DENIED) {
            /* must retry checkout; fileserver no longer thinks we have
@@ -2991,6 +3045,9 @@ attach_volume_header(Error *ec, Volume *vp, struct DiskPartition64 *partp,
     }
 
     if (*ec) {
+       VOL_LOCK;
+       FreeVolumeHeader(vp);
+       VOL_UNLOCK;
        return;
     }
     if (retry) {
@@ -3051,12 +3108,14 @@ attach_check_vop(Error *ec, VolumeId volid, struct DiskPartition64 *partp,
        switch (vp->pending_vol_op->vol_op_state) {
        case FSSYNC_VolOpPending:
            /* this should never happen */
-           osi_Assert(vp->pending_vol_op->vol_op_state != FSSYNC_VolOpPending);
+           opr_Assert(vp->pending_vol_op->vol_op_state
+                           != FSSYNC_VolOpPending);
            break;
 
        case FSSYNC_VolOpRunningUnknown:
            /* this should never happen; we resolved 'unknown' above */
-           osi_Assert(vp->pending_vol_op->vol_op_state != FSSYNC_VolOpRunningUnknown);
+           opr_Assert(vp->pending_vol_op->vol_op_state
+                           != FSSYNC_VolOpRunningUnknown);
            break;
 
        case FSSYNC_VolOpRunningOffline:
@@ -3070,7 +3129,11 @@ attach_check_vop(Error *ec, VolumeId volid, struct DiskPartition64 *partp,
 
            /* check to see if we should set the specialStatus flag */
            if (VVolOpSetVBusy_r(vp, vp->pending_vol_op)) {
-               vp->specialStatus = VBUSY;
+               /* don't overwrite specialStatus if it was already set to
+                * something else (e.g. VMOVED) */
+               if (!vp->specialStatus) {
+                   vp->specialStatus = VBUSY;
+               }
            }
            break;
 
@@ -3112,7 +3175,7 @@ attach_check_vop(Error *ec, VolumeId volid, struct DiskPartition64 *partp,
  * @post VOL_LOCK held
  */
 static Volume *
-attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
+attach2(Error * ec, VolumeId volumeId, char *path, struct DiskPartition64 *partp,
         Volume * vp, int isbusy, int mode, int *acheckedOut)
 {
     /* have we read in the header successfully? */
@@ -3155,7 +3218,11 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
     if (!*ec) {
        read_header = 1;
 
-       vp->specialStatus = (byte) (isbusy ? VBUSY : 0);
+       /* ensure that we don't override specialStatus if it was set to
+        * something else (e.g. VMOVED) */
+       if (isbusy && !vp->specialStatus) {
+           vp->specialStatus = VBUSY;
+       }
        vp->shuttingDown = 0;
        vp->goingOffline = 0;
        vp->nUsers = 1;
@@ -3181,7 +3248,6 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
     if (!*ec) {
        struct IndexFileHeader iHead;
 
-#if OPENAFS_VOL_STATS
        /*
         * We just read in the diskstuff part of the header.  If the detailed
         * volume stats area has not yet been initialized, we should bzero the
@@ -3191,7 +3257,6 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
            memset((V_stat_area(vp)), 0, VOL_STATS_BYTES);
            V_stat_initialized(vp) = 1;
        }
-#endif /* OPENAFS_VOL_STATS */
 
        (void)ReadHeader(ec, vp->vnodeIndex[vSmall].handle,
                         (char *)&iHead, sizeof(iHead),
@@ -3233,14 +3298,17 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
        if (!VCanScheduleSalvage()) {
            Log("VAttachVolume: Error attaching volume %s; volume needs salvage; error=%u\n", path, *ec);
        }
-       VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER |
-                                                 VOL_SALVAGE_NO_OFFLINE);
+       VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_NO_OFFLINE);
        vp->nUsers = 0;
 
        goto locked_error;
     } else if (*ec) {
        /* volume operation in progress */
-       goto unlocked_error;
+       VOL_LOCK;
+       /* we have already transitioned the vp away from ATTACHING state, so we
+        * can go right to the end of attach2, and we do not need to transition
+        * to ERROR. */
+       goto error_notbroken;
     }
 #else /* AFS_DEMAND_ATTACH_FS */
     if (*ec) {
@@ -3257,8 +3325,7 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
        if (!VCanScheduleSalvage()) {
            Log("VAttachVolume: volume salvage flag is ON for %s; volume needs salvage\n", path);
        }
-       VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER |
-                                                  VOL_SALVAGE_NO_OFFLINE);
+       VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_NO_OFFLINE);
        vp->nUsers = 0;
 
 #else /* AFS_DEMAND_ATTACH_FS */
@@ -3280,8 +3347,7 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
        if (!VCanScheduleSalvage()) {
            Log("VAttachVolume: volume %s needs to be salvaged; not attached.\n", path);
        }
-       VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER |
-                                                  VOL_SALVAGE_NO_OFFLINE);
+       VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_NO_OFFLINE);
        vp->nUsers = 0;
 
 #else /* AFS_DEMAND_ATTACH_FS */
@@ -3303,8 +3369,7 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
 
 #if defined(AFS_DEMAND_ATTACH_FS)
        /* schedule a salvage so the volume goes away on disk */
-       VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER |
-                                                 VOL_SALVAGE_NO_OFFLINE);
+       VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_NO_OFFLINE);
        VChangeState_r(vp, VOL_STATE_ERROR);
        vp->nUsers = 0;
        forcefree = 1;
@@ -3322,8 +3387,7 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
            VGetBitmap_r(ec, vp, i);
            if (*ec) {
 #ifdef AFS_DEMAND_ATTACH_FS
-               VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER |
-                                                         VOL_SALVAGE_NO_OFFLINE);
+               VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_NO_OFFLINE);
                vp->nUsers = 0;
 #endif /* AFS_DEMAND_ATTACH_FS */
                Log("VAttachVolume: error getting bitmap for volume (%s)\n",
@@ -3371,8 +3435,7 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
                "%lu; needs salvage\n", (int)*ec,
                afs_printable_uint32_lu(V_id(vp)));
 #ifdef AFS_DEMAND_ATTACH_FS
-           VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER |
-                                                     VOL_SALVAGE_NO_OFFLINE);
+           VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_NO_OFFLINE);
            vp->nUsers = 0;
 #else /* !AFS_DEMAND_ATTACH_FS */
            *ec = VSALVAGE;
@@ -3388,15 +3451,21 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
            V_inUse(vp) = fileServer;
            V_offlineMessage(vp)[0] = '\0';
        }
+#ifdef AFS_DEMAND_ATTACH_FS
+       /* check if the volume is actually usable. only do this for DAFS; for
+        * non-DAFS, volumes that are not inService/blessed can still be
+        * attached, even if clients cannot access them. this is relevant
+        * because for non-DAFS, we try to attach the volume when e.g.
+        * volserver gives us back then vol when its done with it, but
+        * volserver may give us back a volume that is not inService/blessed. */
+
        if (!V_inUse(vp)) {
            *ec = VNOVOL;
-#ifdef AFS_DEMAND_ATTACH_FS
            /* Put the vol into PREATTACHED state, so if someone tries to
             * access it again, we try to attach, see that we're not blessed,
             * and give a VNOVOL error again. Putting it into UNATTACHED state
             * would result in a VOFFLINE error instead. */
            error_state = VOL_STATE_PREATTACHED;
-#endif /* AFS_DEMAND_ATTACH_FS */
 
            /* mimic e.g. GetVolume errors */
            if (!V_blessed(vp)) {
@@ -3408,26 +3477,23 @@ attach2(Error * ec, VolId volumeId, char *path, struct DiskPartition64 *partp,
            } else {
                Log("Volume %lu offline: needs salvage\n", afs_printable_uint32_lu(V_id(vp)));
                *ec = VSALVAGE;
-#ifdef AFS_DEMAND_ATTACH_FS
                error_state = VOL_STATE_ERROR;
                /* see if we can recover */
-               VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_INVALIDATE_HEADER);
-#endif
+               VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, VOL_SALVAGE_NO_OFFLINE);
            }
-#ifdef AFS_DEMAND_ATTACH_FS
            vp->nUsers = 0;
-#endif
            goto locked_error;
        }
+#endif /* AFS_DEMAND_ATTACH_FS */
     } else {
 #ifdef AFS_DEMAND_ATTACH_FS
-       if ((mode != V_PEEK) && (mode != V_SECRETLY))
+       if ((mode != V_PEEK) && (mode != V_SECRETLY) && (mode != V_READONLY))
            V_inUse(vp) = programType;
 #endif /* AFS_DEMAND_ATTACH_FS */
        V_checkoutMode(vp) = mode;
     }
 
-    AddVolumeToHashTable(vp, V_id(vp));
+    AddVolumeToHashTable(vp, vp->hashid);
 #ifdef AFS_DEMAND_ATTACH_FS
     if (VCanUnlockAttached() && (V_attachFlags(vp) & VOL_LOCKED)) {
        VUnlockVolume(vp);
@@ -3449,6 +3515,17 @@ unlocked_error:
 locked_error:
 #ifdef AFS_DEMAND_ATTACH_FS
     if (!VIsErrorState(V_attachState(vp))) {
+       if (programType != fileServer && *ec == VNOVOL) {
+           /* do not log anything in this case; it is common for
+            * non-fileserver programs to fail here with VNOVOL, since that
+            * is what happens when they simply try to use a volume, but that
+            * volume doesn't exist. */
+
+       } else if (VIsErrorState(error_state)) {
+           Log("attach2: forcing vol %" AFS_VOLID_FMT " to error state (state %u flags 0x%x ec %d)\n",
+               afs_printable_VolumeId_lu(vp->hashid), V_attachState(vp),
+               V_attachFlags(vp), *ec);
+       }
        VChangeState_r(vp, error_state);
     }
 #endif /* AFS_DEMAND_ATTACH_FS */
@@ -3458,7 +3535,15 @@ locked_error:
     }
 
 #ifdef AFS_DEMAND_ATTACH_FS
-    VCheckSalvage(vp);
+ error_notbroken:
+    if (VCheckSalvage(vp) == VCHECK_SALVAGE_FAIL) {
+       /* The salvage could not be scheduled with the salvage server
+        * due to a hard error. Reset the error code to prevent retry loops by
+        * callers. */
+       if (*ec == VSALVAGING) {
+           *ec = VSALVAGE;
+       }
+    }
     if (forcefree) {
        FreeVolume(vp);
     } else {
@@ -3495,7 +3580,7 @@ VAttachVolume_r(Error * ec, VolumeId volumeId, int mode)
        Error error;
        vp = VGetVolume_r(&error, volumeId);
        if (vp) {
-           osi_Assert(V_inUse(vp) == 0);
+           opr_Assert(V_inUse(vp) == 0);
            VDetachVolume_r(ec, vp);
        }
        return NULL;
@@ -3654,7 +3739,8 @@ static const struct timespec *
 VOfflineTimeout(struct timespec *ats)
 {
     if (vol_shutting_down) {
-       osi_Assert(pthread_once(&shutdown_timeout_once, VShutdownTimeoutInit) == 0);
+       opr_Verify(pthread_once(&shutdown_timeout_once,
+                               VShutdownTimeoutInit) == 0);
        return shutdown_timeout;
     } else {
        return VCalcTimeout(ats, vol_opts.offline_timeout);
@@ -3810,7 +3896,7 @@ VDeregisterCall_r(Volume *vp, struct VCallByVol *cbv)
 void
 VPutVolume_r(Volume * vp)
 {
-    osi_Assert(--vp->nUsers >= 0);
+    opr_Verify(--vp->nUsers >= 0);
     if (vp->nUsers == 0) {
        VCheckOffline(vp);
        ReleaseVolumeHeader(vp->header);
@@ -3854,7 +3940,7 @@ VPutVolumeWithCall(Volume *vp, struct VCallByVol *cbv)
    of whether or not the volume is in service or on/off line.  An error
    code, however, is returned with an indication of the volume's status */
 Volume *
-VGetVolume(Error * ec, Error * client_ec, VolId volumeId)
+VGetVolume(Error * ec, Error * client_ec, VolumeId volumeId)
 {
     Volume *retVal;
     VOL_LOCK;
@@ -3885,7 +3971,7 @@ VGetVolume(Error * ec, Error * client_ec, VolId volumeId)
  *       VPutVolumeWithCall
  */
 Volume *
-VGetVolumeWithCall(Error * ec, Error * client_ec, VolId volumeId,
+VGetVolumeWithCall(Error * ec, Error * client_ec, VolumeId volumeId,
                    const struct timespec *ts, struct VCallByVol *cbv)
 {
     Volume *retVal;
@@ -3897,7 +3983,7 @@ VGetVolumeWithCall(Error * ec, Error * client_ec, VolId volumeId,
 }
 
 Volume *
-VGetVolume_r(Error * ec, VolId volumeId)
+VGetVolume_r(Error * ec, VolumeId volumeId)
 {
     return GetVolume(ec, NULL, volumeId, NULL, NULL);
 }
@@ -3933,7 +4019,7 @@ VGetVolumeByVp_r(Error * ec, Volume * vp)
  * @note for LWP builds, 'timeout' must be NULL
  */
 static Volume *
-GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint,
+GetVolume(Error * ec, Error * client_ec, VolumeId volumeId, Volume * hint,
           const struct timespec *timeout)
 {
     Volume *vp = hint;
@@ -4025,18 +4111,18 @@ GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint,
         *   - VOL_STATE_SHUTTING_DOWN
         */
        if ((V_attachState(vp) == VOL_STATE_ERROR) ||
-           (V_attachState(vp) == VOL_STATE_SHUTTING_DOWN) ||
-           (V_attachState(vp) == VOL_STATE_GOING_OFFLINE)) {
+           (V_attachState(vp) == VOL_STATE_SHUTTING_DOWN)) {
            *ec = VNOVOL;
            vp = NULL;
            break;
        }
 
        /*
-        * short circuit with VOFFLINE for VOL_STATE_UNATTACHED and
+        * short circuit with VOFFLINE for VOL_STATE_UNATTACHED/GOING_OFFLINE and
         *                    VNOVOL   for VOL_STATE_DELETED
         */
        if ((V_attachState(vp) == VOL_STATE_UNATTACHED) ||
+           (V_attachState(vp) == VOL_STATE_GOING_OFFLINE) ||
            (V_attachState(vp) == VOL_STATE_DELETED)) {
           if (vp->specialStatus) {
               *ec = vp->specialStatus;
@@ -4061,6 +4147,11 @@ GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint,
        }
 
        if (V_attachState(vp) == VOL_STATE_PREATTACHED) {
+           if (vp->specialStatus) {
+               *ec = vp->specialStatus;
+               vp = NULL;
+               break;
+           }
            avp = VAttachVolumeByVp_r(ec, vp, 0);
            if (avp) {
                if (vp != avp) {
@@ -4081,12 +4172,18 @@ GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint,
                case VSALVAGING:
                    break;
                case VOFFLINE:
-                   if (!vp->pending_vol_op) {
-                       endloop = 1;
+                   endloop = 1;
+                   if (vp->specialStatus) {
+                       *ec = vp->specialStatus;
                    }
                    break;
+
                default:
-                   *ec = VNOVOL;
+                   if (vp->specialStatus) {
+                       *ec = vp->specialStatus;
+                   } else {
+                       *ec = VNOVOL;
+                   }
                    endloop = 1;
                }
                if (endloop) {
@@ -4111,50 +4208,24 @@ GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint,
            vp = NULL;
            break;
        }
-#endif
 
-#ifdef AFS_DEMAND_ATTACH_FS
+       if (VIsErrorState(V_attachState(vp))) {
+           /* make sure we don't take a vp in VOL_STATE_ERROR state and use
+            * it, or transition it out of that state */
+           if (!*ec) {
+               *ec = VNOVOL;
+           }
+           vp = NULL;
+           break;
+       }
+
        /*
-        * this test MUST happen after VAttachVolymeByVp, so vol_op_state is
-        * not VolOpRunningUnknown (attach2 would have converted it to Online
-        * or Offline)
+        * this test MUST happen after VAttachVolymeByVp, so we have no
+        * conflicting vol op. (attach2 would have errored out if we had one;
+        * specifically attach_check_vop must have detected a conflicting vop)
         */
+         opr_Assert(!vp->pending_vol_op || vp->pending_vol_op->vol_op_state == FSSYNC_VolOpRunningOnline);
 
-         /* only valid before/during demand attachment */
-         osi_Assert(!vp->pending_vol_op || vp->pending_vol_op->vol_op_state != FSSYNC_VolOpRunningUnknown);
-
-         /* deny getvolume due to running mutually exclusive vol op */
-         if (vp->pending_vol_op && vp->pending_vol_op->vol_op_state==FSSYNC_VolOpRunningOffline) {
-          /*
-           * volume cannot remain online during this volume operation.
-           * notify client.
-           */
-          if (vp->specialStatus) {
-              /*
-               * special status codes outrank normal VOFFLINE code
-               */
-              *ec = vp->specialStatus;
-              if (client_ec) {
-                  *client_ec = vp->specialStatus;
-              }
-          } else {
-              if (client_ec) {
-                  /* see CheckVnode() in afsfileprocs.c for an explanation
-                   * of this error code logic */
-                  afs_uint32 now = FT_ApproxTime();
-                  if ((vp->stats.last_vol_op + (10 * 60)) >= now) {
-                      *client_ec = VBUSY;
-                  } else {
-                      *client_ec = VRESTARTING;
-                  }
-              }
-              *ec = VOFFLINE;
-          }
-          VChangeState_r(vp, VOL_STATE_UNATTACHED);
-          FreeVolumeHeader(vp);
-          vp = NULL;
-          break;
-       }
 #endif /* AFS_DEMAND_ATTACH_FS */
 
        LoadVolumeHeader(ec, vp);
@@ -4163,11 +4234,11 @@ GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint,
            /* Only log the error if it was a totally unexpected error.  Simply
             * a missing inode is likely to be caused by the volume being deleted */
            if (errno != ENXIO || LogLevel)
-               Log("Volume %u: couldn't reread volume header\n",
-                   vp->hashid);
+               Log("Volume %" AFS_VOLID_FMT ": couldn't reread volume header\n",
+                   afs_printable_VolumeId_lu(vp->hashid));
 #ifdef AFS_DEMAND_ATTACH_FS
            if (VCanScheduleSalvage()) {
-               VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
+               VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0 /*flags*/);
            } else {
                FreeVolume(vp);
                vp = NULL;
@@ -4204,7 +4275,7 @@ GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint,
 #else /* AFS_PTHREAD_ENV */
                    /* LWP has no timed wait, so the caller better not be
                     * expecting one */
-                   osi_Assert(!timeout);
+                   opr_Assert(!timeout);
                    LWP_WaitProcess(VPutVolume);
 #endif /* AFS_PTHREAD_ENV */
                    continue;
@@ -4251,7 +4322,7 @@ GetVolume(Error * ec, Error * client_ec, VolId volumeId, Volume * hint,
 #endif /* AFS_DEMAND_ATTACH_FS */
 
  not_inited:
-    osi_Assert(vp || *ec);
+    opr_Assert(vp || *ec);
     return vp;
 }
 
@@ -4267,8 +4338,8 @@ VTakeOffline_r(Volume * vp)
 {
     Error error;
 
-    osi_Assert(vp->nUsers > 0);
-    osi_Assert(programType == fileServer);
+    opr_Assert(vp->nUsers > 0);
+    opr_Assert(programType == fileServer);
 
     VCreateReservation_r(vp);
     VWaitExclusiveState_r(vp);
@@ -4283,8 +4354,8 @@ VTakeOffline_r(Volume * vp)
 void
 VTakeOffline_r(Volume * vp)
 {
-    osi_Assert(vp->nUsers > 0);
-    osi_Assert(programType == fileServer);
+    opr_Assert(vp->nUsers > 0);
+    opr_Assert(programType == fileServer);
 
     vp->goingOffline = 1;
     V_needsSalvaged(vp) = 1;
@@ -4342,7 +4413,7 @@ VForceOffline_r(Volume * vp, int flags)
 
     strcpy(V_offlineMessage(vp),
           "Forced offline due to internal error: volume needs to be salvaged");
-    Log("Volume %u forced offline:  it needs salvaging!\n", V_id(vp));
+    Log("Volume %" AFS_VOLID_FMT " forced offline:  it needs salvaging!\n", afs_printable_VolumeId_lu(V_id(vp)));
 
     V_inUse(vp) = 0;
     vp->goingOffline = 0;
@@ -4352,11 +4423,11 @@ VForceOffline_r(Volume * vp, int flags)
     }
 
 #ifdef AFS_DEMAND_ATTACH_FS
-    VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
+    VRequestSalvage_r(&error, vp, SALVSYNC_ERROR, 0 /*flags*/);
 #endif /* AFS_DEMAND_ATTACH_FS */
 
 #ifdef AFS_PTHREAD_ENV
-    CV_BROADCAST(&vol_put_volume_cond);
+    opr_cv_broadcast(&vol_put_volume_cond);
 #else /* AFS_PTHREAD_ENV */
     LWP_NoYieldSignal(VPutVolume);
 #endif /* AFS_PTHREAD_ENV */
@@ -4415,9 +4486,9 @@ VScanCalls_r(struct Volume *vp)
            char hoststr[16];
            peer = rx_PeerOf(rx_ConnectionOf(cbv->call));
 
-           Log("Offlining volume %lu while client %s:%u is trying to read "
+           Log("Offlining volume %" AFS_VOLID_FMT " while client %s:%u is trying to read "
                "from it; kicking client off with error %ld\n",
-               (long unsigned) vp->hashid,
+               afs_printable_VolumeId_lu(vp->hashid),
                afs_inet_ntoa_r(rx_HostOf(peer), hoststr),
                (unsigned) ntohs(rx_PortOf(peer)),
                (long) err);
@@ -4545,7 +4616,7 @@ VOffline_r(Volume * vp, char *message)
     VolumeId vid = V_id(vp);
 #endif
 
-    osi_Assert(programType != volumeUtility && programType != volumeServer);
+    opr_Assert(programType != volumeUtility && programType != volumeServer);
     if (!V_inUse(vp)) {
        VPutVolume_r(vp);
        return;
@@ -4599,7 +4670,7 @@ void
 VOfflineForVolOp_r(Error *ec, Volume *vp, char *message)
 {
     int salvok = 1;
-    osi_Assert(vp->pending_vol_op);
+    opr_Assert(vp->pending_vol_op);
     if (!V_inUse(vp)) {
        VPutVolume_r(vp);
         *ec = 1;
@@ -4654,11 +4725,17 @@ VDetachVolume_r(Error * ec, Volume * vp)
        notifyServer = vp->needsPutBack;
        if (V_destroyMe(vp) == DESTROY_ME)
            useDone = FSYNC_VOL_LEAVE_OFF;
-#ifdef AFS_DEMAND_ATTACH_FS
+# ifdef AFS_DEMAND_ATTACH_FS
        else if (!V_blessed(vp) || !V_inService(vp))
            useDone = FSYNC_VOL_LEAVE_OFF;
-#endif
+# endif
+    }
+# ifdef AFS_DEMAND_ATTACH_FS
+    if (V_needsSalvaged(vp)) {
+       notifyServer = 0;
+       VRequestSalvage_r(ec, vp, SALVSYNC_NEEDED, 0);
     }
+# endif
     tpartp = vp->partition;
     volume = V_id(vp);
 #endif /* FSSYNC_BUILD_CLIENT */
@@ -4736,15 +4813,17 @@ VCloseVolumeHandles_r(Volume * vp)
     VolState state_save;
 
     state_save = VChangeState_r(vp, VOL_STATE_OFFLINING);
+
+    VOL_UNLOCK;
 #endif
 
-    /* demand attach fs
-     *
-     * XXX need to investigate whether we can perform
-     * DFlushVolume outside of vol_glock_mutex...
-     *
-     * VCloseVnodeFiles_r drops the glock internally */
     DFlushVolume(vp->hashid);
+
+#ifdef AFS_DEMAND_ATTACH_FS
+    VOL_LOCK;
+#endif
+
+    /* DAFS: VCloseVnodeFiles_r drops the glock internally */
     VCloseVnodeFiles_r(vp);
 
 #ifdef AFS_DEMAND_ATTACH_FS
@@ -4756,9 +4835,9 @@ VCloseVolumeHandles_r(Volume * vp)
        IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
        IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
        IH_CONDSYNC(vp->diskDataHandle);
-#ifdef AFS_NT40_ENV
+#ifdef AFS_NAMEI_ENV
        IH_CONDSYNC(vp->linkHandle);
-#endif /* AFS_NT40_ENV */
+#endif /* AFS_NAMEI_ENV */
     }
 
     IH_REALLYCLOSE(vp->vnodeIndex[vLarge].handle);
@@ -4788,13 +4867,17 @@ VReleaseVolumeHandles_r(Volume * vp)
     VolState state_save;
 
     state_save = VChangeState_r(vp, VOL_STATE_DETACHING);
+
+    VOL_UNLOCK;
 #endif
 
-    /* XXX need to investigate whether we can perform
-     * DFlushVolume outside of vol_glock_mutex... */
     DFlushVolume(vp->hashid);
 
-    VReleaseVnodeFiles_r(vp); /* releases the glock internally */
+#ifdef AFS_DEMAND_ATTACH_FS
+    VOL_LOCK;
+#endif
+
+    VReleaseVnodeFiles_r(vp); /* DAFS: releases the glock internally */
 
 #ifdef AFS_DEMAND_ATTACH_FS
     VOL_UNLOCK;
@@ -4805,9 +4888,9 @@ VReleaseVolumeHandles_r(Volume * vp)
        IH_CONDSYNC(vp->vnodeIndex[vLarge].handle);
        IH_CONDSYNC(vp->vnodeIndex[vSmall].handle);
        IH_CONDSYNC(vp->diskDataHandle);
-#ifdef AFS_NT40_ENV
+#ifdef AFS_NAMEI_ENV
        IH_CONDSYNC(vp->linkHandle);
-#endif /* AFS_NT40_ENV */
+#endif /* AFS_NAMEI_ENV */
     }
 
     IH_RELEASE(vp->vnodeIndex[vLarge].handle);
@@ -4843,10 +4926,20 @@ VUpdateVolume_r(Error * ec, Volume * vp, int flags)
 #endif
 
     *ec = 0;
-    if (programType == fileServer)
-       V_uniquifier(vp) =
-           (V_inUse(vp) ? V_nextVnodeUnique(vp) +
-            200 : V_nextVnodeUnique(vp));
+    if (programType == fileServer) {
+       if (!V_inUse(vp)) {
+           V_uniquifier(vp) = V_nextVnodeUnique(vp);
+       } else {
+           V_uniquifier(vp) =
+               V_nextVnodeUnique(vp) + VOLUME_UPDATE_UNIQUIFIER_BUMP;
+           if (V_uniquifier(vp) < V_nextVnodeUnique(vp)) {
+               /* uniquifier rolled over; reset the counters */
+               V_nextVnodeUnique(vp) = 2;      /* 1 is reserved for the root vnode */
+               V_uniquifier(vp) =
+                   V_nextVnodeUnique(vp) + VOLUME_UPDATE_UNIQUIFIER_BUMP;
+           }
+       }
+    }
 
 #ifdef AFS_DEMAND_ATTACH_FS
     state_save = VChangeState_r(vp, VOL_STATE_UPDATING);
@@ -4864,8 +4957,8 @@ VUpdateVolume_r(Error * ec, Volume * vp, int flags)
 #endif
 
     if (*ec) {
-       Log("VUpdateVolume: error updating volume header, volume %u (%s)\n",
-           V_id(vp), V_name(vp));
+       Log("VUpdateVolume: error updating volume header, volume %" AFS_VOLID_FMT " (%s)\n",
+           afs_printable_VolumeId_lu(V_id(vp)), V_name(vp));
        /* try to update on-disk header,
         * while preventing infinite recursion */
        if (!(flags & VOL_UPDATE_NOFORCEOFF)) {
@@ -4902,9 +4995,9 @@ VSyncVolume_r(Error * ec, Volume * vp, int flags)
        VOL_UNLOCK;
 #endif
        fdP = IH_OPEN(V_diskDataHandle(vp));
-       osi_Assert(fdP != NULL);
+       opr_Assert(fdP != NULL);
        code = FDH_SYNC(fdP);
-       osi_Assert(code == 0);
+       opr_Assert(code == 0);
        FDH_CLOSE(fdP);
 #ifdef AFS_DEMAND_ATTACH_FS
        VOL_LOCK;
@@ -4988,15 +5081,15 @@ VCheckDetach(Volume * vp)
            V_inUse(vp) = 0;
            VUpdateVolume_r(&ec, vp, VOL_UPDATE_NOFORCEOFF);
            if (ec) {
-               Log("VCheckDetach: volume header update for volume %u "
-                   "failed with errno %d\n", vp->hashid, errno);
+               Log("VCheckDetach: volume header update for volume %" AFS_VOLID_FMT " "
+                   "failed with errno %d\n", afs_printable_VolumeId_lu(vp->hashid), errno);
            }
        }
        VReleaseVolumeHandles_r(vp);
        VCheckSalvage(vp);
        ReallyFreeVolume(vp);
        if (programType == fileServer) {
-           CV_BROADCAST(&vol_put_volume_cond);
+           opr_cv_broadcast(&vol_put_volume_cond);
        }
     }
     return ret;
@@ -5022,15 +5115,15 @@ VCheckDetach(Volume * vp)
            V_inUse(vp) = 0;
            VUpdateVolume_r(&ec, vp, VOL_UPDATE_NOFORCEOFF);
            if (ec) {
-               Log("VCheckDetach: volume header update for volume %u failed with errno %d\n",
-                   vp->hashid, errno);
+               Log("VCheckDetach: volume header update for volume %" AFS_VOLID_FMT " failed with errno %d\n",
+                   afs_printable_VolumeId_lu(vp->hashid), errno);
            }
        }
        VReleaseVolumeHandles_r(vp);
        ReallyFreeVolume(vp);
        if (programType == fileServer) {
 #if defined(AFS_PTHREAD_ENV)
-           CV_BROADCAST(&vol_put_volume_cond);
+           opr_cv_broadcast(&vol_put_volume_cond);
 #else /* AFS_PTHREAD_ENV */
            LWP_NoYieldSignal(VPutVolume);
 #endif /* AFS_PTHREAD_ENV */
@@ -5050,8 +5143,8 @@ VCheckOffline(Volume * vp)
 
     if (vp->goingOffline && !vp->nUsers) {
        Error error;
-       osi_Assert(programType == fileServer);
-       osi_Assert((V_attachState(vp) != VOL_STATE_ATTACHED) &&
+       opr_Assert(programType == fileServer);
+       opr_Assert((V_attachState(vp) != VOL_STATE_ATTACHED) &&
               (V_attachState(vp) != VOL_STATE_FREED) &&
               (V_attachState(vp) != VOL_STATE_PREATTACHED) &&
               (V_attachState(vp) != VOL_STATE_UNATTACHED) &&
@@ -5111,7 +5204,7 @@ VCheckOffline(Volume * vp)
 
     if (vp->goingOffline && !vp->nUsers) {
        Error error;
-       osi_Assert(programType == fileServer);
+       opr_Assert(programType == fileServer);
 
        ret = 1;
        vp->goingOffline = 0;
@@ -5130,7 +5223,7 @@ VCheckOffline(Volume * vp)
        }
        FreeVolumeHeader(vp);
 #ifdef AFS_PTHREAD_ENV
-       CV_BROADCAST(&vol_put_volume_cond);
+       opr_cv_broadcast(&vol_put_volume_cond);
 #else /* AFS_PTHREAD_ENV */
        LWP_NoYieldSignal(VPutVolume);
 #endif /* AFS_PTHREAD_ENV */
@@ -5181,7 +5274,7 @@ VCheckOffline(Volume * vp)
 void
 VCancelReservation_r(Volume * vp)
 {
-    osi_Assert(--vp->nWaiters >= 0);
+    opr_Verify(--vp->nWaiters >= 0);
     if (vp->nWaiters == 0) {
        VCheckOffline(vp);
        if (!VCheckDetach(vp)) {
@@ -5237,8 +5330,8 @@ VRegisterVolOp_r(Volume * vp, FSSYNC_VolOp_info * vopinfo)
     FSSYNC_VolOp_info * info;
 
     /* attach a vol op info node to the volume struct */
-    info = (FSSYNC_VolOp_info *) malloc(sizeof(FSSYNC_VolOp_info));
-    osi_Assert(info != NULL);
+    info = malloc(sizeof(FSSYNC_VolOp_info));
+    opr_Assert(info != NULL);
     memcpy(info, vopinfo, sizeof(FSSYNC_VolOp_info));
     vp->pending_vol_op = info;
 
@@ -5434,8 +5527,11 @@ VOfflineForSalvage_r(struct Volume *vp)
  * @param[in] vp   pointer to volume object
  *
  * @return status code
- *    @retval 0 no salvage scheduled
- *    @retval 1 a salvage has been scheduled with the salvageserver
+ *    @retval VCHECK_SALVAGE_OK (0)         no pending salvage
+ *    @retval VCHECK_SALVAGE_SCHEDULED (1)  salvage has been scheduled
+ *    @retval VCHECK_SALVAGE_ASYNC (2)      salvage being scheduled
+ *    @retval VCHECK_SALVAGE_DENIED (3)     salvage not scheduled; denied
+ *    @retval VCHECK_SALVAGE_FAIL (4)       salvage not scheduled; failed
  *
  * @pre VOL_LOCK is held
  *
@@ -5455,31 +5551,32 @@ VOfflineForSalvage_r(struct Volume *vp)
 static int
 VCheckSalvage(Volume * vp)
 {
-    int ret = 0;
+    int ret = VCHECK_SALVAGE_OK;
+
 #if defined(SALVSYNC_BUILD_CLIENT) || defined(FSSYNC_BUILD_CLIENT)
-    if (vp->nUsers)
-       return ret;
     if (!vp->salvage.requested) {
-       return ret;
+       return VCHECK_SALVAGE_OK;
+    }
+    if (vp->nUsers) {
+       return VCHECK_SALVAGE_ASYNC;
     }
 
     /* prevent recursion; some of the code below creates and removes
      * lightweight refs, which can call VCheckSalvage */
     if (vp->salvage.scheduling) {
-       return ret;
+       return VCHECK_SALVAGE_ASYNC;
     }
     vp->salvage.scheduling = 1;
 
     if (V_attachState(vp) == VOL_STATE_SALVAGE_REQ) {
        if (!VOfflineForSalvage_r(vp)) {
            vp->salvage.scheduling = 0;
-           return ret;
+           return VCHECK_SALVAGE_FAIL;
        }
     }
 
     if (vp->salvage.requested) {
-       VScheduleSalvage_r(vp);
-       ret = 1;
+       ret = VScheduleSalvage_r(vp);
     }
     vp->salvage.scheduling = 0;
 #endif /* SALVSYNC_BUILD_CLIENT || FSSYNC_BUILD_CLIENT */
@@ -5495,8 +5592,8 @@ VCheckSalvage(Volume * vp)
  * @param[in]  flags   see flags note below
  *
  * @note flags:
- *       VOL_SALVAGE_INVALIDATE_HEADER causes volume header cache entry
- *                                     to be invalidated.
+ *       VOL_SALVAGE_NO_OFFLINE do not need to wait to offline the volume; it has
+ *                              not been fully attached
  *
  * @pre VOL_LOCK is held.
  *
@@ -5569,9 +5666,13 @@ VRequestSalvage_r(Error * ec, Volume * vp, int reason, int flags)
                    VOfflineForSalvage_r(vp);
                }
            }
+           /* If we are non-fileserver, we're telling the fileserver to
+            * salvage the vol, so we don't need to give it back separately. */
+           vp->needsPutBack = 0;
+
            *ec = VSALVAGING;
        } else {
-           Log("VRequestSalvage: volume %u online salvaged too many times; forced offline.\n", vp->hashid);
+           Log("VRequestSalvage: volume %" AFS_VOLID_FMT " online salvaged too many times; forced offline.\n", afs_printable_VolumeId_lu(vp->hashid));
 
            /* make sure neither VScheduleSalvage_r nor
             * VUpdateSalvagePriority_r try to schedule another salvage */
@@ -5581,13 +5682,35 @@ VRequestSalvage_r(Error * ec, Volume * vp, int reason, int flags)
            *ec = VSALVAGE;
            code = 1;
        }
-       if (flags & VOL_SALVAGE_INVALIDATE_HEADER) {
-           /* Instead of ReleaseVolumeHeader, we do FreeVolumeHeader()
-               so that the the next VAttachVolumeByVp_r() invocation
-               of attach2() will pull in a cached header
-               entry and fail, then load a fresh one from disk and attach
-               it to the volume.
-           */
+       if ((flags & VOL_SALVAGE_NO_OFFLINE)) {
+           /* Here, we free the header for the volume, but make sure to only
+            * do this if VOL_SALVAGE_NO_OFFLINE is specified. The reason for
+            * this requires a bit of explanation.
+            *
+            * Normally, the volume header will be freed when the volume goes
+            * goes offline. However, if VOL_SALVAGE_NO_OFFLINE has been
+            * specified, the volume was in the process of being attached when
+            * we discovered that it needed salvaging. Thus, the volume will
+            * never go offline, since it never went fully online in the first
+            * place. Specifically, we do not call VOfflineForSalvage_r above,
+            * and we never get rid of the volume via VPutVolume_r; the volume
+            * has not been initialized enough for those to work.
+            *
+            * So instead, explicitly free the volume header here. If we do not
+            * do this, we are wasting a header that some other volume could be
+            * using, since the header remains attached to the volume. Also if
+            * we do not free the header here, we end up with a volume where
+            * nUsers == 0, but the volume has a header that is not on the
+            * header LRU. Some code expects that all nUsers == 0 volumes have
+            * their header on the header LRU (or have no header).
+            *
+            * Also note that we must not free the volume header here if
+            * VOL_SALVAGE_NO_OFFLINE is not set. Since, if
+            * VOL_SALVAGE_NO_OFFLINE is not set, someone else may have a
+            * reference to this volume, and they assume they can use the
+            * volume's header. If we free the volume out from under them, they
+            * can easily segfault.
+            */
            FreeVolumeHeader(vp);
        }
     }
@@ -5664,8 +5787,8 @@ static_inline int
 try_SALVSYNC(Volume *vp, char *partName, int *code) {
 #ifdef SALVSYNC_BUILD_CLIENT
     if (VCanUseSALVSYNC()) {
-       Log("Scheduling salvage for volume %lu on part %s over SALVSYNC\n",
-           afs_printable_uint32_lu(vp->hashid), partName);
+       Log("Scheduling salvage for volume %" AFS_VOLID_FMT " on part %s over SALVSYNC\n",
+           afs_printable_VolumeId_lu(vp->hashid), partName);
 
        /* can't use V_id() since there's no guarantee
         * we have the disk data header at this point */
@@ -5685,8 +5808,8 @@ static_inline int
 try_FSSYNC(Volume *vp, char *partName, int *code) {
 #ifdef FSSYNC_BUILD_CLIENT
     if (VCanUseFSSYNC()) {
-       Log("Scheduling salvage for volume %lu on part %s over FSSYNC\n",
-           afs_printable_uint32_lu(vp->hashid), partName);
+       Log("Scheduling salvage for volume %" AFS_VOLID_FMT " on part %s over FSSYNC\n",
+           afs_printable_VolumeId_lu(vp->hashid), partName);
 
        /*
         * If we aren't the fileserver, tell the fileserver the volume
@@ -5709,8 +5832,11 @@ try_FSSYNC(Volume *vp, char *partName, int *code) {
  * @param[in] vp  pointer to volume object
  *
  * @return operation status
- *    @retval 0 salvage scheduled successfully
- *    @retval 1 salvage not scheduled, or SALVSYNC/FSSYNC com error
+ *    @retval VCHECK_SALVAGE_OK (0)         no pending salvage
+ *    @retval VCHECK_SALVAGE_SCHEDULED (1)  salvage has been scheduled
+ *    @retval VCHECK_SALVAGE_ASYNC (2)      salvage being scheduled
+ *    @retval VCHECK_SALVAGE_DENIED (3)     salvage not scheduled; denied
+ *    @retval VCHECK_SALVAGE_FAIL (4)       salvage not scheduled; failed
  *
  * @pre
  *    @arg VOL_LOCK is held.
@@ -5733,21 +5859,22 @@ try_FSSYNC(Volume *vp, char *partName, int *code) {
 static int
 VScheduleSalvage_r(Volume * vp)
 {
-    int ret=0;
+    int ret = VCHECK_SALVAGE_SCHEDULED;
     int code = 0;
     VolState state_save;
     VThreadOptions_t * thread_opts;
     char partName[16];
 
-    osi_Assert(VCanUseSALVSYNC() || VCanUseFSSYNC());
+    opr_Verify(VCanUseSALVSYNC() || VCanUseFSSYNC());
 
     if (vp->nWaiters || vp->nUsers) {
-       return 1;
+       return VCHECK_SALVAGE_ASYNC;
     }
 
     /* prevent endless salvage,attach,salvage,attach,... loops */
-    if (vp->stats.salvages >= SALVAGE_COUNT_MAX)
-       return 1;
+    if (vp->stats.salvages >= SALVAGE_COUNT_MAX) {
+       return VCHECK_SALVAGE_FAIL;
+    }
 
     /*
      * don't perform salvsync ops on certain threads
@@ -5757,11 +5884,11 @@ VScheduleSalvage_r(Volume * vp)
        thread_opts = &VThread_defaults;
     }
     if (thread_opts->disallow_salvsync || vol_disallow_salvsync) {
-       return 1;
+       return VCHECK_SALVAGE_ASYNC;
     }
 
     if (vp->salvage.scheduled) {
-       return ret;
+       return VCHECK_SALVAGE_SCHEDULED;
     }
 
     VCreateReservation_r(vp);
@@ -5771,23 +5898,26 @@ VScheduleSalvage_r(Volume * vp)
      * XXX the scheduling process should really be done asynchronously
      *     to avoid fssync deadlocks
      */
-    if (!vp->salvage.scheduled) {
+    if (vp->salvage.scheduled) {
+       ret = VCHECK_SALVAGE_SCHEDULED;
+    } else {
        /* if we haven't previously scheduled a salvage, do so now
         *
         * set the volume to an exclusive state and drop the lock
         * around the SALVSYNC call
         */
-       strlcpy(partName, VPartitionPath(vp->partition), sizeof(partName));
+       strlcpy(partName, vp->partition->name, sizeof(partName));
        state_save = VChangeState_r(vp, VOL_STATE_SALVSYNC_REQ);
        VOL_UNLOCK;
 
-       osi_Assert(try_SALVSYNC(vp, partName, &code) ||
-              try_FSSYNC(vp, partName, &code));
+       opr_Verify(try_SALVSYNC(vp, partName, &code)
+                  || try_FSSYNC(vp, partName, &code));
 
        VOL_LOCK;
        VChangeState_r(vp, state_save);
 
        if (code == SYNC_OK) {
+           ret = VCHECK_SALVAGE_SCHEDULED;
            vp->salvage.scheduled = 1;
            vp->stats.last_salvage_req = FT_ApproxTime();
            if (VCanUseSALVSYNC()) {
@@ -5797,19 +5927,26 @@ VScheduleSalvage_r(Volume * vp)
                IncUInt64(&VStats.salvages);
            }
        } else {
-           ret = 1;
            switch(code) {
            case SYNC_BAD_COMMAND:
            case SYNC_COM_ERROR:
+               ret = VCHECK_SALVAGE_FAIL;
                break;
            case SYNC_DENIED:
-               Log("VScheduleSalvage_r: Salvage request for volume %lu "
-                   "denied\n", afs_printable_uint32_lu(vp->hashid));
+               ret = VCHECK_SALVAGE_DENIED;
+               Log("VScheduleSalvage_r: Salvage request for volume %" AFS_VOLID_FMT " "
+                   "denied\n", afs_printable_VolumeId_lu(vp->hashid));
+               break;
+           case SYNC_FAILED:
+               ret = VCHECK_SALVAGE_FAIL;
+               Log("VScheduleSalvage_r: Salvage request for volume %" AFS_VOLID_FMT " "
+                   "failed\n", afs_printable_VolumeId_lu(vp->hashid));
                break;
            default:
-               Log("VScheduleSalvage_r: Salvage request for volume %lu "
+               ret = VCHECK_SALVAGE_FAIL;
+               Log("VScheduleSalvage_r: Salvage request for volume %" AFS_VOLID_FMT " "
                    "received unknown protocol error %d\n",
-                   afs_printable_uint32_lu(vp->hashid), code);
+                   afs_printable_VolumeId_lu(vp->hashid), code);
                break;
            }
 
@@ -5825,7 +5962,7 @@ VScheduleSalvage_r(Volume * vp)
      * this, as the caller may reference vp without any refs. Instead, it
      * is the duty of the caller to inspect 'vp' after we return to see if
      * needs to be freed. */
-    osi_Assert(--vp->nWaiters >= 0);
+    opr_Verify(--vp->nWaiters >= 0);
     return ret;
 }
 #endif /* SALVSYNC_BUILD_CLIENT || FSSYNC_BUILD_CLIENT */
@@ -6041,7 +6178,7 @@ int
 VConnectFS_r(void)
 {
     int rc;
-    osi_Assert((VInit == 2) &&
+    opr_Assert((VInit == 2) &&
           (programType != fileServer) &&
           (programType != salvager));
     rc = FSYNC_clientInit();
@@ -6070,7 +6207,7 @@ VConnectFS_r(void)
 void
 VDisconnectFS_r(void)
 {
-    osi_Assert((programType != fileServer) &&
+    opr_Assert((programType != fileServer) &&
           (programType != salvager));
     FSYNC_clientFinis();
     VSetVInit_r(2);
@@ -6155,6 +6292,25 @@ VChildProcReconnectFS(void)
 /* volume bitmap routines                          */
 /***************************************************/
 
+/*
+ * Grow the bitmap by the defined increment
+ */
+void
+VGrowBitmap(struct vnodeIndex *index)
+{
+    byte *bp;
+
+    bp = realloc(index->bitmap, index->bitmapSize + VOLUME_BITMAP_GROWSIZE);
+    osi_Assert(bp != NULL);
+    index->bitmap = bp;
+    bp += index->bitmapSize;
+    memset(bp, 0, VOLUME_BITMAP_GROWSIZE);
+    index->bitmapOffset = index->bitmapSize;
+    index->bitmapSize += VOLUME_BITMAP_GROWSIZE;
+
+    return;
+}
+
 /**
  * allocate a vnode bitmap number for the vnode
  *
@@ -6237,7 +6393,7 @@ VAllocBitmapEntry_r(Error * ec, Volume * vp,
                VGetBitmap_r(ec, vp, i);
                if (*ec) {
 #ifdef AFS_DEMAND_ATTACH_FS
-                   VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, VOL_SALVAGE_INVALIDATE_HEADER);
+                   VRequestSalvage_r(ec, vp, SALVSYNC_ERROR, 0 /*flags*/);
 #else /* AFS_DEMAND_ATTACH_FS */
                    DeleteVolumeFromHashTable(vp);
                    vp->shuttingDown = 1;       /* Let who has it free it. */
@@ -6265,7 +6421,7 @@ VAllocBitmapEntry_r(Error * ec, Volume * vp,
            index->bitmapOffset = (afs_uint32) (bp - index->bitmap);
            while (*bp == 0xff)
                bp++;
-           o = ffs(~*bp) - 1;  /* ffs is documented in BSTRING(3) */
+           o = opr_ffs(~*bp) - 1;
            *bp |= (1 << o);
            ret = ((bp - index->bitmap) * 8 + o);
 #ifdef AFS_DEMAND_ATTACH_FS
@@ -6276,14 +6432,8 @@ VAllocBitmapEntry_r(Error * ec, Volume * vp,
        bp += sizeof(bit32) /* i.e. 4 */ ;
     }
     /* No bit map entry--must grow bitmap */
-    bp = (byte *)
-       realloc(index->bitmap, index->bitmapSize + VOLUME_BITMAP_GROWSIZE);
-    osi_Assert(bp != NULL);
-    index->bitmap = bp;
-    bp += index->bitmapSize;
-    memset(bp, 0, VOLUME_BITMAP_GROWSIZE);
-    index->bitmapOffset = index->bitmapSize;
-    index->bitmapSize += VOLUME_BITMAP_GROWSIZE;
+    VGrowBitmap(index);
+    bp = index->bitmap + index->bitmapOffset;
     *bp = 1;
     ret = index->bitmapOffset * 8;
 #ifdef AFS_DEMAND_ATTACH_FS
@@ -6311,32 +6461,52 @@ VAllocBitmapEntry(Error * ec, Volume * vp, struct vnodeIndex * index)
 }
 
 void
-VFreeBitMapEntry_r(Error * ec, struct vnodeIndex *index,
-                  unsigned bitNumber)
+VFreeBitMapEntry_r(Error * ec, Volume *vp, struct vnodeIndex *index,
+                  unsigned bitNumber, int flags)
 {
     unsigned int offset;
 
     *ec = 0;
+
+#ifdef AFS_DEMAND_ATTACH_FS
+    if (flags & VOL_FREE_BITMAP_WAIT) {
+       /* VAllocBitmapEntry_r allocs bitmap entries under an exclusive volume
+        * state, so ensure we're not in an exclusive volume state when we update
+        * the bitmap */
+       VCreateReservation_r(vp);
+       VWaitExclusiveState_r(vp);
+    }
+#endif
+
 #ifdef BITMAP_LATER
     if (!index->bitmap)
-       return;
+       goto done;
 #endif /* BITMAP_LATER */
+
     offset = bitNumber >> 3;
     if (offset >= index->bitmapSize) {
        *ec = VNOVNODE;
-       return;
+       goto done;
     }
     if (offset < index->bitmapOffset)
        index->bitmapOffset = offset & ~3;      /* Truncate to nearest bit32 */
     *(index->bitmap + offset) &= ~(1 << (bitNumber & 0x7));
+
+ done:
+#ifdef AFS_DEMAND_ATTACH_FS
+    if (flags & VOL_FREE_BITMAP_WAIT) {
+       VCancelReservation_r(vp);
+    }
+#endif
+    return; /* make the compiler happy for non-DAFS */
 }
 
 void
-VFreeBitMapEntry(Error * ec, struct vnodeIndex *index,
+VFreeBitMapEntry(Error * ec, Volume *vp, struct vnodeIndex *index,
                 unsigned bitNumber)
 {
     VOL_LOCK;
-    VFreeBitMapEntry_r(ec, index, bitNumber);
+    VFreeBitMapEntry_r(ec, vp, index, bitNumber, VOL_FREE_BITMAP_WAIT);
     VOL_UNLOCK;
 }
 
@@ -6370,13 +6540,13 @@ VGetBitmap_r(Error * ec, Volume * vp, VnodeClass class)
     VOL_UNLOCK;
 
     fdP = IH_OPEN(vip->handle);
-    osi_Assert(fdP != NULL);
+    opr_Assert(fdP != NULL);
     file = FDH_FDOPEN(fdP, "r");
-    osi_Assert(file != NULL);
-    vnode = (VnodeDiskObject *) malloc(vcp->diskSize);
-    osi_Assert(vnode != NULL);
+    opr_Assert(file != NULL);
+    vnode = malloc(vcp->diskSize);
+    opr_Assert(vnode != NULL);
     size = OS_SIZE(fdP->fd_fd);
-    osi_Assert(size != -1);
+    opr_Assert(size != -1);
     nVnodes = (size <= vcp->diskSize ? 0 : size - vcp->diskSize)
        >> vcp->logSize;
     vip->bitmapSize = ((nVnodes / 8) + 10) / 4 * 4;    /* The 10 is a little extra so
@@ -6386,10 +6556,10 @@ VGetBitmap_r(Error * ec, Volume * vp, VnodeClass class)
                                                         * it that way */
 #ifdef BITMAP_LATER
     BitMap = (byte *) calloc(1, vip->bitmapSize);
-    osi_Assert(BitMap != NULL);
+    opr_Assert(BitMap != NULL);
 #else /* BITMAP_LATER */
     vip->bitmap = (byte *) calloc(1, vip->bitmapSize);
-    osi_Assert(vip->bitmap != NULL);
+    opr_Assert(vip->bitmap != NULL);
     vip->bitmapOffset = 0;
 #endif /* BITMAP_LATER */
     if (STREAM_ASEEK(file, vcp->diskSize) != -1) {
@@ -6440,7 +6610,7 @@ VGetBitmap_r(Error * ec, Volume * vp, VnodeClass class)
        vip->bitmap = BitMap;
        vip->bitmapOffset = 0;
     } else
-       free((byte *) BitMap);
+       free(BitMap);
 #endif /* BITMAP_LATER */
 #ifdef AFS_DEMAND_ATTACH_FS
     VChangeState_r(vp, state_save);
@@ -6472,7 +6642,7 @@ VGetBitmap_r(Error * ec, Volume * vp, VnodeClass class)
  *
  */
 void
-VGetVolumePath(Error * ec, VolId volumeId, char **partitionp, char **namep)
+VGetVolumePath(Error * ec, VolumeId volumeId, char **partitionp, char **namep)
 {
     static char partition[VMAXPATHLEN], name[VMAXPATHLEN];
     char path[VMAXPATHLEN];
@@ -6480,10 +6650,11 @@ VGetVolumePath(Error * ec, VolId volumeId, char **partitionp, char **namep)
     struct DiskPartition64 *dp;
 
     *ec = 0;
-    name[0] = '/';
-    (void)afs_snprintf(&name[1], (sizeof name) - 1, VFORMAT, afs_printable_uint32_lu(volumeId));
+    name[0] = OS_DIRSEPC;
+    snprintf(&name[1], (sizeof name) - 1, VFORMAT,
+            afs_printable_VolumeId_lu(volumeId));
     for (dp = DiskPartitionList; dp; dp = dp->next) {
-       struct afs_stat status;
+       struct afs_stat_st status;
        strcpy(path, VPartitionPath(dp));
        strcat(path, name);
        if (afs_stat(path, &status) == 0) {
@@ -6509,16 +6680,16 @@ VGetVolumePath(Error * ec, VolId volumeId, char **partitionp, char **namep)
  * @return volume number
  *
  * @note the string must be of the form VFORMAT.  the only permissible
- *       deviation is a leading '/' character.
+ *       deviation is a leading OS_DIRSEPC character.
  *
  * @see VFORMAT
  */
 int
 VolumeNumber(char *name)
 {
-    if (*name == '/')
+    if (*name == OS_DIRSEPC)
        name++;
-    return atoi(name + 1);
+    return strtoul(name + 1, NULL, 10);
 }
 
 /**
@@ -6544,7 +6715,7 @@ char *
 VolumeExternalName(VolumeId volumeId)
 {
     static char name[VMAXPATHLEN];
-    (void)afs_snprintf(name, sizeof name, VFORMAT, afs_printable_uint32_lu(volumeId));
+    snprintf(name, sizeof name, VFORMAT, afs_printable_VolumeId_lu(volumeId));
     return name;
 }
 
@@ -6565,7 +6736,7 @@ VolumeExternalName(VolumeId volumeId)
 int
 VolumeExternalName_r(VolumeId volumeId, char * name, size_t len)
 {
-    return afs_snprintf(name, len, VFORMAT, afs_printable_uint32_lu(volumeId));
+    return snprintf(name, len, VFORMAT, afs_printable_VolumeId_lu(volumeId));
 }
 
 
@@ -6573,11 +6744,7 @@ VolumeExternalName_r(VolumeId volumeId, char * name, size_t len)
 /* Volume Usage Statistics routines                */
 /***************************************************/
 
-#if OPENAFS_VOL_STATS
 #define OneDay (86400)         /* 24 hours' worth of seconds */
-#else
-#define OneDay (24*60*60)      /* 24 hours */
-#endif /* OPENAFS_VOL_STATS */
 
 static time_t
 Midnight(time_t t) {
@@ -6648,14 +6815,12 @@ VAdjustVolumeStatistics_r(Volume * vp)
        V_dayUse(vp) = 0;
        V_dayUseDate(vp) = Midnight(now);
 
-#if OPENAFS_VOL_STATS
        /*
         * All we need to do is bzero the entire VOL_STATS_BYTES of
         * the detailed volume statistics area.
         */
        memset((V_stat_area(vp)), 0, VOL_STATS_BYTES);
-#endif /* OPENAFS_VOL_STATS */
-    }
+       }
 
     /*It's been more than a day of collection */
     /*
@@ -6682,10 +6847,16 @@ VBumpVolumeUsage_r(Volume * vp)
     if (now - V_dayUseDate(vp) > OneDay)
        VAdjustVolumeStatistics_r(vp);
     /*
-     * Save the volume header image to disk after every 128 bumps to dayUse.
+     * Save the volume header image to disk after a threshold of bumps to dayUse,
+     * at most every usage_rate_limit seconds.
      */
-    if ((V_dayUse(vp)++ & 127) == 0) {
+    V_dayUse(vp)++;
+    vp->usage_bumps_outstanding++;
+    if (vp->usage_bumps_outstanding >= vol_opts.usage_threshold
+       && vp->usage_bumps_next_write <= now) {
        Error error;
+       vp->usage_bumps_outstanding = 0;
+       vp->usage_bumps_next_write = now + vol_opts.usage_rate_limit;
        VUpdateVolume_r(&error, vp, VOL_UPDATE_WAIT);
     }
 }
@@ -6773,7 +6944,7 @@ VAddToVolumeUpdateList_r(Error * ec, Volume * vp)
        return;
     if (UpdateList == NULL) {
        updateSize = UPDATE_LIST_SIZE;
-       UpdateList = (VolumeId *) malloc(sizeof(VolumeId) * updateSize);
+       UpdateList = malloc(sizeof(VolumeId) * updateSize);
     } else {
        if (nUpdatedVolumes == updateSize) {
            updateSize <<= 1;
@@ -6781,12 +6952,11 @@ VAddToVolumeUpdateList_r(Error * ec, Volume * vp)
                Log("warning: there is likely a bug in the volume update scanner\n");
                return;
            }
-           UpdateList =
-               (VolumeId *) realloc(UpdateList,
-                                    sizeof(VolumeId) * updateSize);
+           UpdateList = realloc(UpdateList,
+                                sizeof(VolumeId) * updateSize);
        }
     }
-    osi_Assert(UpdateList != NULL);
+    opr_Assert(UpdateList != NULL);
     UpdateList[nUpdatedVolumes++] = V_id(vp);
 #endif /* !AFS_DEMAND_ATTACH_FS */
 }
@@ -7042,7 +7212,7 @@ VInitVLRU(void)
        queue_Init(&volume_LRU.q[i]);
        volume_LRU.q[i].len = 0;
        volume_LRU.q[i].busy = 0;
-       CV_INIT(&volume_LRU.q[i].cv, "vol lru", CV_DEFAULT, 0);
+       opr_cv_init(&volume_LRU.q[i].cv);
     }
 
     /* setup the timing constants */
@@ -7060,10 +7230,12 @@ VInitVLRU(void)
     /* start up the VLRU scanner */
     volume_LRU.scanner_state = VLRU_SCANNER_STATE_OFFLINE;
     if (programType == fileServer) {
-       CV_INIT(&volume_LRU.cv, "vol lru", CV_DEFAULT, 0);
-       osi_Assert(pthread_attr_init(&attrs) == 0);
-       osi_Assert(pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED) == 0);
-       osi_Assert(pthread_create(&tid, &attrs, &VLRU_ScannerThread, NULL) == 0);
+       opr_cv_init(&volume_LRU.cv);
+       opr_Verify(pthread_attr_init(&attrs) == 0);
+       opr_Verify(pthread_attr_setdetachstate(&attrs,
+                                              PTHREAD_CREATE_DETACHED) == 0);
+       opr_Verify(pthread_create(&tid, &attrs,
+                                 &VLRU_ScannerThread, NULL) == 0);
     }
 }
 
@@ -7089,7 +7261,7 @@ VLRU_Init_Node_r(Volume * vp)
     if (!VLRU_enabled)
        return;
 
-    osi_Assert(queue_IsNotOnQueue(&vp->vlru));
+    opr_Assert(queue_IsNotOnQueue(&vp->vlru));
     vp->vlru.idx = VLRU_QUEUE_INVALID;
 }
 
@@ -7233,7 +7405,7 @@ VLRU_UpdateAccess_r(Volume * vp)
     if (queue_IsNotOnQueue(&vp->vlru))
        return;
 
-    osi_Assert(V_attachFlags(vp) & VOL_ON_VLRU);
+    opr_Assert(V_attachFlags(vp) & VOL_ON_VLRU);
 
     /* update the access timestamp */
     vp->stats.last_get = FT_ApproxTime();
@@ -7366,7 +7538,7 @@ VLRU_ScannerThread(void * args)
        /* check to see if we've been asked to pause */
        if (volume_LRU.scanner_state == VLRU_SCANNER_STATE_PAUSING) {
            volume_LRU.scanner_state = VLRU_SCANNER_STATE_PAUSED;
-           CV_BROADCAST(&volume_LRU.cv);
+           opr_cv_broadcast(&volume_LRU.cv);
            do {
                VOL_CV_WAIT(&volume_LRU.cv);
            } while (volume_LRU.scanner_state == VLRU_SCANNER_STATE_PAUSED);
@@ -7442,7 +7614,7 @@ VLRU_ScannerThread(void * args)
 
     /* signal that scanner is down */
     volume_LRU.scanner_state = VLRU_SCANNER_STATE_OFFLINE;
-    CV_BROADCAST(&volume_LRU.cv);
+    opr_cv_broadcast(&volume_LRU.cv);
     VOL_UNLOCK;
     return NULL;
 }
@@ -7545,7 +7717,7 @@ VLRU_Demote_r(int idx)
     Volume ** salv_flag_vec = NULL;
     int salv_vec_offset = 0;
 
-    osi_Assert(idx == VLRU_QUEUE_MID || idx == VLRU_QUEUE_OLD);
+    opr_Assert(idx == VLRU_QUEUE_MID || idx == VLRU_QUEUE_OLD);
 
     /* get exclusive access to two chains, and drop the glock */
     VLRU_Wait_r(&volume_LRU.q[idx-1]);
@@ -7556,7 +7728,7 @@ VLRU_Demote_r(int idx)
 
     /* no big deal if this allocation fails */
     if (volume_LRU.q[idx].len) {
-       salv_flag_vec = (Volume **) malloc(volume_LRU.q[idx].len * sizeof(Volume *));
+       salv_flag_vec = malloc(volume_LRU.q[idx].len * sizeof(Volume *));
     }
 
     now = FT_ApproxTime();
@@ -7644,7 +7816,7 @@ VLRU_Scan_r(int idx)
     Volume * vp;
     int i, locked = 1;
 
-    osi_Assert(idx == VLRU_QUEUE_NEW || idx == VLRU_QUEUE_CANDIDATE);
+    opr_Assert(idx == VLRU_QUEUE_NEW || idx == VLRU_QUEUE_CANDIDATE);
 
     /* gain exclusive access to the idx VLRU */
     VLRU_Wait_r(&volume_LRU.q[idx]);
@@ -7735,7 +7907,7 @@ VCheckSoftDetachCandidate(Volume * vp, afs_uint32 thresh)
 
     idx = vp->vlru.idx;
 
-    osi_Assert(idx == VLRU_QUEUE_NEW);
+    opr_Assert(idx == VLRU_QUEUE_NEW);
 
     if (vp->stats.last_get <= thresh) {
        /* move to candidate pool */
@@ -7755,7 +7927,7 @@ VCheckSoftDetachCandidate(Volume * vp, afs_uint32 thresh)
 static void
 VLRU_BeginExclusive_r(struct VLRU_q * q)
 {
-    osi_Assert(q->busy == 0);
+    opr_Assert(q->busy == 0);
     q->busy = 1;
 }
 
@@ -7763,9 +7935,9 @@ VLRU_BeginExclusive_r(struct VLRU_q * q)
 static void
 VLRU_EndExclusive_r(struct VLRU_q * q)
 {
-    osi_Assert(q->busy);
+    opr_Assert(q->busy);
     q->busy = 0;
-    CV_BROADCAST(&q->cv);
+    opr_cv_broadcast(&q->cv);
 }
 
 /* wait for another thread to end exclusive access on VLRU */
@@ -7787,7 +7959,7 @@ VSoftDetachVolume_r(Volume * vp, afs_uint32 thresh)
     afs_uint32 ts_save;
     int ret = 0;
 
-    osi_Assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
+    opr_Assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
 
     ts_save = vp->stats.last_get;
     if (ts_save > thresh)
@@ -7842,7 +8014,7 @@ VSoftDetachVolume_r(Volume * vp, afs_uint32 thresh)
            vp = NULL;
        } else {
            /* pull it off the VLRU */
-           osi_Assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
+           opr_Assert(vp->vlru.idx == VLRU_QUEUE_CANDIDATE);
            volume_LRU.q[VLRU_QUEUE_CANDIDATE].len--;
            queue_Remove(&vp->vlru);
            vp->vlru.idx = VLRU_QUEUE_INVALID;
@@ -7904,7 +8076,7 @@ VInitVolumeHeaderCache(afs_uint32 howMany)
     volume_hdr_LRU.stats.used = howMany;
     volume_hdr_LRU.stats.attached = 0;
     hp = (struct volHeader *)(calloc(howMany, sizeof(struct volHeader)));
-    osi_Assert(hp != NULL);
+    opr_Assert(hp != NULL);
 
     while (howMany--)
        /* We are using ReleaseVolumeHeader to initialize the values on the header list
@@ -7913,6 +8085,51 @@ VInitVolumeHeaderCache(afs_uint32 howMany)
        ReleaseVolumeHeader(hp++);
 }
 
+/* get a volume header off of the volume header LRU.
+ *
+ * @return volume header
+ *  @retval NULL no usable volume header is available on the LRU
+ *
+ * @pre VOL_LOCK held
+ *
+ * @post for DAFS, if the returned header is associated with a volume, that
+ *       volume is NOT in an exclusive state
+ *
+ * @internal volume package internal use only.
+ */
+#ifdef AFS_DEMAND_ATTACH_FS
+static struct volHeader*
+GetVolHeaderFromLRU(void)
+{
+    struct volHeader *hd = NULL, *qh, *nqh;
+    /* Usually, a volume in an exclusive state will not have its header on
+     * the LRU. However, it is possible for this to occur when a salvage
+     * request is received over FSSYNC, and possibly in other corner cases.
+     * So just skip over headers whose volumes are in an exclusive state. We
+     * could VWaitExclusiveState_r instead, but not waiting is faster and
+     * easier to do */
+    for (queue_Scan(&volume_hdr_LRU, qh, nqh, volHeader)) {
+       if (!qh->back || !VIsExclusiveState(V_attachState(qh->back))) {
+           queue_Remove(qh);
+           hd = qh;
+           break;
+       }
+    }
+    return hd;
+}
+#else /* AFS_DEMAND_ATTACH_FS */
+static struct volHeader*
+GetVolHeaderFromLRU(void)
+{
+    struct volHeader *hd = NULL;
+    if (queue_IsNotEmpty(&volume_hdr_LRU)) {
+       hd = queue_First(&volume_hdr_LRU, volHeader);
+       queue_Remove(hd);
+    }
+    return hd;
+}
+#endif /* !AFS_DEMAND_ATTACH_FS */
+
 /**
  * get a volume header and attach it to the volume object.
  *
@@ -7961,8 +8178,8 @@ GetVolumeHeader(Volume * vp)
     if (programType != fileServer) {
        /* for volume utilities, we allocate volHeaders as needed */
        if (!vp->header) {
-           hd = (struct volHeader *)calloc(1, sizeof(*vp->header));
-           osi_Assert(hd != NULL);
+           hd = calloc(1, sizeof(*vp->header));
+           opr_Assert(hd != NULL);
            vp->header = hd;
            hd->back = vp;
 #ifdef AFS_DEMAND_ATTACH_FS
@@ -7976,21 +8193,17 @@ GetVolumeHeader(Volume * vp)
             * still available. pull it off the lru and return */
            hd = vp->header;
            queue_Remove(hd);
-           osi_Assert(hd->back == vp);
+           opr_Assert(hd->back == vp);
 #ifdef AFS_DEMAND_ATTACH_FS
             V_attachFlags(vp) &= ~(VOL_HDR_IN_LRU);
 #endif
        } else {
-           /* we need to grab a new element off the LRU */
-           if (queue_IsNotEmpty(&volume_hdr_LRU)) {
-               /* grab an element and pull off of LRU */
-               hd = queue_First(&volume_hdr_LRU, volHeader);
-               queue_Remove(hd);
-           } else {
+           hd = GetVolHeaderFromLRU();
+           if (!hd) {
                /* LRU is empty, so allocate a new volHeader
                 * this is probably indicative of a leak, so let the user know */
-               hd = (struct volHeader *)calloc(1, sizeof(struct volHeader));
-               osi_Assert(hd != NULL);
+               hd = calloc(1, sizeof(struct volHeader));
+               opr_Assert(hd != NULL);
                if (!everLogged) {
                    Log("****Allocated more volume headers, probably leak****\n");
                    everLogged = 1;
@@ -8003,9 +8216,9 @@ GetVolumeHeader(Volume * vp)
                 * be sync'd out to disk */
 
 #ifdef AFS_DEMAND_ATTACH_FS
-               /* if hd->back were in an exclusive state, then
-                * its volHeader would not be on the LRU... */
-               osi_Assert(!VIsExclusiveState(V_attachState(hd->back)));
+               /* GetVolHeaderFromLRU had better not give us back a header
+                * with a volume in exclusive state... */
+               opr_Assert(!VIsExclusiveState(V_attachState(hd->back)));
 #endif
 
                if (hd->diskstuff.inUse) {
@@ -8210,8 +8423,8 @@ VSetVolHashSize(int logsize)
     }
 
     if (!VInit) {
-        VolumeHashTable.Size = 1 << logsize;
-        VolumeHashTable.Mask = VolumeHashTable.Size - 1;
+        VolumeHashTable.Size = opr_jhash_size(logsize);
+        VolumeHashTable.Mask = opr_jhash_mask(logsize);
     } else {
        /* we can't yet support runtime modification of this
         * parameter. we'll need a configuration rwlock to
@@ -8235,12 +8448,12 @@ VInitVolumeHash(void)
 
     VolumeHashTable.Table = (VolumeHashChainHead *) calloc(VolumeHashTable.Size,
                                                           sizeof(VolumeHashChainHead));
-    osi_Assert(VolumeHashTable.Table != NULL);
+    opr_Assert(VolumeHashTable.Table != NULL);
 
     for (i=0; i < VolumeHashTable.Size; i++) {
        queue_Init(&VolumeHashTable.Table[i]);
 #ifdef AFS_DEMAND_ATTACH_FS
-       CV_INIT(&VolumeHashTable.Table[i].chain_busy_cv, "vhash busy", CV_DEFAULT, 0);
+       opr_cv_init(&VolumeHashTable.Table[i].chain_busy_cv);
 #endif /* AFS_DEMAND_ATTACH_FS */
     }
 }
@@ -8262,7 +8475,7 @@ VInitVolumeHash(void)
  *       asynchronous hash chain reordering to finish.
  */
 static void
-AddVolumeToHashTable(Volume * vp, int hashid)
+AddVolumeToHashTable(Volume * vp, VolumeId hashid)
 {
     VolumeHashChainHead * head;
 
@@ -8282,7 +8495,6 @@ AddVolumeToHashTable(Volume * vp, int hashid)
     head->len++;
     vp->hashid = hashid;
     queue_Append(head, vp);
-    vp->vnodeHashOffset = VolumeHashOffset_r();
 }
 
 /**
@@ -8357,7 +8569,7 @@ DeleteVolumeFromHashTable(Volume * vp)
  *       hint volume object.
  */
 Volume *
-VLookupVolume_r(Error * ec, VolId volumeId, Volume * hint)
+VLookupVolume_r(Error * ec, VolumeId volumeId, Volume * hint)
 {
     int looks = 0;
     Volume * vp, *np;
@@ -8387,7 +8599,7 @@ VLookupVolume_r(Error * ec, VolId volumeId, Volume * hint)
     /* search the chain for this volume id */
     for(queue_Scan(head, vp, np, Volume)) {
        looks++;
-       if ((vp->hashid == volumeId)) {
+       if (vp->hashid == volumeId) {
            break;
        }
     }
@@ -8509,7 +8721,7 @@ VReorderHash_r(VolumeHashChainHead * head, Volume * pp, Volume * vp)
 static void
 VHashBeginExclusive_r(VolumeHashChainHead * head)
 {
-    osi_Assert(head->busy == 0);
+    opr_Assert(head->busy == 0);
     head->busy = 1;
 }
 
@@ -8533,9 +8745,9 @@ VHashBeginExclusive_r(VolumeHashChainHead * head)
 static void
 VHashEndExclusive_r(VolumeHashChainHead * head)
 {
-    osi_Assert(head->busy);
+    opr_Assert(head->busy);
     head->busy = 0;
-    CV_BROADCAST(&head->chain_busy_cv);
+    opr_cv_broadcast(&head->chain_busy_cv);
 }
 
 /**
@@ -8673,7 +8885,7 @@ DeleteVolumeFromVByPList_r(Volume * vp)
 static void
 VVByPListBeginExclusive_r(struct DiskPartition64 * dp)
 {
-    osi_Assert(dp->vol_list.busy == 0);
+    opr_Assert(dp->vol_list.busy == 0);
     dp->vol_list.busy = 1;
 }
 
@@ -8697,9 +8909,9 @@ VVByPListBeginExclusive_r(struct DiskPartition64 * dp)
 static void
 VVByPListEndExclusive_r(struct DiskPartition64 * dp)
 {
-    osi_Assert(dp->vol_list.busy);
+    opr_Assert(dp->vol_list.busy);
     dp->vol_list.busy = 0;
-    CV_BROADCAST(&dp->vol_list.cv);
+    opr_cv_broadcast(&dp->vol_list.cv);
 }
 
 /**
@@ -8742,16 +8954,14 @@ VVByPListWait_r(struct DiskPartition64 * dp)
 void
 VPrintCacheStats_r(void)
 {
-    afs_uint32 get_hi, get_lo, load_hi, load_lo;
     struct VnodeClassInfo *vcp;
     vcp = &VnodeClassInfo[vLarge];
     Log("Large vnode cache, %d entries, %d allocs, %d gets (%d reads), %d writes\n", vcp->cacheSize, vcp->allocs, vcp->gets, vcp->reads, vcp->writes);
     vcp = &VnodeClassInfo[vSmall];
     Log("Small vnode cache,%d entries, %d allocs, %d gets (%d reads), %d writes\n", vcp->cacheSize, vcp->allocs, vcp->gets, vcp->reads, vcp->writes);
-    SplitInt64(VStats.hdr_gets, get_hi, get_lo);
-    SplitInt64(VStats.hdr_loads, load_hi, load_lo);
-    Log("Volume header cache, %d entries, %d gets, %d replacements\n",
-       VStats.hdr_cache_size, get_lo, load_lo);
+    Log("Volume header cache, %d entries, %"AFS_INT64_FMT" gets, "
+        "%"AFS_INT64_FMT" replacements\n",
+       VStats.hdr_cache_size, VStats.hdr_gets, VStats.hdr_loads);
 }
 
 void
@@ -8873,9 +9083,7 @@ VVLRUExtStats_r(struct VLRUExtStats * stats, afs_uint32 nvols)
 
 #define ENUMTOSTRING(en)  #en
 #define ENUMCASE(en) \
-    case en: \
-        return ENUMTOSTRING(en); \
-        break
+    case en: return ENUMTOSTRING(en)
 
 static char *
 vlru_idx_to_string(int idx)