quorum.c 10 KB

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