vsf_quorum.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 Red Hat 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. #include <config.h>
  35. #include <pwd.h>
  36. #include <grp.h>
  37. #include <sys/types.h>
  38. #include <sys/poll.h>
  39. #include <sys/uio.h>
  40. #include <sys/mman.h>
  41. #include <sys/socket.h>
  42. #include <sys/un.h>
  43. #include <sys/time.h>
  44. #include <sys/resource.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <corosync/corotypes.h>
  55. #include <corosync/coroipc_types.h>
  56. #include <corosync/corodefs.h>
  57. #include <corosync/swab.h>
  58. #include <corosync/list.h>
  59. #include <corosync/mar_gen.h>
  60. #include <corosync/ipc_quorum.h>
  61. #include <corosync/lcr/lcr_comp.h>
  62. #include <corosync/lcr/lcr_ifact.h>
  63. #include <corosync/mar_gen.h>
  64. #include <corosync/engine/coroapi.h>
  65. #include <corosync/engine/logsys.h>
  66. #include <corosync/engine/quorum.h>
  67. LOGSYS_DECLARE_SUBSYS ("QUORUM");
  68. struct quorum_pd {
  69. unsigned char track_flags;
  70. int tracking_enabled;
  71. struct list_head list;
  72. void *conn;
  73. };
  74. struct internal_callback_pd {
  75. struct list_head list;
  76. quorum_callback_fn_t callback;
  77. void *context;
  78. };
  79. static void message_handler_req_lib_quorum_getquorate (void *conn,
  80. const void *msg);
  81. static void message_handler_req_lib_quorum_trackstart (void *conn,
  82. const void *msg);
  83. static void message_handler_req_lib_quorum_trackstop (void *conn,
  84. const void *msg);
  85. static void send_library_notification(void *conn);
  86. static void send_internal_notification(void);
  87. static int quorum_exec_init_fn (struct corosync_api_v1 *api);
  88. static int quorum_lib_init_fn (void *conn);
  89. static int quorum_lib_exit_fn (void *conn);
  90. static int primary_designated = 0;
  91. static struct corosync_api_v1 *corosync_api;
  92. static struct list_head lib_trackers_list;
  93. static struct list_head internal_trackers_list;
  94. static struct memb_ring_id quorum_ring_id;
  95. static size_t quorum_view_list_entries = 0;
  96. static int quorum_view_list[PROCESSOR_COUNT_MAX];
  97. struct quorum_services_api_ver1 *quorum_iface = NULL;
  98. /* Internal quorum API function */
  99. static void quorum_api_set_quorum(const unsigned int *view_list,
  100. size_t view_list_entries,
  101. int quorum, struct memb_ring_id *ring_id)
  102. {
  103. int i;
  104. primary_designated = quorum;
  105. if (primary_designated) {
  106. log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the primary component and will provide service.\n");
  107. } else {
  108. log_printf (LOGSYS_LEVEL_NOTICE, "This node is within the non-primary component and will NOT provide any services.\n");
  109. }
  110. quorum_view_list_entries = view_list_entries;
  111. memcpy(&quorum_ring_id, ring_id, sizeof (quorum_ring_id));
  112. memcpy(quorum_view_list, view_list, sizeof(unsigned int)*view_list_entries);
  113. log_printf (LOGSYS_LEVEL_NOTICE, "Members[%d]: ", (int)view_list_entries);
  114. for (i=0; i<view_list_entries; i++)
  115. log_printf (LOGSYS_LEVEL_NOTICE, " %d ", view_list[i]);
  116. /* Tell internal listeners */
  117. send_internal_notification();
  118. /* Tell IPC listeners */
  119. send_library_notification(NULL);
  120. }
  121. static struct corosync_lib_handler quorum_lib_service[] =
  122. {
  123. { /* 0 */
  124. .lib_handler_fn = message_handler_req_lib_quorum_getquorate,
  125. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  126. },
  127. { /* 1 */
  128. .lib_handler_fn = message_handler_req_lib_quorum_trackstart,
  129. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  130. },
  131. { /* 2 */
  132. .lib_handler_fn = message_handler_req_lib_quorum_trackstop,
  133. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  134. }
  135. };
  136. static struct corosync_service_engine quorum_service_handler = {
  137. .name = "corosync cluster quorum service v0.1",
  138. .id = QUORUM_SERVICE,
  139. .private_data_size = sizeof (struct quorum_pd),
  140. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  141. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  142. .lib_init_fn = quorum_lib_init_fn,
  143. .lib_exit_fn = quorum_lib_exit_fn,
  144. .lib_engine = quorum_lib_service,
  145. .exec_init_fn = quorum_exec_init_fn,
  146. .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler),
  147. };
  148. static struct lcr_iface corosync_quorum_ver0[1] = {
  149. {
  150. .name = "corosync_quorum",
  151. .version = 0,
  152. .versions_replace = 0,
  153. .versions_replace_count = 0,
  154. .dependencies = 0,
  155. .dependency_count = 0,
  156. .constructor = NULL,
  157. .destructor = NULL,
  158. .interfaces = NULL,
  159. },
  160. };
  161. static struct corosync_service_engine *quorum_get_service_handler_ver0 (void)
  162. {
  163. return (&quorum_service_handler);
  164. }
  165. static struct lcr_comp quorum_comp_ver0 = {
  166. .iface_count = 1,
  167. .ifaces = corosync_quorum_ver0
  168. };
  169. static struct corosync_service_engine_iface_ver0 quorum_service_handler_iface = {
  170. .corosync_get_service_engine_ver0 = quorum_get_service_handler_ver0
  171. };
  172. __attribute__ ((constructor)) static void quorum_comp_register (void) {
  173. lcr_component_register (&quorum_comp_ver0);
  174. lcr_interfaces_set (&corosync_quorum_ver0[0], &quorum_service_handler_iface);
  175. }
  176. /* -------------------------------------------------- */
  177. /*
  178. * Internal API functions for corosync
  179. */
  180. static int quorum_quorate(void)
  181. {
  182. return primary_designated;
  183. }
  184. static int quorum_register_callback(quorum_callback_fn_t function, void *context)
  185. {
  186. struct internal_callback_pd *pd = malloc(sizeof(struct internal_callback_pd));
  187. if (!pd)
  188. return -1;
  189. pd->context = context;
  190. pd->callback = function;
  191. list_add (&pd->list, &internal_trackers_list);
  192. return 0;
  193. }
  194. static int quorum_unregister_callback(quorum_callback_fn_t function, void *context)
  195. {
  196. struct internal_callback_pd *pd;
  197. struct list_head *tmp;
  198. for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
  199. pd = list_entry(tmp, struct internal_callback_pd, list);
  200. if (pd->callback == function && pd->context == context) {
  201. list_del(&pd->list);
  202. return 0;
  203. }
  204. }
  205. return -1;
  206. }
  207. static struct quorum_callin_functions callins = {
  208. .quorate = quorum_quorate,
  209. .register_callback = quorum_register_callback,
  210. .unregister_callback = quorum_unregister_callback
  211. };
  212. /* --------------------------------------------------------------------- */
  213. static int quorum_exec_init_fn (struct corosync_api_v1 *api)
  214. {
  215. hdb_handle_t find_handle;
  216. hdb_handle_t quorum_handle = 0;
  217. hdb_handle_t q_handle;
  218. char *quorum_module;
  219. int res;
  220. void *quorum_iface_p;
  221. corosync_api = api;
  222. list_init (&lib_trackers_list);
  223. list_init (&internal_trackers_list);
  224. /*
  225. * Tell corosync we have a quorum engine.
  226. */
  227. api->quorum_initialize(&callins);
  228. /*
  229. * Look for a quorum provider
  230. */
  231. api->object_find_create(OBJECT_PARENT_HANDLE, "quorum", strlen("quorum"), &find_handle);
  232. api->object_find_next(find_handle, &quorum_handle);
  233. api->object_find_destroy(find_handle);
  234. if (quorum_handle) {
  235. if ( !(res = api->object_key_get(quorum_handle,
  236. "provider",
  237. strlen("provider"),
  238. (void *)&quorum_module,
  239. NULL))) {
  240. res = lcr_ifact_reference (
  241. &q_handle,
  242. quorum_module,
  243. 0,
  244. &quorum_iface_p,
  245. 0);
  246. if (res == -1) {
  247. log_printf (LOGSYS_LEVEL_NOTICE,
  248. "Couldn't load quorum provider %s\n",
  249. quorum_module);
  250. return (-1);
  251. }
  252. log_printf (LOGSYS_LEVEL_NOTICE,
  253. "Using quorum provider %s\n", quorum_module);
  254. quorum_iface = (struct quorum_services_api_ver1 *)quorum_iface_p;
  255. quorum_iface->init (api, quorum_api_set_quorum);
  256. }
  257. }
  258. if (!quorum_iface) {
  259. /*
  260. * With no quorum provider, we are always quorate
  261. */
  262. primary_designated = 1;
  263. }
  264. return (0);
  265. }
  266. static int quorum_lib_init_fn (void *conn)
  267. {
  268. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  269. log_printf(LOGSYS_LEVEL_DEBUG, "lib_init_fn: conn=%p\n", conn);
  270. list_init (&pd->list);
  271. pd->conn = conn;
  272. return (0);
  273. }
  274. static int quorum_lib_exit_fn (void *conn)
  275. {
  276. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  277. log_printf(LOGSYS_LEVEL_DEBUG, "lib_exit_fn: conn=%p\n", conn);
  278. if (quorum_pd->tracking_enabled) {
  279. list_del (&quorum_pd->list);
  280. list_init (&quorum_pd->list);
  281. }
  282. return (0);
  283. }
  284. static void send_internal_notification(void)
  285. {
  286. struct list_head *tmp;
  287. struct internal_callback_pd *pd;
  288. for (tmp = internal_trackers_list.next; tmp != &internal_trackers_list; tmp = tmp->next) {
  289. pd = list_entry(tmp, struct internal_callback_pd, list);
  290. pd->callback(primary_designated, pd->context);
  291. }
  292. }
  293. static void send_library_notification(void *conn)
  294. {
  295. int size = sizeof(struct res_lib_quorum_notification) + sizeof(unsigned int)*quorum_view_list_entries;
  296. char buf[size];
  297. struct res_lib_quorum_notification *res_lib_quorum_notification = (struct res_lib_quorum_notification *)buf;
  298. struct list_head *tmp;
  299. int i;
  300. log_printf(LOGSYS_LEVEL_DEBUG, "sending quorum notification to %p, length = %d\n", conn, size);
  301. res_lib_quorum_notification->quorate = primary_designated;
  302. res_lib_quorum_notification->ring_seq = quorum_ring_id.seq;
  303. res_lib_quorum_notification->view_list_entries = quorum_view_list_entries;
  304. for (i=0; i<quorum_view_list_entries; i++) {
  305. res_lib_quorum_notification->view_list[i] = quorum_view_list[i];
  306. }
  307. res_lib_quorum_notification->header.id = MESSAGE_RES_QUORUM_NOTIFICATION;
  308. res_lib_quorum_notification->header.size = size;
  309. res_lib_quorum_notification->header.error = CS_OK;
  310. /* Send it to all interested parties */
  311. if (conn) {
  312. corosync_api->ipc_response_send(conn, res_lib_quorum_notification, size);
  313. }
  314. else {
  315. struct quorum_pd *qpd;
  316. for (tmp = lib_trackers_list.next; tmp != &lib_trackers_list; tmp = tmp->next) {
  317. qpd = list_entry(tmp, struct quorum_pd, list);
  318. corosync_api->ipc_dispatch_send(qpd->conn,
  319. res_lib_quorum_notification, size);
  320. }
  321. }
  322. return;
  323. }
  324. static void message_handler_req_lib_quorum_getquorate (void *conn,
  325. const void *msg)
  326. {
  327. struct res_lib_quorum_getquorate res_lib_quorum_getquorate;
  328. log_printf(LOGSYS_LEVEL_DEBUG, "got quorate request on %p\n", conn);
  329. /* send status */
  330. res_lib_quorum_getquorate.quorate = primary_designated;
  331. res_lib_quorum_getquorate.header.size = sizeof(res_lib_quorum_getquorate);
  332. res_lib_quorum_getquorate.header.id = MESSAGE_RES_QUORUM_GETQUORATE;
  333. res_lib_quorum_getquorate.header.error = CS_OK;
  334. corosync_api->ipc_response_send(conn, &res_lib_quorum_getquorate, sizeof(res_lib_quorum_getquorate));
  335. }
  336. static void message_handler_req_lib_quorum_trackstart (void *conn,
  337. const void *msg)
  338. {
  339. const struct req_lib_quorum_trackstart *req_lib_quorum_trackstart = msg;
  340. coroipc_response_header_t res;
  341. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  342. log_printf(LOGSYS_LEVEL_DEBUG, "got trackstart request on %p\n", conn);
  343. /*
  344. * If an immediate listing of the current cluster membership
  345. * is requested, generate membership list
  346. */
  347. if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  348. req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  349. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p\n", conn);
  350. send_library_notification(conn);
  351. }
  352. /*
  353. * Record requests for tracking
  354. */
  355. if (req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  356. req_lib_quorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  357. quorum_pd->track_flags = req_lib_quorum_trackstart->track_flags;
  358. quorum_pd->tracking_enabled = 1;
  359. list_add (&quorum_pd->list, &lib_trackers_list);
  360. }
  361. /* send status */
  362. res.size = sizeof(res);
  363. res.id = MESSAGE_RES_QUORUM_TRACKSTART;
  364. res.error = CS_OK;
  365. corosync_api->ipc_response_send(conn, &res, sizeof(coroipc_response_header_t));
  366. }
  367. static void message_handler_req_lib_quorum_trackstop (void *conn, const void *msg)
  368. {
  369. coroipc_response_header_t res;
  370. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  371. log_printf(LOGSYS_LEVEL_DEBUG, "got trackstop request on %p\n", conn);
  372. if (quorum_pd->tracking_enabled) {
  373. res.error = CS_OK;
  374. quorum_pd->tracking_enabled = 0;
  375. list_del (&quorum_pd->list);
  376. list_init (&quorum_pd->list);
  377. } else {
  378. res.error = CS_ERR_NOT_EXIST;
  379. }
  380. /* send status */
  381. res.size = sizeof(res);
  382. res.id = MESSAGE_RES_QUORUM_TRACKSTOP;
  383. res.error = CS_OK;
  384. corosync_api->ipc_response_send(conn, &res, sizeof(coroipc_response_header_t));
  385. }