quorum.c 12 KB

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