quorum.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright (c) 2008-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 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 <errno.h>
  44. #include <qb/qbipcc.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/corodefs.h>
  47. #include <corosync/hdb.h>
  48. #include <corosync/quorum.h>
  49. #include <corosync/ipc_quorum.h>
  50. #include "util.h"
  51. struct quorum_inst {
  52. qb_ipcc_connection_t *c;
  53. int finalize;
  54. const void *context;
  55. quorum_callbacks_t callbacks;
  56. };
  57. static void quorum_inst_free (void *inst);
  58. DECLARE_HDB_DATABASE(quorum_handle_t_db, quorum_inst_free);
  59. cs_error_t quorum_initialize (
  60. quorum_handle_t *handle,
  61. quorum_callbacks_t *callbacks,
  62. uint32_t *quorum_type)
  63. {
  64. cs_error_t error;
  65. struct quorum_inst *quorum_inst;
  66. struct iovec iov;
  67. struct qb_ipc_request_header req;
  68. struct res_lib_quorum_gettype res_lib_quorum_gettype;
  69. error = hdb_error_to_cs(hdb_handle_create (&quorum_handle_t_db, sizeof (struct quorum_inst), handle));
  70. if (error != CS_OK) {
  71. goto error_no_destroy;
  72. }
  73. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, *handle, (void *)&quorum_inst));
  74. if (error != CS_OK) {
  75. goto error_destroy;
  76. }
  77. error = CS_OK;
  78. quorum_inst->finalize = 0;
  79. quorum_inst->c = qb_ipcc_connect ("quorum", IPC_REQUEST_SIZE);
  80. if (quorum_inst->c == NULL) {
  81. error = qb_to_cs_error(-errno);
  82. goto error_put_destroy;
  83. }
  84. req.size = sizeof (req);
  85. req.id = MESSAGE_REQ_QUORUM_GETTYPE;
  86. iov.iov_base = (char *)&req;
  87. iov.iov_len = sizeof (req);
  88. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  89. quorum_inst->c,
  90. &iov,
  91. 1,
  92. &res_lib_quorum_gettype,
  93. sizeof (struct res_lib_quorum_gettype), -1));
  94. if (error != CS_OK) {
  95. goto error_put_destroy;
  96. }
  97. error = res_lib_quorum_gettype.header.error;
  98. *quorum_type = res_lib_quorum_gettype.quorum_type;
  99. if (callbacks)
  100. memcpy(&quorum_inst->callbacks, callbacks, sizeof (*callbacks));
  101. else
  102. memset(&quorum_inst->callbacks, 0, sizeof (*callbacks));
  103. (void)hdb_handle_put (&quorum_handle_t_db, *handle);
  104. return (CS_OK);
  105. error_put_destroy:
  106. (void)hdb_handle_put (&quorum_handle_t_db, *handle);
  107. error_destroy:
  108. (void)hdb_handle_destroy (&quorum_handle_t_db, *handle);
  109. error_no_destroy:
  110. return (error);
  111. }
  112. static void quorum_inst_free (void *inst)
  113. {
  114. struct quorum_inst *quorum_inst = (struct quorum_inst *)inst;
  115. qb_ipcc_disconnect(quorum_inst->c);
  116. }
  117. cs_error_t quorum_finalize (
  118. quorum_handle_t handle)
  119. {
  120. struct quorum_inst *quorum_inst;
  121. cs_error_t error;
  122. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  123. if (error != CS_OK) {
  124. return (error);
  125. }
  126. /*
  127. * Another thread has already started finalizing
  128. */
  129. if (quorum_inst->finalize) {
  130. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  131. return (CS_ERR_BAD_HANDLE);
  132. }
  133. quorum_inst->finalize = 1;
  134. (void)hdb_handle_destroy (&quorum_handle_t_db, handle);
  135. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  136. return (CS_OK);
  137. }
  138. cs_error_t quorum_getquorate (
  139. quorum_handle_t handle,
  140. int *quorate)
  141. {
  142. cs_error_t error;
  143. struct quorum_inst *quorum_inst;
  144. struct iovec iov;
  145. struct qb_ipc_request_header req;
  146. struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
  147. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  148. if (error != CS_OK) {
  149. return (error);
  150. }
  151. req.size = sizeof (req);
  152. req.id = MESSAGE_REQ_QUORUM_GETQUORATE;
  153. iov.iov_base = (char *)&req;
  154. iov.iov_len = sizeof (req);
  155. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  156. quorum_inst->c,
  157. &iov,
  158. 1,
  159. &res_lib_quorum_getquorate,
  160. sizeof (struct res_lib_quorum_getquorate), CS_IPC_TIMEOUT_MS));
  161. if (error != CS_OK) {
  162. goto error_exit;
  163. }
  164. error = res_lib_quorum_getquorate.header.error;
  165. *quorate = res_lib_quorum_getquorate.quorate;
  166. error_exit:
  167. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  168. return (error);
  169. }
  170. cs_error_t quorum_fd_get (
  171. quorum_handle_t handle,
  172. int *fd)
  173. {
  174. cs_error_t error;
  175. struct quorum_inst *quorum_inst;
  176. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  177. if (error != CS_OK) {
  178. return (error);
  179. }
  180. error = qb_to_cs_error(qb_ipcc_fd_get (quorum_inst->c, fd));
  181. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  182. return (error);
  183. }
  184. cs_error_t quorum_context_get (
  185. quorum_handle_t handle,
  186. const void **context)
  187. {
  188. cs_error_t error;
  189. struct quorum_inst *quorum_inst;
  190. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  191. if (error != CS_OK) {
  192. return (error);
  193. }
  194. *context = quorum_inst->context;
  195. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  196. return (CS_OK);
  197. }
  198. cs_error_t quorum_context_set (
  199. quorum_handle_t handle,
  200. const void *context)
  201. {
  202. cs_error_t error;
  203. struct quorum_inst *quorum_inst;
  204. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  205. if (error != CS_OK) {
  206. return (error);
  207. }
  208. quorum_inst->context = context;
  209. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  210. return (CS_OK);
  211. }
  212. cs_error_t quorum_trackstart (
  213. quorum_handle_t handle,
  214. unsigned int flags )
  215. {
  216. cs_error_t error;
  217. struct quorum_inst *quorum_inst;
  218. struct iovec iov;
  219. struct req_lib_quorum_trackstart req_lib_quorum_trackstart;
  220. struct qb_ipc_response_header res;
  221. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  222. if (error != CS_OK) {
  223. return (error);
  224. }
  225. req_lib_quorum_trackstart.header.size = sizeof (struct req_lib_quorum_trackstart);
  226. req_lib_quorum_trackstart.header.id = MESSAGE_REQ_QUORUM_TRACKSTART;
  227. req_lib_quorum_trackstart.track_flags = flags;
  228. iov.iov_base = (char *)&req_lib_quorum_trackstart;
  229. iov.iov_len = sizeof (struct req_lib_quorum_trackstart);
  230. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  231. quorum_inst->c,
  232. &iov,
  233. 1,
  234. &res,
  235. sizeof (res), CS_IPC_TIMEOUT_MS));
  236. if (error != CS_OK) {
  237. goto error_exit;
  238. }
  239. error = res.error;
  240. error_exit:
  241. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  242. return (error);
  243. }
  244. cs_error_t quorum_trackstop (
  245. quorum_handle_t handle)
  246. {
  247. cs_error_t error;
  248. struct quorum_inst *quorum_inst;
  249. struct iovec iov;
  250. struct qb_ipc_request_header req;
  251. struct qb_ipc_response_header res;
  252. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  253. if (error != CS_OK) {
  254. return (error);
  255. }
  256. req.size = sizeof (req);
  257. req.id = MESSAGE_REQ_QUORUM_TRACKSTOP;
  258. iov.iov_base = (char *)&req;
  259. iov.iov_len = sizeof (req);
  260. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  261. quorum_inst->c,
  262. &iov,
  263. 1,
  264. &res,
  265. sizeof (res), CS_IPC_TIMEOUT_MS));
  266. if (error != CS_OK) {
  267. goto error_exit;
  268. }
  269. error = res.error;
  270. error_exit:
  271. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  272. return (error);
  273. }
  274. cs_error_t quorum_dispatch (
  275. quorum_handle_t handle,
  276. cs_dispatch_flags_t dispatch_types)
  277. {
  278. int timeout = -1;
  279. cs_error_t error;
  280. int cont = 1; /* always continue do loop except when set to 0 */
  281. struct quorum_inst *quorum_inst;
  282. quorum_callbacks_t callbacks;
  283. struct qb_ipc_response_header *dispatch_data;
  284. char dispatch_buf[IPC_DISPATCH_SIZE];
  285. struct res_lib_quorum_notification *res_lib_quorum_notification;
  286. if (dispatch_types != CS_DISPATCH_ONE &&
  287. dispatch_types != CS_DISPATCH_ALL &&
  288. dispatch_types != CS_DISPATCH_BLOCKING &&
  289. dispatch_types != CS_DISPATCH_ONE_NONBLOCKING) {
  290. return (CS_ERR_INVALID_PARAM);
  291. }
  292. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle,
  293. (void *)&quorum_inst));
  294. if (error != CS_OK) {
  295. return (error);
  296. }
  297. /*
  298. * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
  299. * wait indefinately for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
  300. */
  301. if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  302. timeout = 0;
  303. }
  304. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  305. do {
  306. error = qb_to_cs_error (qb_ipcc_event_recv (
  307. quorum_inst->c,
  308. dispatch_buf,
  309. IPC_DISPATCH_SIZE,
  310. timeout));
  311. if (error == CS_ERR_BAD_HANDLE) {
  312. error = CS_OK;
  313. goto error_put;
  314. }
  315. if (error == CS_ERR_TRY_AGAIN) {
  316. if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  317. /*
  318. * Don't mask error
  319. */
  320. goto error_put;
  321. }
  322. error = CS_OK;
  323. if (dispatch_types == CS_DISPATCH_ALL) {
  324. break; /* exit do while cont is 1 loop */
  325. } else {
  326. continue; /* next poll */
  327. }
  328. }
  329. if (error != CS_OK) {
  330. goto error_put;
  331. }
  332. /*
  333. * Make copy of callbacks, message data, unlock instance, and call callback
  334. * A risk of this dispatch method is that the callback routines may
  335. * operate at the same time that quorum_finalize has been called in another thread.
  336. */
  337. memcpy (&callbacks, &quorum_inst->callbacks, sizeof (quorum_callbacks_t));
  338. /*
  339. * Dispatch incoming message
  340. */
  341. switch (dispatch_data->id) {
  342. case MESSAGE_RES_QUORUM_NOTIFICATION:
  343. if (callbacks.quorum_notify_fn == NULL) {
  344. break;
  345. }
  346. res_lib_quorum_notification = (struct res_lib_quorum_notification *)dispatch_data;
  347. callbacks.quorum_notify_fn ( handle,
  348. res_lib_quorum_notification->quorate,
  349. res_lib_quorum_notification->ring_seq,
  350. res_lib_quorum_notification->view_list_entries,
  351. res_lib_quorum_notification->view_list);
  352. break;
  353. default:
  354. error = CS_ERR_LIBRARY;
  355. goto error_put;
  356. break;
  357. }
  358. if (quorum_inst->finalize) {
  359. /*
  360. * If the finalize has been called then get out of the dispatch.
  361. */
  362. error = CS_ERR_BAD_HANDLE;
  363. goto error_put;
  364. }
  365. /*
  366. * Determine if more messages should be processed
  367. */
  368. if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  369. cont = 0;
  370. }
  371. } while (cont);
  372. error_put:
  373. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  374. return (error);
  375. }