votequorum.c 24 KB

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