Index: LVM2.2.02.39/daemons/clvmd/clvmd-openais.c =================================================================== --- LVM2.2.02.39.orig/daemons/clvmd/clvmd-openais.c 2008-06-20 20:46:21.000000000 +0800 +++ LVM2.2.02.39/daemons/clvmd/clvmd-openais.c 2009-01-22 12:36:41.000000000 +0800 @@ -34,9 +34,9 @@ #include #include #include +#include #include -#include #include #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, @@ -77,9 +79,15 @@ /* 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 +101,6 @@ 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,19 +310,9 @@ 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); @@ -335,16 +326,14 @@ 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) { + 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 +341,7 @@ 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, 1); syslog(LOG_ERR, "Cannot join clvmd process group"); DEBUGLOG("Cannot join clvmd process group: %d\n", err); return ais_to_errno(err); @@ -362,15 +351,12 @@ &our_nodeid); if (err != SA_AIS_OK) { cpg_finalize(cpg_handle); - saLckFinalize(lck_handle); + dlm_release_lockspace(LOCKSPACE_NAME, lockspace, 1); 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 +367,7 @@ DEBUGLOG("cluster_closedown\n"); unlock_all(); - saLckFinalize(lck_handle); + dlm_release_lockspace(LOCKSPACE_NAME, lockspace, 1); cpg_finalize(cpg_handle); } @@ -472,156 +458,82 @@ return somedown; } -/* Real locking */ -static int _lock_resource(char *resource, int mode, int flags, int *lockid) -{ - struct lock_info *linfo; - SaLckResourceHandleT res_handle; - SaAisErrorT err; - SaLckLockIdT lock_id; - SaLckLockStatusT lockStatus; +static void sync_ast_routine(void *arg) +{ + struct lock_wait *lwait = arg; + + pthread_mutex_lock(&lwait->mutex); + pthread_cond_signal(&lwait->cond); + pthread_mutex_unlock(&lwait->mutex); +} - /* This needs to be converted from DLM/LVM2 value for OpenAIS LCK */ - if (flags & LCK_NONBLOCK) flags = SA_LCK_LOCK_NO_QUEUE; +static int _sync_lock(const char *resource, int mode, int flags, int *lockid) +{ + int status; + struct lock_wait lwait; - linfo = malloc(sizeof(struct lock_info)); - if (!linfo) + if (!lockid) { + errno = EINVAL; return -1; - - 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); } - err = saLckResourceLock( - res_handle, - &lock_id, + DEBUGLOG("sync_lock: '%s' mode:%d flags=%d\n", resource,mode,flags); + /* Conversions need the lockid in the LKSB */ + if (flags & LKF_CONVERT) + lwait.lksb.sb_lkid = *lockid; + + pthread_cond_init(&lwait.cond, NULL); + pthread_mutex_init(&lwait.mutex, NULL); + pthread_mutex_lock(&lwait.mutex); + + status = dlm_ls_lock(lockspace, mode, + &lwait.lksb, flags, - 0, - SA_TIME_END, - &lockStatus); - if (err != SA_AIS_OK && lockStatus != SA_LCK_LOCK_GRANTED) - { - free(linfo); - saLckResourceClose(res_handle); - return ais_to_errno(err); - } - - /* Wait for it to complete */ + resource, + strlen(resource), + 0, sync_ast_routine, &lwait, NULL, NULL); + if (status) + return status; - DEBUGLOG("lock_resource returning %d, lock_id=%llx\n", err, - lock_id); - - linfo->lock_id = lock_id; - linfo->res_handle = res_handle; - - dm_hash_insert(lock_hash, resource, linfo); - - return ais_to_errno(err); -} + /* Wait for it to complete */ + pthread_cond_wait(&lwait.cond, &lwait.mutex); + pthread_mutex_unlock(&lwait.mutex); + *lockid = lwait.lksb.sb_lkid; -static int _unlock_resource(char *resource, int lockid) -{ - SaAisErrorT err; - struct lock_info *linfo; - - DEBUGLOG("unlock_resource %s\n", resource); - linfo = dm_hash_lookup(lock_hash, resource); - if (!linfo) + errno = lwait.lksb.sb_status; + DEBUGLOG("sync_lock: returning lkid %x\n", *lockid); + if (lwait.lksb.sb_status) + return -1; + else return 0; - - DEBUGLOG("unlock_resource: lockid: %llx\n", linfo->lock_id); - err = saLckResourceUnlock(linfo->lock_id, SA_TIME_END); - if (err != SA_AIS_OK) - { - DEBUGLOG("Unlock returned %d\n", err); - return ais_to_errno(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) - { - 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; - } -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]; + struct lock_wait lwait; - snprintf(lock1, sizeof(lock1), "%s-1", resource); - snprintf(lock2, sizeof(lock2), "%s-2", resource); + DEBUGLOG("sync_unlock: '%s' lkid:%x\n", resource, lockid); - _unlock_resource(lock1, lockid); - _unlock_resource(lock2, lockid); + pthread_cond_init(&lwait.cond, NULL); + pthread_mutex_init(&lwait.mutex, NULL); + pthread_mutex_lock(&lwait.mutex); - return status; + status = dlm_ls_unlock(lockspace, lockid, 0, &lwait.lksb, &lwait); + + if (status) + return status; + /* Wait for it to complete */ + pthread_cond_wait(&lwait.cond, &lwait.mutex); + pthread_mutex_unlock(&lwait.mutex); + + errno = lwait.lksb.sb_status; + if (lwait.lksb.sb_status != EUNLOCK) + return -1; + else + return 0; } /* We are always quorate ! */ Index: LVM2.2.02.39/daemons/clvmd/Makefile.in =================================================================== --- LVM2.2.02.39.orig/daemons/clvmd/Makefile.in 2009-01-22 12:26:13.000000000 +0800 +++ LVM2.2.02.39/daemons/clvmd/Makefile.in 2009-01-22 12:27:34.000000000 +0800 @@ -59,7 +59,7 @@ 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 2009-01-22 12:26:13.000000000 +0800 +++ LVM2.2.02.39/daemons/clvmd/clvm.h 2009-01-22 12:35:08.000000000 +0800 @@ -79,6 +79,7 @@ /* 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 @@ #define LKF_HEADQUE (0x00001000) #define LKF_NOORDER (0x00002000) #endif +#endif