votequorum.c 42 KB

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