votequorum.c 43 KB

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