votequorum.c 46 KB

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