quorum.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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/ais_util.h>
  48. #include "corosync/quorum.h"
  49. #include "corosync/ipc_quorum.h"
  50. struct quorum_inst {
  51. int response_fd;
  52. int dispatch_fd;
  53. int finalize;
  54. 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 = saServiceConnect (&quorum_inst->dispatch_fd,
  89. &quorum_inst->response_fd,
  90. QUORUM_SERVICE);
  91. if (error != CS_OK) {
  92. goto error_put_destroy;
  93. }
  94. pthread_mutex_init (&quorum_inst->response_mutex, NULL);
  95. pthread_mutex_init (&quorum_inst->dispatch_mutex, NULL);
  96. if (callbacks)
  97. memcpy(&quorum_inst->callbacks, callbacks, sizeof (callbacks));
  98. else
  99. memset(&quorum_inst->callbacks, 0, sizeof (callbacks));
  100. (void)saHandleInstancePut (&quorum_handle_t_db, *handle);
  101. return (CS_OK);
  102. error_put_destroy:
  103. (void)saHandleInstancePut (&quorum_handle_t_db, *handle);
  104. error_destroy:
  105. (void)saHandleDestroy (&quorum_handle_t_db, *handle);
  106. error_no_destroy:
  107. return (error);
  108. }
  109. cs_error_t quorum_finalize (
  110. quorum_handle_t handle)
  111. {
  112. struct quorum_inst *quorum_inst;
  113. cs_error_t error;
  114. error = saHandleInstanceGet (&quorum_handle_t_db, handle, (void *)&quorum_inst);
  115. if (error != CS_OK) {
  116. return (error);
  117. }
  118. pthread_mutex_lock (&quorum_inst->response_mutex);
  119. /*
  120. * Another thread has already started finalizing
  121. */
  122. if (quorum_inst->finalize) {
  123. pthread_mutex_unlock (&quorum_inst->response_mutex);
  124. (void)saHandleInstancePut (&quorum_handle_t_db, handle);
  125. return (CS_ERR_BAD_HANDLE);
  126. }
  127. quorum_inst->finalize = 1;
  128. pthread_mutex_unlock (&quorum_inst->response_mutex);
  129. (void)saHandleDestroy (&quorum_handle_t_db, handle);
  130. /*
  131. * Disconnect from the server
  132. */
  133. if (quorum_inst->response_fd != -1) {
  134. shutdown(quorum_inst->response_fd, 0);
  135. close(quorum_inst->response_fd);
  136. }
  137. (void)saHandleInstancePut (&quorum_handle_t_db, handle);
  138. return (CS_OK);
  139. }
  140. cs_error_t quorum_getquorate (
  141. quorum_handle_t handle,
  142. int *quorate)
  143. {
  144. cs_error_t error;
  145. struct quorum_inst *quorum_inst;
  146. struct iovec iov[2];
  147. mar_req_header_t req;
  148. struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
  149. error = saHandleInstanceGet (&quorum_handle_t_db, handle, (void *)&quorum_inst);
  150. if (error != CS_OK) {
  151. return (error);
  152. }
  153. pthread_mutex_lock (&quorum_inst->response_mutex);
  154. req.size = sizeof (req);
  155. req.id = MESSAGE_REQ_QUORUM_GETQUORATE;
  156. iov[0].iov_base = (char *)&req;
  157. iov[0].iov_len = sizeof (req);
  158. error = saSendMsgReceiveReply (quorum_inst->response_fd, iov, 1,
  159. &res_lib_quorum_getquorate, sizeof (struct res_lib_quorum_getquorate));
  160. pthread_mutex_unlock (&quorum_inst->response_mutex);
  161. if (error != CS_OK) {
  162. goto error_exit;
  163. }
  164. error = res_lib_quorum_getquorate.header.error;
  165. *quorate = res_lib_quorum_getquorate.quorate;
  166. error_exit:
  167. (void)saHandleInstancePut (&quorum_handle_t_db, handle);
  168. return (error);
  169. }
  170. cs_error_t quorum_fd_get (
  171. quorum_handle_t handle,
  172. int *fd)
  173. {
  174. cs_error_t error;
  175. struct quorum_inst *quorum_inst;
  176. error = saHandleInstanceGet (&quorum_handle_t_db, handle, (void *)&quorum_inst);
  177. if (error != CS_OK) {
  178. return (error);
  179. }
  180. *fd = quorum_inst->dispatch_fd;
  181. (void)saHandleInstancePut (&quorum_handle_t_db, handle);
  182. return (CS_OK);
  183. }
  184. cs_error_t quorum_context_get (
  185. quorum_handle_t handle,
  186. void **context)
  187. {
  188. cs_error_t error;
  189. struct quorum_inst *quorum_inst;
  190. error = saHandleInstanceGet (&quorum_handle_t_db, handle, (void *)&quorum_inst);
  191. if (error != CS_OK) {
  192. return (error);
  193. }
  194. *context = quorum_inst->context;
  195. (void)saHandleInstancePut (&quorum_handle_t_db, handle);
  196. return (CS_OK);
  197. }
  198. cs_error_t quorum_context_set (
  199. quorum_handle_t handle,
  200. void *context)
  201. {
  202. cs_error_t error;
  203. struct quorum_inst *quorum_inst;
  204. error = saHandleInstanceGet (&quorum_handle_t_db, handle, (void *)&quorum_inst);
  205. if (error != CS_OK) {
  206. return (error);
  207. }
  208. quorum_inst->context = context;
  209. (void)saHandleInstancePut (&quorum_handle_t_db, handle);
  210. return (CS_OK);
  211. }
  212. cs_error_t quorum_trackstart (
  213. quorum_handle_t handle,
  214. unsigned int flags )
  215. {
  216. cs_error_t error;
  217. struct quorum_inst *quorum_inst;
  218. struct iovec iov[2];
  219. struct req_lib_quorum_trackstart req_lib_quorum_trackstart;
  220. mar_res_header_t res;
  221. error = saHandleInstanceGet (&quorum_handle_t_db, handle, (void *)&quorum_inst);
  222. if (error != CS_OK) {
  223. return (error);
  224. }
  225. pthread_mutex_lock (&quorum_inst->response_mutex);
  226. req_lib_quorum_trackstart.header.size = sizeof (struct req_lib_quorum_trackstart);
  227. req_lib_quorum_trackstart.header.id = MESSAGE_REQ_QUORUM_TRACKSTART;
  228. req_lib_quorum_trackstart.track_flags = flags;
  229. iov[0].iov_base = (char *)&req_lib_quorum_trackstart;
  230. iov[0].iov_len = sizeof (struct req_lib_quorum_trackstart);
  231. error = saSendMsgReceiveReply (quorum_inst->response_fd, iov, 1,
  232. &res, 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[2];
  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[0].iov_base = (char *)&req;
  258. iov[0].iov_len = sizeof (req);
  259. error = saSendMsgReceiveReply (quorum_inst->response_fd, iov, 1,
  260. &res, sizeof (res));
  261. pthread_mutex_unlock (&quorum_inst->response_mutex);
  262. if (error != CS_OK) {
  263. goto error_exit;
  264. }
  265. error = res.error;
  266. error_exit:
  267. (void)saHandleInstancePut (&quorum_handle_t_db, handle);
  268. return (error);
  269. }
  270. struct quorum_res_overlay {
  271. mar_res_header_t header __attribute__((aligned(8)));
  272. char data[512000];
  273. };
  274. cs_error_t quorum_dispatch (
  275. quorum_handle_t handle,
  276. cs_dispatch_flags_t dispatch_types)
  277. {
  278. struct pollfd ufds;
  279. int timeout = -1;
  280. cs_error_t error;
  281. int cont = 1; /* always continue do loop except when set to 0 */
  282. int dispatch_avail;
  283. struct quorum_inst *quorum_inst;
  284. quorum_callbacks_t callbacks;
  285. struct quorum_res_overlay dispatch_data;
  286. struct res_lib_quorum_notification *res_lib_quorum_notification;
  287. if (dispatch_types != CS_DISPATCH_ONE &&
  288. dispatch_types != CS_DISPATCH_ALL &&
  289. dispatch_types != CS_DISPATCH_BLOCKING) {
  290. return (CS_ERR_INVALID_PARAM);
  291. }
  292. error = saHandleInstanceGet (&quorum_handle_t_db, handle,
  293. (void *)&quorum_inst);
  294. if (error != CS_OK) {
  295. return (error);
  296. }
  297. /*
  298. * Timeout instantly for CS_DISPATCH_ONE or SA_DISPATCH_ALL and
  299. * wait indefinately for CS_DISPATCH_BLOCKING
  300. */
  301. if (dispatch_types == CS_DISPATCH_ALL) {
  302. timeout = 0;
  303. }
  304. do {
  305. ufds.fd = quorum_inst->dispatch_fd;
  306. ufds.events = POLLIN;
  307. ufds.revents = 0;
  308. pthread_mutex_lock (&quorum_inst->dispatch_mutex);
  309. error = saPollRetry (&ufds, 1, timeout);
  310. if (error != CS_OK) {
  311. goto error_unlock;
  312. }
  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 ((ufds.revents & (POLLERR|POLLHUP|POLLNVAL)) != 0) {
  321. error = CS_ERR_BAD_HANDLE;
  322. goto error_unlock;
  323. }
  324. dispatch_avail = ufds.revents & POLLIN;
  325. if (dispatch_avail == 0 && dispatch_types == CS_DISPATCH_ALL) {
  326. pthread_mutex_unlock (&quorum_inst->dispatch_mutex);
  327. break; /* exit do while cont is 1 loop */
  328. } else
  329. if (dispatch_avail == 0) {
  330. pthread_mutex_unlock (&quorum_inst->dispatch_mutex);
  331. continue; /* next poll */
  332. }
  333. if (ufds.revents & POLLIN) {
  334. error = saRecvRetry (quorum_inst->dispatch_fd, &dispatch_data.header,
  335. sizeof (mar_res_header_t));
  336. if (error != CS_OK) {
  337. goto error_unlock;
  338. }
  339. if (dispatch_data.header.size > sizeof (mar_res_header_t)) {
  340. error = saRecvRetry (quorum_inst->dispatch_fd, &dispatch_data.data,
  341. dispatch_data.header.size - sizeof (mar_res_header_t));
  342. if (error != CS_OK) {
  343. goto error_unlock;
  344. }
  345. }
  346. } else {
  347. pthread_mutex_unlock (&quorum_inst->dispatch_mutex);
  348. continue;
  349. }
  350. /*
  351. * Make copy of callbacks, message data, unlock instance, and call callback
  352. * A risk of this dispatch method is that the callback routines may
  353. * operate at the same time that quorum_finalize has been called in another thread.
  354. */
  355. memcpy (&callbacks, &quorum_inst->callbacks, sizeof (quorum_callbacks_t));
  356. pthread_mutex_unlock (&quorum_inst->dispatch_mutex);
  357. /*
  358. * Dispatch incoming message
  359. */
  360. switch (dispatch_data.header.id) {
  361. case MESSAGE_RES_QUORUM_NOTIFICATION:
  362. if (callbacks.quorum_notify_fn == NULL) {
  363. continue;
  364. }
  365. res_lib_quorum_notification = (struct res_lib_quorum_notification *)&dispatch_data;
  366. callbacks.quorum_notify_fn ( handle,
  367. res_lib_quorum_notification->quorate,
  368. res_lib_quorum_notification->ring_seq,
  369. res_lib_quorum_notification->view_list_entries,
  370. res_lib_quorum_notification->view_list);
  371. break;
  372. default:
  373. error = CS_ERR_LIBRARY;
  374. goto error_put;
  375. break;
  376. }
  377. /*
  378. * Determine if more messages should be processed
  379. * */
  380. switch (dispatch_types) {
  381. case CS_DISPATCH_ONE:
  382. cont = 0;
  383. break;
  384. case CS_DISPATCH_ALL:
  385. break;
  386. case CS_DISPATCH_BLOCKING:
  387. break;
  388. }
  389. } while (cont);
  390. goto error_put;
  391. error_unlock:
  392. pthread_mutex_unlock (&quorum_inst->dispatch_mutex);
  393. error_put:
  394. (void)saHandleInstancePut (&quorum_handle_t_db, handle);
  395. return (error);
  396. }