votequorum.c 44 KB

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