votequorum.c 44 KB

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