vol: Make VLockFile arguments consistent
[openafs.git] / src / vol / vutil.c
index a927e7c..92aaad2 100644 (file)
@@ -17,6 +17,7 @@
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
 
 #include <stdio.h>
 #include <sys/types.h>
 #endif
 #include <dirent.h>
 #include <sys/stat.h>
-#ifdef AFS_PTHREAD_ENV
-#include <assert.h>
-#else /* AFS_PTHREAD_ENV */
-#include <afs/assert.h>
-#endif /* AFS_PTHREAD_ENV */
+#include <afs/afs_assert.h>
 
 #include <rx/xdr.h>
 #include <afs/afsint.h>
 #include <strings.h>
 #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 */
@@ -193,7 +183,7 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
     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",
+    (void)afs_snprintf(volumePath, sizeof volumePath, "%s" OS_DIRSEP "%s",
                       VPartitionPath(partition), headerName);
     rc = stat(volumePath, &st);
     if (rc == 0 || errno != ENOENT) {
@@ -225,11 +215,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) {
@@ -277,7 +263,7 @@ VCreateVolume_r(Error * ec, char *partname, VolId volumeId, VolId parentId)
        }
        if (FDH_PWRITE(fdP, (char *)&p->stamp, sizeof(p->stamp), 0) !=
            sizeof(p->stamp)) {
-           Log("VCreateVolume:  Problem writing to  inode %s (err=%d)\n",
+           Log("VCreateVolume:  Problem writing to inode %s (err=%d)\n",
                PrintInode(stmp, *(p->inode)), errno);
            FDH_REALLYCLOSE(fdP);
            goto bad;
@@ -425,7 +411,7 @@ VReadVolumeDiskHeader(VolumeId volid,
     char path[MAXPATHLEN];
 
     (void)afs_snprintf(path, sizeof(path),
-                      "%s/" VFORMAT,
+                      "%s" OS_DIRSEP VFORMAT,
                       VPartitionPath(dp), afs_printable_uint32_lu(volid));
     fd = open(path, O_RDONLY);
     if (fd < 0) {
@@ -481,7 +467,7 @@ _VWriteVolumeDiskHeader(VolumeDiskHeader_t * hdr,
     flags |= O_RDWR;
 
     (void)afs_snprintf(path, sizeof(path),
-                      "%s/" VFORMAT,
+                      "%s" OS_DIRSEP VFORMAT,
                       VPartitionPath(dp), afs_printable_uint32_lu(hdr->id));
     fd = open(path, flags, 0644);
     if (fd < 0) {
@@ -661,7 +647,7 @@ VDestroyVolumeDiskHeader(struct DiskPartition64 * dp,
 #endif /* AFS_DEMAND_ATTACH_FS */
 
     (void)afs_snprintf(path, sizeof(path),
-                       "%s/" VFORMAT,
+                       "%s" OS_DIRSEP VFORMAT,
                        VPartitionPath(dp), afs_printable_uint32_lu(volid));
     code = unlink(path);
     if (code) {
@@ -724,17 +710,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
@@ -832,7 +818,7 @@ VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath,
        if (p != NULL && strcmp(p, VHDREXT) == 0) {
            char name[VMAXPATHLEN];
 
-           sprintf(name, "%s/%s", partpath, dentry->d_name);
+           sprintf(name, "%s" OS_DIRSEP "%s", partpath, dentry->d_name);
 
            code = _VHandleVolumeHeader(dp, volfunc, name, &diskHeader, -1, rock);
            if (code < 0) {
@@ -863,14 +849,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.
  *
@@ -884,9 +862,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 */
+    MUTEX_INIT(&lf->mutex, "vlockfile", MUTEX_DEFAULT, 0);
 }
 
 #ifdef AFS_NT40_ENV
@@ -936,20 +912,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 */
@@ -962,7 +938,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;
@@ -989,7 +965,7 @@ _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;
@@ -1039,7 +1015,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",
@@ -1054,7 +1030,7 @@ _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;
 
@@ -1087,9 +1063,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 */
+    MUTEX_ENTER(&lf->mutex);
 
     if (lf->fd != INVALID_FD) {
        _VCloseFd(lf->fd);
@@ -1098,9 +1072,7 @@ VLockFileReinit(struct VLockFile *lf)
 
     lf->refcount = 0;
 
-#ifdef AFS_PTHREAD_ENV
-    assert(pthread_mutex_unlock(&lf->mutex) == 0);
-#endif /* AFS_PTHREAD_ENV */
+    MUTEX_EXIT(&lf->mutex);
 }
 
 /**
@@ -1130,31 +1102,31 @@ VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonbloc
 {
     int code;
 
-    assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
+    osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
 
-    AFS_LF_LOCK(lf);
+    MUTEX_ENTER(&lf->mutex);
 
     if (lf->fd == INVALID_FD) {
        lf->fd = _VOpenPath(lf->path);
        if (lf->fd == INVALID_FD) {
-           AFS_LF_UNLOCK(lf);
+           MUTEX_EXIT(&lf->mutex);
            return EIO;
        }
     }
 
     lf->refcount++;
 
-    AFS_LF_UNLOCK(lf);
+    MUTEX_EXIT(&lf->mutex);
 
     code = _VLockFd(lf->fd, offset, locktype, nonblock);
 
     if (code) {
-       AFS_LF_LOCK(lf);
+       MUTEX_ENTER(&lf->mutex);
        if (--lf->refcount < 1) {
            _VCloseFd(lf->fd);
            lf->fd = INVALID_FD;
        }
-       AFS_LF_UNLOCK(lf);
+       MUTEX_EXIT(&lf->mutex);
     }
 
     return code;
@@ -1163,9 +1135,9 @@ VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonbloc
 void
 VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
 {
-    AFS_LF_LOCK(lf);
+    MUTEX_ENTER(&lf->mutex);
 
-    assert(lf->fd != INVALID_FD);
+    osi_Assert(lf->fd != INVALID_FD);
 
     if (--lf->refcount < 1) {
        _VCloseFd(lf->fd);
@@ -1174,7 +1146,7 @@ VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
        _VUnlockFd(lf->fd, offset);
     }
 
-    AFS_LF_UNLOCK(lf);
+    MUTEX_EXIT(&lf->mutex);
 }
 
 #ifdef AFS_DEMAND_ATTACH_FS
@@ -1188,11 +1160,11 @@ VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset)
 void
 VDiskLockInit(struct VDiskLock *dl, struct VLockFile *lf, afs_uint32 offset)
 {
-    assert(lf);
+    osi_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);
+    MUTEX_INIT(&dl->mutex, "disklock", MUTEX_DEFAULT, 0);
+    CV_INIT(&dl->cv, "disklock cv", CV_DEFAULT, 0);
     dl->lockfile = lf;
     dl->offset = offset;
 }
@@ -1223,7 +1195,7 @@ int
 VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
 {
     int code = 0;
-    assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
+    osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
 
     if (nonblock) {
        if (locktype == READ_LOCK) {
@@ -1242,7 +1214,7 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
        ObtainWriteLock(&dl->rwlock);
     }
 
-    assert(pthread_mutex_lock(&dl->mutex) == 0);
+    MUTEX_ENTER(&dl->mutex);
 
     if ((dl->flags & VDISKLOCK_ACQUIRING)) {
        /* Some other thread is waiting to acquire an fs lock. If nonblock=1,
@@ -1253,7 +1225,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);
+               CV_WAIT(&dl->cv, &dl->mutex);
            }
        }
     }
@@ -1270,9 +1242,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);
+           MUTEX_EXIT(&dl->mutex);
            code = VLockFileLock(dl->lockfile, dl->offset, locktype, nonblock);
-           assert(pthread_mutex_lock(&dl->mutex) == 0);
+           MUTEX_ENTER(&dl->mutex);
 
            dl->flags &= ~VDISKLOCK_ACQUIRING;
 
@@ -1280,7 +1252,7 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
                dl->flags |= VDISKLOCK_ACQUIRED;
            }
 
-           assert(pthread_cond_broadcast(&dl->cv) == 0);
+           CV_BROADCAST(&dl->cv);
        }
     }
 
@@ -1296,7 +1268,7 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
        ++dl->lockers;
     }
 
-    assert(pthread_mutex_unlock(&dl->mutex) == 0);
+    MUTEX_EXIT(&dl->mutex);
 
     return code;
 }
@@ -1314,10 +1286,10 @@ VGetDiskLock(struct VDiskLock *dl, int locktype, int nonblock)
 void
 VReleaseDiskLock(struct VDiskLock *dl, int locktype)
 {
-    assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
+    osi_Assert(locktype == READ_LOCK || locktype == WRITE_LOCK);
 
-    assert(pthread_mutex_lock(&dl->mutex) == 0);
-    assert(dl->lockers > 0);
+    MUTEX_ENTER(&dl->mutex);
+    osi_Assert(dl->lockers > 0);
 
     if (--dl->lockers < 1) {
        /* no threads are holding this lock anymore, so we can release the
@@ -1326,7 +1298,7 @@ VReleaseDiskLock(struct VDiskLock *dl, int locktype)
        dl->flags &= ~VDISKLOCK_ACQUIRED;
     }
 
-    assert(pthread_mutex_unlock(&dl->mutex) == 0);
+    MUTEX_EXIT(&dl->mutex);
 
     if (locktype == READ_LOCK) {
        ReleaseReadLock(&dl->rwlock);