votequorum.c 45 KB

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