vsf_quorum.c 14 KB

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