quorum.c 10 KB

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