votequorum.c 43 KB

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