quorum.c 11 KB

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