votequorum.c 24 KB

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