votequorum.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. /*
  2. * Copyright (c) 2009-2011 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Authors: Christine Caulfield (ccaulfie@redhat.com)
  7. * Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <sys/types.h>
  37. #ifdef HAVE_ALLOCA_H
  38. #include <alloca.h>
  39. #endif
  40. #include <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/un.h>
  43. #include <sys/time.h>
  44. #include <sys/ioctl.h>
  45. #include <netinet/in.h>
  46. #include <sys/uio.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <time.h>
  53. #include <unistd.h>
  54. #include <netinet/in.h>
  55. #include <arpa/inet.h>
  56. #include <qb/qbipc_common.h>
  57. #include <qb/qbdefs.h>
  58. #include <qb/qbutil.h>
  59. #include <corosync/corotypes.h>
  60. #include <corosync/corodefs.h>
  61. #include <corosync/cfg.h>
  62. #include <corosync/list.h>
  63. #include <corosync/lcr/lcr_comp.h>
  64. #include <corosync/logsys.h>
  65. #include <corosync/mar_gen.h>
  66. #include <corosync/coroapi.h>
  67. #include <corosync/engine/quorum.h>
  68. #include <corosync/engine/icmap.h>
  69. #include <corosync/ipc_votequorum.h>
  70. #define VOTEQUORUM_MAJOR_VERSION 7
  71. #define VOTEQUORUM_MINOR_VERSION 0
  72. #define VOTEQUORUM_PATCH_VERSION 0
  73. /* Silly default to prevent accidents! */
  74. #define DEFAULT_EXPECTED 1024
  75. #define DEFAULT_QDEV_POLL 10000
  76. #define DEFAULT_LEAVE_TMO 10000
  77. #define DEFAULT_LMS_WIN 10000
  78. LOGSYS_DECLARE_SUBSYS ("VOTEQ");
  79. enum quorum_message_req_types {
  80. MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO = 0,
  81. MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE = 1,
  82. };
  83. #define NODE_FLAGS_BEENDOWN 1
  84. #define NODE_FLAGS_HASSTATE 8
  85. #define NODE_FLAGS_QDISK 16
  86. #define NODE_FLAGS_REMOVED 32
  87. #define NODE_FLAGS_US 64
  88. #define NODEID_US 0
  89. #define NODEID_QDEVICE -1
  90. typedef enum { NODESTATE_JOINING=1, NODESTATE_MEMBER,
  91. NODESTATE_DEAD, NODESTATE_LEAVING } nodestate_t;
  92. struct cluster_node {
  93. int flags;
  94. int node_id;
  95. unsigned int expected_votes;
  96. unsigned int votes;
  97. time_t join_time;
  98. nodestate_t state;
  99. unsigned long long int last_hello; /* Only used for quorum devices */
  100. struct list_head list;
  101. };
  102. static int quorum_flags;
  103. #define VOTEQUORUM_FLAG_FEATURE_TWONODE 1
  104. static int quorum;
  105. static int cluster_is_quorate;
  106. static int first_trans = 1;
  107. static unsigned int quorumdev_poll = DEFAULT_QDEV_POLL;
  108. static unsigned int leaving_timeout = DEFAULT_LEAVE_TMO;
  109. static uint8_t wait_for_all = 0;
  110. static uint8_t auto_tie_breaker = 0;
  111. static int lowest_node_id = -1;
  112. static uint8_t last_man_standing = 0;
  113. static uint32_t last_man_standing_window = DEFAULT_LMS_WIN;
  114. static int last_man_standing_timer_set = 0;
  115. static corosync_timer_handle_t last_man_standing_timer;
  116. static struct cluster_node *us;
  117. static struct cluster_node *quorum_device = NULL;
  118. static char quorum_device_name[VOTEQUORUM_MAX_QDISK_NAME_LEN];
  119. static corosync_timer_handle_t quorum_device_timer;
  120. static corosync_timer_handle_t leaving_timer;
  121. static struct list_head cluster_members_list;
  122. static struct corosync_api_v1 *corosync_api;
  123. static struct list_head trackers_list;
  124. static unsigned int quorum_members[PROCESSOR_COUNT_MAX+1];
  125. static int quorum_members_entries = 0;
  126. static struct memb_ring_id quorum_ringid;
  127. #define max(a,b) (((a) > (b)) ? (a) : (b))
  128. static struct cluster_node *find_node_by_nodeid(int nodeid);
  129. static struct cluster_node *allocate_node(int nodeid);
  130. #define list_iterate(v, head) \
  131. for (v = (head)->next; v != head; v = v->next)
  132. struct quorum_pd {
  133. unsigned char track_flags;
  134. int tracking_enabled;
  135. uint64_t tracking_context;
  136. struct list_head list;
  137. void *conn;
  138. };
  139. /*
  140. * Service Interfaces required by service_message_handler struct
  141. */
  142. static void votequorum_init(struct corosync_api_v1 *api,
  143. quorum_set_quorate_fn_t report);
  144. static void quorum_confchg_fn (
  145. enum totem_configuration_type configuration_type,
  146. const unsigned int *member_list, size_t member_list_entries,
  147. const unsigned int *left_list, size_t left_list_entries,
  148. const unsigned int *joined_list, size_t joined_list_entries,
  149. const struct memb_ring_id *ring_id);
  150. static int votequorum_exec_init_fn (struct corosync_api_v1 *api);
  151. static int quorum_lib_init_fn (void *conn);
  152. static int quorum_lib_exit_fn (void *conn);
  153. static void message_handler_req_exec_votequorum_nodeinfo (
  154. const void *message,
  155. unsigned int nodeid);
  156. static void message_handler_req_exec_votequorum_reconfigure (
  157. const void *message,
  158. unsigned int nodeid);
  159. static void message_handler_req_lib_votequorum_getinfo (void *conn,
  160. const void *message);
  161. static void message_handler_req_lib_votequorum_setexpected (void *conn,
  162. const void *message);
  163. static void message_handler_req_lib_votequorum_setvotes (void *conn,
  164. const void *message);
  165. static void message_handler_req_lib_votequorum_qdisk_register (void *conn,
  166. const void *message);
  167. static void message_handler_req_lib_votequorum_qdisk_unregister (void *conn,
  168. const void *message);
  169. static void message_handler_req_lib_votequorum_qdisk_poll (void *conn,
  170. const void *message);
  171. static void message_handler_req_lib_votequorum_qdisk_getinfo (void *conn,
  172. const void *message);
  173. static void message_handler_req_lib_votequorum_setstate (void *conn,
  174. const void *message);
  175. static void message_handler_req_lib_votequorum_leaving (void *conn,
  176. const void *message);
  177. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  178. const void *msg);
  179. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  180. const void *msg);
  181. static int quorum_exec_send_nodeinfo(void);
  182. static int quorum_exec_send_reconfigure(int param, int nodeid, int value);
  183. static void exec_votequorum_nodeinfo_endian_convert (void *msg);
  184. static void exec_votequorum_reconfigure_endian_convert (void *msg);
  185. static void add_votequorum_config_notification(void);
  186. static void recalculate_quorum(int allow_decrease, int by_current_nodes);
  187. /*
  188. * Library Handler Definition
  189. */
  190. static struct corosync_lib_handler quorum_lib_service[] =
  191. {
  192. { /* 0 */
  193. .lib_handler_fn = message_handler_req_lib_votequorum_getinfo,
  194. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  195. },
  196. { /* 1 */
  197. .lib_handler_fn = message_handler_req_lib_votequorum_setexpected,
  198. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  199. },
  200. { /* 2 */
  201. .lib_handler_fn = message_handler_req_lib_votequorum_setvotes,
  202. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  203. },
  204. { /* 3 */
  205. .lib_handler_fn = message_handler_req_lib_votequorum_qdisk_register,
  206. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  207. },
  208. { /* 4 */
  209. .lib_handler_fn = message_handler_req_lib_votequorum_qdisk_unregister,
  210. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  211. },
  212. { /* 5 */
  213. .lib_handler_fn = message_handler_req_lib_votequorum_qdisk_poll,
  214. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  215. },
  216. { /* 6 */
  217. .lib_handler_fn = message_handler_req_lib_votequorum_qdisk_getinfo,
  218. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  219. },
  220. { /* 7 */
  221. .lib_handler_fn = message_handler_req_lib_votequorum_setstate,
  222. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  223. },
  224. { /* 8 */
  225. .lib_handler_fn = message_handler_req_lib_votequorum_leaving,
  226. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  227. },
  228. { /* 9 */
  229. .lib_handler_fn = message_handler_req_lib_votequorum_trackstart,
  230. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  231. },
  232. { /* 10 */
  233. .lib_handler_fn = message_handler_req_lib_votequorum_trackstop,
  234. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  235. }
  236. };
  237. static struct corosync_exec_handler votequorum_exec_engine[] =
  238. {
  239. { /* 0 */
  240. .exec_handler_fn = message_handler_req_exec_votequorum_nodeinfo,
  241. .exec_endian_convert_fn = exec_votequorum_nodeinfo_endian_convert
  242. },
  243. { /* 1 */
  244. .exec_handler_fn = message_handler_req_exec_votequorum_reconfigure,
  245. .exec_endian_convert_fn = exec_votequorum_reconfigure_endian_convert
  246. },
  247. };
  248. static quorum_set_quorate_fn_t set_quorum;
  249. /*
  250. * lcrso object definition
  251. */
  252. static struct quorum_services_api_ver1 votequorum_iface_ver0 = {
  253. .init = votequorum_init
  254. };
  255. static struct corosync_service_engine quorum_service_handler = {
  256. .name = "corosync votes quorum service v0.91",
  257. .id = VOTEQUORUM_SERVICE,
  258. .private_data_size = sizeof (struct quorum_pd),
  259. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  260. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED,
  261. .lib_init_fn = quorum_lib_init_fn,
  262. .lib_exit_fn = quorum_lib_exit_fn,
  263. .lib_engine = quorum_lib_service,
  264. .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler),
  265. .exec_init_fn = votequorum_exec_init_fn,
  266. .exec_engine = votequorum_exec_engine,
  267. .exec_engine_count = sizeof (votequorum_exec_engine) / sizeof (struct corosync_exec_handler),
  268. .confchg_fn = quorum_confchg_fn,
  269. .sync_mode = CS_SYNC_V1
  270. };
  271. /*
  272. * Dynamic loader definition
  273. */
  274. static struct corosync_service_engine *quorum_get_service_handler_ver0 (void);
  275. static struct corosync_service_engine_iface_ver0 quorum_service_handler_iface = {
  276. .corosync_get_service_engine_ver0 = quorum_get_service_handler_ver0
  277. };
  278. static struct lcr_iface corosync_quorum_ver0[2] = {
  279. {
  280. .name = "corosync_votequorum",
  281. .version = 0,
  282. .versions_replace = 0,
  283. .versions_replace_count = 0,
  284. .dependencies = 0,
  285. .dependency_count = 0,
  286. .constructor = NULL,
  287. .destructor = NULL,
  288. .interfaces = (void **)(void *)&votequorum_iface_ver0
  289. },
  290. {
  291. .name = "corosync_votequorum_iface",
  292. .version = 0,
  293. .versions_replace = 0,
  294. .versions_replace_count = 0,
  295. .dependencies = 0,
  296. .dependency_count = 0,
  297. .constructor = NULL,
  298. .destructor = NULL,
  299. .interfaces = NULL
  300. }
  301. };
  302. static struct lcr_comp quorum_comp_ver0 = {
  303. .iface_count = 2,
  304. .ifaces = corosync_quorum_ver0
  305. };
  306. static struct corosync_service_engine *quorum_get_service_handler_ver0 (void)
  307. {
  308. return (&quorum_service_handler);
  309. }
  310. #ifdef COROSYNC_SOLARIS
  311. void corosync_lcr_component_register (void);
  312. void corosync_lcr_component_register (void) {
  313. #else
  314. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  315. #endif
  316. lcr_interfaces_set (&corosync_quorum_ver0[0], &votequorum_iface_ver0);
  317. lcr_interfaces_set (&corosync_quorum_ver0[1], &quorum_service_handler_iface);
  318. lcr_component_register (&quorum_comp_ver0);
  319. }
  320. static void votequorum_init(struct corosync_api_v1 *api,
  321. quorum_set_quorate_fn_t report)
  322. {
  323. ENTER();
  324. set_quorum = report;
  325. icmap_get_uint8("quorum.wait_for_all", &wait_for_all);
  326. icmap_get_uint8("quorum.auto_tie_breaker", &auto_tie_breaker);
  327. icmap_get_uint8("quorum.last_man_standing", &last_man_standing);
  328. icmap_get_uint32("quorum.last_man_standing_window", &last_man_standing_window);
  329. /*
  330. * TODO: we need to know the lowest node-id in the cluster
  331. * current lack of node list with node-id's requires us to see all nodes
  332. * to determine which is the lowest.
  333. */
  334. if (auto_tie_breaker) {
  335. wait_for_all = 1;
  336. }
  337. /* Load the library-servicing part of this module */
  338. api->service_link_and_init(api, "corosync_votequorum_iface", 0);
  339. LEAVE();
  340. }
  341. struct req_exec_quorum_nodeinfo {
  342. struct qb_ipc_request_header header __attribute__((aligned(8)));
  343. unsigned int first_trans;
  344. unsigned int votes;
  345. unsigned int expected_votes;
  346. unsigned int major_version; /* Not backwards compatible */
  347. unsigned int minor_version; /* Backwards compatible */
  348. unsigned int patch_version; /* Backwards/forwards compatible */
  349. unsigned int config_version;
  350. unsigned int flags;
  351. unsigned int wait_for_all;
  352. } __attribute__((packed));
  353. /* Parameters for RECONFIG command */
  354. #define RECONFIG_PARAM_EXPECTED_VOTES 1
  355. #define RECONFIG_PARAM_NODE_VOTES 2
  356. #define RECONFIG_PARAM_LEAVING 3
  357. struct req_exec_quorum_reconfigure {
  358. struct qb_ipc_request_header header __attribute__((aligned(8)));
  359. unsigned int param;
  360. unsigned int nodeid;
  361. unsigned int value;
  362. };
  363. static void read_quorum_config(void)
  364. {
  365. uint8_t value = 0;
  366. int cluster_members = 0;
  367. struct list_head *tmp;
  368. log_printf(LOGSYS_LEVEL_INFO, "Reading configuration\n");
  369. if (icmap_get_uint32("quorum.expected_votes", &us->expected_votes) != CS_OK) {
  370. us->expected_votes = DEFAULT_EXPECTED;
  371. }
  372. if (icmap_get_uint32("quorum.votes", &us->votes) != CS_OK) {
  373. us->votes = 1;
  374. }
  375. if (icmap_get_uint32("quorum.quorumdev_poll", &quorumdev_poll) != CS_OK) {
  376. quorumdev_poll = DEFAULT_QDEV_POLL;
  377. }
  378. if (icmap_get_uint32("quorum.leaving_timeout", &leaving_timeout) != CS_OK) {
  379. leaving_timeout = DEFAULT_LEAVE_TMO;
  380. }
  381. value = 0;
  382. icmap_get_uint8("quorum.two_node", &value);
  383. if (value)
  384. quorum_flags |= VOTEQUORUM_FLAG_FEATURE_TWONODE;
  385. else
  386. quorum_flags &= ~VOTEQUORUM_FLAG_FEATURE_TWONODE;
  387. /*
  388. * two_node mode is invalid if there are more than 2 nodes in the cluster!
  389. */
  390. list_iterate(tmp, &cluster_members_list) {
  391. cluster_members++;
  392. }
  393. if (quorum_flags & VOTEQUORUM_FLAG_FEATURE_TWONODE && cluster_members > 2) {
  394. log_printf(LOGSYS_LEVEL_WARNING, "quorum.two_node was set but there are more than 2 nodes in the cluster. It will be ignored.");
  395. quorum_flags &= ~VOTEQUORUM_FLAG_FEATURE_TWONODE;
  396. }
  397. }
  398. static int votequorum_exec_init_fn (struct corosync_api_v1 *api)
  399. {
  400. #ifdef COROSYNC_SOLARIS
  401. logsys_subsys_init();
  402. #endif
  403. ENTER();
  404. corosync_api = api;
  405. list_init(&cluster_members_list);
  406. list_init(&trackers_list);
  407. /* Allocate a cluster_node for us */
  408. us = allocate_node(corosync_api->totem_nodeid_get());
  409. if (!us)
  410. return (1);
  411. us->flags |= NODE_FLAGS_US;
  412. us->state = NODESTATE_MEMBER;
  413. us->expected_votes = DEFAULT_EXPECTED;
  414. us->votes = 1;
  415. time(&us->join_time);
  416. read_quorum_config();
  417. recalculate_quorum(0, 0);
  418. /* Listen for changes */
  419. add_votequorum_config_notification();
  420. /* Start us off with one node */
  421. quorum_exec_send_nodeinfo();
  422. LEAVE();
  423. return (0);
  424. }
  425. static int quorum_lib_exit_fn (void *conn)
  426. {
  427. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  428. ENTER();
  429. if (quorum_pd->tracking_enabled) {
  430. list_del (&quorum_pd->list);
  431. list_init (&quorum_pd->list);
  432. }
  433. LEAVE();
  434. return (0);
  435. }
  436. static int send_quorum_notification(void *conn, uint64_t context)
  437. {
  438. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  439. struct list_head *tmp;
  440. struct cluster_node *node;
  441. int cluster_members = 0;
  442. int i = 0;
  443. int size;
  444. char *buf;
  445. ENTER();
  446. list_iterate(tmp, &cluster_members_list) {
  447. node = list_entry(tmp, struct cluster_node, list);
  448. cluster_members++;
  449. }
  450. if (quorum_device)
  451. cluster_members++;
  452. size = sizeof(struct res_lib_votequorum_notification) + sizeof(struct votequorum_node) * cluster_members;
  453. buf = alloca(size);
  454. if (!buf) {
  455. LEAVE();
  456. return -1;
  457. }
  458. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)buf;
  459. res_lib_votequorum_notification->quorate = cluster_is_quorate;
  460. res_lib_votequorum_notification->node_list_entries = cluster_members;
  461. res_lib_votequorum_notification->context = context;
  462. list_iterate(tmp, &cluster_members_list) {
  463. node = list_entry(tmp, struct cluster_node, list);
  464. res_lib_votequorum_notification->node_list[i].nodeid = node->node_id;
  465. res_lib_votequorum_notification->node_list[i++].state = node->state;
  466. }
  467. if (quorum_device) {
  468. res_lib_votequorum_notification->node_list[i].nodeid = 0;
  469. res_lib_votequorum_notification->node_list[i++].state = quorum_device->state | 0x80;
  470. }
  471. res_lib_votequorum_notification->header.id = MESSAGE_RES_VOTEQUORUM_NOTIFICATION;
  472. res_lib_votequorum_notification->header.size = size;
  473. res_lib_votequorum_notification->header.error = CS_OK;
  474. /* Send it to all interested parties */
  475. if (conn) {
  476. int ret = corosync_api->ipc_dispatch_send(conn, buf, size);
  477. LEAVE();
  478. return ret;
  479. }
  480. else {
  481. struct quorum_pd *qpd;
  482. list_iterate(tmp, &trackers_list) {
  483. qpd = list_entry(tmp, struct quorum_pd, list);
  484. res_lib_votequorum_notification->context = qpd->tracking_context;
  485. corosync_api->ipc_dispatch_send(qpd->conn, buf, size);
  486. }
  487. }
  488. LEAVE();
  489. return 0;
  490. }
  491. static void send_expectedvotes_notification(void)
  492. {
  493. struct res_lib_votequorum_expectedvotes_notification res_lib_votequorum_expectedvotes_notification;
  494. struct quorum_pd *qpd;
  495. struct list_head *tmp;
  496. log_printf(LOGSYS_LEVEL_DEBUG, "Sending expected votes callback\n");
  497. res_lib_votequorum_expectedvotes_notification.header.id = MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION;
  498. res_lib_votequorum_expectedvotes_notification.header.size = sizeof(res_lib_votequorum_expectedvotes_notification);
  499. res_lib_votequorum_expectedvotes_notification.header.error = CS_OK;
  500. res_lib_votequorum_expectedvotes_notification.expected_votes = us->expected_votes;
  501. list_iterate(tmp, &trackers_list) {
  502. qpd = list_entry(tmp, struct quorum_pd, list);
  503. res_lib_votequorum_expectedvotes_notification.context = qpd->tracking_context;
  504. corosync_api->ipc_dispatch_send(qpd->conn, &res_lib_votequorum_expectedvotes_notification,
  505. sizeof(struct res_lib_votequorum_expectedvotes_notification));
  506. }
  507. }
  508. static void get_lowest_node_id(void)
  509. {
  510. struct cluster_node *node = NULL;
  511. struct list_head *tmp;
  512. lowest_node_id = us->node_id;
  513. list_iterate(tmp, &cluster_members_list) {
  514. node = list_entry(tmp, struct cluster_node, list);
  515. if (node->node_id < lowest_node_id)
  516. lowest_node_id = node->node_id;
  517. }
  518. log_printf(LOGSYS_LEVEL_DEBUG, "lowest node id: %d us: %d\n", lowest_node_id, us->node_id);
  519. }
  520. static int check_low_node_id_partition(void)
  521. {
  522. struct cluster_node *node = NULL;
  523. struct list_head *tmp;
  524. int found = 0;
  525. list_iterate(tmp, &cluster_members_list) {
  526. node = list_entry(tmp, struct cluster_node, list);
  527. if (node->state == NODESTATE_MEMBER) {
  528. if (node->node_id == lowest_node_id) {
  529. found = 1;
  530. }
  531. }
  532. }
  533. return found;
  534. }
  535. static void set_quorate(int total_votes)
  536. {
  537. int quorate;
  538. ENTER();
  539. /*
  540. * wait for all nodes to show up before granting quorum
  541. */
  542. if (wait_for_all) {
  543. if (total_votes != us->expected_votes) {
  544. log_printf(LOGSYS_LEVEL_NOTICE,
  545. "Waiting for all cluster members. "
  546. "Current votes: %d expected_votes: %d\n",
  547. total_votes, us->expected_votes);
  548. cluster_is_quorate = 0;
  549. return;
  550. }
  551. wait_for_all = 0;
  552. get_lowest_node_id();
  553. }
  554. if (quorum > total_votes) {
  555. quorate = 0;
  556. }
  557. else {
  558. quorate = 1;
  559. }
  560. if ((auto_tie_breaker) &&
  561. (total_votes == (us->expected_votes / 2)) &&
  562. (check_low_node_id_partition() == 1)) {
  563. quorate = 1;
  564. }
  565. if (cluster_is_quorate && !quorate)
  566. log_printf(LOGSYS_LEVEL_INFO, "quorum lost, blocking activity\n");
  567. if (!cluster_is_quorate && quorate)
  568. log_printf(LOGSYS_LEVEL_INFO, "quorum regained, resuming activity\n");
  569. cluster_is_quorate = quorate;
  570. ENTER();
  571. }
  572. static int calculate_quorum(int allow_decrease, int max_expected, unsigned int *ret_total_votes)
  573. {
  574. struct list_head *nodelist;
  575. struct cluster_node *node;
  576. unsigned int total_votes = 0;
  577. unsigned int highest_expected = 0;
  578. unsigned int newquorum, q1, q2;
  579. unsigned int total_nodes = 0;
  580. ENTER();
  581. list_iterate(nodelist, &cluster_members_list) {
  582. node = list_entry(nodelist, struct cluster_node, list);
  583. log_printf(LOGSYS_LEVEL_DEBUG, "node %x state=%d, votes=%d, expected=%d\n",
  584. node->node_id, node->state, node->votes, node->expected_votes);
  585. if (node->state == NODESTATE_MEMBER) {
  586. if (max_expected)
  587. node->expected_votes = max_expected;
  588. else
  589. highest_expected = max(highest_expected, node->expected_votes);
  590. total_votes += node->votes;
  591. total_nodes++;
  592. }
  593. }
  594. if (quorum_device && quorum_device->state == NODESTATE_MEMBER)
  595. total_votes += quorum_device->votes;
  596. if (max_expected > 0)
  597. highest_expected = max_expected;
  598. /* This quorum calculation is taken from the OpenVMS Cluster Systems
  599. * manual, but, then, you guessed that didn't you */
  600. q1 = (highest_expected + 2) / 2;
  601. q2 = (total_votes + 2) / 2;
  602. newquorum = max(q1, q2);
  603. /* Normally quorum never decreases but the system administrator can
  604. * force it down by setting expected votes to a maximum value */
  605. if (!allow_decrease)
  606. newquorum = max(quorum, newquorum);
  607. /* The special two_node mode allows each of the two nodes to retain
  608. * quorum if the other fails. Only one of the two should live past
  609. * fencing (as both nodes try to fence each other in split-brain.)
  610. * Also: if there are more than two nodes, force us inquorate to avoid
  611. * any damage or confusion.
  612. */
  613. if ((quorum_flags & VOTEQUORUM_FLAG_FEATURE_TWONODE) && total_nodes <= 2)
  614. newquorum = 1;
  615. if (ret_total_votes)
  616. *ret_total_votes = total_votes;
  617. LEAVE();
  618. return newquorum;
  619. }
  620. /* Recalculate cluster quorum, set quorate and notify changes */
  621. static void recalculate_quorum(int allow_decrease, int by_current_nodes)
  622. {
  623. unsigned int total_votes = 0;
  624. int cluster_members = 0;
  625. struct list_head *nodelist;
  626. struct cluster_node *node;
  627. ENTER();
  628. list_iterate(nodelist, &cluster_members_list) {
  629. node = list_entry(nodelist, struct cluster_node, list);
  630. if (node->state == NODESTATE_MEMBER) {
  631. if (by_current_nodes)
  632. cluster_members++;
  633. total_votes += node->votes;
  634. }
  635. }
  636. /* Keep expected_votes at the highest number of votes in the cluster */
  637. log_printf(LOGSYS_LEVEL_DEBUG, "total_votes=%d, expected_votes=%d\n", total_votes, us->expected_votes);
  638. if (total_votes > us->expected_votes) {
  639. us->expected_votes = total_votes;
  640. send_expectedvotes_notification();
  641. }
  642. quorum = calculate_quorum(allow_decrease, cluster_members, &total_votes);
  643. set_quorate(total_votes);
  644. send_quorum_notification(NULL, 0L);
  645. LEAVE();
  646. }
  647. static void node_add_ordered(struct cluster_node *newnode)
  648. {
  649. struct cluster_node *node = NULL;
  650. struct list_head *tmp;
  651. struct list_head *newlist = &newnode->list;
  652. list_iterate(tmp, &cluster_members_list) {
  653. node = list_entry(tmp, struct cluster_node, list);
  654. if (newnode->node_id < node->node_id)
  655. break;
  656. }
  657. if (!node)
  658. list_add(&newnode->list, &cluster_members_list);
  659. else {
  660. newlist->prev = tmp->prev;
  661. newlist->next = tmp;
  662. tmp->prev->next = newlist;
  663. tmp->prev = newlist;
  664. }
  665. }
  666. static struct cluster_node *allocate_node(int nodeid)
  667. {
  668. struct cluster_node *cl;
  669. cl = malloc(sizeof(struct cluster_node));
  670. if (cl) {
  671. memset(cl, 0, sizeof(struct cluster_node));
  672. cl->node_id = nodeid;
  673. if (nodeid)
  674. node_add_ordered(cl);
  675. }
  676. return cl;
  677. }
  678. static struct cluster_node *find_node_by_nodeid(int nodeid)
  679. {
  680. struct cluster_node *node;
  681. struct list_head *tmp;
  682. if (nodeid == NODEID_US)
  683. return us;
  684. if (nodeid == NODEID_QDEVICE)
  685. return quorum_device;
  686. list_iterate(tmp, &cluster_members_list) {
  687. node = list_entry(tmp, struct cluster_node, list);
  688. if (node->node_id == nodeid)
  689. return node;
  690. }
  691. return NULL;
  692. }
  693. static int quorum_exec_send_nodeinfo()
  694. {
  695. struct req_exec_quorum_nodeinfo req_exec_quorum_nodeinfo;
  696. struct iovec iov[1];
  697. int ret;
  698. ENTER();
  699. req_exec_quorum_nodeinfo.expected_votes = us->expected_votes;
  700. req_exec_quorum_nodeinfo.votes = us->votes;
  701. req_exec_quorum_nodeinfo.major_version = VOTEQUORUM_MAJOR_VERSION;
  702. req_exec_quorum_nodeinfo.minor_version = VOTEQUORUM_MINOR_VERSION;
  703. req_exec_quorum_nodeinfo.patch_version = VOTEQUORUM_PATCH_VERSION;
  704. req_exec_quorum_nodeinfo.flags = us->flags;
  705. req_exec_quorum_nodeinfo.first_trans = first_trans;
  706. req_exec_quorum_nodeinfo.wait_for_all = wait_for_all;
  707. req_exec_quorum_nodeinfo.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO);
  708. req_exec_quorum_nodeinfo.header.size = sizeof(req_exec_quorum_nodeinfo);
  709. iov[0].iov_base = (void *)&req_exec_quorum_nodeinfo;
  710. iov[0].iov_len = sizeof(req_exec_quorum_nodeinfo);
  711. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  712. LEAVE();
  713. return ret;
  714. }
  715. static int quorum_exec_send_reconfigure(int param, int nodeid, int value)
  716. {
  717. struct req_exec_quorum_reconfigure req_exec_quorum_reconfigure;
  718. struct iovec iov[1];
  719. int ret;
  720. ENTER();
  721. req_exec_quorum_reconfigure.param = param;
  722. req_exec_quorum_reconfigure.nodeid = nodeid;
  723. req_exec_quorum_reconfigure.value = value;
  724. req_exec_quorum_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE);
  725. req_exec_quorum_reconfigure.header.size = sizeof(req_exec_quorum_reconfigure);
  726. iov[0].iov_base = (void *)&req_exec_quorum_reconfigure;
  727. iov[0].iov_len = sizeof(req_exec_quorum_reconfigure);
  728. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  729. LEAVE();
  730. return ret;
  731. }
  732. static void lms_timer_fn(void *arg)
  733. {
  734. ENTER();
  735. last_man_standing_timer_set = 0;
  736. if (cluster_is_quorate) {
  737. recalculate_quorum(1,1);
  738. }
  739. LEAVE();
  740. }
  741. static void quorum_confchg_fn (
  742. enum totem_configuration_type configuration_type,
  743. const unsigned int *member_list, size_t member_list_entries,
  744. const unsigned int *left_list, size_t left_list_entries,
  745. const unsigned int *joined_list, size_t joined_list_entries,
  746. const struct memb_ring_id *ring_id)
  747. {
  748. int i;
  749. int leaving = 0;
  750. struct cluster_node *node;
  751. ENTER();
  752. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  753. return;
  754. }
  755. if (member_list_entries > 1)
  756. first_trans = 0;
  757. if (left_list_entries) {
  758. for (i = 0; i< left_list_entries; i++) {
  759. node = find_node_by_nodeid(left_list[i]);
  760. if (node) {
  761. if (node->state == NODESTATE_LEAVING)
  762. leaving = 1;
  763. node->state = NODESTATE_DEAD;
  764. node->flags |= NODE_FLAGS_BEENDOWN;
  765. }
  766. }
  767. }
  768. if (last_man_standing) {
  769. if ((member_list_entries >= quorum) && (left_list_entries)) {
  770. if (last_man_standing_timer_set) {
  771. corosync_api->timer_delete(last_man_standing_timer);
  772. last_man_standing_timer_set = 0;
  773. }
  774. corosync_api->timer_add_duration((unsigned long long)last_man_standing_window*1000000, NULL, lms_timer_fn, &last_man_standing_timer);
  775. last_man_standing_timer_set = 1;
  776. }
  777. }
  778. if (member_list_entries) {
  779. memcpy(quorum_members, member_list, sizeof(unsigned int) * member_list_entries);
  780. quorum_members_entries = member_list_entries;
  781. if (quorum_device) {
  782. quorum_members[quorum_members_entries++] = 0;
  783. }
  784. quorum_exec_send_nodeinfo();
  785. }
  786. if (left_list_entries)
  787. recalculate_quorum(leaving, leaving);
  788. memcpy(&quorum_ringid, ring_id, sizeof(*ring_id));
  789. set_quorum(quorum_members, quorum_members_entries, cluster_is_quorate, &quorum_ringid);
  790. LEAVE();
  791. }
  792. static void exec_votequorum_nodeinfo_endian_convert (void *msg)
  793. {
  794. struct req_exec_quorum_nodeinfo *nodeinfo = msg;
  795. nodeinfo->votes = swab32(nodeinfo->votes);
  796. nodeinfo->expected_votes = swab32(nodeinfo->expected_votes);
  797. nodeinfo->major_version = swab32(nodeinfo->major_version);
  798. nodeinfo->minor_version = swab32(nodeinfo->minor_version);
  799. nodeinfo->patch_version = swab32(nodeinfo->patch_version);
  800. nodeinfo->config_version = swab32(nodeinfo->config_version);
  801. nodeinfo->flags = swab32(nodeinfo->flags);
  802. nodeinfo->wait_for_all = swab32(nodeinfo->wait_for_all);
  803. }
  804. static void exec_votequorum_reconfigure_endian_convert (void *msg)
  805. {
  806. struct req_exec_quorum_reconfigure *reconfigure = msg;
  807. reconfigure->nodeid = swab32(reconfigure->nodeid);
  808. reconfigure->value = swab32(reconfigure->value);
  809. }
  810. static void message_handler_req_exec_votequorum_nodeinfo (
  811. const void *message,
  812. unsigned int nodeid)
  813. {
  814. const struct req_exec_quorum_nodeinfo *req_exec_quorum_nodeinfo = message;
  815. struct cluster_node *node;
  816. int old_votes;
  817. int old_expected;
  818. nodestate_t old_state;
  819. int new_node = 0;
  820. ENTER();
  821. log_printf(LOGSYS_LEVEL_DEBUG, "got nodeinfo message from cluster node %d\n", nodeid);
  822. node = find_node_by_nodeid(nodeid);
  823. if (!node) {
  824. node = allocate_node(nodeid);
  825. new_node = 1;
  826. }
  827. if (!node) {
  828. corosync_api->error_memory_failure();
  829. return;
  830. }
  831. old_votes = node->votes;
  832. old_expected = node->expected_votes;
  833. old_state = node->state;
  834. /* Update node state */
  835. node->votes = req_exec_quorum_nodeinfo->votes;
  836. node->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  837. node->state = NODESTATE_MEMBER;
  838. log_printf(LOGSYS_LEVEL_DEBUG, "nodeinfo message: votes: %d, expected: %d wfa: %d\n", req_exec_quorum_nodeinfo->votes, req_exec_quorum_nodeinfo->expected_votes, req_exec_quorum_nodeinfo->wait_for_all);
  839. if ((last_man_standing) && (req_exec_quorum_nodeinfo->votes > 1)) {
  840. log_printf(LOGSYS_LEVEL_WARNING, "Last Man Standing feature is supported only when all"
  841. "cluster nodes votes are set to 1. Disabling LMS.");
  842. last_man_standing = 0;
  843. if (last_man_standing_timer_set) {
  844. corosync_api->timer_delete(last_man_standing_timer);
  845. last_man_standing_timer_set = 0;
  846. }
  847. }
  848. node->flags &= ~NODE_FLAGS_BEENDOWN;
  849. if (new_node || req_exec_quorum_nodeinfo->first_trans ||
  850. old_votes != node->votes || old_expected != node->expected_votes || old_state != node->state)
  851. recalculate_quorum(0, 0);
  852. if (!nodeid) {
  853. free(node);
  854. }
  855. if ((wait_for_all) && (!req_exec_quorum_nodeinfo->wait_for_all)) {
  856. wait_for_all = 0;
  857. }
  858. LEAVE();
  859. }
  860. static void message_handler_req_exec_votequorum_reconfigure (
  861. const void *message,
  862. unsigned int nodeid)
  863. {
  864. const struct req_exec_quorum_reconfigure *req_exec_quorum_reconfigure = message;
  865. struct cluster_node *node;
  866. struct list_head *nodelist;
  867. log_printf(LOGSYS_LEVEL_DEBUG, "got reconfigure message from cluster node %d\n", nodeid);
  868. node = find_node_by_nodeid(req_exec_quorum_reconfigure->nodeid);
  869. if (!node)
  870. return;
  871. switch(req_exec_quorum_reconfigure->param)
  872. {
  873. case RECONFIG_PARAM_EXPECTED_VOTES:
  874. list_iterate(nodelist, &cluster_members_list) {
  875. node = list_entry(nodelist, struct cluster_node, list);
  876. if (node->state == NODESTATE_MEMBER &&
  877. node->expected_votes > req_exec_quorum_reconfigure->value) {
  878. node->expected_votes = req_exec_quorum_reconfigure->value;
  879. }
  880. }
  881. send_expectedvotes_notification();
  882. recalculate_quorum(1, 0); /* Allow decrease */
  883. break;
  884. case RECONFIG_PARAM_NODE_VOTES:
  885. node->votes = req_exec_quorum_reconfigure->value;
  886. recalculate_quorum(1, 0); /* Allow decrease */
  887. break;
  888. case RECONFIG_PARAM_LEAVING:
  889. if (req_exec_quorum_reconfigure->value == 1 && node->state == NODESTATE_MEMBER)
  890. node->state = NODESTATE_LEAVING;
  891. if (req_exec_quorum_reconfigure->value == 0 && node->state == NODESTATE_LEAVING)
  892. node->state = NODESTATE_MEMBER;
  893. break;
  894. }
  895. }
  896. static int quorum_lib_init_fn (void *conn)
  897. {
  898. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  899. ENTER();
  900. list_init (&pd->list);
  901. pd->conn = conn;
  902. LEAVE();
  903. return (0);
  904. }
  905. /*
  906. * Someone called votequorum_leave AGES ago!
  907. * Assume they forgot to shut down the node.
  908. */
  909. static void leaving_timer_fn(void *arg)
  910. {
  911. ENTER();
  912. if (us->state == NODESTATE_LEAVING)
  913. us->state = NODESTATE_MEMBER;
  914. /* Tell everyone else we made a mistake */
  915. quorum_exec_send_reconfigure(RECONFIG_PARAM_LEAVING, us->node_id, 0);
  916. LEAVE();
  917. }
  918. /* Message from the library */
  919. static void message_handler_req_lib_votequorum_getinfo (void *conn, const void *message)
  920. {
  921. const struct req_lib_votequorum_getinfo *req_lib_votequorum_getinfo = message;
  922. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  923. struct cluster_node *node;
  924. unsigned int highest_expected = 0;
  925. unsigned int total_votes = 0;
  926. cs_error_t error = CS_OK;
  927. log_printf(LOGSYS_LEVEL_DEBUG, "got getinfo request on %p for node %d\n", conn, req_lib_votequorum_getinfo->nodeid);
  928. node = find_node_by_nodeid(req_lib_votequorum_getinfo->nodeid);
  929. if (node) {
  930. struct cluster_node *iternode;
  931. struct list_head *nodelist;
  932. list_iterate(nodelist, &cluster_members_list) {
  933. iternode = list_entry(nodelist, struct cluster_node, list);
  934. if (iternode->state == NODESTATE_MEMBER) {
  935. highest_expected =
  936. max(highest_expected, iternode->expected_votes);
  937. total_votes += iternode->votes;
  938. }
  939. }
  940. if (quorum_device && quorum_device->state == NODESTATE_MEMBER) {
  941. total_votes += quorum_device->votes;
  942. }
  943. res_lib_votequorum_getinfo.votes = us->votes;
  944. res_lib_votequorum_getinfo.expected_votes = us->expected_votes;
  945. res_lib_votequorum_getinfo.highest_expected = highest_expected;
  946. res_lib_votequorum_getinfo.quorum = quorum;
  947. res_lib_votequorum_getinfo.total_votes = total_votes;
  948. res_lib_votequorum_getinfo.flags = 0;
  949. res_lib_votequorum_getinfo.nodeid = node->node_id;
  950. if (us->flags & NODE_FLAGS_HASSTATE)
  951. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_HASSTATE;
  952. if (quorum_flags & VOTEQUORUM_FLAG_FEATURE_TWONODE)
  953. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_TWONODE;
  954. if (cluster_is_quorate)
  955. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_QUORATE;
  956. }
  957. else {
  958. error = CS_ERR_NOT_EXIST;
  959. }
  960. res_lib_votequorum_getinfo.header.size = sizeof(res_lib_votequorum_getinfo);
  961. res_lib_votequorum_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  962. res_lib_votequorum_getinfo.header.error = error;
  963. corosync_api->ipc_response_send(conn, &res_lib_votequorum_getinfo, sizeof(res_lib_votequorum_getinfo));
  964. log_printf(LOGSYS_LEVEL_DEBUG, "getinfo response error: %d\n", error);
  965. }
  966. /* Message from the library */
  967. static void message_handler_req_lib_votequorum_setexpected (void *conn, const void *message)
  968. {
  969. const struct req_lib_votequorum_setexpected *req_lib_votequorum_setexpected = message;
  970. struct res_lib_votequorum_status res_lib_votequorum_status;
  971. cs_error_t error = CS_OK;
  972. unsigned int newquorum;
  973. unsigned int total_votes;
  974. ENTER();
  975. /* Validate new expected votes */
  976. newquorum = calculate_quorum(1, req_lib_votequorum_setexpected->expected_votes, &total_votes);
  977. if (newquorum < total_votes / 2
  978. || newquorum > total_votes) {
  979. error = CS_ERR_INVALID_PARAM;
  980. goto error_exit;
  981. }
  982. quorum_exec_send_reconfigure(RECONFIG_PARAM_EXPECTED_VOTES, us->node_id, req_lib_votequorum_setexpected->expected_votes);
  983. /* send status */
  984. error_exit:
  985. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  986. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  987. res_lib_votequorum_status.header.error = error;
  988. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  989. LEAVE();
  990. }
  991. /* Message from the library */
  992. static void message_handler_req_lib_votequorum_setvotes (void *conn, const void *message)
  993. {
  994. const struct req_lib_votequorum_setvotes *req_lib_votequorum_setvotes = message;
  995. struct res_lib_votequorum_status res_lib_votequorum_status;
  996. struct cluster_node *node;
  997. unsigned int newquorum;
  998. unsigned int total_votes;
  999. unsigned int saved_votes;
  1000. cs_error_t error = CS_OK;
  1001. unsigned int nodeid;
  1002. ENTER();
  1003. nodeid = req_lib_votequorum_setvotes->nodeid;
  1004. node = find_node_by_nodeid(nodeid);
  1005. if (!node) {
  1006. error = CS_ERR_NAME_NOT_FOUND;
  1007. goto error_exit;
  1008. }
  1009. /* Check votes is valid */
  1010. saved_votes = node->votes;
  1011. node->votes = req_lib_votequorum_setvotes->votes;
  1012. newquorum = calculate_quorum(1, 0, &total_votes);
  1013. if (newquorum < total_votes / 2 || newquorum > total_votes) {
  1014. node->votes = saved_votes;
  1015. error = CS_ERR_INVALID_PARAM;
  1016. goto error_exit;
  1017. }
  1018. if (!nodeid)
  1019. nodeid = corosync_api->totem_nodeid_get();
  1020. quorum_exec_send_reconfigure(RECONFIG_PARAM_NODE_VOTES, nodeid,
  1021. req_lib_votequorum_setvotes->votes);
  1022. error_exit:
  1023. /* send status */
  1024. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1025. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1026. res_lib_votequorum_status.header.error = error;
  1027. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1028. LEAVE();
  1029. }
  1030. static void message_handler_req_lib_votequorum_leaving (void *conn, const void *message)
  1031. {
  1032. struct res_lib_votequorum_status res_lib_votequorum_status;
  1033. cs_error_t error = CS_OK;
  1034. ENTER();
  1035. quorum_exec_send_reconfigure(RECONFIG_PARAM_LEAVING, us->node_id, 1);
  1036. /*
  1037. * If we don't shut down in a sensible amount of time then cancel the
  1038. * leave status.
  1039. */
  1040. if (leaving_timeout)
  1041. corosync_api->timer_add_duration((unsigned long long)leaving_timeout*1000000, NULL,
  1042. leaving_timer_fn, &leaving_timer);
  1043. /* send status */
  1044. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1045. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1046. res_lib_votequorum_status.header.error = error;
  1047. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1048. LEAVE();
  1049. }
  1050. static void quorum_device_timer_fn(void *arg)
  1051. {
  1052. ENTER();
  1053. if (!quorum_device || quorum_device->state == NODESTATE_DEAD)
  1054. return;
  1055. if ( (quorum_device->last_hello / QB_TIME_NS_IN_SEC) + quorumdev_poll/1000 <
  1056. (qb_util_nano_current_get () / QB_TIME_NS_IN_SEC)) {
  1057. quorum_device->state = NODESTATE_DEAD;
  1058. log_printf(LOGSYS_LEVEL_INFO, "lost contact with quorum device\n");
  1059. recalculate_quorum(0, 0);
  1060. }
  1061. else {
  1062. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1063. quorum_device_timer_fn, &quorum_device_timer);
  1064. }
  1065. LEAVE();
  1066. }
  1067. static void message_handler_req_lib_votequorum_qdisk_register (void *conn,
  1068. const void *message)
  1069. {
  1070. const struct req_lib_votequorum_qdisk_register
  1071. *req_lib_votequorum_qdisk_register = message;
  1072. struct res_lib_votequorum_status res_lib_votequorum_status;
  1073. cs_error_t error = CS_OK;
  1074. ENTER();
  1075. if (quorum_device) {
  1076. error = CS_ERR_EXIST;
  1077. }
  1078. else {
  1079. quorum_device = allocate_node(0);
  1080. quorum_device->state = NODESTATE_DEAD;
  1081. quorum_device->votes = req_lib_votequorum_qdisk_register->votes;
  1082. strcpy(quorum_device_name, req_lib_votequorum_qdisk_register->name);
  1083. list_add(&quorum_device->list, &cluster_members_list);
  1084. }
  1085. /* send status */
  1086. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1087. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1088. res_lib_votequorum_status.header.error = error;
  1089. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1090. LEAVE();
  1091. }
  1092. static void message_handler_req_lib_votequorum_qdisk_unregister (void *conn,
  1093. const void *message)
  1094. {
  1095. struct res_lib_votequorum_status res_lib_votequorum_status;
  1096. cs_error_t error = CS_OK;
  1097. ENTER();
  1098. if (quorum_device) {
  1099. struct cluster_node *node = quorum_device;
  1100. quorum_device = NULL;
  1101. list_del(&node->list);
  1102. free(node);
  1103. recalculate_quorum(0, 0);
  1104. }
  1105. else {
  1106. error = CS_ERR_NOT_EXIST;
  1107. }
  1108. /* send status */
  1109. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1110. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1111. res_lib_votequorum_status.header.error = error;
  1112. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1113. LEAVE();
  1114. }
  1115. static void message_handler_req_lib_votequorum_qdisk_poll (void *conn,
  1116. const void *message)
  1117. {
  1118. const struct req_lib_votequorum_qdisk_poll
  1119. *req_lib_votequorum_qdisk_poll = message;
  1120. struct res_lib_votequorum_status res_lib_votequorum_status;
  1121. cs_error_t error = CS_OK;
  1122. ENTER();
  1123. if (quorum_device) {
  1124. if (req_lib_votequorum_qdisk_poll->state) {
  1125. quorum_device->last_hello = qb_util_nano_current_get ();
  1126. if (quorum_device->state == NODESTATE_DEAD) {
  1127. quorum_device->state = NODESTATE_MEMBER;
  1128. recalculate_quorum(0, 0);
  1129. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1130. quorum_device_timer_fn, &quorum_device_timer);
  1131. }
  1132. }
  1133. else {
  1134. if (quorum_device->state == NODESTATE_MEMBER) {
  1135. quorum_device->state = NODESTATE_DEAD;
  1136. recalculate_quorum(0, 0);
  1137. corosync_api->timer_delete(quorum_device_timer);
  1138. }
  1139. }
  1140. }
  1141. else {
  1142. error = CS_ERR_NOT_EXIST;
  1143. }
  1144. /* send status */
  1145. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1146. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1147. res_lib_votequorum_status.header.error = error;
  1148. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1149. LEAVE();
  1150. }
  1151. static void message_handler_req_lib_votequorum_qdisk_getinfo (void *conn,
  1152. const void *message)
  1153. {
  1154. struct res_lib_votequorum_qdisk_getinfo res_lib_votequorum_qdisk_getinfo;
  1155. cs_error_t error = CS_OK;
  1156. ENTER();
  1157. if (quorum_device) {
  1158. log_printf(LOGSYS_LEVEL_DEBUG, "got qdisk_getinfo state %d\n", quorum_device->state);
  1159. res_lib_votequorum_qdisk_getinfo.votes = quorum_device->votes;
  1160. if (quorum_device->state == NODESTATE_MEMBER)
  1161. res_lib_votequorum_qdisk_getinfo.state = 1;
  1162. else
  1163. res_lib_votequorum_qdisk_getinfo.state = 0;
  1164. strcpy(res_lib_votequorum_qdisk_getinfo.name, quorum_device_name);
  1165. }
  1166. else {
  1167. error = CS_ERR_NOT_EXIST;
  1168. }
  1169. /* send status */
  1170. res_lib_votequorum_qdisk_getinfo.header.size = sizeof(res_lib_votequorum_qdisk_getinfo);
  1171. res_lib_votequorum_qdisk_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1172. res_lib_votequorum_qdisk_getinfo.header.error = error;
  1173. corosync_api->ipc_response_send(conn, &res_lib_votequorum_qdisk_getinfo, sizeof(res_lib_votequorum_qdisk_getinfo));
  1174. LEAVE();
  1175. }
  1176. static void message_handler_req_lib_votequorum_setstate (void *conn,
  1177. const void *message)
  1178. {
  1179. struct res_lib_votequorum_status res_lib_votequorum_status;
  1180. cs_error_t error = CS_OK;
  1181. ENTER();
  1182. us->flags |= NODE_FLAGS_HASSTATE;
  1183. /* send status */
  1184. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1185. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1186. res_lib_votequorum_status.header.error = error;
  1187. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1188. LEAVE();
  1189. }
  1190. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  1191. const void *msg)
  1192. {
  1193. const struct req_lib_votequorum_trackstart
  1194. *req_lib_votequorum_trackstart = msg;
  1195. struct res_lib_votequorum_status res_lib_votequorum_status;
  1196. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1197. ENTER();
  1198. /*
  1199. * If an immediate listing of the current cluster membership
  1200. * is requested, generate membership list
  1201. */
  1202. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  1203. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  1204. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p\n", conn);
  1205. send_quorum_notification(conn, req_lib_votequorum_trackstart->context);
  1206. }
  1207. /*
  1208. * Record requests for tracking
  1209. */
  1210. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  1211. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  1212. quorum_pd->track_flags = req_lib_votequorum_trackstart->track_flags;
  1213. quorum_pd->tracking_enabled = 1;
  1214. quorum_pd->tracking_context = req_lib_votequorum_trackstart->context;
  1215. list_add (&quorum_pd->list, &trackers_list);
  1216. }
  1217. /* Send status */
  1218. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1219. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1220. res_lib_votequorum_status.header.error = CS_OK;
  1221. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1222. LEAVE();
  1223. }
  1224. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  1225. const void *msg)
  1226. {
  1227. struct res_lib_votequorum_status res_lib_votequorum_status;
  1228. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1229. int error = CS_OK;
  1230. ENTER();
  1231. if (quorum_pd->tracking_enabled) {
  1232. error = CS_OK;
  1233. quorum_pd->tracking_enabled = 0;
  1234. list_del (&quorum_pd->list);
  1235. list_init (&quorum_pd->list);
  1236. } else {
  1237. error = CS_ERR_NOT_EXIST;
  1238. }
  1239. /* send status */
  1240. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1241. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1242. res_lib_votequorum_status.header.error = error;
  1243. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1244. LEAVE();
  1245. }
  1246. static void reread_config(void)
  1247. {
  1248. unsigned int old_votes;
  1249. unsigned int old_expected;
  1250. old_votes = us->votes;
  1251. old_expected = us->expected_votes;
  1252. /*
  1253. * Reload the configuration
  1254. */
  1255. read_quorum_config();
  1256. /*
  1257. * Check for fundamental changes that we need to propogate
  1258. */
  1259. if (old_votes != us->votes) {
  1260. quorum_exec_send_reconfigure(RECONFIG_PARAM_NODE_VOTES, us->node_id, us->votes);
  1261. }
  1262. if (old_expected != us->expected_votes) {
  1263. quorum_exec_send_reconfigure(RECONFIG_PARAM_EXPECTED_VOTES, us->node_id, us->expected_votes);
  1264. }
  1265. }
  1266. static void key_change_quorum(
  1267. int32_t event,
  1268. const char *key_name,
  1269. struct icmap_notify_value new_val,
  1270. struct icmap_notify_value old_val,
  1271. void *user_data)
  1272. {
  1273. reread_config();
  1274. }
  1275. static void add_votequorum_config_notification(void)
  1276. {
  1277. icmap_track_t icmap_track;
  1278. icmap_track_add("quorum.",
  1279. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  1280. key_change_quorum,
  1281. NULL,
  1282. &icmap_track);
  1283. }