votequorum.c 43 KB

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