quorum.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Copyright (c) 2008-2020 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 CONTRIBUTORS "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 <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/uio.h>
  44. #include <errno.h>
  45. #include <qb/qbipcc.h>
  46. #include <corosync/corotypes.h>
  47. #include <corosync/corodefs.h>
  48. #include <corosync/hdb.h>
  49. #include <corosync/quorum.h>
  50. #include <corosync/ipc_quorum.h>
  51. #include "util.h"
  52. struct quorum_inst {
  53. qb_ipcc_connection_t *c;
  54. int finalize;
  55. const void *context;
  56. union {
  57. quorum_model_data_t model_data;
  58. quorum_model_v0_data_t model_v0_data;
  59. quorum_model_v1_data_t model_v1_data;
  60. };
  61. };
  62. static void quorum_inst_free (void *inst);
  63. DECLARE_HDB_DATABASE(quorum_handle_t_db, quorum_inst_free);
  64. cs_error_t quorum_initialize (
  65. quorum_handle_t *handle,
  66. quorum_callbacks_t *callbacks,
  67. uint32_t *quorum_type)
  68. {
  69. quorum_model_v0_data_t model_v0_data;
  70. memset (&model_v0_data, 0, sizeof(quorum_model_v0_data_t));
  71. if (callbacks) {
  72. model_v0_data.quorum_notify_fn = callbacks->quorum_notify_fn;
  73. }
  74. return (quorum_model_initialize(handle, QUORUM_MODEL_V0,
  75. (quorum_model_data_t *)&model_v0_data, quorum_type, NULL));
  76. }
  77. cs_error_t quorum_model_initialize (
  78. quorum_handle_t *handle,
  79. quorum_model_t model,
  80. quorum_model_data_t *model_data,
  81. uint32_t *quorum_type,
  82. void *context)
  83. {
  84. cs_error_t error;
  85. struct quorum_inst *quorum_inst;
  86. struct iovec iov;
  87. struct qb_ipc_request_header quorum_gettype_req;
  88. struct req_lib_quorum_model_gettype quorum_model_gettype_req;
  89. struct res_lib_quorum_gettype res_lib_quorum_gettype;
  90. struct res_lib_quorum_model_gettype res_lib_quorum_model_gettype;
  91. uint32_t local_quorum_type;
  92. if (model != QUORUM_MODEL_V0 && model != QUORUM_MODEL_V1) {
  93. error = CS_ERR_INVALID_PARAM;
  94. goto error_no_destroy;
  95. }
  96. error = hdb_error_to_cs(hdb_handle_create (&quorum_handle_t_db, sizeof (struct quorum_inst), handle));
  97. if (error != CS_OK) {
  98. goto error_no_destroy;
  99. }
  100. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, *handle, (void *)&quorum_inst));
  101. if (error != CS_OK) {
  102. goto error_destroy;
  103. }
  104. error = CS_OK;
  105. quorum_inst->finalize = 0;
  106. quorum_inst->c = qb_ipcc_connect ("quorum", IPC_REQUEST_SIZE);
  107. if (quorum_inst->c == NULL) {
  108. error = qb_to_cs_error(-errno);
  109. goto error_put_destroy;
  110. }
  111. switch (model) {
  112. case QUORUM_MODEL_V0:
  113. quorum_gettype_req.size = sizeof (quorum_gettype_req);
  114. quorum_gettype_req.id = MESSAGE_REQ_QUORUM_GETTYPE;
  115. iov.iov_base = (char *)&quorum_gettype_req;
  116. iov.iov_len = sizeof (quorum_gettype_req);
  117. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  118. quorum_inst->c,
  119. &iov,
  120. 1,
  121. &res_lib_quorum_gettype,
  122. sizeof(res_lib_quorum_gettype), -1));
  123. if (error != CS_OK) {
  124. goto error_put_destroy;
  125. }
  126. error = res_lib_quorum_gettype.header.error;
  127. local_quorum_type = res_lib_quorum_gettype.quorum_type;
  128. break;
  129. case QUORUM_MODEL_V1:
  130. quorum_model_gettype_req.header.size = sizeof (quorum_model_gettype_req);
  131. quorum_model_gettype_req.header.id = MESSAGE_REQ_QUORUM_MODEL_GETTYPE;
  132. quorum_model_gettype_req.model = model;
  133. iov.iov_base = (char *)&quorum_model_gettype_req;
  134. iov.iov_len = sizeof (quorum_model_gettype_req);
  135. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  136. quorum_inst->c,
  137. &iov,
  138. 1,
  139. &res_lib_quorum_model_gettype,
  140. sizeof(res_lib_quorum_model_gettype), -1));
  141. if (error != CS_OK) {
  142. goto error_put_destroy;
  143. }
  144. error = res_lib_quorum_model_gettype.header.error;
  145. local_quorum_type = res_lib_quorum_model_gettype.quorum_type;
  146. break;
  147. }
  148. if (quorum_type != NULL) {
  149. *quorum_type = local_quorum_type;
  150. }
  151. if (model_data != NULL) {
  152. switch (model) {
  153. case QUORUM_MODEL_V0:
  154. memcpy(&quorum_inst->model_v0_data, model_data, sizeof(quorum_model_v0_data_t));
  155. break;
  156. case QUORUM_MODEL_V1:
  157. memcpy(&quorum_inst->model_v1_data, model_data, sizeof(quorum_model_v1_data_t));
  158. break;
  159. }
  160. }
  161. quorum_inst->model_data.model = model;
  162. quorum_inst->context = context;
  163. (void)hdb_handle_put (&quorum_handle_t_db, *handle);
  164. return (error);
  165. error_put_destroy:
  166. (void)hdb_handle_put (&quorum_handle_t_db, *handle);
  167. error_destroy:
  168. (void)hdb_handle_destroy (&quorum_handle_t_db, *handle);
  169. error_no_destroy:
  170. return (error);
  171. }
  172. static void quorum_inst_free (void *inst)
  173. {
  174. struct quorum_inst *quorum_inst = (struct quorum_inst *)inst;
  175. qb_ipcc_disconnect(quorum_inst->c);
  176. }
  177. cs_error_t quorum_finalize (
  178. quorum_handle_t handle)
  179. {
  180. struct quorum_inst *quorum_inst;
  181. cs_error_t error;
  182. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  183. if (error != CS_OK) {
  184. return (error);
  185. }
  186. /*
  187. * Another thread has already started finalizing
  188. */
  189. if (quorum_inst->finalize) {
  190. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  191. return (CS_ERR_BAD_HANDLE);
  192. }
  193. quorum_inst->finalize = 1;
  194. (void)hdb_handle_destroy (&quorum_handle_t_db, handle);
  195. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  196. return (CS_OK);
  197. }
  198. cs_error_t quorum_getquorate (
  199. quorum_handle_t handle,
  200. int *quorate)
  201. {
  202. cs_error_t error;
  203. struct quorum_inst *quorum_inst;
  204. struct iovec iov;
  205. struct qb_ipc_request_header req;
  206. struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
  207. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  208. if (error != CS_OK) {
  209. return (error);
  210. }
  211. req.size = sizeof (req);
  212. req.id = MESSAGE_REQ_QUORUM_GETQUORATE;
  213. iov.iov_base = (char *)&req;
  214. iov.iov_len = sizeof (req);
  215. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  216. quorum_inst->c,
  217. &iov,
  218. 1,
  219. &res_lib_quorum_getquorate,
  220. sizeof (struct res_lib_quorum_getquorate), CS_IPC_TIMEOUT_MS));
  221. if (error != CS_OK) {
  222. goto error_exit;
  223. }
  224. error = res_lib_quorum_getquorate.header.error;
  225. *quorate = res_lib_quorum_getquorate.quorate;
  226. error_exit:
  227. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  228. return (error);
  229. }
  230. cs_error_t quorum_fd_get (
  231. quorum_handle_t handle,
  232. int *fd)
  233. {
  234. cs_error_t error;
  235. struct quorum_inst *quorum_inst;
  236. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  237. if (error != CS_OK) {
  238. return (error);
  239. }
  240. error = qb_to_cs_error(qb_ipcc_fd_get (quorum_inst->c, fd));
  241. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  242. return (error);
  243. }
  244. cs_error_t quorum_context_get (
  245. quorum_handle_t handle,
  246. const void **context)
  247. {
  248. cs_error_t error;
  249. struct quorum_inst *quorum_inst;
  250. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  251. if (error != CS_OK) {
  252. return (error);
  253. }
  254. *context = quorum_inst->context;
  255. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  256. return (CS_OK);
  257. }
  258. cs_error_t quorum_context_set (
  259. quorum_handle_t handle,
  260. const void *context)
  261. {
  262. cs_error_t error;
  263. struct quorum_inst *quorum_inst;
  264. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  265. if (error != CS_OK) {
  266. return (error);
  267. }
  268. quorum_inst->context = context;
  269. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  270. return (CS_OK);
  271. }
  272. cs_error_t quorum_trackstart (
  273. quorum_handle_t handle,
  274. unsigned int flags )
  275. {
  276. cs_error_t error;
  277. struct quorum_inst *quorum_inst;
  278. struct iovec iov;
  279. struct req_lib_quorum_trackstart req_lib_quorum_trackstart;
  280. struct qb_ipc_response_header res;
  281. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  282. if (error != CS_OK) {
  283. return (error);
  284. }
  285. req_lib_quorum_trackstart.header.size = sizeof (struct req_lib_quorum_trackstart);
  286. req_lib_quorum_trackstart.header.id = MESSAGE_REQ_QUORUM_TRACKSTART;
  287. req_lib_quorum_trackstart.track_flags = flags;
  288. iov.iov_base = (char *)&req_lib_quorum_trackstart;
  289. iov.iov_len = sizeof (struct req_lib_quorum_trackstart);
  290. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  291. quorum_inst->c,
  292. &iov,
  293. 1,
  294. &res,
  295. sizeof (res), CS_IPC_TIMEOUT_MS));
  296. if (error != CS_OK) {
  297. goto error_exit;
  298. }
  299. error = res.error;
  300. error_exit:
  301. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  302. return (error);
  303. }
  304. cs_error_t quorum_trackstop (
  305. quorum_handle_t handle)
  306. {
  307. cs_error_t error;
  308. struct quorum_inst *quorum_inst;
  309. struct iovec iov;
  310. struct qb_ipc_request_header req;
  311. struct qb_ipc_response_header res;
  312. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  313. if (error != CS_OK) {
  314. return (error);
  315. }
  316. req.size = sizeof (req);
  317. req.id = MESSAGE_REQ_QUORUM_TRACKSTOP;
  318. iov.iov_base = (char *)&req;
  319. iov.iov_len = sizeof (req);
  320. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  321. quorum_inst->c,
  322. &iov,
  323. 1,
  324. &res,
  325. sizeof (res), CS_IPC_TIMEOUT_MS));
  326. if (error != CS_OK) {
  327. goto error_exit;
  328. }
  329. error = res.error;
  330. error_exit:
  331. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  332. return (error);
  333. }
  334. cs_error_t quorum_dispatch (
  335. quorum_handle_t handle,
  336. cs_dispatch_flags_t dispatch_types)
  337. {
  338. int timeout = -1;
  339. cs_error_t error;
  340. int cont = 1; /* always continue do loop except when set to 0 */
  341. struct quorum_inst *quorum_inst;
  342. struct quorum_inst quorum_inst_copy;
  343. struct qb_ipc_response_header *dispatch_data;
  344. char dispatch_buf[IPC_DISPATCH_SIZE];
  345. struct res_lib_quorum_notification *res_lib_quorum_notification;
  346. struct res_lib_quorum_v1_quorum_notification *res_lib_quorum_v1_quorum_notification;
  347. struct res_lib_quorum_v1_nodelist_notification *res_lib_quorum_v1_nodelist_notification;
  348. struct quorum_ring_id ring_id;
  349. mar_uint32_t *joined_list;
  350. mar_uint32_t *left_list;
  351. if (dispatch_types != CS_DISPATCH_ONE &&
  352. dispatch_types != CS_DISPATCH_ALL &&
  353. dispatch_types != CS_DISPATCH_BLOCKING &&
  354. dispatch_types != CS_DISPATCH_ONE_NONBLOCKING) {
  355. return (CS_ERR_INVALID_PARAM);
  356. }
  357. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle,
  358. (void *)&quorum_inst));
  359. if (error != CS_OK) {
  360. return (error);
  361. }
  362. /*
  363. * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
  364. * wait indefinately for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
  365. */
  366. if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  367. timeout = 0;
  368. }
  369. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  370. do {
  371. error = qb_to_cs_error (qb_ipcc_event_recv (
  372. quorum_inst->c,
  373. dispatch_buf,
  374. IPC_DISPATCH_SIZE,
  375. timeout));
  376. if (error == CS_ERR_BAD_HANDLE) {
  377. error = CS_OK;
  378. goto error_put;
  379. }
  380. if (error == CS_ERR_TRY_AGAIN) {
  381. if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  382. /*
  383. * Don't mask error
  384. */
  385. goto error_put;
  386. }
  387. error = CS_OK;
  388. if (dispatch_types == CS_DISPATCH_ALL) {
  389. break; /* exit do while cont is 1 loop */
  390. } else {
  391. continue; /* next poll */
  392. }
  393. }
  394. if (error != CS_OK) {
  395. goto error_put;
  396. }
  397. /*
  398. * Make copy of callbacks, message data, unlock instance, and call callback
  399. * A risk of this dispatch method is that the callback routines may
  400. * operate at the same time that quorum_finalize has been called in another thread.
  401. */
  402. memcpy (&quorum_inst_copy, quorum_inst, sizeof(quorum_inst_copy));
  403. switch (quorum_inst_copy.model_data.model) {
  404. case QUORUM_MODEL_V0:
  405. /*
  406. * Dispatch incoming message
  407. */
  408. switch (dispatch_data->id) {
  409. case MESSAGE_RES_QUORUM_NOTIFICATION:
  410. if (quorum_inst_copy.model_v0_data.quorum_notify_fn == NULL) {
  411. break;
  412. }
  413. res_lib_quorum_notification = (struct res_lib_quorum_notification *)dispatch_data;
  414. quorum_inst_copy.model_v0_data.quorum_notify_fn ( handle,
  415. res_lib_quorum_notification->quorate,
  416. res_lib_quorum_notification->ring_seq,
  417. res_lib_quorum_notification->view_list_entries,
  418. res_lib_quorum_notification->view_list);
  419. break;
  420. default:
  421. error = CS_ERR_LIBRARY;
  422. goto error_put;
  423. break;
  424. } /* switch (dispatch_data->id) */
  425. break; /* case QUORUM_MODEL_V0 */
  426. case QUORUM_MODEL_V1:
  427. /*
  428. * Dispatch incoming message
  429. */
  430. switch (dispatch_data->id) {
  431. case MESSAGE_RES_QUORUM_V1_QUORUM_NOTIFICATION:
  432. if (quorum_inst_copy.model_v1_data.quorum_notify_fn == NULL) {
  433. break;
  434. }
  435. res_lib_quorum_v1_quorum_notification =
  436. (struct res_lib_quorum_v1_quorum_notification *)dispatch_data;
  437. ring_id.nodeid = res_lib_quorum_v1_quorum_notification->ring_id.nodeid;
  438. ring_id.seq = res_lib_quorum_v1_quorum_notification->ring_id.seq;
  439. quorum_inst_copy.model_v1_data.quorum_notify_fn ( handle,
  440. res_lib_quorum_v1_quorum_notification->quorate,
  441. ring_id,
  442. res_lib_quorum_v1_quorum_notification->view_list_entries,
  443. res_lib_quorum_v1_quorum_notification->view_list);
  444. break;
  445. case MESSAGE_RES_QUORUM_V1_NODELIST_NOTIFICATION:
  446. if (quorum_inst_copy.model_v1_data.nodelist_notify_fn == NULL) {
  447. break;
  448. }
  449. res_lib_quorum_v1_nodelist_notification =
  450. (struct res_lib_quorum_v1_nodelist_notification *)dispatch_data;
  451. ring_id.nodeid = res_lib_quorum_v1_nodelist_notification->ring_id.nodeid;
  452. ring_id.seq = res_lib_quorum_v1_nodelist_notification->ring_id.seq;
  453. joined_list = res_lib_quorum_v1_nodelist_notification->member_list +
  454. res_lib_quorum_v1_nodelist_notification->member_list_entries;
  455. left_list = joined_list +
  456. res_lib_quorum_v1_nodelist_notification->joined_list_entries;
  457. quorum_inst_copy.model_v1_data.nodelist_notify_fn ( handle,
  458. ring_id,
  459. res_lib_quorum_v1_nodelist_notification->member_list_entries,
  460. res_lib_quorum_v1_nodelist_notification->member_list,
  461. res_lib_quorum_v1_nodelist_notification->joined_list_entries,
  462. joined_list,
  463. res_lib_quorum_v1_nodelist_notification->left_list_entries,
  464. left_list);
  465. break;
  466. default:
  467. error = CS_ERR_LIBRARY;
  468. goto error_put;
  469. break;
  470. } /* switch (dispatch_data->id) */
  471. break; /* case QUORUM_MODEL_V1 */
  472. }
  473. if (quorum_inst->finalize) {
  474. /*
  475. * If the finalize has been called then get out of the dispatch.
  476. */
  477. error = CS_ERR_BAD_HANDLE;
  478. goto error_put;
  479. }
  480. /*
  481. * Determine if more messages should be processed
  482. */
  483. if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  484. cont = 0;
  485. }
  486. } while (cont);
  487. error_put:
  488. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  489. return (error);
  490. }