votequorum.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. /*
  2. * Copyright (c) 2009-2012 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 <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/uio.h>
  45. #include <errno.h>
  46. #include <qb/qbdefs.h>
  47. #include <qb/qbipcc.h>
  48. #include <corosync/corotypes.h>
  49. #include <corosync/corodefs.h>
  50. #include <corosync/hdb.h>
  51. #include <corosync/votequorum.h>
  52. #include <corosync/ipc_votequorum.h>
  53. #include "util.h"
  54. struct votequorum_inst {
  55. qb_ipcc_connection_t *c;
  56. int finalize;
  57. void *context;
  58. votequorum_model_v1_data_t callbacks;
  59. };
  60. static void votequorum_inst_free (void *inst);
  61. DECLARE_HDB_DATABASE(votequorum_handle_t_db, votequorum_inst_free);
  62. cs_error_t votequorum_initialize (
  63. votequorum_handle_t *handle,
  64. votequorum_callbacks_t *callbacks)
  65. {
  66. votequorum_model_v1_data_t model = {
  67. .model = VOTEQUORUM_MODEL_V1,
  68. .votequorum_quorum_notify_fn = callbacks->votequorum_quorum_notify_fn,
  69. .votequorum_expectedvotes_notify_fn = callbacks->votequorum_expectedvotes_notify_fn,
  70. .votequorum_nodelist_notify_fn = callbacks->votequorum_nodelist_notify_fn,
  71. .votequorum_qdevice_extra_info_fn = NULL,
  72. };
  73. return votequorum_model_initialize (handle, &model);
  74. }
  75. cs_error_t votequorum_model_initialize (
  76. votequorum_handle_t *handle,
  77. const votequorum_model_v1_data_t *model)
  78. {
  79. cs_error_t error;
  80. struct votequorum_inst *votequorum_inst;
  81. error = hdb_error_to_cs(hdb_handle_create (&votequorum_handle_t_db, sizeof (struct votequorum_inst), handle));
  82. if (error != CS_OK) {
  83. goto error_no_destroy;
  84. }
  85. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, *handle, (void *)&votequorum_inst));
  86. if (error != CS_OK) {
  87. goto error_destroy;
  88. }
  89. votequorum_inst->finalize = 0;
  90. votequorum_inst->c = qb_ipcc_connect ("votequorum", IPC_REQUEST_SIZE);
  91. if (votequorum_inst->c == NULL) {
  92. error = qb_to_cs_error(-errno);
  93. goto error_put_destroy;
  94. }
  95. if (model)
  96. memcpy(&votequorum_inst->callbacks, model, sizeof (*model));
  97. else
  98. memset(&votequorum_inst->callbacks, 0, sizeof (*model));
  99. hdb_handle_put (&votequorum_handle_t_db, *handle);
  100. return (CS_OK);
  101. error_put_destroy:
  102. hdb_handle_put (&votequorum_handle_t_db, *handle);
  103. error_destroy:
  104. hdb_handle_destroy (&votequorum_handle_t_db, *handle);
  105. error_no_destroy:
  106. return (error);
  107. }
  108. static void votequorum_inst_free (void *inst)
  109. {
  110. struct votequorum_inst *vq_inst = (struct votequorum_inst *)inst;
  111. qb_ipcc_disconnect(vq_inst->c);
  112. }
  113. cs_error_t votequorum_finalize (
  114. votequorum_handle_t handle)
  115. {
  116. struct votequorum_inst *votequorum_inst;
  117. cs_error_t error;
  118. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  119. if (error != CS_OK) {
  120. return (error);
  121. }
  122. /*
  123. * Another thread has already started finalizing
  124. */
  125. if (votequorum_inst->finalize) {
  126. hdb_handle_put (&votequorum_handle_t_db, handle);
  127. return (CS_ERR_BAD_HANDLE);
  128. }
  129. votequorum_inst->finalize = 1;
  130. hdb_handle_destroy (&votequorum_handle_t_db, handle);
  131. hdb_handle_put (&votequorum_handle_t_db, handle);
  132. return (CS_OK);
  133. }
  134. cs_error_t votequorum_getinfo (
  135. votequorum_handle_t handle,
  136. unsigned int nodeid,
  137. struct votequorum_info *info)
  138. {
  139. cs_error_t error;
  140. struct votequorum_inst *votequorum_inst;
  141. struct iovec iov;
  142. struct req_lib_votequorum_getinfo req_lib_votequorum_getinfo;
  143. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  144. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  145. if (error != CS_OK) {
  146. return (error);
  147. }
  148. req_lib_votequorum_getinfo.header.size = sizeof (struct req_lib_votequorum_getinfo);
  149. req_lib_votequorum_getinfo.header.id = MESSAGE_REQ_VOTEQUORUM_GETINFO;
  150. req_lib_votequorum_getinfo.nodeid = nodeid;
  151. iov.iov_base = (char *)&req_lib_votequorum_getinfo;
  152. iov.iov_len = sizeof (struct req_lib_votequorum_getinfo);
  153. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  154. votequorum_inst->c,
  155. &iov,
  156. 1,
  157. &res_lib_votequorum_getinfo,
  158. sizeof (struct res_lib_votequorum_getinfo), CS_IPC_TIMEOUT_MS));
  159. if (error != CS_OK) {
  160. goto error_exit;
  161. }
  162. error = res_lib_votequorum_getinfo.header.error;
  163. info->node_id = res_lib_votequorum_getinfo.nodeid;
  164. info->node_state = res_lib_votequorum_getinfo.state;
  165. info->node_votes = res_lib_votequorum_getinfo.votes;
  166. info->node_expected_votes = res_lib_votequorum_getinfo.expected_votes;
  167. info->highest_expected = res_lib_votequorum_getinfo.highest_expected;
  168. info->total_votes = res_lib_votequorum_getinfo.total_votes;
  169. info->quorum = res_lib_votequorum_getinfo.quorum;
  170. info->flags = res_lib_votequorum_getinfo.flags;
  171. info->qdevice_votes = res_lib_votequorum_getinfo.qdevice_votes;
  172. memset(info->qdevice_name, 0, VOTEQUORUM_QDEVICE_MAX_NAME_LEN);
  173. strcpy(info->qdevice_name, res_lib_votequorum_getinfo.qdevice_name);
  174. error_exit:
  175. hdb_handle_put (&votequorum_handle_t_db, handle);
  176. return (error);
  177. }
  178. cs_error_t votequorum_setexpected (
  179. votequorum_handle_t handle,
  180. unsigned int expected_votes)
  181. {
  182. cs_error_t error;
  183. struct votequorum_inst *votequorum_inst;
  184. struct iovec iov;
  185. struct req_lib_votequorum_setexpected req_lib_votequorum_setexpected;
  186. struct res_lib_votequorum_status res_lib_votequorum_status;
  187. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  188. if (error != CS_OK) {
  189. return (error);
  190. }
  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.iov_base = (char *)&req_lib_votequorum_setexpected;
  195. iov.iov_len = sizeof (struct req_lib_votequorum_setexpected);
  196. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  197. votequorum_inst->c,
  198. &iov,
  199. 1,
  200. &res_lib_votequorum_status,
  201. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  202. if (error != CS_OK) {
  203. goto error_exit;
  204. }
  205. error = res_lib_votequorum_status.header.error;
  206. error_exit:
  207. hdb_handle_put (&votequorum_handle_t_db, handle);
  208. return (error);
  209. }
  210. cs_error_t votequorum_setvotes (
  211. votequorum_handle_t handle,
  212. unsigned int nodeid,
  213. unsigned int votes)
  214. {
  215. cs_error_t error;
  216. struct votequorum_inst *votequorum_inst;
  217. struct iovec iov;
  218. struct req_lib_votequorum_setvotes req_lib_votequorum_setvotes;
  219. struct res_lib_votequorum_status res_lib_votequorum_status;
  220. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  221. if (error != CS_OK) {
  222. return (error);
  223. }
  224. req_lib_votequorum_setvotes.header.size = sizeof (struct req_lib_votequorum_setvotes);
  225. req_lib_votequorum_setvotes.header.id = MESSAGE_REQ_VOTEQUORUM_SETVOTES;
  226. req_lib_votequorum_setvotes.nodeid = nodeid;
  227. req_lib_votequorum_setvotes.votes = votes;
  228. iov.iov_base = (char *)&req_lib_votequorum_setvotes;
  229. iov.iov_len = sizeof (struct req_lib_votequorum_setvotes);
  230. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  231. votequorum_inst->c,
  232. &iov,
  233. 1,
  234. &res_lib_votequorum_status,
  235. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  236. if (error != CS_OK) {
  237. goto error_exit;
  238. }
  239. error = res_lib_votequorum_status.header.error;
  240. error_exit:
  241. hdb_handle_put (&votequorum_handle_t_db, handle);
  242. return (error);
  243. }
  244. cs_error_t votequorum_trackstart (
  245. votequorum_handle_t handle,
  246. uint64_t context,
  247. unsigned int flags)
  248. {
  249. cs_error_t error;
  250. struct votequorum_inst *votequorum_inst;
  251. struct iovec iov;
  252. struct req_lib_votequorum_trackstart req_lib_votequorum_trackstart;
  253. struct res_lib_votequorum_status res_lib_votequorum_status;
  254. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  255. if (error != CS_OK) {
  256. return (error);
  257. }
  258. req_lib_votequorum_trackstart.header.size = sizeof (struct req_lib_votequorum_trackstart);
  259. req_lib_votequorum_trackstart.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTART;
  260. req_lib_votequorum_trackstart.track_flags = flags;
  261. req_lib_votequorum_trackstart.context = context;
  262. iov.iov_base = (char *)&req_lib_votequorum_trackstart;
  263. iov.iov_len = sizeof (struct req_lib_votequorum_trackstart);
  264. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  265. votequorum_inst->c,
  266. &iov,
  267. 1,
  268. &res_lib_votequorum_status,
  269. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  270. if (error != CS_OK) {
  271. goto error_exit;
  272. }
  273. error = res_lib_votequorum_status.header.error;
  274. error_exit:
  275. hdb_handle_put (&votequorum_handle_t_db, handle);
  276. return (error);
  277. }
  278. cs_error_t votequorum_trackstop (
  279. votequorum_handle_t handle)
  280. {
  281. cs_error_t error;
  282. struct votequorum_inst *votequorum_inst;
  283. struct iovec iov;
  284. struct req_lib_votequorum_general req_lib_votequorum_general;
  285. struct res_lib_votequorum_status res_lib_votequorum_status;
  286. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  287. if (error != CS_OK) {
  288. return (error);
  289. }
  290. req_lib_votequorum_general.header.size = sizeof (struct req_lib_votequorum_general);
  291. req_lib_votequorum_general.header.id = MESSAGE_REQ_VOTEQUORUM_TRACKSTOP;
  292. iov.iov_base = (char *)&req_lib_votequorum_general;
  293. iov.iov_len = sizeof (struct req_lib_votequorum_general);
  294. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  295. votequorum_inst->c,
  296. &iov,
  297. 1,
  298. &res_lib_votequorum_status,
  299. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  300. if (error != CS_OK) {
  301. goto error_exit;
  302. }
  303. error = res_lib_votequorum_status.header.error;
  304. error_exit:
  305. hdb_handle_put (&votequorum_handle_t_db, handle);
  306. return (error);
  307. }
  308. cs_error_t votequorum_context_get (
  309. votequorum_handle_t handle,
  310. void **context)
  311. {
  312. cs_error_t error;
  313. struct votequorum_inst *votequorum_inst;
  314. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  315. if (error != CS_OK) {
  316. return (error);
  317. }
  318. *context = votequorum_inst->context;
  319. hdb_handle_put (&votequorum_handle_t_db, handle);
  320. return (CS_OK);
  321. }
  322. cs_error_t votequorum_context_set (
  323. votequorum_handle_t handle,
  324. void *context)
  325. {
  326. cs_error_t error;
  327. struct votequorum_inst *votequorum_inst;
  328. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  329. if (error != CS_OK) {
  330. return (error);
  331. }
  332. votequorum_inst->context = context;
  333. hdb_handle_put (&votequorum_handle_t_db, handle);
  334. return (CS_OK);
  335. }
  336. cs_error_t votequorum_fd_get (
  337. votequorum_handle_t handle,
  338. int *fd)
  339. {
  340. cs_error_t error;
  341. struct votequorum_inst *votequorum_inst;
  342. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  343. if (error != CS_OK) {
  344. return (error);
  345. }
  346. error = qb_to_cs_error(qb_ipcc_fd_get (votequorum_inst->c, fd));
  347. (void)hdb_handle_put (&votequorum_handle_t_db, handle);
  348. return (error);
  349. }
  350. cs_error_t votequorum_dispatch (
  351. votequorum_handle_t handle,
  352. cs_dispatch_flags_t dispatch_types)
  353. {
  354. int timeout = -1;
  355. cs_error_t error;
  356. int cont = 1; /* always continue do loop except when set to 0 */
  357. struct votequorum_inst *votequorum_inst;
  358. votequorum_model_v1_data_t callbacks;
  359. struct qb_ipc_response_header *dispatch_data;
  360. struct res_lib_votequorum_quorum_notification *res_lib_votequorum_quorum_notification;
  361. struct res_lib_votequorum_nodelist_notification *res_lib_votequorum_nodelist_notification;
  362. struct res_lib_votequorum_expectedvotes_notification *res_lib_votequorum_expectedvotes_notification;
  363. struct res_lib_votequorum_qdevice_extra_info_notification *res_lib_votequorum_qdevice_extra_info_notification;
  364. char dispatch_buf[IPC_DISPATCH_SIZE];
  365. votequorum_ring_id_t ring_id;
  366. if (dispatch_types != CS_DISPATCH_ONE &&
  367. dispatch_types != CS_DISPATCH_ALL &&
  368. dispatch_types != CS_DISPATCH_BLOCKING &&
  369. dispatch_types != CS_DISPATCH_ONE_NONBLOCKING) {
  370. return (CS_ERR_INVALID_PARAM);
  371. }
  372. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle,
  373. (void *)&votequorum_inst));
  374. if (error != CS_OK) {
  375. return (error);
  376. }
  377. /*
  378. * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
  379. * wait indefinately for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
  380. */
  381. if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  382. timeout = 0;
  383. }
  384. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  385. do {
  386. error = qb_to_cs_error (qb_ipcc_event_recv (
  387. votequorum_inst->c,
  388. dispatch_buf,
  389. IPC_DISPATCH_SIZE,
  390. timeout));
  391. if (error == CS_ERR_BAD_HANDLE) {
  392. error = CS_OK;
  393. goto error_put;
  394. }
  395. if (error == CS_ERR_TRY_AGAIN) {
  396. if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  397. /*
  398. * Don't mask error
  399. */
  400. goto error_put;
  401. }
  402. error = CS_OK;
  403. if (dispatch_types == CS_DISPATCH_ALL) {
  404. break; /* exit do while cont is 1 loop */
  405. } else {
  406. continue; /* next poll */
  407. }
  408. }
  409. if (error != CS_OK) {
  410. goto error_put;
  411. }
  412. /*
  413. * Make copy of callbacks, message data, unlock instance, and call callback
  414. * A risk of this dispatch method is that the callback routines may
  415. * operate at the same time that votequorum_finalize has been called in another thread.
  416. */
  417. memcpy (&callbacks, &votequorum_inst->callbacks, sizeof (votequorum_model_v1_data_t));
  418. /*
  419. * Dispatch incoming message
  420. */
  421. switch (dispatch_data->id) {
  422. case MESSAGE_RES_VOTEQUORUM_QUORUM_NOTIFICATION:
  423. if (callbacks.votequorum_quorum_notify_fn == NULL) {
  424. break;
  425. }
  426. res_lib_votequorum_quorum_notification = (struct res_lib_votequorum_quorum_notification *)dispatch_data;
  427. callbacks.votequorum_quorum_notify_fn ( handle,
  428. res_lib_votequorum_quorum_notification->context,
  429. res_lib_votequorum_quorum_notification->quorate,
  430. res_lib_votequorum_quorum_notification->node_list_entries,
  431. (votequorum_node_t *)res_lib_votequorum_quorum_notification->node_list );
  432. break;
  433. case MESSAGE_RES_VOTEQUORUM_NODELIST_NOTIFICATION:
  434. if (callbacks.votequorum_nodelist_notify_fn == NULL) {
  435. break;
  436. }
  437. res_lib_votequorum_nodelist_notification = (struct res_lib_votequorum_nodelist_notification *)dispatch_data;
  438. marshall_from_mar_votequorum_ring_id (&ring_id, &res_lib_votequorum_nodelist_notification->ring_id);
  439. callbacks.votequorum_nodelist_notify_fn ( handle,
  440. res_lib_votequorum_nodelist_notification->context,
  441. ring_id,
  442. res_lib_votequorum_nodelist_notification->node_list_entries,
  443. res_lib_votequorum_nodelist_notification->node_list );
  444. break;
  445. case MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION:
  446. if (callbacks.votequorum_expectedvotes_notify_fn == NULL) {
  447. break;
  448. }
  449. res_lib_votequorum_expectedvotes_notification = (struct res_lib_votequorum_expectedvotes_notification *)dispatch_data;
  450. callbacks.votequorum_expectedvotes_notify_fn ( handle,
  451. res_lib_votequorum_expectedvotes_notification->context,
  452. res_lib_votequorum_expectedvotes_notification->expected_votes);
  453. break;
  454. case MESSAGE_RES_VOTEQUORUM_QDEVICE_EXTRA_INFO_NOTIFICATION:
  455. if (callbacks.votequorum_qdevice_extra_info_fn == NULL) {
  456. break;
  457. }
  458. res_lib_votequorum_qdevice_extra_info_notification = (struct res_lib_votequorum_qdevice_extra_info_notification *)dispatch_data;
  459. callbacks.votequorum_qdevice_extra_info_fn(handle,
  460. res_lib_votequorum_qdevice_extra_info_notification->context,
  461. res_lib_votequorum_qdevice_extra_info_notification->nodeid,
  462. res_lib_votequorum_qdevice_extra_info_notification->ei_size,
  463. res_lib_votequorum_qdevice_extra_info_notification->extra_info);
  464. break;
  465. default:
  466. error = CS_ERR_LIBRARY;
  467. goto error_put;
  468. break;
  469. }
  470. if (votequorum_inst->finalize) {
  471. /*
  472. * If the finalize has been called then get out of the dispatch.
  473. */
  474. error = CS_ERR_BAD_HANDLE;
  475. goto error_put;
  476. }
  477. /*
  478. * Determine if more messages should be processed
  479. */
  480. if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  481. cont = 0;
  482. }
  483. } while (cont);
  484. error_put:
  485. hdb_handle_put (&votequorum_handle_t_db, handle);
  486. return (error);
  487. }
  488. cs_error_t votequorum_qdevice_register (
  489. votequorum_handle_t handle,
  490. const char *name)
  491. {
  492. cs_error_t error;
  493. struct votequorum_inst *votequorum_inst;
  494. struct iovec iov;
  495. struct req_lib_votequorum_qdevice_register req_lib_votequorum_qdevice_register;
  496. struct res_lib_votequorum_status res_lib_votequorum_status;
  497. if ((strlen(name) == 0) ||
  498. (strlen(name) >= VOTEQUORUM_QDEVICE_MAX_NAME_LEN)) {
  499. return CS_ERR_INVALID_PARAM;
  500. }
  501. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  502. if (error != CS_OK) {
  503. return (error);
  504. }
  505. req_lib_votequorum_qdevice_register.header.size = sizeof (struct req_lib_votequorum_qdevice_register);
  506. req_lib_votequorum_qdevice_register.header.id = MESSAGE_REQ_VOTEQUORUM_QDEVICE_REGISTER;
  507. strcpy(req_lib_votequorum_qdevice_register.name, name);
  508. iov.iov_base = (char *)&req_lib_votequorum_qdevice_register;
  509. iov.iov_len = sizeof (struct req_lib_votequorum_qdevice_register);
  510. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  511. votequorum_inst->c,
  512. &iov,
  513. 1,
  514. &res_lib_votequorum_status,
  515. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  516. if (error != CS_OK) {
  517. goto error_exit;
  518. }
  519. error = res_lib_votequorum_status.header.error;
  520. error_exit:
  521. hdb_handle_put (&votequorum_handle_t_db, handle);
  522. return (error);
  523. }
  524. cs_error_t votequorum_qdevice_poll (
  525. votequorum_handle_t handle,
  526. const char *name,
  527. unsigned int cast_vote,
  528. votequorum_ring_id_t ring_id)
  529. {
  530. cs_error_t error;
  531. struct votequorum_inst *votequorum_inst;
  532. struct iovec iov;
  533. struct req_lib_votequorum_qdevice_poll req_lib_votequorum_qdevice_poll;
  534. struct res_lib_votequorum_status res_lib_votequorum_status;
  535. if ((strlen(name) == 0) ||
  536. (strlen(name) >= VOTEQUORUM_QDEVICE_MAX_NAME_LEN)) {
  537. return CS_ERR_INVALID_PARAM;
  538. }
  539. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  540. if (error != CS_OK) {
  541. return (error);
  542. }
  543. req_lib_votequorum_qdevice_poll.header.size = sizeof (struct req_lib_votequorum_qdevice_poll);
  544. req_lib_votequorum_qdevice_poll.header.id = MESSAGE_REQ_VOTEQUORUM_QDEVICE_POLL;
  545. strcpy(req_lib_votequorum_qdevice_poll.name, name);
  546. req_lib_votequorum_qdevice_poll.cast_vote = cast_vote;
  547. marshall_to_mar_votequorum_ring_id(&req_lib_votequorum_qdevice_poll.ring_id, &ring_id);
  548. iov.iov_base = (char *)&req_lib_votequorum_qdevice_poll;
  549. iov.iov_len = sizeof (struct req_lib_votequorum_qdevice_poll);
  550. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  551. votequorum_inst->c,
  552. &iov,
  553. 1,
  554. &res_lib_votequorum_status,
  555. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  556. if (error != CS_OK) {
  557. goto error_exit;
  558. }
  559. error = res_lib_votequorum_status.header.error;
  560. error_exit:
  561. hdb_handle_put (&votequorum_handle_t_db, handle);
  562. return (error);
  563. }
  564. cs_error_t votequorum_qdevice_master_wins (
  565. votequorum_handle_t handle,
  566. const char *name,
  567. unsigned int allow)
  568. {
  569. cs_error_t error;
  570. struct votequorum_inst *votequorum_inst;
  571. struct iovec iov;
  572. struct req_lib_votequorum_qdevice_master_wins req_lib_votequorum_qdevice_master_wins;
  573. struct res_lib_votequorum_status res_lib_votequorum_status;
  574. if ((strlen(name) == 0) ||
  575. (strlen(name) >= VOTEQUORUM_QDEVICE_MAX_NAME_LEN)) {
  576. return CS_ERR_INVALID_PARAM;
  577. }
  578. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  579. if (error != CS_OK) {
  580. return (error);
  581. }
  582. req_lib_votequorum_qdevice_master_wins.header.size = sizeof (struct req_lib_votequorum_qdevice_master_wins);
  583. req_lib_votequorum_qdevice_master_wins.header.id = MESSAGE_REQ_VOTEQUORUM_QDEVICE_MASTER_WINS;
  584. strcpy(req_lib_votequorum_qdevice_master_wins.name, name);
  585. req_lib_votequorum_qdevice_master_wins.allow = allow;
  586. iov.iov_base = (char *)&req_lib_votequorum_qdevice_master_wins;
  587. iov.iov_len = sizeof (struct req_lib_votequorum_qdevice_master_wins);
  588. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  589. votequorum_inst->c,
  590. &iov,
  591. 1,
  592. &res_lib_votequorum_status,
  593. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  594. if (error != CS_OK) {
  595. goto error_exit;
  596. }
  597. error = res_lib_votequorum_status.header.error;
  598. error_exit:
  599. hdb_handle_put (&votequorum_handle_t_db, handle);
  600. return (error);
  601. }
  602. cs_error_t votequorum_qdevice_update (
  603. votequorum_handle_t handle,
  604. const char *oldname,
  605. const char *newname)
  606. {
  607. cs_error_t error;
  608. struct votequorum_inst *votequorum_inst;
  609. struct iovec iov;
  610. struct req_lib_votequorum_qdevice_update req_lib_votequorum_qdevice_update;
  611. struct res_lib_votequorum_status res_lib_votequorum_status;
  612. if ((strlen(oldname) == 0) ||
  613. (strlen(oldname) >= VOTEQUORUM_QDEVICE_MAX_NAME_LEN) ||
  614. (strlen(newname) == 0) ||
  615. (strlen(newname) >= VOTEQUORUM_QDEVICE_MAX_NAME_LEN)) {
  616. return CS_ERR_INVALID_PARAM;
  617. }
  618. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  619. if (error != CS_OK) {
  620. return (error);
  621. }
  622. req_lib_votequorum_qdevice_update.header.size = sizeof (struct req_lib_votequorum_qdevice_update);
  623. req_lib_votequorum_qdevice_update.header.id = MESSAGE_REQ_VOTEQUORUM_QDEVICE_UPDATE;
  624. strcpy(req_lib_votequorum_qdevice_update.oldname, oldname);
  625. strcpy(req_lib_votequorum_qdevice_update.newname, newname);
  626. iov.iov_base = (char *)&req_lib_votequorum_qdevice_update;
  627. iov.iov_len = sizeof (struct req_lib_votequorum_qdevice_update);
  628. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  629. votequorum_inst->c,
  630. &iov,
  631. 1,
  632. &res_lib_votequorum_status,
  633. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  634. if (error != CS_OK) {
  635. goto error_exit;
  636. }
  637. error = res_lib_votequorum_status.header.error;
  638. error_exit:
  639. hdb_handle_put (&votequorum_handle_t_db, handle);
  640. return (error);
  641. }
  642. cs_error_t votequorum_qdevice_unregister (
  643. votequorum_handle_t handle,
  644. const char *name)
  645. {
  646. cs_error_t error;
  647. struct votequorum_inst *votequorum_inst;
  648. struct iovec iov;
  649. struct req_lib_votequorum_qdevice_unregister req_lib_votequorum_qdevice_unregister;
  650. struct res_lib_votequorum_status res_lib_votequorum_status;
  651. if ((strlen(name) == 0) ||
  652. (strlen(name) >= VOTEQUORUM_QDEVICE_MAX_NAME_LEN)) {
  653. return CS_ERR_INVALID_PARAM;
  654. }
  655. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&votequorum_inst));
  656. if (error != CS_OK) {
  657. return (error);
  658. }
  659. req_lib_votequorum_qdevice_unregister.header.size = sizeof (struct req_lib_votequorum_qdevice_unregister);
  660. req_lib_votequorum_qdevice_unregister.header.id = MESSAGE_REQ_VOTEQUORUM_QDEVICE_UNREGISTER;
  661. strcpy(req_lib_votequorum_qdevice_unregister.name, name);
  662. iov.iov_base = (char *)&req_lib_votequorum_qdevice_unregister;
  663. iov.iov_len = sizeof (struct req_lib_votequorum_qdevice_unregister);
  664. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  665. votequorum_inst->c,
  666. &iov,
  667. 1,
  668. &res_lib_votequorum_status,
  669. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  670. if (error != CS_OK) {
  671. goto error_exit;
  672. }
  673. error = res_lib_votequorum_status.header.error;
  674. error_exit:
  675. hdb_handle_put (&votequorum_handle_t_db, handle);
  676. return (error);
  677. }
  678. cs_error_t votequorum_get_qdevice_extra_info (
  679. votequorum_handle_t handle,
  680. unsigned int nodeid,
  681. uint32_t *ei_size,
  682. void *extra_info)
  683. {
  684. if (!ei_size || !extra_info) {
  685. return CS_ERR_INVALID_PARAM;
  686. }
  687. cs_error_t error;
  688. struct votequorum_inst *inst;
  689. struct iovec iov;
  690. struct req_lib_votequorum_get_qdevice_extra_info req_lib_votequorum_get_qdevice_extra_info;
  691. char buf[sizeof(struct res_lib_votequorum_get_qdevice_extra_info) + VOTEQUORUM_QDEVICE_EXTRA_NODEINFO_MAXSIZE];
  692. struct res_lib_votequorum_get_qdevice_extra_info *res_lib_votequorum_get_qdevice_extra_info
  693. = (struct res_lib_votequorum_get_qdevice_extra_info *)buf;
  694. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&inst));
  695. if (error != CS_OK) {
  696. return (error);
  697. }
  698. req_lib_votequorum_get_qdevice_extra_info.header.size = sizeof(req_lib_votequorum_get_qdevice_extra_info);
  699. req_lib_votequorum_get_qdevice_extra_info.header.id = MESSAGE_REQ_VOTEQUORUM_QDEVICE_GET_EXTRA_INFO;
  700. req_lib_votequorum_get_qdevice_extra_info.nodeid = nodeid;
  701. iov.iov_base = (char *)&req_lib_votequorum_get_qdevice_extra_info;
  702. iov.iov_len = sizeof (struct req_lib_votequorum_get_qdevice_extra_info);
  703. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  704. inst->c,
  705. &iov,
  706. 1,
  707. buf,
  708. sizeof (buf), CS_IPC_TIMEOUT_MS));
  709. if (error != CS_OK) {
  710. goto error_exit;
  711. }
  712. error = res_lib_votequorum_get_qdevice_extra_info->header.error;
  713. *ei_size = res_lib_votequorum_get_qdevice_extra_info->ei_size;
  714. memcpy(extra_info,
  715. res_lib_votequorum_get_qdevice_extra_info->extra_info,
  716. res_lib_votequorum_get_qdevice_extra_info->ei_size);
  717. error_exit:
  718. hdb_handle_put (&votequorum_handle_t_db, handle);
  719. return (error);
  720. }
  721. cs_error_t votequorum_set_qdevice_extra_info (
  722. votequorum_handle_t handle,
  723. uint32_t ei_size,
  724. const void *extra_info)
  725. {
  726. cs_error_t error;
  727. struct votequorum_inst *inst;
  728. struct iovec iov;
  729. struct res_lib_votequorum_status res_lib_votequorum_status;
  730. char buf[sizeof(struct req_lib_votequorum_set_qdevice_extra_info) + VOTEQUORUM_QDEVICE_EXTRA_NODEINFO_MAXSIZE];
  731. struct req_lib_votequorum_set_qdevice_extra_info *req_lib_votequorum_set_qdevice_extra_info
  732. = (struct req_lib_votequorum_set_qdevice_extra_info *)buf;
  733. error = hdb_error_to_cs(hdb_handle_get (&votequorum_handle_t_db, handle, (void *)&inst));
  734. if (error != CS_OK) {
  735. return (error);
  736. }
  737. if(!extra_info) { // Force NULL to mean zero size
  738. ei_size = 0;
  739. }
  740. if(ei_size > VOTEQUORUM_QDEVICE_EXTRA_NODEINFO_MAXSIZE) {
  741. return CS_ERR_INVALID_PARAM;
  742. }
  743. req_lib_votequorum_set_qdevice_extra_info->header.size = sizeof(*req_lib_votequorum_set_qdevice_extra_info) + ei_size;
  744. req_lib_votequorum_set_qdevice_extra_info->header.id = MESSAGE_REQ_VOTEQUORUM_QDEVICE_SET_EXTRA_INFO;
  745. req_lib_votequorum_set_qdevice_extra_info->ei_size = ei_size;
  746. if(extra_info) {
  747. memcpy(req_lib_votequorum_set_qdevice_extra_info->extra_info, extra_info, ei_size);
  748. }
  749. iov.iov_base = buf;
  750. iov.iov_len = req_lib_votequorum_set_qdevice_extra_info->header.size;
  751. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  752. inst->c,
  753. &iov,
  754. 1,
  755. &res_lib_votequorum_status,
  756. sizeof (struct res_lib_votequorum_status), CS_IPC_TIMEOUT_MS));
  757. if (error != CS_OK) {
  758. goto error_exit;
  759. }
  760. error = res_lib_votequorum_status.header.error;
  761. error_exit:
  762. hdb_handle_put (&votequorum_handle_t_db, handle);
  763. return (error);
  764. }