votequorum.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*
  35. * Provides a quorum API using the corosync executive
  36. */
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include <pthread.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <errno.h>
  44. #include <corosync/mar_gen.h>
  45. #include <corosync/ipc_gen.h>
  46. #include <corosync/coroipc.h>
  47. #include "corosync/votequorum.h"
  48. #include "corosync/ipc_votequorum.h"
  49. struct votequorum_inst {
  50. void *ipc_ctx;
  51. int finalize;
  52. void *context;
  53. votequorum_callbacks_t callbacks;
  54. pthread_mutex_t response_mutex;
  55. pthread_mutex_t dispatch_mutex;
  56. };
  57. static void votequorum_instance_destructor (void *instance);
  58. static struct saHandleDatabase votequorum_handle_t_db = {
  59. .handleCount = 0,
  60. .handles = 0,
  61. .mutex = PTHREAD_MUTEX_INITIALIZER,
  62. .handleInstanceDestructor = votequorum_instance_destructor
  63. };
  64. /*
  65. * Clean up function for a quorum instance (votequorum_initialize) handle
  66. */
  67. static void votequorum_instance_destructor (void *instance)
  68. {
  69. struct votequorum_inst *votequorum_inst = instance;
  70. pthread_mutex_destroy (&votequorum_inst->response_mutex);
  71. }
  72. cs_error_t votequorum_initialize (
  73. votequorum_handle_t *handle,
  74. votequorum_callbacks_t *callbacks)
  75. {
  76. cs_error_t error;
  77. struct votequorum_inst *votequorum_inst;
  78. error = saHandleCreate (&votequorum_handle_t_db, sizeof (struct votequorum_inst), handle);
  79. if (error != CS_OK) {
  80. goto error_no_destroy;
  81. }
  82. error = saHandleInstanceGet (&votequorum_handle_t_db, *handle, (void *)&votequorum_inst);
  83. if (error != CS_OK) {
  84. goto error_destroy;
  85. }
  86. error = cslib_service_connect (VOTEQUORUM_SERVICE, &votequorum_inst->ipc_ctx);
  87. if (error != CS_OK) {
  88. goto error_put_destroy;
  89. }
  90. pthread_mutex_init (&votequorum_inst->response_mutex, NULL);
  91. pthread_mutex_init (&votequorum_inst->dispatch_mutex, NULL);
  92. if (callbacks)
  93. memcpy(&votequorum_inst->callbacks, callbacks, sizeof (*callbacks));
  94. else
  95. memset(&votequorum_inst->callbacks, 0, sizeof (*callbacks));
  96. saHandleInstancePut (&votequorum_handle_t_db, *handle);
  97. return (CS_OK);
  98. error_put_destroy:
  99. saHandleInstancePut (&votequorum_handle_t_db, *handle);
  100. error_destroy:
  101. saHandleDestroy (&votequorum_handle_t_db, *handle);
  102. error_no_destroy:
  103. return (error);
  104. }
  105. cs_error_t votequorum_finalize (
  106. votequorum_handle_t handle)
  107. {
  108. struct votequorum_inst *votequorum_inst;
  109. cs_error_t error;
  110. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  111. if (error != CS_OK) {
  112. return (error);
  113. }
  114. pthread_mutex_lock (&votequorum_inst->response_mutex);
  115. /*
  116. * Another thread has already started finalizing
  117. */
  118. if (votequorum_inst->finalize) {
  119. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  120. saHandleInstancePut (&votequorum_handle_t_db, handle);
  121. return (CS_ERR_BAD_HANDLE);
  122. }
  123. votequorum_inst->finalize = 1;
  124. cslib_service_disconnect (votequorum_inst->ipc_ctx);
  125. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  126. saHandleDestroy (&votequorum_handle_t_db, handle);
  127. saHandleInstancePut (&votequorum_handle_t_db, handle);
  128. return (CS_OK);
  129. }
  130. cs_error_t votequorum_getinfo (
  131. votequorum_handle_t handle,
  132. unsigned int nodeid,
  133. struct votequorum_info *info)
  134. {
  135. cs_error_t error;
  136. struct votequorum_inst *votequorum_inst;
  137. struct iovec iov;
  138. struct req_lib_votequorum_getinfo req_lib_votequorum_getinfo;
  139. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  140. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  141. if (error != CS_OK) {
  142. return (error);
  143. }
  144. req_lib_votequorum_getinfo.header.size = sizeof (struct req_lib_votequorum_getinfo);
  145. req_lib_votequorum_getinfo.header.id = MESSAGE_REQ_VOTEQUORUM_GETINFO;
  146. req_lib_votequorum_getinfo.nodeid = nodeid;
  147. iov.iov_base = (char *)&req_lib_votequorum_getinfo;
  148. iov.iov_len = sizeof (struct req_lib_votequorum_getinfo);
  149. pthread_mutex_lock (&votequorum_inst->response_mutex);
  150. error = cslib_msg_send_reply_receive (
  151. votequorum_inst->ipc_ctx,
  152. &iov,
  153. 1,
  154. &res_lib_votequorum_getinfo,
  155. sizeof (struct res_lib_votequorum_getinfo));
  156. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  157. if (error != CS_OK) {
  158. goto error_exit;
  159. }
  160. error = res_lib_votequorum_getinfo.header.error;
  161. info->node_id = res_lib_votequorum_getinfo.nodeid;
  162. info->node_votes = res_lib_votequorum_getinfo.votes;
  163. info->node_expected_votes = res_lib_votequorum_getinfo.expected_votes;
  164. info->highest_expected = res_lib_votequorum_getinfo.highest_expected;
  165. info->total_votes = res_lib_votequorum_getinfo.total_votes;
  166. info->quorum = res_lib_votequorum_getinfo.quorum;
  167. info->flags = res_lib_votequorum_getinfo.flags;
  168. error_exit:
  169. saHandleInstancePut (&votequorum_handle_t_db, handle);
  170. return (error);
  171. }
  172. cs_error_t votequorum_setexpected (
  173. votequorum_handle_t handle,
  174. unsigned int expected_votes)
  175. {
  176. cs_error_t error;
  177. struct votequorum_inst *votequorum_inst;
  178. struct iovec iov;
  179. struct req_lib_votequorum_setexpected req_lib_votequorum_setexpected;
  180. struct res_lib_votequorum_status res_lib_votequorum_status;
  181. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  182. if (error != CS_OK) {
  183. return (error);
  184. }
  185. req_lib_votequorum_setexpected.header.size = sizeof (struct req_lib_votequorum_setexpected);
  186. req_lib_votequorum_setexpected.header.id = MESSAGE_REQ_VOTEQUORUM_SETEXPECTED;
  187. req_lib_votequorum_setexpected.expected_votes = expected_votes;
  188. iov.iov_base = (char *)&req_lib_votequorum_setexpected;
  189. iov.iov_len = sizeof (struct req_lib_votequorum_setexpected);
  190. pthread_mutex_lock (&votequorum_inst->response_mutex);
  191. error = cslib_msg_send_reply_receive (
  192. votequorum_inst->ipc_ctx,
  193. &iov,
  194. 1,
  195. &res_lib_votequorum_status,
  196. sizeof (struct res_lib_votequorum_status));
  197. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  198. if (error != CS_OK) {
  199. goto error_exit;
  200. }
  201. error = res_lib_votequorum_status.header.error;
  202. error_exit:
  203. saHandleInstancePut (&votequorum_handle_t_db, handle);
  204. return (error);
  205. }
  206. cs_error_t votequorum_setvotes (
  207. votequorum_handle_t handle,
  208. unsigned int nodeid,
  209. unsigned int votes)
  210. {
  211. cs_error_t error;
  212. struct votequorum_inst *votequorum_inst;
  213. struct iovec iov;
  214. struct req_lib_votequorum_setvotes req_lib_votequorum_setvotes;
  215. struct res_lib_votequorum_status res_lib_votequorum_status;
  216. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  217. if (error != CS_OK) {
  218. return (error);
  219. }
  220. req_lib_votequorum_setvotes.header.size = sizeof (struct req_lib_votequorum_setvotes);
  221. req_lib_votequorum_setvotes.header.id = MESSAGE_REQ_VOTEQUORUM_SETVOTES;
  222. req_lib_votequorum_setvotes.nodeid = nodeid;
  223. req_lib_votequorum_setvotes.votes = votes;
  224. iov.iov_base = (char *)&req_lib_votequorum_setvotes;
  225. iov.iov_len = sizeof (struct req_lib_votequorum_setvotes);
  226. pthread_mutex_lock (&votequorum_inst->response_mutex);
  227. error = cslib_msg_send_reply_receive (
  228. votequorum_inst->ipc_ctx,
  229. &iov,
  230. 1,
  231. &res_lib_votequorum_status,
  232. sizeof (struct res_lib_votequorum_status));
  233. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  234. if (error != CS_OK) {
  235. goto error_exit;
  236. }
  237. error = res_lib_votequorum_status.header.error;
  238. error_exit:
  239. saHandleInstancePut (&votequorum_handle_t_db, handle);
  240. return (error);
  241. }
  242. cs_error_t votequorum_qdisk_register (
  243. votequorum_handle_t handle,
  244. char *name,
  245. unsigned int votes)
  246. {
  247. cs_error_t error;
  248. struct votequorum_inst *votequorum_inst;
  249. struct iovec iov;
  250. struct req_lib_votequorum_qdisk_register req_lib_votequorum_qdisk_register;
  251. struct res_lib_votequorum_status res_lib_votequorum_status;
  252. if (strlen(name) > VOTEQUORUM_MAX_QDISK_NAME_LEN)
  253. return CS_ERR_INVALID_PARAM;
  254. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  255. if (error != CS_OK) {
  256. return (error);
  257. }
  258. req_lib_votequorum_qdisk_register.header.size = sizeof (struct req_lib_votequorum_qdisk_register);
  259. req_lib_votequorum_qdisk_register.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_REGISTER;
  260. strcpy(req_lib_votequorum_qdisk_register.name, name);
  261. req_lib_votequorum_qdisk_register.votes = votes;
  262. iov.iov_base = (char *)&req_lib_votequorum_qdisk_register;
  263. iov.iov_len = sizeof (struct req_lib_votequorum_qdisk_register);
  264. pthread_mutex_lock (&votequorum_inst->response_mutex);
  265. error = cslib_msg_send_reply_receive (
  266. votequorum_inst->ipc_ctx,
  267. &iov,
  268. 1,
  269. &res_lib_votequorum_status,
  270. sizeof (struct res_lib_votequorum_status));
  271. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  272. if (error != CS_OK) {
  273. goto error_exit;
  274. }
  275. error = res_lib_votequorum_status.header.error;
  276. error_exit:
  277. saHandleInstancePut (&votequorum_handle_t_db, handle);
  278. return (error);
  279. }
  280. cs_error_t votequorum_qdisk_poll (
  281. votequorum_handle_t handle,
  282. unsigned int state)
  283. {
  284. cs_error_t error;
  285. struct votequorum_inst *votequorum_inst;
  286. struct iovec iov;
  287. struct req_lib_votequorum_qdisk_poll req_lib_votequorum_qdisk_poll;
  288. struct res_lib_votequorum_status res_lib_votequorum_status;
  289. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  290. if (error != CS_OK) {
  291. return (error);
  292. }
  293. req_lib_votequorum_qdisk_poll.header.size = sizeof (struct req_lib_votequorum_qdisk_poll);
  294. req_lib_votequorum_qdisk_poll.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_POLL;
  295. req_lib_votequorum_qdisk_poll.state = state;
  296. iov.iov_base = (char *)&req_lib_votequorum_qdisk_poll;
  297. iov.iov_len = sizeof (struct req_lib_votequorum_qdisk_poll);
  298. pthread_mutex_lock (&votequorum_inst->response_mutex);
  299. error = cslib_msg_send_reply_receive (
  300. votequorum_inst->ipc_ctx,
  301. &iov,
  302. 1,
  303. &res_lib_votequorum_status,
  304. sizeof (struct res_lib_votequorum_status));
  305. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  306. if (error != CS_OK) {
  307. goto error_exit;
  308. }
  309. error = res_lib_votequorum_status.header.error;
  310. error_exit:
  311. saHandleInstancePut (&votequorum_handle_t_db, handle);
  312. return (error);
  313. }
  314. cs_error_t votequorum_qdisk_unregister (
  315. votequorum_handle_t handle)
  316. {
  317. cs_error_t error;
  318. struct votequorum_inst *votequorum_inst;
  319. struct iovec iov;
  320. struct req_lib_votequorum_general req_lib_votequorum_general;
  321. struct res_lib_votequorum_status res_lib_votequorum_status;
  322. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  323. if (error != CS_OK) {
  324. return (error);
  325. }
  326. pthread_mutex_lock (&votequorum_inst->response_mutex);
  327. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  328. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_UNREGISTER;
  329. iov.iov_base = (char *)&req_lib_votequorum_general;
  330. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  331. error = cslib_msg_send_reply_receive (
  332. votequorum_inst->ipc_ctx,
  333. &iov,
  334. 1,
  335. &res_lib_votequorum_status,
  336. sizeof (struct res_lib_votequorum_status));
  337. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  338. if (error != CS_OK) {
  339. goto error_exit;
  340. }
  341. error = res_lib_votequorum_status.header.error;
  342. error_exit:
  343. saHandleInstancePut (&votequorum_handle_t_db, handle);
  344. return (error);
  345. }
  346. cs_error_t votequorum_qdisk_getinfo (
  347. votequorum_handle_t handle,
  348. struct votequorum_qdisk_info *qinfo)
  349. {
  350. cs_error_t error;
  351. struct votequorum_inst *votequorum_inst;
  352. struct iovec iov;
  353. struct req_lib_votequorum_general req_lib_votequorum_general;
  354. struct res_lib_votequorum_qdisk_getinfo res_lib_votequorum_qdisk_getinfo;
  355. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  356. if (error != CS_OK) {
  357. return (error);
  358. }
  359. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  360. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_QDISK_GETINFO;
  361. iov.iov_base = (char *)&req_lib_votequorum_general;
  362. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  363. pthread_mutex_lock (&votequorum_inst->response_mutex);
  364. error = cslib_msg_send_reply_receive (
  365. votequorum_inst->ipc_ctx,
  366. &iov,
  367. 1,
  368. &res_lib_votequorum_qdisk_getinfo,
  369. sizeof (struct res_lib_votequorum_qdisk_getinfo));
  370. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  371. if (error != CS_OK) {
  372. goto error_exit;
  373. }
  374. error = res_lib_votequorum_qdisk_getinfo.header.error;
  375. qinfo->votes = res_lib_votequorum_qdisk_getinfo.votes;
  376. qinfo->state = res_lib_votequorum_qdisk_getinfo.state;
  377. strcpy(qinfo->name, res_lib_votequorum_qdisk_getinfo.name);
  378. error_exit:
  379. saHandleInstancePut (&votequorum_handle_t_db, handle);
  380. return (error);
  381. }
  382. cs_error_t votequorum_setstate (
  383. votequorum_handle_t handle)
  384. {
  385. cs_error_t error;
  386. struct votequorum_inst *votequorum_inst;
  387. struct iovec iov;
  388. struct req_lib_votequorum_general req_lib_votequorum_general;
  389. struct res_lib_votequorum_status res_lib_votequorum_status;
  390. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  391. if (error != CS_OK) {
  392. return (error);
  393. }
  394. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  395. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_SETSTATE;
  396. iov.iov_base = (char *)&req_lib_votequorum_general;
  397. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  398. pthread_mutex_lock (&votequorum_inst->response_mutex);
  399. error = cslib_msg_send_reply_receive (
  400. votequorum_inst->ipc_ctx,
  401. &iov,
  402. 1,
  403. &res_lib_votequorum_status,
  404. sizeof (struct res_lib_votequorum_status));
  405. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  406. if (error != CS_OK) {
  407. goto error_exit;
  408. }
  409. error = res_lib_votequorum_status.header.error;
  410. error_exit:
  411. saHandleInstancePut (&votequorum_handle_t_db, handle);
  412. return (error);
  413. }
  414. cs_error_t votequorum_leaving (
  415. votequorum_handle_t handle)
  416. {
  417. cs_error_t error;
  418. struct votequorum_inst *votequorum_inst;
  419. struct iovec iov;
  420. struct req_lib_votequorum_general req_lib_votequorum_general;
  421. struct res_lib_votequorum_status res_lib_votequorum_status;
  422. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  423. if (error != CS_OK) {
  424. return (error);
  425. }
  426. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  427. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_LEAVING;
  428. iov.iov_base = (char *)&req_lib_votequorum_general;
  429. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  430. pthread_mutex_lock (&votequorum_inst->response_mutex);
  431. error = cslib_msg_send_reply_receive (
  432. votequorum_inst->ipc_ctx,
  433. &iov,
  434. 1,
  435. &res_lib_votequorum_status,
  436. sizeof (struct res_lib_votequorum_status));
  437. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  438. if (error != CS_OK) {
  439. goto error_exit;
  440. }
  441. error = res_lib_votequorum_status.header.error;
  442. error_exit:
  443. saHandleInstancePut (&votequorum_handle_t_db, handle);
  444. return (error);
  445. }
  446. cs_error_t votequorum_trackstart (
  447. votequorum_handle_t handle,
  448. uint64_t context,
  449. unsigned int flags)
  450. {
  451. cs_error_t error;
  452. struct votequorum_inst *votequorum_inst;
  453. struct iovec iov;
  454. struct req_lib_votequorum_trackstart req_lib_votequorum_trackstart;
  455. struct res_lib_votequorum_status res_lib_votequorum_status;
  456. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  457. if (error != CS_OK) {
  458. return (error);
  459. }
  460. req_lib_votequorum_trackstart.header.size = sizeof (struct req_lib_votequorum_trackstart);
  461. req_lib_votequorum_trackstart.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTART;
  462. req_lib_votequorum_trackstart.track_flags = flags;
  463. req_lib_votequorum_trackstart.context = context;
  464. iov.iov_base = (char *)&req_lib_votequorum_trackstart;
  465. iov.iov_len = sizeof (struct req_lib_votequorum_trackstart);
  466. pthread_mutex_lock (&votequorum_inst->response_mutex);
  467. error = cslib_msg_send_reply_receive (
  468. votequorum_inst->ipc_ctx,
  469. &iov,
  470. 1,
  471. &res_lib_votequorum_status,
  472. sizeof (struct res_lib_votequorum_status));
  473. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  474. if (error != CS_OK) {
  475. goto error_exit;
  476. }
  477. error = res_lib_votequorum_status.header.error;
  478. error_exit:
  479. saHandleInstancePut (&votequorum_handle_t_db, handle);
  480. return (error);
  481. }
  482. cs_error_t votequorum_trackstop (
  483. votequorum_handle_t handle)
  484. {
  485. cs_error_t error;
  486. struct votequorum_inst *votequorum_inst;
  487. struct iovec iov;
  488. struct req_lib_votequorum_general req_lib_votequorum_general;
  489. struct res_lib_votequorum_status res_lib_votequorum_status;
  490. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  491. if (error != CS_OK) {
  492. return (error);
  493. }
  494. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  495. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTOP;
  496. iov.iov_base = (char *)&req_lib_votequorum_general;
  497. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  498. pthread_mutex_lock (&votequorum_inst->response_mutex);
  499. error = cslib_msg_send_reply_receive (
  500. votequorum_inst->ipc_ctx,
  501. &iov,
  502. 1,
  503. &res_lib_votequorum_status,
  504. sizeof (struct res_lib_votequorum_status));
  505. pthread_mutex_unlock (&votequorum_inst->response_mutex);
  506. if (error != CS_OK) {
  507. goto error_exit;
  508. }
  509. error = res_lib_votequorum_status.header.error;
  510. error_exit:
  511. saHandleInstancePut (&votequorum_handle_t_db, handle);
  512. return (error);
  513. }
  514. cs_error_t votequorum_context_get (
  515. votequorum_handle_t handle,
  516. void **context)
  517. {
  518. cs_error_t error;
  519. struct votequorum_inst *votequorum_inst;
  520. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  521. if (error != CS_OK) {
  522. return (error);
  523. }
  524. *context = votequorum_inst->context;
  525. saHandleInstancePut (&votequorum_handle_t_db, handle);
  526. return (CS_OK);
  527. }
  528. cs_error_t votequorum_context_set (
  529. votequorum_handle_t handle,
  530. void *context)
  531. {
  532. cs_error_t error;
  533. struct votequorum_inst *votequorum_inst;
  534. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  535. if (error != CS_OK) {
  536. return (error);
  537. }
  538. votequorum_inst->context = context;
  539. saHandleInstancePut (&votequorum_handle_t_db, handle);
  540. return (CS_OK);
  541. }
  542. cs_error_t votequorum_fd_get (
  543. votequorum_handle_t handle,
  544. int *fd)
  545. {
  546. cs_error_t error;
  547. struct votequorum_inst *votequorum_inst;
  548. error = saHandleInstanceGet (&votequorum_handle_t_db, handle, (void *)&votequorum_inst);
  549. if (error != CS_OK) {
  550. return (error);
  551. }
  552. *fd = cslib_fd_get (votequorum_inst->ipc_ctx);
  553. (void)saHandleInstancePut (&votequorum_handle_t_db, handle);
  554. return (CS_OK);
  555. }
  556. struct res_overlay {
  557. mar_res_header_t header __attribute__((aligned(8)));
  558. char data[512000];
  559. };
  560. cs_error_t votequorum_dispatch (
  561. votequorum_handle_t handle,
  562. cs_dispatch_flags_t dispatch_types)
  563. {
  564. int timeout = -1;
  565. cs_error_t error;
  566. int cont = 1; /* always continue do loop except when set to 0 */
  567. int dispatch_avail;
  568. struct votequorum_inst *votequorum_inst;
  569. votequorum_callbacks_t callbacks;
  570. struct res_overlay dispatch_data;
  571. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  572. struct res_lib_votequorum_expectedvotes_notification *res_lib_votequorum_expectedvotes_notification;
  573. if (dispatch_types != CS_DISPATCH_ONE &&
  574. dispatch_types != CS_DISPATCH_ALL &&
  575. dispatch_types != CS_DISPATCH_BLOCKING) {
  576. return (CS_ERR_INVALID_PARAM);
  577. }
  578. error = saHandleInstanceGet (&votequorum_handle_t_db, handle,
  579. (void *)&votequorum_inst);
  580. if (error != CS_OK) {
  581. return (error);
  582. }
  583. /*
  584. * Timeout instantly for CS_DISPATCH_ONE or CS_DISPATCH_ALL and
  585. * wait indefinitely for CS_DISPATCH_BLOCKING
  586. */
  587. if (dispatch_types == CS_DISPATCH_ALL) {
  588. timeout = 0;
  589. }
  590. do {
  591. pthread_mutex_lock (&votequorum_inst->dispatch_mutex);
  592. dispatch_avail = cslib_dispatch_recv (
  593. votequorum_inst->ipc_ctx,
  594. (void *)&dispatch_data, timeout);
  595. /*
  596. * Handle has been finalized in another thread
  597. */
  598. if (votequorum_inst->finalize == 1) {
  599. error = CS_OK;
  600. goto error_unlock;
  601. }
  602. if (dispatch_avail == 0 && dispatch_types == CS_DISPATCH_ALL) {
  603. pthread_mutex_unlock (&votequorum_inst->dispatch_mutex);
  604. break; /* exit do while cont is 1 loop */
  605. } else
  606. if (dispatch_avail == 0) {
  607. pthread_mutex_unlock (&votequorum_inst->dispatch_mutex);
  608. continue; /* next poll */
  609. }
  610. /*
  611. * Make copy of callbacks, message data, unlock instance, and call callback
  612. * A risk of this dispatch method is that the callback routines may
  613. * operate at the same time that votequorum_finalize has been called in another thread.
  614. */
  615. memcpy (&callbacks, &votequorum_inst->callbacks, sizeof (votequorum_callbacks_t));
  616. pthread_mutex_unlock (&votequorum_inst->dispatch_mutex);
  617. /*
  618. * Dispatch incoming message
  619. */
  620. switch (dispatch_data.header.id) {
  621. case MESSAGE_RES_VOTEQUORUM_NOTIFICATION:
  622. if (callbacks.votequorum_notify_fn == NULL) {
  623. continue;
  624. }
  625. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)&dispatch_data;
  626. callbacks.votequorum_notify_fn ( handle,
  627. res_lib_votequorum_notification->context,
  628. res_lib_votequorum_notification->quorate,
  629. res_lib_votequorum_notification->node_list_entries,
  630. (votequorum_node_t *)res_lib_votequorum_notification->node_list );
  631. ;
  632. break;
  633. case MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION:
  634. if (callbacks.votequorum_expectedvotes_notify_fn == NULL) {
  635. continue;
  636. }
  637. res_lib_votequorum_expectedvotes_notification = (struct res_lib_votequorum_expectedvotes_notification *)&dispatch_data;
  638. callbacks.votequorum_expectedvotes_notify_fn ( handle,
  639. res_lib_votequorum_expectedvotes_notification->context,
  640. res_lib_votequorum_expectedvotes_notification->expected_votes);
  641. break;
  642. default:
  643. error = CS_ERR_LIBRARY;
  644. goto error_put;
  645. break;
  646. }
  647. /*
  648. * Determine if more messages should be processed
  649. * */
  650. switch (dispatch_types) {
  651. case CS_DISPATCH_ONE:
  652. cont = 0;
  653. break;
  654. case CS_DISPATCH_ALL:
  655. break;
  656. case CS_DISPATCH_BLOCKING:
  657. break;
  658. }
  659. } while (cont);
  660. goto error_put;
  661. error_unlock:
  662. pthread_mutex_unlock (&votequorum_inst->dispatch_mutex);
  663. error_put:
  664. saHandleInstancePut (&votequorum_handle_t_db, handle);
  665. return (error);
  666. }