Index: LVM2.2.02.39/daemons/clvmd/clvmd-openais.c
===================================================================
--- LVM2.2.02.39.orig/daemons/clvmd/clvmd-openais.c
+++ LVM2.2.02.39/daemons/clvmd/clvmd-openais.c
@@ -34,9 +34,9 @@
 #include <syslog.h>
 #include <assert.h>
 #include <libdevmapper.h>
+#include <libdlm.h>
 
 #include <openais/saAis.h>
-#include <openais/saLck.h>
 #include <openais/cpg.h>
 
 #include "list.h"
@@ -50,6 +50,8 @@
 /* Timeout value for several openais calls */
 #define TIMEOUT 10
 
+#define LOCKSPACE_NAME "clvmd"
+
 static void cpg_deliver_callback (cpg_handle_t handle,
 				  struct cpg_name *groupName,
 				  uint32_t nodeid,
@@ -66,9 +68,6 @@ static void _cluster_closedown(void);
 /* Hash list of nodes in the cluster */
 static struct dm_hash_table *node_hash;
 
-/* For associating lock IDs & resource handles */
-static struct dm_hash_table *lock_hash;
-
 /* Number of active nodes */
 static int num_nodes;
 static unsigned int our_nodeid;
@@ -77,9 +76,15 @@ static struct local_client *cluster_clie
 
 /* OpenAIS handles */
 static cpg_handle_t cpg_handle;
-static SaLckHandleT lck_handle;
 
 static struct cpg_name cpg_group_name;
+static dlm_lshandle_t *lockspace;
+
+struct lock_wait {
+	pthread_cond_t cond;
+	pthread_mutex_t mutex;
+	struct dlm_lksb lksb;
+};
 
 /* Openais callback structs */
 cpg_callbacks_t cpg_callbacks = {
@@ -93,13 +98,6 @@ struct node_info
 	int nodeid;
 };
 
-struct lock_info
-{
-	SaLckResourceHandleT res_handle;
-	SaLckLockIdT         lock_id;
-	SaNameT              lock_name;
-};
-
 /* Set errno to something approximating the right value and return 0 or -1 */
 static int ais_to_errno(SaAisErrorT err)
 {
@@ -309,22 +307,11 @@ static void cpg_confchg_callback(cpg_han
 	num_nodes = member_list_entries;
 }
 
-static int lck_dispatch(struct local_client *client, char *buf, int len,
-			const char *csid, struct local_client **new_client)
-{
-	*new_client = NULL;
-	saLckDispatch(lck_handle, SA_DISPATCH_ONE);
-	return 1;
-}
-
 static int _init_cluster(void)
 {
 	SaAisErrorT err;
-	SaVersionT  ver = { 'B', 1, 1 };
-	int select_fd;
 
 	node_hash = dm_hash_create(100);
-	lock_hash = dm_hash_create(10);
 
 	err = cpg_initialize(&cpg_handle,
 			     &cpg_callbacks);
@@ -335,16 +322,19 @@ static int _init_cluster(void)
 		return ais_to_errno(err);
 	}
 
-	err = saLckInitialize(&lck_handle,
-					NULL,
-			      &ver);
-	if (err != SA_AIS_OK) {
-		cpg_initialize(&cpg_handle, &cpg_callbacks);
-		syslog(LOG_ERR, "Cannot initialise OpenAIS lock service: %d",
-		       err);
-		DEBUGLOG("Cannot initialise OpenAIS lock service: %d\n\n", err);
-		return ais_to_errno(err);
+	/* Create a lockspace for LV & VG locks to live in */
+	lockspace = dlm_create_lockspace(LOCKSPACE_NAME, 0600);
+	if (!lockspace) {
+		if (errno == EEXIST) {
+			lockspace = dlm_open_lockspace(LOCKSPACE_NAME);
+		}
+		if (!lockspace) {
+			syslog(LOG_ERR, "Unable to create lockspace for CLVM: %m");
+			return -1;
+		}
 	}
+	dlm_ls_pthread_init(lockspace);
+	DEBUGLOG("DLM initialisation complete\n");
 
 	/* Connect to the clvmd group */
 	strcpy((char *)cpg_group_name.value, "clvmd");
@@ -352,7 +342,7 @@ static int _init_cluster(void)
 	err = cpg_join(cpg_handle, &cpg_group_name);
 	if (err != SA_AIS_OK) {
 		cpg_finalize(cpg_handle);
-		saLckFinalize(lck_handle);
+		dlm_release_lockspace(LOCKSPACE_NAME, lockspace, 0);
 		syslog(LOG_ERR, "Cannot join clvmd process group");
 		DEBUGLOG("Cannot join clvmd process group: %d\n", err);
 		return ais_to_errno(err);
@@ -362,15 +352,12 @@ static int _init_cluster(void)
 			    &our_nodeid);
 	if (err != SA_AIS_OK) {
 		cpg_finalize(cpg_handle);
-		saLckFinalize(lck_handle);
+		dlm_release_lockspace(LOCKSPACE_NAME, lockspace, 0);
 		syslog(LOG_ERR, "Cannot get local node id\n");
 		return ais_to_errno(err);
 	}
 	DEBUGLOG("Our local node id is %d\n", our_nodeid);
 
-	saLckSelectionObjectGet(lck_handle, (SaSelectionObjectT *)&select_fd);
-	add_internal_client(select_fd, lck_dispatch);
-
 	DEBUGLOG("Connected to OpenAIS\n");
 
 	return 0;
@@ -381,7 +368,7 @@ static void _cluster_closedown(void)
 	DEBUGLOG("cluster_closedown\n");
 	unlock_all();
 
-	saLckFinalize(lck_handle);
+	dlm_release_lockspace(LOCKSPACE_NAME, lockspace, 0);
 	cpg_finalize(cpg_handle);
 }
 
@@ -472,156 +459,69 @@ static int _cluster_do_node_callback(str
 	return somedown;
 }
 
-/* Real locking */
-static int _lock_resource(char *resource, int mode, int flags, int *lockid)
+static int _sync_lock(const char *resource, int mode, int flags, int *lockid)
 {
-	struct lock_info *linfo;
-	SaLckResourceHandleT res_handle;
-	SaAisErrorT err;
-	SaLckLockIdT lock_id;
-	SaLckLockStatusT lockStatus;
-
-	/* This needs to be converted from DLM/LVM2 value for OpenAIS LCK */
-	if (flags & LCK_NONBLOCK) flags = SA_LCK_LOCK_NO_QUEUE;
-
-	linfo = malloc(sizeof(struct lock_info));
-	if (!linfo)
-		return -1;
+	struct dlm_lksb lksb;
+	int err;
 
 	DEBUGLOG("lock_resource '%s', flags=%d, mode=%d\n", resource, flags, mode);
 
-	linfo->lock_name.length = strlen(resource)+1;
-	strcpy((char *)linfo->lock_name.value, resource);
-
-	err = saLckResourceOpen(lck_handle, &linfo->lock_name,
-				SA_LCK_RESOURCE_CREATE, TIMEOUT, &res_handle);
-	if (err != SA_AIS_OK)
-	{
-		DEBUGLOG("ResourceOpen returned %d\n", err);
-		free(linfo);
-		return ais_to_errno(err);
-	}
+	if (flags & LKF_CONVERT)
+		lksb.sb_lkid = *lockid;
 
-	err = saLckResourceLock(
-			res_handle,
-			&lock_id,
+	err = dlm_ls_lock_wait(lockspace,
 			mode,
+			&lksb,
 			flags,
+			resource,
+			strlen(resource),
 			0,
-			SA_TIME_END,
-			&lockStatus);
-	if (err != SA_AIS_OK && lockStatus != SA_LCK_LOCK_GRANTED)
+			NULL, NULL, NULL);
+
+	if (err != 0)
 	{
-		free(linfo);
-		saLckResourceClose(res_handle);
-		return ais_to_errno(err);
+		DEBUGLOG("dlm_ls_lock returned %d\n", errno);
+		return err;
+	}
+	errno = lksb.sb_status;
+	if (lksb.sb_status != 0)
+	{
+		DEBUGLOG("dlm_ls_lock returns lksb.sb_status %d\n", lksb.sb_status);
+		return -1;
 	}
-			
-	/* Wait for it to complete */
-
-	DEBUGLOG("lock_resource returning %d, lock_id=%llx\n", err,
-		 lock_id);
 
-	linfo->lock_id = lock_id;
-	linfo->res_handle = res_handle;
+	DEBUGLOG("lock_resource returning %d, lock_id=%x\n", err, lksb.sb_lkid);
 
-	dm_hash_insert(lock_hash, resource, linfo);
+	*lockid = lksb.sb_lkid;
 
-	return ais_to_errno(err);
+	return 0;
 }
 
-
-static int _unlock_resource(char *resource, int lockid)
+static int _sync_unlock(const char *resource, int lockid)
 {
-	SaAisErrorT err;
-	struct lock_info *linfo;
+    struct dlm_lksb lksb;
+    int err;
 
-	DEBUGLOG("unlock_resource %s\n", resource);
-	linfo = dm_hash_lookup(lock_hash, resource);
-	if (!linfo)
-		return 0;
+    DEBUGLOG("unlock_resource: %s lockid: %x\n", resource, lockid);
+    lksb.sb_lkid = lockid;
 
-	DEBUGLOG("unlock_resource: lockid: %llx\n", linfo->lock_id);
-	err = saLckResourceUnlock(linfo->lock_id, SA_TIME_END);
-	if (err != SA_AIS_OK)
+    err = dlm_ls_unlock_wait(lockspace,
+			lockid,
+			0,
+			&lksb);
+	if (err != 0)
 	{
 		DEBUGLOG("Unlock returned %d\n", err);
-		return ais_to_errno(err);
+		return err;
 	}
-
-	/* Release the resource */
-	dm_hash_remove(lock_hash, resource);
-	saLckResourceClose(linfo->res_handle);
-	free(linfo);
-
-	return ais_to_errno(err);
-}
-
-static int _sync_lock(const char *resource, int mode, int flags, int *lockid)
-{
-	int status;
-	char lock1[strlen(resource)+3];
-	char lock2[strlen(resource)+3];
-
-	snprintf(lock1, sizeof(lock1), "%s-1", resource);
-	snprintf(lock2, sizeof(lock2), "%s-2", resource);
-
-	switch (mode)
+	errno = lksb.sb_status;
+	if (lksb.sb_status != EUNLOCK)
 	{
-	case LCK_EXCL:
-		status = _lock_resource(lock1, SA_LCK_EX_LOCK_MODE, flags, lockid);
-		if (status)
-			goto out;
-
-		/* If we can't get this lock too then bail out */
-		status = _lock_resource(lock2, SA_LCK_EX_LOCK_MODE, LCK_NONBLOCK,
-					lockid);
-		if (status == SA_LCK_LOCK_NOT_QUEUED)
-		{
-			_unlock_resource(lock1, *lockid);
-			status = -1;
-			errno = EAGAIN;
-		}
-		break;
-
-	case LCK_PREAD:
-	case LCK_READ:
-		status = _lock_resource(lock1, SA_LCK_PR_LOCK_MODE, flags, lockid);
-		if (status)
-			goto out;
-		_unlock_resource(lock2, *lockid);
-		break;
-
-	case LCK_WRITE:
-		status = _lock_resource(lock2, SA_LCK_EX_LOCK_MODE, flags, lockid);
-		if (status)
-			goto out;
-		_unlock_resource(lock1, *lockid);
-		break;
-
-	default:
-		status = -1;
-		errno = EINVAL;
-		break;
+		DEBUGLOG("dlm_ls_unlock_wait returns lksb.sb_status: %x\n", lksb.sb_status);
+		return -1;
 	}
-out:
-	*lockid = mode;
-	return status;
-}
-
-static int _sync_unlock(const char *resource, int lockid)
-{
-	int status = 0;
-	char lock1[strlen(resource)+3];
-	char lock2[strlen(resource)+3];
-
-	snprintf(lock1, sizeof(lock1), "%s-1", resource);
-	snprintf(lock2, sizeof(lock2), "%s-2", resource);
-
-	_unlock_resource(lock1, lockid);
-	_unlock_resource(lock2, lockid);
+    return 0;
 
-	return status;
 }
 
 /* We are always quorate ! */
Index: LVM2.2.02.39/daemons/clvmd/Makefile.in
===================================================================
--- LVM2.2.02.39.orig/daemons/clvmd/Makefile.in
+++ LVM2.2.02.39/daemons/clvmd/Makefile.in
@@ -59,7 +59,7 @@ endif
 
 ifeq ("$(OPENAIS)", "yes")
 	SOURCES += clvmd-openais.c
-	LMLIBS += -lSaLck -lcpg
+	LMLIBS += -ldlm -lcpg
 	DEFS += -DUSE_OPENAIS
 endif
 
Index: LVM2.2.02.39/daemons/clvmd/clvm.h
===================================================================
--- LVM2.2.02.39.orig/daemons/clvmd/clvm.h
+++ LVM2.2.02.39/daemons/clvmd/clvm.h
@@ -79,6 +79,7 @@ static const char CLVMD_SOCKNAME[] = "\0
 /* Locking flags - these match the ones
  * in dlm.h
  */
+#ifndef LKF_NOQUEUE
 #define LKF_NOQUEUE        (0x00000001)
 #define LKF_CANCEL         (0x00000002)
 #define LKF_CONVERT        (0x00000004)
@@ -94,3 +95,4 @@ static const char CLVMD_SOCKNAME[] = "\0
 #define LKF_HEADQUE        (0x00001000)
 #define LKF_NOORDER        (0x00002000)
 #endif
+#endif
