Convert ObtainSalvageLock to the VLockFile API
authorAndrew Deason <adeason@sinenomine.net>
Thu, 18 Feb 2010 17:36:10 +0000 (11:36 -0600)
committerDerrick Brashear <shadow@dementia.org>
Fri, 26 Feb 2010 23:02:10 +0000 (15:02 -0800)
Make ObtainSalvageLock use the VLockFile API, to consolidate
platform-specific locking code, and to make it possible to acquire a
shared lock on the salvage lock file. Create the ObtainSharedSalvageLock
function to acquire such a lock.

Change-Id: Ieadd77ac8d4030b31c48fff1c98712a6c95f93ad
Reviewed-on: http://gerrit.openafs.org/1347
Tested-by: Andrew Deason <adeason@sinenomine.net>
Reviewed-by: Derrick Brashear <shadow@dementia.org>
Tested-by: Derrick Brashear <shadow@dementia.org>

src/vol/vol-salvage.c
src/vol/vol-salvage.h

index bf3c325..56b38ad 100644 (file)
@@ -338,40 +338,43 @@ extern pthread_t main_thread;
 childJob_t myjob = { SALVAGER_MAGIC, NOT_CHILD, "" };
 #endif
 
-/* Get the salvage lock if not already held. Hold until process exits. */
-void
-ObtainSalvageLock(void)
+/**
+ * Get the salvage lock if not already held. Hold until process exits.
+ *
+ * @param[in] locktype READ_LOCK or WRITE_LOCK
+ */
+static void
+_ObtainSalvageLock(int locktype)
 {
-    FD_t salvageLock;
+    struct VLockFile salvageLock;
+    int offset = 0;
+    int nonblock = 1;
+    int code;
 
-#ifdef AFS_NT40_ENV
-    salvageLock =
-       (FD_t)CreateFile(AFSDIR_SERVER_SLVGLOCK_FILEPATH, 0, 0, NULL,
-                       OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-    if (salvageLock == INVALID_FD) {
-       fprintf(stderr,
-               "salvager:  There appears to be another salvager running!  Aborted.\n");
-       Exit(1);
-    }
-#else
-    salvageLock =
-       afs_open(AFSDIR_SERVER_SLVGLOCK_FILEPATH, O_CREAT | O_RDWR, 0666);
-    if (salvageLock < 0) {
+    VLockFileInit(&salvageLock, AFSDIR_SERVER_SLVGLOCK_FILEPATH);
+
+    code = VLockFileLock(&salvageLock, offset, locktype, nonblock);
+    if (code == EBUSY) {
        fprintf(stderr,
-               "salvager:  can't open salvage lock file %s, aborting\n",
-               AFSDIR_SERVER_SLVGLOCK_FILEPATH);
+               "salvager:  There appears to be another salvager running!  "
+               "Aborted.\n");
        Exit(1);
-    }
-#ifdef AFS_DARWIN_ENV
-    if (flock(salvageLock, LOCK_EX) == -1) {
-#else
-    if (lockf(salvageLock, F_LOCK, 0) == -1) {
-#endif
+    } else if (code) {
        fprintf(stderr,
-               "salvager:  There appears to be another salvager running!  Aborted.\n");
+               "salvager:  Error %d trying to acquire salvage lock!  "
+               "Aborted.\n", code);
        Exit(1);
     }
-#endif
+}
+void
+ObtainSalvageLock(void)
+{
+    _ObtainSalvageLock(WRITE_LOCK);
+}
+void
+ObtainSharedSalvageLock(void)
+{
+    _ObtainSalvageLock(READ_LOCK);
 }
 
 
index 002e12b..9af93df 100644 (file)
@@ -243,6 +243,7 @@ extern int JudgeEntry(void *dirVal, char *name, afs_int32 vnodeNumber,
 extern void MaybeZapVolume(register struct InodeSummary *isp, char *message,
                           int deleteMe, int check);
 extern void ObtainSalvageLock(void);
+extern void ObtainSharedSalvageLock(void);
 extern void PrintInodeList(void);
 extern void PrintInodeSummary(void);
 extern void PrintVolumeSummary(void);