quorum.c 11 KB

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