vol: fix vutil format-truncation nit
[openafs.git] / src / vol / vutil.c
index d9c3246..d5b8d8d 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
+#include <afs/opr.h>
 
-#include <stdio.h>
-#include <sys/types.h>
-#include <errno.h>
-#ifdef AFS_NT40_ENV
-#include <time.h>
-#include <fcntl.h>
-#else
-#include <sys/time.h>
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
-#include <unistd.h>
 #endif
-#include <dirent.h>
-#include <sys/stat.h>
+
+#ifdef HAVE_SYS_LOCKF_H
+#include <sys/lockf.h>
+#endif
+
 #ifdef AFS_PTHREAD_ENV
-#include <assert.h>
-#else /* AFS_PTHREAD_ENV */
-#include <afs/assert.h>
-#endif /* AFS_PTHREAD_ENV */
+# include <opr/lock.h>
+#else
+# include <opr/lockstub.h>
+#endif
 
+#include <rx/rx_queue.h>
 #include <rx/xdr.h>
 #include <afs/afsint.h>
 #include "nfs.h"
@@ -48,7 +46,6 @@
 #include <afs/afsutil.h>
 #ifdef AFS_NT40_ENV
 #include "ntops.h"
-#include <io.h>
 #endif
 #include "vnode.h"
 #include "volume.h"
 #include "vol_prototypes.h"
 #include "common.h"
 
-#ifdef AFS_AIX_ENV
-#include <sys/lockf.h>
-#endif
-#if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV) || defined(AFS_LINUX20_ENV)
-#include <string.h>
-#else
-#include <strings.h>
+#ifndef AFS_NT40_ENV
+# ifdef O_LARGEFILE
+#  define AFS_SETLKW   F_SETLKW64
+#  define AFS_SETLK    F_SETLK64
+#  define afs_st_flock flock64
+# else
+#  define AFS_SETLKW   F_SETLKW
+#  define AFS_SETLK    F_SETLK
+#  define afs_st_flock flock
+# endif
 #endif
 
-#ifdef O_LARGEFILE
-#define afs_open       open64
-#else /* !O_LARGEFILE */
-#define afs_open       open
-#endif /* !O_LARGEFILE */
-
-
 /* Note:  the volume creation functions herein leave the destroyMe flag in the
    volume header ON:  this means that the volumes will not be attached by the
    file server and WILL BE DESTROYED the next time a system salvage is performed */
 
 #ifdef FSSYNC_BUILD_CLIENT
 static void
-RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId vid)
+RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId parent,
+             VolumeId vid)
 {
     int i;
     IHandle_t *handle;
@@ -90,17 +84,22 @@ RemoveInodes(struct afs_inode_info *stuff, Device dev, VolumeId vid)
     /* This relies on the fact that IDEC only needs the device and NT only
      * needs the dev and vid to decrement volume special files.
      */
-    IH_INIT(handle, dev, vid, -1);
+    IH_INIT(handle, dev, parent, -1);
     for (i = 0; i < MAXINODETYPE; i++) {
        Inode inode = *stuff[i].inode;
-       if (VALID_INO(inode))
-           IH_DEC(handle, inode, vid);
+       if (VALID_INO(inode)) {
+           if (stuff[i].inodeType == VI_LINKTABLE) {
+               IH_DEC(handle, inode, parent);
+           } else {
+               IH_DEC(handle, inode, vid);
+           }
+       }
     }
     IH_RELEASE(handle);
 }
 
 Volume *
-VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
+VCreateVolume(Error * ec, char *partname, VolumeId volumeId, VolumeId parentId)
 {                              /* Should be the same as volumeId if there is
                                 * no parent */
     Volume *retVal;
@@ -111,22 +110,23 @@ VCreateVolume(Error * ec, char *partname, VolId volumeId, VolId parentId)
 }
 
 Volume *
-VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
+VCreateVolume_r(Error * ec, char *partname, VolumeId volumeId, VolumeId parentId)
 {                              /* Should be the same as volumeId if there is
                                 * no parent */
     VolumeDiskData vol;
     int i, rc;
-    char headerName[VMAXPATHLEN], volumePath[VMAXPATHLEN];
+    char headerName[VMAXPATHLEN], volumePath[VMAXPATHLEN + 1];
     Device device;
     struct DiskPartition64 *partition;
     struct VolumeDiskHeader diskHeader;
     IHandle_t *handle;
     FdHandle_t *fdP;
-    Inode nearInode = 0;
+    Inode nearInode AFS_UNUSED = 0;
     char *part, *name;
     struct stat st;
     struct VolumeHeader tempHeader;
     struct afs_inode_info stuff[MAXINODETYPE];
+    afs_ino_str_t stmp;
 # ifdef AFS_DEMAND_ATTACH_FS
     int locktype = 0;
 # endif /* AFS_DEMAND_ATTACH_FS */
@@ -158,9 +158,13 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     if (*ec == VNOVOL || !strcmp(partition->name, part)) {
        /* this case is ok */
     } else {
-       /* return EXDEV if it's a clone to an alternate partition
+       /* return EXDEV if it's a clone or read-only to an alternate partition
         * otherwise assume it's a move */
        if (vol.parentId != vol.id) {
+           Log("VCreateVolume: volume %" AFS_VOLID_FMT " for parent %" AFS_VOLID_FMT
+               " found on %s; unable to create volume on %s.\n",
+               afs_printable_VolumeId_lu(vol.id),
+               afs_printable_VolumeId_lu(vol.parentId), part, partition->name);
            *ec = EXDEV;
            return NULL;
        }
@@ -191,9 +195,10 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     vol.stamp.magic = VOLUMEINFOMAGIC;
     vol.stamp.version = VOLUMEINFOVERSION;
     vol.destroyMe = DESTROY_ME;
-    (void)afs_snprintf(headerName, sizeof headerName, VFORMAT, afs_printable_uint32_lu(vol.id));
-    (void)afs_snprintf(volumePath, sizeof volumePath, "%s/%s",
-                      VPartitionPath(partition), headerName);
+    snprintf(headerName, sizeof headerName, VFORMAT,
+            afs_printable_VolumeId_lu(vol.id));
+    snprintf(volumePath, sizeof volumePath, "%s" OS_DIRSEP "%s",
+            VPartitionPath(partition), headerName);
     rc = stat(volumePath, &st);
     if (rc == 0 || errno != ENOENT) {
        if (rc == 0) {
@@ -224,11 +229,7 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
                IHandle_t *lh;
                int code;
 
-#ifdef AFS_NT40_ENV
-               *(p->inode) = nt_MakeSpecIno(VI_LINKTABLE);
-#else
                *(p->inode) = namei_MakeSpecIno(vol.parentId, VI_LINKTABLE);
-#endif
                IH_INIT(lh, device, parentId, *(p->inode));
                fdP = IH_OPEN(lh);
                if (fdP == NULL) {
@@ -254,7 +255,7 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
          bad:
            if (handle)
                IH_RELEASE(handle);
-           RemoveInodes(stuff, device, vol.id);
+           RemoveInodes(stuff, device, vol.parentId, vol.id);
            if (!*ec) {
                *ec = VNOVOL;
            }
@@ -271,19 +272,13 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
        fdP = IH_OPEN(handle);
        if (fdP == NULL) {
            Log("VCreateVolume:  Problem iopen inode %s (err=%d)\n",
-               PrintInode(NULL, *(p->inode)), errno);
-           goto bad;
-       }
-       if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
-           Log("VCreateVolume:  Problem lseek inode %s (err=%d)\n",
-               PrintInode(NULL, *(p->inode)), errno);
-           FDH_REALLYCLOSE(fdP);
+               PrintInode(stmp, *(p->inode)), errno);
            goto bad;
        }
-       if (FDH_WRITE(fdP, (char *)&p->stamp, sizeof(p->stamp)) !=
+       if (FDH_PWRITE(fdP, (char *)&p->stamp, sizeof(p->stamp), 0) !=
            sizeof(p->stamp)) {
-           Log("VCreateVolume:  Problem writing to  inode %s (err=%d)\n",
-               PrintInode(NULL, *(p->inode)), errno);
+           Log("VCreateVolume:  Problem writing to inode %s (err=%d)\n",
+               PrintInode(stmp, *(p->inode)), errno);
            FDH_REALLYCLOSE(fdP);
            goto bad;
        }
@@ -296,18 +291,12 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     fdP = IH_OPEN(handle);
     if (fdP == NULL) {
        Log("VCreateVolume:  Problem iopen inode %s (err=%d)\n",
-           PrintInode(NULL, tempHeader.volumeInfo), errno);
+           PrintInode(stmp, tempHeader.volumeInfo), errno);
        goto bad;
     }
-    if (FDH_SEEK(fdP, 0, SEEK_SET) < 0) {
-       Log("VCreateVolume:  Problem lseek inode %s (err=%d)\n",
-           PrintInode(NULL, tempHeader.volumeInfo), errno);
-       FDH_REALLYCLOSE(fdP);
-       goto bad;
-    }
-    if (FDH_WRITE(fdP, (char *)&vol, sizeof(vol)) != sizeof(vol)) {
+    if (FDH_PWRITE(fdP, (char *)&vol, sizeof(vol), 0) != sizeof(vol)) {
        Log("VCreateVolume:  Problem writing to  inode %s (err=%d)\n",
-           PrintInode(NULL, tempHeader.volumeInfo), errno);
+           PrintInode(stmp, tempHeader.volumeInfo), errno);
        FDH_REALLYCLOSE(fdP);
        goto bad;
     }
@@ -318,8 +307,8 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     rc = VCreateVolumeDiskHeader(&diskHeader, partition);
     if (rc) {
        Log("VCreateVolume: Error %d trying to write volume header for "
-           "volume %u on partition %s; volume not created\n", rc,
-           vol.id, VPartitionPath(partition));
+           "volume %" AFS_VOLID_FMT " on partition %s; volume not created\n", rc,
+           afs_printable_VolumeId_lu(vol.id), VPartitionPath(partition));
        if (rc == EEXIST) {
            *ec = VVOLEXISTS;
        }
@@ -409,6 +398,34 @@ ClearVolumeStats_r(VolumeDiskData * vol)
     vol->dayUseDate = 0;
 }
 
+void
+CopyVolumeStats_r(VolumeDiskData * from, VolumeDiskData * to)
+{
+    memcpy(to->weekUse, from->weekUse, sizeof(to->weekUse));
+    to->dayUse = from->dayUse;
+    to->dayUseDate = from->dayUseDate;
+    if (from->stat_initialized) {
+       memcpy(to->stat_reads, from->stat_reads, sizeof(to->stat_reads));
+       memcpy(to->stat_writes, from->stat_writes, sizeof(to->stat_writes));
+       memcpy(to->stat_fileSameAuthor, from->stat_fileSameAuthor,
+              sizeof(to->stat_fileSameAuthor));
+       memcpy(to->stat_fileDiffAuthor, from->stat_fileDiffAuthor,
+              sizeof(to->stat_fileDiffAuthor));
+       memcpy(to->stat_dirSameAuthor, from->stat_dirSameAuthor,
+              sizeof(to->stat_dirSameAuthor));
+       memcpy(to->stat_dirDiffAuthor, from->stat_dirDiffAuthor,
+              sizeof(to->stat_dirDiffAuthor));
+    }
+}
+
+void
+CopyVolumeStats(VolumeDiskData * from, VolumeDiskData * to)
+{
+    VOL_LOCK;
+    CopyVolumeStats_r(from, to);
+    VOL_UNLOCK;
+}
+
 /**
  * read an existing volume disk header.
  *
@@ -435,18 +452,17 @@ VReadVolumeDiskHeader(VolumeId volid,
     int fd;
     char path[MAXPATHLEN];
 
-    (void)afs_snprintf(path, sizeof(path),
-                      "%s/" VFORMAT,
-                      VPartitionPath(dp), afs_printable_uint32_lu(volid));
+    snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
+            VPartitionPath(dp), afs_printable_VolumeId_lu(volid));
     fd = open(path, O_RDONLY);
     if (fd < 0) {
-       Log("VReadVolumeDiskHeader: Couldn't open header for volume %lu (errno %d).\n",
-           afs_printable_uint32_lu(volid), errno);
+       Log("VReadVolumeDiskHeader: Couldn't open header for volume %" AFS_VOLID_FMT " (errno %d).\n",
+           afs_printable_VolumeId_lu(volid), errno);
        code = -1;
 
     } else if (hdr && read(fd, hdr, sizeof(*hdr)) != sizeof(*hdr)) {
-       Log("VReadVolumeDiskHeader: Couldn't read header for volume %lu.\n",
-           afs_printable_uint32_lu(volid));
+       Log("VReadVolumeDiskHeader: Couldn't read header for volume %" AFS_VOLID_FMT ".\n",
+           afs_printable_VolumeId_lu(volid));
        code = EIO;
     }
 
@@ -491,9 +507,8 @@ _VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
 
     flags |= O_RDWR;
 
-    (void)afs_snprintf(path, sizeof(path),
-                      "%s/" VFORMAT,
-                      VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
+    snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
+            VPartitionPath(dp), afs_printable_VolumeId_lu(hdr->id));
     fd = open(path, flags, 0644);
     if (fd < 0) {
        code = errno;
@@ -655,9 +670,6 @@ VCreateVolumeDiskHeader(VolumeDiskHeader_t * hdr,
  * @return operation status
  *    @retval 0 success
  *
- * @note if parent is 0, the parent volume ID will be looked up from the
- * fileserver
- *
  * @note for non-DAFS, parent is currently ignored
  */
 afs_int32
@@ -671,9 +683,8 @@ VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
     SYNC_response res;
 #endif /* AFS_DEMAND_ATTACH_FS */
 
-    (void)afs_snprintf(path, sizeof(path),
-                       "%s/" VFORMAT,
-                       VPartitionPath(dp), afs_printable_uint32_lu(volid));
+    snprintf(path, sizeof(path), "%s" OS_DIRSEP VFORMAT,
+             VPartitionPath(dp), afs_printable_VolumeId_lu(volid));
     code = unlink(path);
     if (code) {
        Log("VDestroyVolumeDiskHeader: Couldn't unlink disk header, error = %d\n", errno);
@@ -681,28 +692,14 @@ VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
     }
 
 #ifdef AFS_DEMAND_ATTACH_FS
+    /* Remove the volume entry from the fileserver's volume group cache, if found. */
     memset(&res, 0, sizeof(res));
-    if (!parent) {
-       FSSYNC_VGQry_response_t q_res;
-
-       code = FSYNC_VGCQuery(dp->name, volid, &q_res, &res);
-       if (code) {
-           Log("VDestroyVolumeDiskHeader: FSYNC_VGCQuery(%s, %lu) failed "
-               "with code %ld, reason %ld\n", dp->name,
-               afs_printable_uint32_lu(volid), afs_printable_int32_ld(code),
-               afs_printable_int32_ld(res.hdr.reason));
-           goto done;
-       }
-
-       parent = q_res.rw;
-
-    }
     code = FSYNC_VGCDel(dp->name, parent, volid, FSYNC_WHATEVER, &res);
     if (code) {
-       Log("VDestroyVolumeDiskHeader: FSYNC_VGCDel(%s, %lu, %lu) failed "
+       Log("VDestroyVolumeDiskHeader: FSYNC_VGCDel(%s, %" AFS_VOLID_FMT ", %" AFS_VOLID_FMT ") failed "
            "with code %ld reason %ld\n", dp->name,
-           afs_printable_uint32_lu(parent),
-           afs_printable_uint32_lu(volid),
+           afs_printable_VolumeId_lu(parent),
+           afs_printable_VolumeId_lu(volid),
            afs_printable_int32_ld(code),
            afs_printable_int32_ld(res.hdr.reason));
     }
@@ -735,17 +732,17 @@ _VHandleVolumeHeader(struct DiskPartition64 *dp, VWalkVolFunc volfunc,
                      int locked, void *rock)
 {
     int error = 0;
-    int fd;
+    FD_t fd;
 
-    if ((fd = afs_open(name, O_RDONLY)) == -1
-        || read(fd, hdr, sizeof(*hdr))
+    if ((fd = OS_OPEN(name, O_RDONLY, 0)) == INVALID_FD
+        || OS_READ(fd, hdr, sizeof(*hdr))
         != sizeof(*hdr)
         || hdr->stamp.magic != VOLUMEHEADERMAGIC) {
         error = 1;
     }
 
-    if (fd >= 0) {
-       close(fd);
+    if (fd != INVALID_FD) {
+       OS_CLOSE(fd);
     }
 
 #ifdef AFSFS_DEMAND_ATTACH_FS
@@ -825,8 +822,8 @@ int
 VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
                    VWalkVolFunc volfunc, VWalkErrFunc errfunc, void *rock)
 {
-    DIR *dirp = NULL;
-    struct dirent *dentry = NULL;
+    DIR *dirp;
+    struct dirent *dentry;
     int code = 0;
     struct VolumeDiskHeader diskHeader;
 
@@ -837,13 +834,13 @@ VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
        goto done;
     }
 
-    while ((dentry = readdir(dirp))) {
-       char *p = dentry->d_name;
+    while ((dentry = readdir(dirp)) != NULL) {
+       char *p;
        p = strrchr(dentry->d_name, '.');
        if (p != NULL && strcmp(p, VHDREXT) == 0) {
            char name[VMAXPATHLEN];
 
-           sprintf(name, "%s/%s", partpath, dentry->d_name);
+           snprintf(name, VMAXPATHLEN, "%s" OS_DIRSEP "%s", partpath, dentry->d_name);
 
            code = _VHandleVolumeHeader(dp, volfunc, name, &diskHeader, -1, rock);
            if (code < 0) {
@@ -874,14 +871,6 @@ VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
     return code;
 }
 
-#ifdef AFS_PTHREAD_ENV
-# define AFS_LF_LOCK(lf) assert(pthread_mutex_lock(&((lf)->mutex)) == 0)
-# define AFS_LF_UNLOCK(lf) assert(pthread_mutex_unlock(&((lf)->mutex)) == 0)
-#else
-# define AFS_LF_LOCK(lf)
-# define AFS_LF_UNLOCK(lf)
-#endif /* AFS_PTHREAD_ENV */
-
 /**
  * initialize a struct VLockFile.
  *
@@ -895,9 +884,7 @@ VLockFileInit(struct VLockFile *lf, const char *path)
     memset(lf, 0, sizeof(*lf));
     lf->path = strdup(path);
     lf->fd = INVALID_FD;
-#ifdef AFS_PTHREAD_ENV
-    assert(pthread_mutex_init(&lf->mutex, NULL) == 0);
-#endif /* AFS_PTHREAD_ENV */
+    opr_mutex_init(&lf->mutex);
 }
 
 #ifdef AFS_NT40_ENV
@@ -947,20 +934,20 @@ _VLockFd(FD_t handle, afs_uint32 offset, int locktype, int nonblock)
 }
 
 static_inline void
-_VUnlockFd(struct VLockFile *lf, afs_uint32 offset)
+_VUnlockFd(FD_t handle, afs_uint32 offset)
 {
     OVERLAPPED lap;
 
     memset(&lap, 0, sizeof(lap));
     lap.Offset = offset;
 
-    UnlockFileEx(lf->fd, 0, 1, 0, &lap);
+    UnlockFileEx(handle, 0, 1, 0, &lap);
 }
 
 static_inline void
-_VCloseFd(struct VLockFile *lf)
+_VCloseFd(FD_t handle)
 {
-    CloseHandle(lf->fd);
+    CloseHandle(handle);
 }
 
 #else /* !AFS_NT40_ENV */
@@ -973,7 +960,7 @@ _VCloseFd(struct VLockFile *lf)
  * @return file descriptor
  *  @retval INVALID_FD failure opening file
  */
-static_inline int
+static_inline FD_t
 _VOpenPath(const char *path)
 {
     int fd;
@@ -1000,17 +987,19 @@ _VOpenPath(const char *path)
  *  @retval EIO   error acquiring file lock
  */
 static_inline int
-_VLockFd(int fd, afs_uint32 offset, int locktype, int nonblock)
+_VLockFd(FD_t fd, afs_uint32 offset, int locktype, int nonblock)
 {
     int l_type = F_WRLCK;
-    int cmd = F_SETLKW;
-    struct flock sf;
+    int cmd = AFS_SETLKW;
+    struct afs_st_flock sf;
+
+    opr_Assert(fd >= 0);
 
     if (locktype == READ_LOCK) {
        l_type = F_RDLCK;
     }
     if (nonblock) {
-       cmd = F_SETLK;
+       cmd = AFS_SETLK;
     }
 
     sf.l_start = offset;
@@ -1050,7 +1039,7 @@ _VLockFd(int fd, afs_uint32 offset, int locktype, int nonblock)
  * @param[in] fd file descriptor to close
  */
 static_inline void
-_VCloseFd(int fd)
+_VCloseFd(FD_t fd)
 {
     if (close(fd)) {
        Log("_VCloseFd: error %d closing fd %d\n",
@@ -1065,16 +1054,16 @@ _VCloseFd(int fd)
  * @param[in] offset offset to unlock
  */
 static_inline void
-_VUnlockFd(int fd, afs_uint32 offset)
+_VUnlockFd(FD_t fd, afs_uint32 offset)
 {
-    struct flock sf;
+    struct afs_st_flock sf;
 
     sf.l_start = offset;
     sf.l_len = 1;
     sf.l_type = F_UNLCK;
     sf.l_whence = SEEK_SET;
 
-    if (fcntl(fd, F_SETLK, &sf)) {
+    if (fcntl(fd, AFS_SETLK, &sf)) {
        Log("_VUnlockFd: fcntl failed with error %d when trying to unlock "
            "fd %d\n", errno, fd);
     }
@@ -1098,9 +1087,7 @@ _VUnlockFd(int fd, afs_uint32 offset)
 void
 VLockFileReinit(struct VLockFile *lf)
 {
-#ifdef AFS_PTHREAD_ENV
-    assert(pthread_mutex_lock(&lf->mutex) == 0);
-#endif /* AFS_PTHREAD_ENV */
+    opr_mutex_enter(&lf->mutex);
 
     if (lf->fd != INVALID_FD) {
        _VCloseFd(lf->fd);
@@ -1109,9 +1096,7 @@ VLockFileReinit(struct VLockFile *lf)
 
     lf->refcount = 0;
 
-#ifdef AFS_PTHREAD_ENV
-    assert(pthread_mutex_unlock(&lf->mutex) == 0);
-#endif /* AFS_PTHREAD_ENV */
+    opr_mutex_exit(&lf->mutex);
 }
 
 /**
@@ -1141,31 +1126,35 @@ VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonbloc
 {
     int code;
 
-    assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
+    opr_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
 
-    AFS_LF_LOCK(lf);
+    opr_mutex_enter(&lf->mutex);
 
     if (lf->fd == INVALID_FD) {
+       opr_Assert(lf->refcount == 0);
        lf->fd = _VOpenPath(lf->path);
        if (lf->fd == INVALID_FD) {
-           AFS_LF_UNLOCK(lf);
+           opr_mutex_exit(&lf->mutex);
            return EIO;
        }
     }
 
     lf->refcount++;
 
-    AFS_LF_UNLOCK(lf);
+    opr_Assert(lf->refcount > 0);
+
+    opr_mutex_exit(&lf->mutex);
 
     code = _VLockFd(lf->fd, offset, locktype, nonblock);
 
     if (code) {
-       AFS_LF_LOCK(lf);
+       opr_mutex_enter(&lf->mutex);
+       opr_Assert(lf->refcount > 0);
        if (--lf->refcount < 1) {
            _VCloseFd(lf->fd);
            lf->fd = INVALID_FD;
        }
-       AFS_LF_UNLOCK(lf);
+       opr_mutex_exit(&lf->mutex);
     }
 
     return code;
@@ -1174,9 +1163,10 @@ VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonbloc
 void
 VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
 {
-    AFS_LF_LOCK(lf);
+    opr_mutex_enter(&lf->mutex);
 
-    assert(lf->fd != INVALID_FD);
+    opr_Assert(lf->fd != INVALID_FD);
+    opr_Assert(lf->refcount > 0);
 
     if (--lf->refcount < 1) {
        _VCloseFd(lf->fd);
@@ -1185,7 +1175,7 @@ VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
        _VUnlockFd(lf->fd, offset);
     }
 
-    AFS_LF_UNLOCK(lf);
+    opr_mutex_exit(&lf->mutex);
 }
 
 #ifdef AFS_DEMAND_ATTACH_FS
@@ -1199,11 +1189,11 @@ VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
 void
 VDiskLockInit(struct VDiskLock *dl, struct VLockFile *lf, afs_uint32 offset)
 {
-    assert(lf);
+    opr_Assert(lf);
     memset(dl, 0, sizeof(*dl));
     Lock_Init(&dl->rwlock);
-    assert(pthread_mutex_init(&dl->mutex, NULL) == 0);
-    assert(pthread_cond_init(&dl->cv, NULL) == 0);
+    opr_mutex_init(&dl->mutex);
+    opr_cv_init(&dl->cv);
     dl->lockfile = lf;
     dl->offset = offset;
 }
@@ -1234,7 +1224,7 @@ int
 VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
 {
     int code = 0;
-    assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
+    opr_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
 
     if (nonblock) {
        if (locktype == READ_LOCK) {
@@ -1253,7 +1243,7 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
        ObtainWriteLock(&dl->rwlock);
     }
 
-    assert(pthread_mutex_lock(&dl->mutex) == 0);
+    opr_mutex_enter(&dl->mutex);
 
     if ((dl->flags & VDISKLOCK_ACQUIRING)) {
        /* Some other thread is waiting to acquire an fs lock. If nonblock=1,
@@ -1264,7 +1254,7 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
            code = EBUSY;
        } else {
            while ((dl->flags & VDISKLOCK_ACQUIRING)) {
-               assert(pthread_cond_wait(&dl->cv, &dl->mutex) == 0);
+               opr_cv_wait(&dl->cv, &dl->mutex);
            }
        }
     }
@@ -1281,9 +1271,9 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
            /* mark that we are waiting on the fs lock */
            dl->flags |= VDISKLOCK_ACQUIRING;
 
-           assert(pthread_mutex_unlock(&dl->mutex) == 0);
+           opr_mutex_exit(&dl->mutex);
            code = VLockFileLock(dl->lockfile, dl->offset, locktype, nonblock);
-           assert(pthread_mutex_lock(&dl->mutex) == 0);
+           opr_mutex_enter(&dl->mutex);
 
            dl->flags &= ~VDISKLOCK_ACQUIRING;
 
@@ -1291,7 +1281,7 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
                dl->flags |= VDISKLOCK_ACQUIRED;
            }
 
-           assert(pthread_cond_broadcast(&dl->cv) == 0);
+           opr_cv_broadcast(&dl->cv);
        }
     }
 
@@ -1307,7 +1297,7 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
        ++dl->lockers;
     }
 
-    assert(pthread_mutex_unlock(&dl->mutex) == 0);
+    opr_mutex_exit(&dl->mutex);
 
     return code;
 }
@@ -1325,10 +1315,10 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
 void
 VReleaseDiskLock(struct VDiskLock *dl, int locktype)
 {
-    assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
+    opr_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
 
-    assert(pthread_mutex_lock(&dl->mutex) == 0);
-    assert(dl->lockers > 0);
+    opr_mutex_enter(&dl->mutex);
+    opr_Assert(dl->lockers > 0);
 
     if (--dl->lockers < 1) {
        /* no threads are holding this lock anymore, so we can release the
@@ -1337,7 +1327,7 @@ VReleaseDiskLock(struct VDiskLock *dl, int locktype)
        dl->flags &= ~VDISKLOCK_ACQUIRED;
     }
 
-    assert(pthread_mutex_unlock(&dl->mutex) == 0);
+    opr_mutex_exit(&dl->mutex);
 
     if (locktype == READ_LOCK) {
        ReleaseReadLock(&dl->rwlock);