From 7f4a67b8f0e91f013c840fe15b9cd0e0fb672d36 Mon Sep 17 00:00:00 2001 From: Andrew Deason Date: Thu, 18 Feb 2010 11:52:33 -0600 Subject: [PATCH] Make VLockFile not DAFS-specific The VLockFile API does not require DAFS functionality, so make it available to non-DAFS code, as well. Change-Id: I3d9bebb5d4034f0af739ccee37f64e2254be38cb Reviewed-on: http://gerrit.openafs.org/1346 Tested-by: Andrew Deason Reviewed-by: Derrick Brashear Tested-by: Derrick Brashear --- src/vol/partition.h | 7 ++++--- src/vol/volume.h | 12 ++++++------ src/vol/vutil.c | 32 +++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/vol/partition.h b/src/vol/partition.h index 70dbd95..a36d453 100644 --- a/src/vol/partition.h +++ b/src/vol/partition.h @@ -48,19 +48,20 @@ #define VICE_ALWAYSATTACH_FILE "AlwaysAttach" #endif -#ifdef AFS_DEMAND_ATTACH_FS - /** * abstraction for files used for file-locking. */ struct VLockFile { FD_t fd; /**< fd holding the lock(s) */ char *path; /**< path to the lock file */ + int refcount; /**< how many locks we have on the file */ +#ifdef AFS_PTHREAD_ENV pthread_mutex_t mutex; /**< lock for the VLockFile struct */ - int refcount; /**< how many locks we have on the file */ +#endif /* AFS_PTHREAD_ENV */ }; +#ifdef AFS_DEMAND_ATTACH_FS /* * flag bits for 'flags' in struct VDiskLock. */ diff --git a/src/vol/volume.h b/src/vol/volume.h index 227ec67..d34470b 100644 --- a/src/vol/volume.h +++ b/src/vol/volume.h @@ -819,6 +819,12 @@ extern void VGetVolumePath(Error * ec, VolId volumeId, char **partitionp, char **namep); extern char *vol_DevName(dev_t adev, char *wpath); +struct VLockFile; +extern void VLockFileInit(struct VLockFile *lf, const char *path); +extern int VLockFileLock(struct VLockFile *lf, afs_uint32 offset, + int locktype, int nonblock); +extern void VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset); + #ifdef AFS_DEMAND_ATTACH_FS extern Volume *VPreAttachVolumeByName(Error * ec, char *partition, char *name); extern Volume *VPreAttachVolumeByName_r(Error * ec, char *partition, char *name); @@ -847,12 +853,6 @@ extern void VCancelReservation_r(Volume * vp); extern int VChildProcReconnectFS_r(void); extern void VOfflineForVolOp_r(Error *ec, Volume *vp, char *message); -struct VLockFile; -extern void VLockFileInit(struct VLockFile *lf, const char *path); -extern int VLockFileLock(struct VLockFile *lf, afs_uint32 offset, - int locktype, int nonblock); -extern void VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset); - struct VDiskLock; extern void VDiskLockInit(struct VDiskLock *dl, struct VLockFile *lf, afs_uint32 offset); diff --git a/src/vol/vutil.c b/src/vol/vutil.c index 8f7aefe..0c02696 100644 --- a/src/vol/vutil.c +++ b/src/vol/vutil.c @@ -837,7 +837,13 @@ VWalkVolumeHeaders(struct DiskPartition64 *dp, const char *partpath, return code; } -#ifdef AFS_DEMAND_ATTACH_FS +#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. @@ -852,10 +858,12 @@ 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 */ } -# ifdef AFS_NT40_ENV +#ifdef AFS_NT40_ENV static_inline FD_t _VOpenPath(const char *path) { @@ -918,7 +926,7 @@ _VCloseFd(struct VLockFile *lf) CloseHandle(lf->fd); } -# else /* !AFS_NT40_ENV */ +#else /* !AFS_NT40_ENV */ /** * open a file on the local filesystem suitable for locking @@ -1021,7 +1029,7 @@ _VUnlockFd(int fd, afs_uint32 offset) "fd %d\n", errno, fd); } } -# endif /* !AFS_NT40_ENV */ +#endif /* !AFS_NT40_ENV */ /** * lock a file on disk for the process. @@ -1050,29 +1058,29 @@ VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonbloc { int code; - assert(pthread_mutex_lock(&lf->mutex) == 0); + AFS_LF_LOCK(lf); if (lf->fd == INVALID_FD) { lf->fd = _VOpenPath(lf->path); if (lf->fd == INVALID_FD) { - assert(pthread_mutex_unlock(&lf->mutex) == 0); + AFS_LF_UNLOCK(lf); return EIO; } } lf->refcount++; - assert(pthread_mutex_unlock(&lf->mutex) == 0); + AFS_LF_UNLOCK(lf); code = _VLockFd(lf->fd, offset, locktype, nonblock); if (code) { - assert(pthread_mutex_lock(&lf->mutex) == 0); + AFS_LF_LOCK(lf); if (--lf->refcount < 1) { _VCloseFd(lf->fd); lf->fd = INVALID_FD; } - assert(pthread_mutex_unlock(&lf->mutex) == 0); + AFS_LF_UNLOCK(lf); } return code; @@ -1081,7 +1089,7 @@ VLockFileLock(struct VLockFile *lf, afs_uint32 offset, int locktype, int nonbloc void VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset) { - assert(pthread_mutex_lock(&lf->mutex) == 0); + AFS_LF_LOCK(lf); if (--lf->refcount < 1) { _VCloseFd(lf->fd); @@ -1090,9 +1098,11 @@ VLockFileUnlock(struct VLockFile *lf, afs_uint32 offset) _VUnlockFd(lf->fd, offset); } - assert(pthread_mutex_unlock(&lf->mutex) == 0); + AFS_LF_UNLOCK(lf); } +#ifdef AFS_DEMAND_ATTACH_FS + /** * initialize a struct VDiskLock. * -- 1.9.4