votequorum.c 44 KB

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