votequorum.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  58. #define DEFAULT_QDEV_POLL 10000
  59. static unsigned int quorumdev_poll = DEFAULT_QDEV_POLL;
  60. static char quorum_device_name[VOTEQUORUM_MAX_QDEVICE_NAME_LEN];
  61. #endif
  62. static uint8_t two_node = 0;
  63. static uint8_t wait_for_all = 0;
  64. static uint8_t wait_for_all_status = 0;
  65. static uint8_t auto_tie_breaker = 0;
  66. static int lowest_node_id = -1;
  67. #define DEFAULT_LMS_WIN 10000
  68. static uint8_t last_man_standing = 0;
  69. static uint32_t last_man_standing_window = DEFAULT_LMS_WIN;
  70. static uint8_t leave_remove = 0;
  71. static uint32_t ev_barrier = 0;
  72. /*
  73. * votequorum_exec defines/structs/forward definitions
  74. */
  75. struct req_exec_quorum_nodeinfo {
  76. struct qb_ipc_request_header header __attribute__((aligned(8)));
  77. uint32_t votes;
  78. uint32_t expected_votes;
  79. uint16_t flags;
  80. uint8_t quorate;
  81. uint8_t wait_for_all_status;
  82. uint8_t first_trans;
  83. uint8_t _pad0;
  84. uint8_t _pad1;
  85. uint8_t _pad2;
  86. } __attribute__((packed));
  87. struct req_exec_quorum_reconfigure {
  88. struct qb_ipc_request_header header __attribute__((aligned(8)));
  89. uint32_t nodeid;
  90. uint32_t value;
  91. uint8_t param;
  92. uint8_t _pad0;
  93. uint8_t _pad1;
  94. uint8_t _pad2;
  95. } __attribute__((packed));
  96. /*
  97. * votequorum_exec onwire version (via totem)
  98. */
  99. #include "votequorum.h"
  100. /*
  101. * votequorum_exec onwire messages (via totem)
  102. */
  103. #define MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO 0
  104. #define MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE 1
  105. static void votequorum_exec_send_expectedvotes_notification(void);
  106. static int votequorum_exec_send_quorum_notification(void *conn, uint64_t context);
  107. #define VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES 1
  108. #define VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES 2
  109. static int votequorum_exec_send_reconfigure(uint8_t param, unsigned int nodeid, uint32_t value);
  110. /*
  111. * votequorum internal node status/view
  112. */
  113. #define NODE_FLAGS_LEAVING 1
  114. #define NODEID_US 0
  115. #define NODEID_QDEVICE UINT32_MAX
  116. typedef enum {
  117. NODESTATE_MEMBER=1,
  118. NODESTATE_DEAD,
  119. NODESTATE_LEAVING
  120. } nodestate_t;
  121. struct cluster_node {
  122. int node_id;
  123. nodestate_t state;
  124. uint32_t votes;
  125. uint32_t expected_votes;
  126. uint16_t flags;
  127. unsigned long long int last_hello; /* Only used for quorum devices */
  128. struct list_head list;
  129. };
  130. /*
  131. * votequorum internal quorum status
  132. */
  133. static uint8_t quorum;
  134. static uint8_t cluster_is_quorate;
  135. static uint8_t first_trans = 1;
  136. /*
  137. * votequorum membership data
  138. */
  139. static struct cluster_node *us;
  140. static struct cluster_node *quorum_device = NULL;
  141. static struct list_head cluster_members_list;
  142. static unsigned int quorum_members[PROCESSOR_COUNT_MAX+1];
  143. static int quorum_members_entries = 0;
  144. static struct memb_ring_id quorum_ringid;
  145. /*
  146. * votequorum tracking
  147. */
  148. struct quorum_pd {
  149. unsigned char track_flags;
  150. int tracking_enabled;
  151. uint64_t tracking_context;
  152. struct list_head list;
  153. void *conn;
  154. };
  155. static struct list_head trackers_list;
  156. /*
  157. * votequorum timers
  158. */
  159. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  160. static corosync_timer_handle_t quorum_device_timer;
  161. #endif
  162. static corosync_timer_handle_t last_man_standing_timer;
  163. static int last_man_standing_timer_set = 0;
  164. /*
  165. * Service Interfaces required by service_message_handler struct
  166. */
  167. static void votequorum_confchg_fn (
  168. enum totem_configuration_type configuration_type,
  169. const unsigned int *member_list, size_t member_list_entries,
  170. const unsigned int *left_list, size_t left_list_entries,
  171. const unsigned int *joined_list, size_t joined_list_entries,
  172. const struct memb_ring_id *ring_id);
  173. static quorum_set_quorate_fn_t quorum_callback;
  174. /*
  175. * votequorum_exec handler and definitions
  176. */
  177. static char *votequorum_exec_init_fn (struct corosync_api_v1 *api);
  178. static int votequorum_exec_exit_fn (void);
  179. static void message_handler_req_exec_votequorum_nodeinfo (
  180. const void *message,
  181. unsigned int nodeid);
  182. static void exec_votequorum_nodeinfo_endian_convert (void *message);
  183. static void message_handler_req_exec_votequorum_reconfigure (
  184. const void *message,
  185. unsigned int nodeid);
  186. static void exec_votequorum_reconfigure_endian_convert (void *message);
  187. static struct corosync_exec_handler votequorum_exec_engine[] =
  188. {
  189. { /* 0 */
  190. .exec_handler_fn = message_handler_req_exec_votequorum_nodeinfo,
  191. .exec_endian_convert_fn = exec_votequorum_nodeinfo_endian_convert
  192. },
  193. { /* 1 */
  194. .exec_handler_fn = message_handler_req_exec_votequorum_reconfigure,
  195. .exec_endian_convert_fn = exec_votequorum_reconfigure_endian_convert
  196. },
  197. };
  198. /*
  199. * Library Handler and Functions Definitions
  200. */
  201. static int quorum_lib_init_fn (void *conn);
  202. static int quorum_lib_exit_fn (void *conn);
  203. static void message_handler_req_lib_votequorum_getinfo (void *conn,
  204. const void *message);
  205. static void message_handler_req_lib_votequorum_setexpected (void *conn,
  206. const void *message);
  207. static void message_handler_req_lib_votequorum_setvotes (void *conn,
  208. const void *message);
  209. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  210. const void *message);
  211. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  212. const void *message);
  213. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  214. static void message_handler_req_lib_votequorum_qdevice_register (void *conn,
  215. const void *message);
  216. static void message_handler_req_lib_votequorum_qdevice_unregister (void *conn,
  217. const void *message);
  218. static void message_handler_req_lib_votequorum_qdevice_poll (void *conn,
  219. const void *message);
  220. static void message_handler_req_lib_votequorum_qdevice_getinfo (void *conn,
  221. const void *message);
  222. #endif
  223. static struct corosync_lib_handler quorum_lib_service[] =
  224. {
  225. { /* 0 */
  226. .lib_handler_fn = message_handler_req_lib_votequorum_getinfo,
  227. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  228. },
  229. { /* 1 */
  230. .lib_handler_fn = message_handler_req_lib_votequorum_setexpected,
  231. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  232. },
  233. { /* 2 */
  234. .lib_handler_fn = message_handler_req_lib_votequorum_setvotes,
  235. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  236. },
  237. { /* 3 */
  238. .lib_handler_fn = message_handler_req_lib_votequorum_trackstart,
  239. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  240. },
  241. { /* 4 */
  242. .lib_handler_fn = message_handler_req_lib_votequorum_trackstop,
  243. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  244. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  245. },
  246. { /* 5 */
  247. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_register,
  248. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  249. },
  250. { /* 6 */
  251. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_unregister,
  252. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  253. },
  254. { /* 7 */
  255. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_poll,
  256. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  257. },
  258. { /* 8 */
  259. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_getinfo,
  260. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  261. #endif
  262. }
  263. };
  264. static struct corosync_service_engine votequorum_service_engine = {
  265. .name = "corosync vote quorum service v1.0",
  266. .id = VOTEQUORUM_SERVICE,
  267. .priority = 2,
  268. .private_data_size = sizeof (struct quorum_pd),
  269. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  270. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED,
  271. .lib_init_fn = quorum_lib_init_fn,
  272. .lib_exit_fn = quorum_lib_exit_fn,
  273. .lib_engine = quorum_lib_service,
  274. .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler),
  275. .exec_init_fn = votequorum_exec_init_fn,
  276. .exec_exit_fn = votequorum_exec_exit_fn,
  277. .exec_engine = votequorum_exec_engine,
  278. .exec_engine_count = sizeof (votequorum_exec_engine) / sizeof (struct corosync_exec_handler),
  279. .confchg_fn = votequorum_confchg_fn,
  280. .sync_mode = CS_SYNC_V1
  281. };
  282. struct corosync_service_engine *votequorum_get_service_engine_ver0 (void)
  283. {
  284. return (&votequorum_service_engine);
  285. }
  286. static struct default_service votequorum_service[] = {
  287. {
  288. .name = "corosync_votequorum",
  289. .ver = 0,
  290. .loader = votequorum_get_service_engine_ver0
  291. },
  292. };
  293. /*
  294. * common/utility macros/functions
  295. */
  296. #define max(a,b) (((a) > (b)) ? (a) : (b))
  297. #define list_iterate(v, head) \
  298. for (v = (head)->next; v != head; v = v->next)
  299. static void node_add_ordered(struct cluster_node *newnode)
  300. {
  301. struct cluster_node *node = NULL;
  302. struct list_head *tmp;
  303. struct list_head *newlist = &newnode->list;
  304. ENTER();
  305. list_iterate(tmp, &cluster_members_list) {
  306. node = list_entry(tmp, struct cluster_node, list);
  307. if (newnode->node_id < node->node_id) {
  308. break;
  309. }
  310. }
  311. if (!node) {
  312. list_add(&newnode->list, &cluster_members_list);
  313. } else {
  314. newlist->prev = tmp->prev;
  315. newlist->next = tmp;
  316. tmp->prev->next = newlist;
  317. tmp->prev = newlist;
  318. }
  319. LEAVE();
  320. }
  321. static struct cluster_node *allocate_node(unsigned int nodeid)
  322. {
  323. struct cluster_node *cl;
  324. ENTER();
  325. cl = malloc(sizeof(struct cluster_node));
  326. if (cl) {
  327. memset(cl, 0, sizeof(struct cluster_node));
  328. cl->node_id = nodeid;
  329. if (nodeid) {
  330. node_add_ordered(cl);
  331. }
  332. }
  333. LEAVE();
  334. return cl;
  335. }
  336. static struct cluster_node *find_node_by_nodeid(unsigned int nodeid)
  337. {
  338. struct cluster_node *node;
  339. struct list_head *tmp;
  340. ENTER();
  341. if (nodeid == NODEID_US) {
  342. LEAVE();
  343. return us;
  344. }
  345. if (nodeid == NODEID_QDEVICE) {
  346. LEAVE();
  347. return quorum_device;
  348. }
  349. list_iterate(tmp, &cluster_members_list) {
  350. node = list_entry(tmp, struct cluster_node, list);
  351. if (node->node_id == nodeid) {
  352. LEAVE();
  353. return node;
  354. }
  355. }
  356. LEAVE();
  357. return NULL;
  358. }
  359. static void get_lowest_node_id(void)
  360. {
  361. struct cluster_node *node = NULL;
  362. struct list_head *tmp;
  363. ENTER();
  364. lowest_node_id = us->node_id;
  365. list_iterate(tmp, &cluster_members_list) {
  366. node = list_entry(tmp, struct cluster_node, list);
  367. if ((node->state == NODESTATE_MEMBER) &&
  368. (node->node_id < lowest_node_id)) {
  369. lowest_node_id = node->node_id;
  370. }
  371. }
  372. log_printf(LOGSYS_LEVEL_DEBUG, "lowest node id: %d us: %d", lowest_node_id, us->node_id);
  373. LEAVE();
  374. }
  375. static int check_low_node_id_partition(void)
  376. {
  377. struct cluster_node *node = NULL;
  378. struct list_head *tmp;
  379. int found = 0;
  380. ENTER();
  381. list_iterate(tmp, &cluster_members_list) {
  382. node = list_entry(tmp, struct cluster_node, list);
  383. if ((node->state == NODESTATE_MEMBER) &&
  384. (node->node_id == lowest_node_id)) {
  385. found = 1;
  386. }
  387. }
  388. LEAVE();
  389. return found;
  390. }
  391. /*
  392. * quorum calculation core bits
  393. */
  394. static int calculate_quorum(int allow_decrease, unsigned int max_expected, unsigned int *ret_total_votes)
  395. {
  396. struct list_head *nodelist;
  397. struct cluster_node *node;
  398. unsigned int total_votes = 0;
  399. unsigned int highest_expected = 0;
  400. unsigned int newquorum, q1, q2;
  401. unsigned int total_nodes = 0;
  402. ENTER();
  403. if ((leave_remove) && (allow_decrease) && (max_expected)) {
  404. max_expected = max(ev_barrier, max_expected);
  405. }
  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",
  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",
  469. total_votes, us->expected_votes);
  470. cluster_is_quorate = 0;
  471. return;
  472. }
  473. wait_for_all_status = 0;
  474. }
  475. if (quorum > total_votes) {
  476. quorate = 0;
  477. } else {
  478. quorate = 1;
  479. get_lowest_node_id();
  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");
  489. }
  490. if (!cluster_is_quorate && quorate) {
  491. quorum_change = 1;
  492. log_printf(LOGSYS_LEVEL_DEBUG, "quorum regained, resuming activity");
  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", 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. static int votequorum_read_nodelist_configuration(uint32_t *votes,
  542. uint32_t *expected_votes)
  543. {
  544. icmap_iter_t iter;
  545. const char *iter_key;
  546. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  547. uint32_t our_pos, node_pos;
  548. uint32_t nodelist_expected_votes = 0;
  549. uint32_t node_votes = 0;
  550. int res = 0;
  551. ENTER();
  552. if (icmap_get_uint32("nodelist.local_node_pos", &our_pos) != CS_OK) {
  553. log_printf(LOGSYS_LEVEL_DEBUG,
  554. "No nodelist defined or our node is not in the nodelist");
  555. return -1;
  556. }
  557. iter = icmap_iter_init("nodelist.node.");
  558. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  559. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  560. if (res != 2) {
  561. continue;
  562. }
  563. if (strcmp(tmp_key, "ring0_addr") != 0) {
  564. continue;
  565. }
  566. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.quorum_votes", node_pos);
  567. if (icmap_get_uint32(tmp_key, &node_votes) != CS_OK) {
  568. node_votes = 1;
  569. }
  570. nodelist_expected_votes = nodelist_expected_votes + node_votes;
  571. if (node_pos == our_pos) {
  572. *votes = node_votes;
  573. }
  574. }
  575. *expected_votes = nodelist_expected_votes;
  576. icmap_iter_finalize(iter);
  577. LEAVE();
  578. return 0;
  579. }
  580. /*
  581. * votequorum_readconfig_static is executed before
  582. * votequorum_readconfig_dynamic
  583. */
  584. static char *votequorum_readconfig_static(void)
  585. {
  586. uint32_t node_votes, node_expected_votes, expected_votes;
  587. ENTER();
  588. log_printf(LOGSYS_LEVEL_DEBUG, "Reading static configuration");
  589. icmap_get_uint8("quorum.two_node", &two_node);
  590. if (two_node) {
  591. wait_for_all = 1;
  592. }
  593. icmap_get_uint8("quorum.leave_remove", &leave_remove);
  594. icmap_get_uint8("quorum.wait_for_all", &wait_for_all);
  595. icmap_get_uint8("quorum.auto_tie_breaker", &auto_tie_breaker);
  596. icmap_get_uint8("quorum.last_man_standing", &last_man_standing);
  597. icmap_get_uint32("quorum.last_man_standing_window", &last_man_standing_window);
  598. if ((votequorum_read_nodelist_configuration(&node_votes, &node_expected_votes)) &&
  599. (icmap_get_uint32("quorum.expected_votes", &expected_votes) != CS_OK)) {
  600. return ((char *)"configuration error: nodelist or quorum.expected_votes must be configured!");
  601. }
  602. if (wait_for_all) {
  603. wait_for_all_status = 1;
  604. }
  605. LEAVE();
  606. return (NULL);
  607. }
  608. static void votequorum_readconfig_dynamic(void)
  609. {
  610. int cluster_members = 0;
  611. struct list_head *tmp;
  612. ENTER();
  613. log_printf(LOGSYS_LEVEL_DEBUG, "Reading dynamic configuration");
  614. if (votequorum_read_nodelist_configuration(&us->votes, &us->expected_votes)) {
  615. us->votes = 1;
  616. icmap_get_uint32("quorum.votes", &us->votes);
  617. }
  618. icmap_get_uint32("quorum.expected_votes", &us->expected_votes);
  619. ev_barrier = us->expected_votes;
  620. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  621. if (icmap_get_uint32("quorum.quorumdev_poll", &quorumdev_poll) != CS_OK) {
  622. quorumdev_poll = DEFAULT_QDEV_POLL;
  623. }
  624. #endif
  625. icmap_get_uint8("quorum.two_node", &two_node);
  626. /*
  627. * two_node mode is invalid if there are more than 2 nodes in the cluster!
  628. */
  629. list_iterate(tmp, &cluster_members_list) {
  630. cluster_members++;
  631. }
  632. if (two_node && cluster_members > 2) {
  633. log_printf(LOGSYS_LEVEL_WARNING, "quorum.two_node was set but there are more than 2 nodes in the cluster. It will be ignored.");
  634. two_node = 0;
  635. }
  636. LEAVE();
  637. }
  638. static void votequorum_refresh_config(
  639. int32_t event,
  640. const char *key_name,
  641. struct icmap_notify_value new_val,
  642. struct icmap_notify_value old_val,
  643. void *user_data)
  644. {
  645. unsigned int old_votes;
  646. unsigned int old_expected;
  647. ENTER();
  648. old_votes = us->votes;
  649. old_expected = us->expected_votes;
  650. /*
  651. * Reload the configuration
  652. */
  653. votequorum_readconfig_dynamic();
  654. /*
  655. * Check for fundamental changes that we need to propogate
  656. */
  657. if (old_votes != us->votes) {
  658. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, us->node_id, us->votes);
  659. }
  660. if (old_expected != us->expected_votes) {
  661. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES, us->node_id, us->expected_votes);
  662. }
  663. LEAVE();
  664. }
  665. static void votequorum_exec_add_config_notification(void)
  666. {
  667. icmap_track_t icmap_track_nodelist = NULL;
  668. icmap_track_t icmap_track_quorum = NULL;
  669. ENTER();
  670. icmap_track_add("nodelist.",
  671. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  672. votequorum_refresh_config,
  673. NULL,
  674. &icmap_track_nodelist);
  675. icmap_track_add("quorum.",
  676. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  677. votequorum_refresh_config,
  678. NULL,
  679. &icmap_track_quorum);
  680. LEAVE();
  681. }
  682. /*
  683. * votequorum_exec core
  684. */
  685. static int votequorum_exec_send_reconfigure(uint8_t param, unsigned int nodeid, uint32_t value)
  686. {
  687. struct req_exec_quorum_reconfigure req_exec_quorum_reconfigure;
  688. struct iovec iov[1];
  689. int ret;
  690. ENTER();
  691. req_exec_quorum_reconfigure.nodeid = nodeid;
  692. req_exec_quorum_reconfigure.value = value;
  693. req_exec_quorum_reconfigure.param = param;
  694. req_exec_quorum_reconfigure._pad0 = 0;
  695. req_exec_quorum_reconfigure._pad1 = 0;
  696. req_exec_quorum_reconfigure._pad2 = 0;
  697. req_exec_quorum_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE);
  698. req_exec_quorum_reconfigure.header.size = sizeof(req_exec_quorum_reconfigure);
  699. iov[0].iov_base = (void *)&req_exec_quorum_reconfigure;
  700. iov[0].iov_len = sizeof(req_exec_quorum_reconfigure);
  701. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  702. LEAVE();
  703. return ret;
  704. }
  705. static int votequorum_exec_send_nodeinfo(void)
  706. {
  707. struct req_exec_quorum_nodeinfo req_exec_quorum_nodeinfo;
  708. struct iovec iov[1];
  709. int ret;
  710. ENTER();
  711. req_exec_quorum_nodeinfo.votes = us->votes;
  712. req_exec_quorum_nodeinfo.expected_votes = us->expected_votes;
  713. req_exec_quorum_nodeinfo.flags = us->flags;
  714. req_exec_quorum_nodeinfo.quorate = cluster_is_quorate;
  715. req_exec_quorum_nodeinfo.wait_for_all_status = wait_for_all_status;
  716. req_exec_quorum_nodeinfo.first_trans = first_trans;
  717. req_exec_quorum_nodeinfo._pad0 = 0;
  718. req_exec_quorum_nodeinfo._pad1 = 0;
  719. req_exec_quorum_nodeinfo._pad2 = 0;
  720. req_exec_quorum_nodeinfo.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO);
  721. req_exec_quorum_nodeinfo.header.size = sizeof(req_exec_quorum_nodeinfo);
  722. iov[0].iov_base = (void *)&req_exec_quorum_nodeinfo;
  723. iov[0].iov_len = sizeof(req_exec_quorum_nodeinfo);
  724. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  725. LEAVE();
  726. return ret;
  727. }
  728. static int votequorum_exec_send_quorum_notification(void *conn, uint64_t context)
  729. {
  730. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  731. struct list_head *tmp;
  732. struct cluster_node *node;
  733. int cluster_members = 0;
  734. int i = 0;
  735. int size;
  736. char *buf;
  737. ENTER();
  738. list_iterate(tmp, &cluster_members_list) {
  739. node = list_entry(tmp, struct cluster_node, list);
  740. cluster_members++;
  741. }
  742. if (quorum_device) {
  743. cluster_members++;
  744. }
  745. size = sizeof(struct res_lib_votequorum_notification) + sizeof(struct votequorum_node) * cluster_members;
  746. buf = alloca(size);
  747. if (!buf) {
  748. LEAVE();
  749. return -1;
  750. }
  751. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)buf;
  752. res_lib_votequorum_notification->quorate = cluster_is_quorate;
  753. res_lib_votequorum_notification->node_list_entries = cluster_members;
  754. res_lib_votequorum_notification->context = context;
  755. list_iterate(tmp, &cluster_members_list) {
  756. node = list_entry(tmp, struct cluster_node, list);
  757. res_lib_votequorum_notification->node_list[i].nodeid = node->node_id;
  758. res_lib_votequorum_notification->node_list[i++].state = node->state;
  759. }
  760. if (quorum_device) {
  761. res_lib_votequorum_notification->node_list[i].nodeid = 0;
  762. res_lib_votequorum_notification->node_list[i++].state = quorum_device->state | 0x80;
  763. }
  764. res_lib_votequorum_notification->header.id = MESSAGE_RES_VOTEQUORUM_NOTIFICATION;
  765. res_lib_votequorum_notification->header.size = size;
  766. res_lib_votequorum_notification->header.error = CS_OK;
  767. /* Send it to all interested parties */
  768. if (conn) {
  769. int ret = corosync_api->ipc_dispatch_send(conn, buf, size);
  770. LEAVE();
  771. return ret;
  772. } else {
  773. struct quorum_pd *qpd;
  774. list_iterate(tmp, &trackers_list) {
  775. qpd = list_entry(tmp, struct quorum_pd, list);
  776. res_lib_votequorum_notification->context = qpd->tracking_context;
  777. corosync_api->ipc_dispatch_send(qpd->conn, buf, size);
  778. }
  779. }
  780. LEAVE();
  781. return 0;
  782. }
  783. static void votequorum_exec_send_expectedvotes_notification(void)
  784. {
  785. struct res_lib_votequorum_expectedvotes_notification res_lib_votequorum_expectedvotes_notification;
  786. struct quorum_pd *qpd;
  787. struct list_head *tmp;
  788. ENTER();
  789. log_printf(LOGSYS_LEVEL_DEBUG, "Sending expected votes callback");
  790. res_lib_votequorum_expectedvotes_notification.header.id = MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION;
  791. res_lib_votequorum_expectedvotes_notification.header.size = sizeof(res_lib_votequorum_expectedvotes_notification);
  792. res_lib_votequorum_expectedvotes_notification.header.error = CS_OK;
  793. res_lib_votequorum_expectedvotes_notification.expected_votes = us->expected_votes;
  794. list_iterate(tmp, &trackers_list) {
  795. qpd = list_entry(tmp, struct quorum_pd, list);
  796. res_lib_votequorum_expectedvotes_notification.context = qpd->tracking_context;
  797. corosync_api->ipc_dispatch_send(qpd->conn, &res_lib_votequorum_expectedvotes_notification,
  798. sizeof(struct res_lib_votequorum_expectedvotes_notification));
  799. }
  800. LEAVE();
  801. }
  802. static void exec_votequorum_nodeinfo_endian_convert (void *message)
  803. {
  804. struct req_exec_quorum_nodeinfo *nodeinfo = message;
  805. ENTER();
  806. nodeinfo->votes = swab32(nodeinfo->votes);
  807. nodeinfo->expected_votes = swab32(nodeinfo->expected_votes);
  808. nodeinfo->flags = swab16(nodeinfo->flags);
  809. LEAVE();
  810. }
  811. static void message_handler_req_exec_votequorum_nodeinfo (
  812. const void *message,
  813. unsigned int nodeid)
  814. {
  815. const struct req_exec_quorum_nodeinfo *req_exec_quorum_nodeinfo = message;
  816. struct cluster_node *node;
  817. int old_votes;
  818. int old_expected;
  819. uint16_t old_flags;
  820. nodestate_t old_state;
  821. int new_node = 0;
  822. int allow_downgrade = 0;
  823. int by_node = 0;
  824. ENTER();
  825. log_printf(LOGSYS_LEVEL_DEBUG, "got nodeinfo message from cluster node %u", nodeid);
  826. log_printf(LOGSYS_LEVEL_DEBUG, "nodeinfo message[%u]: votes: %d, expected: %d wfa: %d quorate: %d flags: %d",
  827. nodeid,
  828. req_exec_quorum_nodeinfo->votes,
  829. req_exec_quorum_nodeinfo->expected_votes,
  830. req_exec_quorum_nodeinfo->wait_for_all_status,
  831. req_exec_quorum_nodeinfo->quorate,
  832. req_exec_quorum_nodeinfo->flags);
  833. node = find_node_by_nodeid(nodeid);
  834. if (!node) {
  835. node = allocate_node(nodeid);
  836. new_node = 1;
  837. }
  838. if (!node) {
  839. corosync_api->error_memory_failure();
  840. LEAVE();
  841. return;
  842. }
  843. old_votes = node->votes;
  844. old_expected = node->expected_votes;
  845. old_state = node->state;
  846. old_flags = node->flags;
  847. /* Update node state */
  848. node->votes = req_exec_quorum_nodeinfo->votes;
  849. node->flags = req_exec_quorum_nodeinfo->flags;
  850. if (node->flags & NODE_FLAGS_LEAVING) {
  851. node->state = NODESTATE_LEAVING;
  852. allow_downgrade = 1;
  853. by_node = 1;
  854. } else {
  855. node->state = NODESTATE_MEMBER;
  856. }
  857. if ((!cluster_is_quorate) &&
  858. (req_exec_quorum_nodeinfo->quorate)) {
  859. allow_downgrade = 1;
  860. us->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  861. }
  862. if (req_exec_quorum_nodeinfo->quorate) {
  863. node->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  864. } else {
  865. node->expected_votes = us->expected_votes;
  866. }
  867. if ((last_man_standing) && (req_exec_quorum_nodeinfo->votes > 1)) {
  868. log_printf(LOGSYS_LEVEL_WARNING, "Last Man Standing feature is supported only when all"
  869. "cluster nodes votes are set to 1. Disabling LMS.");
  870. last_man_standing = 0;
  871. if (last_man_standing_timer_set) {
  872. corosync_api->timer_delete(last_man_standing_timer);
  873. last_man_standing_timer_set = 0;
  874. }
  875. }
  876. if (new_node ||
  877. req_exec_quorum_nodeinfo->first_trans ||
  878. old_votes != node->votes ||
  879. old_expected != node->expected_votes ||
  880. old_flags != node->flags ||
  881. old_state != node->state) {
  882. recalculate_quorum(allow_downgrade, by_node);
  883. }
  884. if (!nodeid) {
  885. free(node);
  886. }
  887. if ((wait_for_all) &&
  888. (!req_exec_quorum_nodeinfo->wait_for_all_status) &&
  889. (req_exec_quorum_nodeinfo->quorate)) {
  890. wait_for_all_status = 0;
  891. }
  892. LEAVE();
  893. }
  894. static void exec_votequorum_reconfigure_endian_convert (void *message)
  895. {
  896. struct req_exec_quorum_reconfigure *reconfigure = message;
  897. ENTER();
  898. reconfigure->nodeid = swab32(reconfigure->nodeid);
  899. reconfigure->value = swab32(reconfigure->value);
  900. LEAVE();
  901. }
  902. static void message_handler_req_exec_votequorum_reconfigure (
  903. const void *message,
  904. unsigned int nodeid)
  905. {
  906. const struct req_exec_quorum_reconfigure *req_exec_quorum_reconfigure = message;
  907. struct cluster_node *node;
  908. struct list_head *nodelist;
  909. ENTER();
  910. log_printf(LOGSYS_LEVEL_DEBUG, "got reconfigure message from cluster node %u", nodeid);
  911. node = find_node_by_nodeid(req_exec_quorum_reconfigure->nodeid);
  912. if (!node) {
  913. LEAVE();
  914. return;
  915. }
  916. switch(req_exec_quorum_reconfigure->param)
  917. {
  918. case VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES:
  919. list_iterate(nodelist, &cluster_members_list) {
  920. node = list_entry(nodelist, struct cluster_node, list);
  921. if (node->state == NODESTATE_MEMBER &&
  922. node->expected_votes > req_exec_quorum_reconfigure->value) {
  923. node->expected_votes = req_exec_quorum_reconfigure->value;
  924. }
  925. }
  926. votequorum_exec_send_expectedvotes_notification();
  927. ev_barrier = req_exec_quorum_reconfigure->value;
  928. recalculate_quorum(1, 0); /* Allow decrease */
  929. break;
  930. case VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES:
  931. node->votes = req_exec_quorum_reconfigure->value;
  932. recalculate_quorum(1, 0); /* Allow decrease */
  933. break;
  934. }
  935. LEAVE();
  936. }
  937. static int votequorum_exec_exit_fn (void)
  938. {
  939. int ret = 0;
  940. ENTER();
  941. if (leave_remove) {
  942. us->flags |= NODE_FLAGS_LEAVING;
  943. ret = votequorum_exec_send_nodeinfo();
  944. }
  945. LEAVE();
  946. return ret;
  947. }
  948. static char *votequorum_exec_init_fn (struct corosync_api_v1 *api)
  949. {
  950. #ifdef COROSYNC_SOLARIS
  951. logsys_subsys_init();
  952. #endif
  953. ENTER();
  954. list_init(&cluster_members_list);
  955. list_init(&trackers_list);
  956. /*
  957. * Allocate a cluster_node for us
  958. */
  959. us = allocate_node(corosync_api->totem_nodeid_get());
  960. if (!us) {
  961. LEAVE();
  962. return ((char *)"Could not allocate node.");
  963. }
  964. us->state = NODESTATE_MEMBER;
  965. us->votes = 1;
  966. votequorum_readconfig_dynamic();
  967. recalculate_quorum(0, 0);
  968. /*
  969. * Listen for changes
  970. */
  971. votequorum_exec_add_config_notification();
  972. /*
  973. * Start us off with one node
  974. */
  975. votequorum_exec_send_nodeinfo();
  976. LEAVE();
  977. return (NULL);
  978. }
  979. /*
  980. * votequorum service core
  981. */
  982. static void votequorum_last_man_standing_timer_fn(void *arg)
  983. {
  984. ENTER();
  985. last_man_standing_timer_set = 0;
  986. if (cluster_is_quorate) {
  987. recalculate_quorum(1,1);
  988. }
  989. LEAVE();
  990. }
  991. static void votequorum_confchg_fn (
  992. enum totem_configuration_type configuration_type,
  993. const unsigned int *member_list, size_t member_list_entries,
  994. const unsigned int *left_list, size_t left_list_entries,
  995. const unsigned int *joined_list, size_t joined_list_entries,
  996. const struct memb_ring_id *ring_id)
  997. {
  998. int i;
  999. struct cluster_node *node;
  1000. ENTER();
  1001. if (member_list_entries > 1) {
  1002. first_trans = 0;
  1003. }
  1004. if (left_list_entries) {
  1005. for (i = 0; i< left_list_entries; i++) {
  1006. node = find_node_by_nodeid(left_list[i]);
  1007. if (node) {
  1008. node->state = NODESTATE_DEAD;
  1009. }
  1010. }
  1011. }
  1012. if (last_man_standing) {
  1013. if (((member_list_entries >= quorum) && (left_list_entries)) ||
  1014. ((member_list_entries <= quorum) && (auto_tie_breaker) && (check_low_node_id_partition() == 1))) {
  1015. if (last_man_standing_timer_set) {
  1016. corosync_api->timer_delete(last_man_standing_timer);
  1017. last_man_standing_timer_set = 0;
  1018. }
  1019. corosync_api->timer_add_duration((unsigned long long)last_man_standing_window*1000000,
  1020. NULL, votequorum_last_man_standing_timer_fn,
  1021. &last_man_standing_timer);
  1022. last_man_standing_timer_set = 1;
  1023. }
  1024. }
  1025. if (member_list_entries) {
  1026. memcpy(quorum_members, member_list, sizeof(unsigned int) * member_list_entries);
  1027. quorum_members_entries = member_list_entries;
  1028. if (quorum_device) {
  1029. quorum_members[quorum_members_entries++] = 0;
  1030. }
  1031. votequorum_exec_send_nodeinfo();
  1032. }
  1033. if (left_list_entries) {
  1034. recalculate_quorum(0, 0);
  1035. }
  1036. memcpy(&quorum_ringid, ring_id, sizeof(*ring_id));
  1037. if (configuration_type == TOTEM_CONFIGURATION_REGULAR) {
  1038. quorum_callback(quorum_members, quorum_members_entries,
  1039. cluster_is_quorate, &quorum_ringid);
  1040. }
  1041. LEAVE();
  1042. }
  1043. char *votequorum_init(struct corosync_api_v1 *api,
  1044. quorum_set_quorate_fn_t q_set_quorate_fn)
  1045. {
  1046. char *error;
  1047. ENTER();
  1048. if (q_set_quorate_fn == NULL) {
  1049. return ((char *)"Quorate function not set");
  1050. }
  1051. corosync_api = api;
  1052. quorum_callback = q_set_quorate_fn;
  1053. error = votequorum_readconfig_static();
  1054. if (error) {
  1055. return (error);
  1056. }
  1057. error = corosync_service_link_and_init(corosync_api,
  1058. &votequorum_service[0]);
  1059. if (error) {
  1060. return (error);
  1061. }
  1062. LEAVE();
  1063. return (NULL);
  1064. }
  1065. /*
  1066. * Library Handler init/fini
  1067. */
  1068. static int quorum_lib_init_fn (void *conn)
  1069. {
  1070. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1071. ENTER();
  1072. list_init (&pd->list);
  1073. pd->conn = conn;
  1074. LEAVE();
  1075. return (0);
  1076. }
  1077. static int quorum_lib_exit_fn (void *conn)
  1078. {
  1079. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1080. ENTER();
  1081. if (quorum_pd->tracking_enabled) {
  1082. list_del (&quorum_pd->list);
  1083. list_init (&quorum_pd->list);
  1084. }
  1085. LEAVE();
  1086. return (0);
  1087. }
  1088. /*
  1089. * library internal functions
  1090. */
  1091. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  1092. static void quorum_device_timer_fn(void *arg)
  1093. {
  1094. ENTER();
  1095. if (!quorum_device || quorum_device->state == NODESTATE_DEAD) {
  1096. LEAVE();
  1097. return;
  1098. }
  1099. if ((quorum_device->last_hello / QB_TIME_NS_IN_SEC) + quorumdev_poll/1000 <
  1100. (qb_util_nano_current_get () / QB_TIME_NS_IN_SEC)) {
  1101. quorum_device->state = NODESTATE_DEAD;
  1102. log_printf(LOGSYS_LEVEL_INFO, "lost contact with quorum device");
  1103. recalculate_quorum(0, 0);
  1104. } else {
  1105. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1106. quorum_device_timer_fn, &quorum_device_timer);
  1107. }
  1108. LEAVE();
  1109. }
  1110. #endif
  1111. /*
  1112. * Library Handler Functions
  1113. */
  1114. static void message_handler_req_lib_votequorum_getinfo (void *conn, const void *message)
  1115. {
  1116. const struct req_lib_votequorum_getinfo *req_lib_votequorum_getinfo = message;
  1117. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  1118. struct cluster_node *node;
  1119. unsigned int highest_expected = 0;
  1120. unsigned int total_votes = 0;
  1121. cs_error_t error = CS_OK;
  1122. ENTER();
  1123. log_printf(LOGSYS_LEVEL_DEBUG, "got getinfo request on %p for node %u", conn, req_lib_votequorum_getinfo->nodeid);
  1124. node = find_node_by_nodeid(req_lib_votequorum_getinfo->nodeid);
  1125. if (node) {
  1126. struct cluster_node *iternode;
  1127. struct list_head *nodelist;
  1128. list_iterate(nodelist, &cluster_members_list) {
  1129. iternode = list_entry(nodelist, struct cluster_node, list);
  1130. if (iternode->state == NODESTATE_MEMBER) {
  1131. highest_expected =
  1132. max(highest_expected, iternode->expected_votes);
  1133. total_votes += iternode->votes;
  1134. }
  1135. }
  1136. if (quorum_device && quorum_device->state == NODESTATE_MEMBER) {
  1137. total_votes += quorum_device->votes;
  1138. }
  1139. res_lib_votequorum_getinfo.votes = us->votes;
  1140. res_lib_votequorum_getinfo.expected_votes = us->expected_votes;
  1141. res_lib_votequorum_getinfo.highest_expected = highest_expected;
  1142. res_lib_votequorum_getinfo.quorum = quorum;
  1143. res_lib_votequorum_getinfo.total_votes = total_votes;
  1144. res_lib_votequorum_getinfo.flags = 0;
  1145. res_lib_votequorum_getinfo.nodeid = node->node_id;
  1146. if (two_node) {
  1147. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_TWONODE;
  1148. }
  1149. if (cluster_is_quorate) {
  1150. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_QUORATE;
  1151. }
  1152. if (wait_for_all) {
  1153. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_WAIT_FOR_ALL;
  1154. }
  1155. if (last_man_standing) {
  1156. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_LAST_MAN_STANDING;
  1157. }
  1158. if (auto_tie_breaker) {
  1159. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_AUTO_TIE_BREAKER;
  1160. }
  1161. if (leave_remove) {
  1162. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_LEAVE_REMOVE;
  1163. }
  1164. } else {
  1165. error = CS_ERR_NOT_EXIST;
  1166. }
  1167. res_lib_votequorum_getinfo.header.size = sizeof(res_lib_votequorum_getinfo);
  1168. res_lib_votequorum_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1169. res_lib_votequorum_getinfo.header.error = error;
  1170. corosync_api->ipc_response_send(conn, &res_lib_votequorum_getinfo, sizeof(res_lib_votequorum_getinfo));
  1171. log_printf(LOGSYS_LEVEL_DEBUG, "getinfo response error: %d", error);
  1172. LEAVE();
  1173. }
  1174. static void message_handler_req_lib_votequorum_setexpected (void *conn, const void *message)
  1175. {
  1176. const struct req_lib_votequorum_setexpected *req_lib_votequorum_setexpected = message;
  1177. struct res_lib_votequorum_status res_lib_votequorum_status;
  1178. cs_error_t error = CS_OK;
  1179. unsigned int newquorum;
  1180. unsigned int total_votes;
  1181. ENTER();
  1182. /*
  1183. * Validate new expected votes
  1184. */
  1185. newquorum = calculate_quorum(1, req_lib_votequorum_setexpected->expected_votes, &total_votes);
  1186. if (newquorum < total_votes / 2 ||
  1187. newquorum > total_votes) {
  1188. error = CS_ERR_INVALID_PARAM;
  1189. goto error_exit;
  1190. }
  1191. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES, us->node_id,
  1192. req_lib_votequorum_setexpected->expected_votes);
  1193. error_exit:
  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 = error;
  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_setvotes (void *conn, const void *message)
  1201. {
  1202. const struct req_lib_votequorum_setvotes *req_lib_votequorum_setvotes = message;
  1203. struct res_lib_votequorum_status res_lib_votequorum_status;
  1204. struct cluster_node *node;
  1205. unsigned int newquorum;
  1206. unsigned int total_votes;
  1207. unsigned int saved_votes;
  1208. cs_error_t error = CS_OK;
  1209. unsigned int nodeid;
  1210. ENTER();
  1211. nodeid = req_lib_votequorum_setvotes->nodeid;
  1212. node = find_node_by_nodeid(nodeid);
  1213. if (!node) {
  1214. error = CS_ERR_NAME_NOT_FOUND;
  1215. goto error_exit;
  1216. }
  1217. /*
  1218. * Check votes is valid
  1219. */
  1220. saved_votes = node->votes;
  1221. node->votes = req_lib_votequorum_setvotes->votes;
  1222. newquorum = calculate_quorum(1, 0, &total_votes);
  1223. if (newquorum < total_votes / 2 ||
  1224. newquorum > total_votes) {
  1225. node->votes = saved_votes;
  1226. error = CS_ERR_INVALID_PARAM;
  1227. goto error_exit;
  1228. }
  1229. if (!nodeid) {
  1230. nodeid = corosync_api->totem_nodeid_get();
  1231. }
  1232. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, nodeid,
  1233. req_lib_votequorum_setvotes->votes);
  1234. error_exit:
  1235. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1236. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1237. res_lib_votequorum_status.header.error = error;
  1238. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1239. LEAVE();
  1240. }
  1241. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  1242. const void *message)
  1243. {
  1244. const struct req_lib_votequorum_trackstart *req_lib_votequorum_trackstart = message;
  1245. struct res_lib_votequorum_status res_lib_votequorum_status;
  1246. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1247. ENTER();
  1248. /*
  1249. * If an immediate listing of the current cluster membership
  1250. * is requested, generate membership list
  1251. */
  1252. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  1253. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  1254. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p", conn);
  1255. votequorum_exec_send_quorum_notification(conn, req_lib_votequorum_trackstart->context);
  1256. }
  1257. /*
  1258. * Record requests for tracking
  1259. */
  1260. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  1261. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  1262. quorum_pd->track_flags = req_lib_votequorum_trackstart->track_flags;
  1263. quorum_pd->tracking_enabled = 1;
  1264. quorum_pd->tracking_context = req_lib_votequorum_trackstart->context;
  1265. list_add (&quorum_pd->list, &trackers_list);
  1266. }
  1267. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1268. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1269. res_lib_votequorum_status.header.error = CS_OK;
  1270. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1271. LEAVE();
  1272. }
  1273. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  1274. const void *message)
  1275. {
  1276. struct res_lib_votequorum_status res_lib_votequorum_status;
  1277. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1278. int error = CS_OK;
  1279. ENTER();
  1280. if (quorum_pd->tracking_enabled) {
  1281. error = CS_OK;
  1282. quorum_pd->tracking_enabled = 0;
  1283. list_del (&quorum_pd->list);
  1284. list_init (&quorum_pd->list);
  1285. } else {
  1286. error = CS_ERR_NOT_EXIST;
  1287. }
  1288. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1289. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1290. res_lib_votequorum_status.header.error = error;
  1291. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1292. LEAVE();
  1293. }
  1294. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  1295. static void message_handler_req_lib_votequorum_qdevice_register (void *conn,
  1296. const void *message)
  1297. {
  1298. const struct req_lib_votequorum_qdevice_register *req_lib_votequorum_qdevice_register = message;
  1299. struct res_lib_votequorum_status res_lib_votequorum_status;
  1300. cs_error_t error = CS_OK;
  1301. ENTER();
  1302. if (quorum_device) {
  1303. error = CS_ERR_EXIST;
  1304. } else {
  1305. quorum_device = allocate_node(0);
  1306. quorum_device->state = NODESTATE_DEAD;
  1307. quorum_device->votes = req_lib_votequorum_qdevice_register->votes;
  1308. strcpy(quorum_device_name, req_lib_votequorum_qdevice_register->name);
  1309. list_add(&quorum_device->list, &cluster_members_list);
  1310. }
  1311. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1312. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1313. res_lib_votequorum_status.header.error = error;
  1314. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1315. LEAVE();
  1316. }
  1317. static void message_handler_req_lib_votequorum_qdevice_unregister (void *conn,
  1318. const void *message)
  1319. {
  1320. struct res_lib_votequorum_status res_lib_votequorum_status;
  1321. cs_error_t error = CS_OK;
  1322. ENTER();
  1323. if (quorum_device) {
  1324. struct cluster_node *node = quorum_device;
  1325. quorum_device = NULL;
  1326. list_del(&node->list);
  1327. free(node);
  1328. recalculate_quorum(0, 0);
  1329. } else {
  1330. error = CS_ERR_NOT_EXIST;
  1331. }
  1332. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1333. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1334. res_lib_votequorum_status.header.error = error;
  1335. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1336. LEAVE();
  1337. }
  1338. static void message_handler_req_lib_votequorum_qdevice_poll (void *conn,
  1339. const void *message)
  1340. {
  1341. const struct req_lib_votequorum_qdevice_poll *req_lib_votequorum_qdevice_poll = message;
  1342. struct res_lib_votequorum_status res_lib_votequorum_status;
  1343. cs_error_t error = CS_OK;
  1344. ENTER();
  1345. if (quorum_device) {
  1346. if (req_lib_votequorum_qdevice_poll->state) {
  1347. quorum_device->last_hello = qb_util_nano_current_get ();
  1348. if (quorum_device->state == NODESTATE_DEAD) {
  1349. quorum_device->state = NODESTATE_MEMBER;
  1350. recalculate_quorum(0, 0);
  1351. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1352. quorum_device_timer_fn, &quorum_device_timer);
  1353. }
  1354. } else {
  1355. if (quorum_device->state == NODESTATE_MEMBER) {
  1356. quorum_device->state = NODESTATE_DEAD;
  1357. recalculate_quorum(0, 0);
  1358. corosync_api->timer_delete(quorum_device_timer);
  1359. }
  1360. }
  1361. } else {
  1362. error = CS_ERR_NOT_EXIST;
  1363. }
  1364. /*
  1365. * send status
  1366. */
  1367. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1368. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1369. res_lib_votequorum_status.header.error = error;
  1370. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1371. LEAVE();
  1372. }
  1373. static void message_handler_req_lib_votequorum_qdevice_getinfo (void *conn,
  1374. const void *message)
  1375. {
  1376. struct res_lib_votequorum_qdevice_getinfo res_lib_votequorum_qdevice_getinfo;
  1377. cs_error_t error = CS_OK;
  1378. ENTER();
  1379. if (quorum_device) {
  1380. log_printf(LOGSYS_LEVEL_DEBUG, "got qdevice_getinfo state %d", quorum_device->state);
  1381. res_lib_votequorum_qdevice_getinfo.votes = quorum_device->votes;
  1382. if (quorum_device->state == NODESTATE_MEMBER) {
  1383. res_lib_votequorum_qdevice_getinfo.state = 1;
  1384. } else {
  1385. res_lib_votequorum_qdevice_getinfo.state = 0;
  1386. }
  1387. strcpy(res_lib_votequorum_qdevice_getinfo.name, quorum_device_name);
  1388. } else {
  1389. error = CS_ERR_NOT_EXIST;
  1390. }
  1391. res_lib_votequorum_qdevice_getinfo.header.size = sizeof(res_lib_votequorum_qdevice_getinfo);
  1392. res_lib_votequorum_qdevice_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1393. res_lib_votequorum_qdevice_getinfo.header.error = error;
  1394. corosync_api->ipc_response_send(conn, &res_lib_votequorum_qdevice_getinfo, sizeof(res_lib_votequorum_qdevice_getinfo));
  1395. LEAVE();
  1396. }
  1397. #endif