quorum.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (c) 2008-2012 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 <qb/qbipcc.h>
  45. #include <corosync/corotypes.h>
  46. #include <corosync/corodefs.h>
  47. #include <corosync/hdb.h>
  48. #include <corosync/quorum.h>
  49. #include <corosync/ipc_quorum.h>
  50. #include "util.h"
  51. struct quorum_inst {
  52. qb_ipcc_connection_t *c;
  53. int finalize;
  54. const void *context;
  55. quorum_callbacks_t callbacks;
  56. };
  57. DECLARE_HDB_DATABASE(quorum_handle_t_db,NULL);
  58. cs_error_t quorum_initialize (
  59. quorum_handle_t *handle,
  60. quorum_callbacks_t *callbacks,
  61. uint32_t *quorum_type)
  62. {
  63. cs_error_t error;
  64. struct quorum_inst *quorum_inst;
  65. struct iovec iov;
  66. struct qb_ipc_request_header req;
  67. struct res_lib_quorum_gettype res_lib_quorum_gettype;
  68. error = hdb_error_to_cs(hdb_handle_create (&quorum_handle_t_db, sizeof (struct quorum_inst), handle));
  69. if (error != CS_OK) {
  70. goto error_no_destroy;
  71. }
  72. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, *handle, (void *)&quorum_inst));
  73. if (error != CS_OK) {
  74. goto error_destroy;
  75. }
  76. error = CS_OK;
  77. quorum_inst->c = qb_ipcc_connect ("quorum", IPC_REQUEST_SIZE);
  78. if (quorum_inst->c == NULL) {
  79. error = qb_to_cs_error(-errno);
  80. goto error_put_destroy;
  81. }
  82. req.size = sizeof (req);
  83. req.id = MESSAGE_REQ_QUORUM_GETTYPE;
  84. iov.iov_base = (char *)&req;
  85. iov.iov_len = sizeof (req);
  86. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  87. quorum_inst->c,
  88. &iov,
  89. 1,
  90. &res_lib_quorum_gettype,
  91. sizeof (struct res_lib_quorum_gettype), -1));
  92. if (error != CS_OK) {
  93. goto error_put_destroy;
  94. }
  95. error = res_lib_quorum_gettype.header.error;
  96. *quorum_type = res_lib_quorum_gettype.quorum_type;
  97. if (callbacks)
  98. memcpy(&quorum_inst->callbacks, callbacks, sizeof (*callbacks));
  99. else
  100. memset(&quorum_inst->callbacks, 0, sizeof (*callbacks));
  101. (void)hdb_handle_put (&quorum_handle_t_db, *handle);
  102. return (CS_OK);
  103. error_put_destroy:
  104. (void)hdb_handle_put (&quorum_handle_t_db, *handle);
  105. error_destroy:
  106. (void)hdb_handle_destroy (&quorum_handle_t_db, *handle);
  107. error_no_destroy:
  108. return (error);
  109. }
  110. cs_error_t quorum_finalize (
  111. quorum_handle_t handle)
  112. {
  113. struct quorum_inst *quorum_inst;
  114. cs_error_t error;
  115. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  116. if (error != CS_OK) {
  117. return (error);
  118. }
  119. /*
  120. * Another thread has already started finalizing
  121. */
  122. if (quorum_inst->finalize) {
  123. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  124. return (CS_ERR_BAD_HANDLE);
  125. }
  126. quorum_inst->finalize = 1;
  127. qb_ipcc_disconnect (quorum_inst->c);
  128. (void)hdb_handle_destroy (&quorum_handle_t_db, handle);
  129. (void)hdb_handle_put (&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. struct qb_ipc_request_header req;
  140. struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
  141. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  142. if (error != CS_OK) {
  143. return (error);
  144. }
  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 = qb_to_cs_error(qb_ipcc_sendv_recv (
  150. quorum_inst->c,
  151. &iov,
  152. 1,
  153. &res_lib_quorum_getquorate,
  154. sizeof (struct res_lib_quorum_getquorate), CS_IPC_TIMEOUT_MS));
  155. if (error != CS_OK) {
  156. goto error_exit;
  157. }
  158. error = res_lib_quorum_getquorate.header.error;
  159. *quorate = res_lib_quorum_getquorate.quorate;
  160. error_exit:
  161. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  162. return (error);
  163. }
  164. cs_error_t quorum_fd_get (
  165. quorum_handle_t handle,
  166. int *fd)
  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. error = qb_to_cs_error(qb_ipcc_fd_get (quorum_inst->c, fd));
  175. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  176. return (error);
  177. }
  178. cs_error_t quorum_context_get (
  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. *context = quorum_inst->context;
  189. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  190. return (CS_OK);
  191. }
  192. cs_error_t quorum_context_set (
  193. quorum_handle_t handle,
  194. const void *context)
  195. {
  196. cs_error_t error;
  197. struct quorum_inst *quorum_inst;
  198. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  199. if (error != CS_OK) {
  200. return (error);
  201. }
  202. quorum_inst->context = context;
  203. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  204. return (CS_OK);
  205. }
  206. cs_error_t quorum_trackstart (
  207. quorum_handle_t handle,
  208. unsigned int flags )
  209. {
  210. cs_error_t error;
  211. struct quorum_inst *quorum_inst;
  212. struct iovec iov;
  213. struct req_lib_quorum_trackstart req_lib_quorum_trackstart;
  214. struct qb_ipc_response_header res;
  215. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  216. if (error != CS_OK) {
  217. return (error);
  218. }
  219. req_lib_quorum_trackstart.header.size = sizeof (struct req_lib_quorum_trackstart);
  220. req_lib_quorum_trackstart.header.id = MESSAGE_REQ_QUORUM_TRACKSTART;
  221. req_lib_quorum_trackstart.track_flags = flags;
  222. iov.iov_base = (char *)&req_lib_quorum_trackstart;
  223. iov.iov_len = sizeof (struct req_lib_quorum_trackstart);
  224. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  225. quorum_inst->c,
  226. &iov,
  227. 1,
  228. &res,
  229. sizeof (res), CS_IPC_TIMEOUT_MS));
  230. if (error != CS_OK) {
  231. goto error_exit;
  232. }
  233. error = res.error;
  234. error_exit:
  235. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  236. return (error);
  237. }
  238. cs_error_t quorum_trackstop (
  239. quorum_handle_t handle)
  240. {
  241. cs_error_t error;
  242. struct quorum_inst *quorum_inst;
  243. struct iovec iov;
  244. struct qb_ipc_request_header req;
  245. struct qb_ipc_response_header res;
  246. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle, (void *)&quorum_inst));
  247. if (error != CS_OK) {
  248. return (error);
  249. }
  250. req.size = sizeof (req);
  251. req.id = MESSAGE_REQ_QUORUM_TRACKSTOP;
  252. iov.iov_base = (char *)&req;
  253. iov.iov_len = sizeof (req);
  254. error = qb_to_cs_error(qb_ipcc_sendv_recv (
  255. quorum_inst->c,
  256. &iov,
  257. 1,
  258. &res,
  259. sizeof (res), CS_IPC_TIMEOUT_MS));
  260. if (error != CS_OK) {
  261. goto error_exit;
  262. }
  263. error = res.error;
  264. error_exit:
  265. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  266. return (error);
  267. }
  268. cs_error_t quorum_dispatch (
  269. quorum_handle_t handle,
  270. cs_dispatch_flags_t dispatch_types)
  271. {
  272. int timeout = -1;
  273. cs_error_t error;
  274. int cont = 1; /* always continue do loop except when set to 0 */
  275. struct quorum_inst *quorum_inst;
  276. quorum_callbacks_t callbacks;
  277. struct qb_ipc_response_header *dispatch_data;
  278. char dispatch_buf[IPC_DISPATCH_SIZE];
  279. struct res_lib_quorum_notification *res_lib_quorum_notification;
  280. if (dispatch_types != CS_DISPATCH_ONE &&
  281. dispatch_types != CS_DISPATCH_ALL &&
  282. dispatch_types != CS_DISPATCH_BLOCKING &&
  283. dispatch_types != CS_DISPATCH_ONE_NONBLOCKING) {
  284. return (CS_ERR_INVALID_PARAM);
  285. }
  286. error = hdb_error_to_cs(hdb_handle_get (&quorum_handle_t_db, handle,
  287. (void *)&quorum_inst));
  288. if (error != CS_OK) {
  289. return (error);
  290. }
  291. /*
  292. * Timeout instantly for CS_DISPATCH_ONE_NONBLOCKING or CS_DISPATCH_ALL and
  293. * wait indefinately for CS_DISPATCH_ONE or CS_DISPATCH_BLOCKING
  294. */
  295. if (dispatch_types == CS_DISPATCH_ALL || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  296. timeout = 0;
  297. }
  298. dispatch_data = (struct qb_ipc_response_header *)dispatch_buf;
  299. do {
  300. error = qb_to_cs_error (qb_ipcc_event_recv (
  301. quorum_inst->c,
  302. dispatch_buf,
  303. IPC_DISPATCH_SIZE,
  304. timeout));
  305. if (error == CS_ERR_BAD_HANDLE) {
  306. error = CS_OK;
  307. goto error_put;
  308. }
  309. if (error == CS_ERR_TRY_AGAIN) {
  310. if (dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  311. /*
  312. * Don't mask error
  313. */
  314. goto error_put;
  315. }
  316. error = CS_OK;
  317. if (dispatch_types == CS_DISPATCH_ALL) {
  318. break; /* exit do while cont is 1 loop */
  319. } else {
  320. continue; /* next poll */
  321. }
  322. }
  323. if (error != CS_OK) {
  324. goto error_put;
  325. }
  326. /*
  327. * Make copy of callbacks, message data, unlock instance, and call callback
  328. * A risk of this dispatch method is that the callback routines may
  329. * operate at the same time that quorum_finalize has been called in another thread.
  330. */
  331. memcpy (&callbacks, &quorum_inst->callbacks, sizeof (quorum_callbacks_t));
  332. /*
  333. * Dispatch incoming message
  334. */
  335. switch (dispatch_data->id) {
  336. case MESSAGE_RES_QUORUM_NOTIFICATION:
  337. if (callbacks.quorum_notify_fn == NULL) {
  338. break;
  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. if (dispatch_types == CS_DISPATCH_ONE || dispatch_types == CS_DISPATCH_ONE_NONBLOCKING) {
  356. cont = 0;
  357. }
  358. } while (cont);
  359. goto error_put;
  360. error_put:
  361. (void)hdb_handle_put (&quorum_handle_t_db, handle);
  362. return (error);
  363. }