votequorum.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <sys/types.h>
  36. #ifdef HAVE_ALLOCA_H
  37. #include <alloca.h>
  38. #endif
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <sys/un.h>
  42. #include <sys/time.h>
  43. #include <sys/ioctl.h>
  44. #include <netinet/in.h>
  45. #include <sys/uio.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <time.h>
  52. #include <unistd.h>
  53. #include <netinet/in.h>
  54. #include <arpa/inet.h>
  55. #include <qb/qbipc_common.h>
  56. #include <qb/qbdefs.h>
  57. #include <qb/qbutil.h>
  58. #include <corosync/corotypes.h>
  59. #include <corosync/corodefs.h>
  60. #include <corosync/cfg.h>
  61. #include <corosync/list.h>
  62. #include <corosync/lcr/lcr_comp.h>
  63. #include <corosync/logsys.h>
  64. #include <corosync/mar_gen.h>
  65. #include <corosync/coroapi.h>
  66. #include <corosync/engine/quorum.h>
  67. #include <corosync/engine/icmap.h>
  68. #include <corosync/ipc_votequorum.h>
  69. #define VOTEQUORUM_MAJOR_VERSION 7
  70. #define VOTEQUORUM_MINOR_VERSION 0
  71. #define VOTEQUORUM_PATCH_VERSION 0
  72. /* Silly default to prevent accidents! */
  73. #define DEFAULT_EXPECTED 1024
  74. #define DEFAULT_QDEV_POLL 10000
  75. #define DEFAULT_LEAVE_TMO 10000
  76. #define DEFAULT_LMS_WIN 10000
  77. LOGSYS_DECLARE_SUBSYS ("VOTEQ");
  78. enum quorum_message_req_types {
  79. MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO = 0,
  80. MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE = 1,
  81. };
  82. #define NODE_FLAGS_BEENDOWN 1
  83. #define NODE_FLAGS_HASSTATE 8
  84. #define NODE_FLAGS_QDISK 16
  85. #define NODE_FLAGS_REMOVED 32
  86. #define NODE_FLAGS_US 64
  87. #define NODEID_US 0
  88. #define NODEID_QDEVICE -1
  89. typedef enum { NODESTATE_JOINING=1, NODESTATE_MEMBER,
  90. NODESTATE_DEAD, NODESTATE_LEAVING } nodestate_t;
  91. struct cluster_node {
  92. int flags;
  93. int node_id;
  94. unsigned int expected_votes;
  95. unsigned int votes;
  96. time_t join_time;
  97. nodestate_t state;
  98. unsigned long long int last_hello; /* Only used for quorum devices */
  99. struct list_head list;
  100. };
  101. static int quorum_flags;
  102. #define VOTEQUORUM_FLAG_FEATURE_TWONODE 1
  103. static int quorum;
  104. static int cluster_is_quorate;
  105. static int first_trans = 1;
  106. static unsigned int quorumdev_poll = DEFAULT_QDEV_POLL;
  107. static unsigned int leaving_timeout = DEFAULT_LEAVE_TMO;
  108. static uint8_t wait_for_all = 0;
  109. static uint8_t auto_tie_breaker = 0;
  110. static int lowest_node_id = -1;
  111. static uint8_t last_man_standing = 0;
  112. static uint32_t last_man_standing_window = DEFAULT_LMS_WIN;
  113. static int last_man_standing_timer_set = 0;
  114. static corosync_timer_handle_t last_man_standing_timer;
  115. static struct cluster_node *us;
  116. static struct cluster_node *quorum_device = NULL;
  117. static char quorum_device_name[VOTEQUORUM_MAX_QDISK_NAME_LEN];
  118. static corosync_timer_handle_t quorum_device_timer;
  119. static corosync_timer_handle_t leaving_timer;
  120. static struct list_head cluster_members_list;
  121. static struct corosync_api_v1 *corosync_api;
  122. static struct list_head trackers_list;
  123. static unsigned int quorum_members[PROCESSOR_COUNT_MAX+1];
  124. static int quorum_members_entries = 0;
  125. static struct memb_ring_id quorum_ringid;
  126. #define max(a,b) (((a) > (b)) ? (a) : (b))
  127. static struct cluster_node *find_node_by_nodeid(int nodeid);
  128. static struct cluster_node *allocate_node(int nodeid);
  129. #define list_iterate(v, head) \
  130. for (v = (head)->next; v != head; v = v->next)
  131. struct quorum_pd {
  132. unsigned char track_flags;
  133. int tracking_enabled;
  134. uint64_t tracking_context;
  135. struct list_head list;
  136. void *conn;
  137. };
  138. /*
  139. * Service Interfaces required by service_message_handler struct
  140. */
  141. static void votequorum_init(struct corosync_api_v1 *api,
  142. quorum_set_quorate_fn_t report);
  143. static void quorum_confchg_fn (
  144. enum totem_configuration_type configuration_type,
  145. const unsigned int *member_list, size_t member_list_entries,
  146. const unsigned int *left_list, size_t left_list_entries,
  147. const unsigned int *joined_list, size_t joined_list_entries,
  148. const struct memb_ring_id *ring_id);
  149. static int votequorum_exec_init_fn (struct corosync_api_v1 *api);
  150. static int quorum_lib_init_fn (void *conn);
  151. static int quorum_lib_exit_fn (void *conn);
  152. static void message_handler_req_exec_votequorum_nodeinfo (
  153. const void *message,
  154. unsigned int nodeid);
  155. static void message_handler_req_exec_votequorum_reconfigure (
  156. const void *message,
  157. unsigned int nodeid);
  158. static void message_handler_req_lib_votequorum_getinfo (void *conn,
  159. const void *message);
  160. static void message_handler_req_lib_votequorum_setexpected (void *conn,
  161. const void *message);
  162. static void message_handler_req_lib_votequorum_setvotes (void *conn,
  163. const void *message);
  164. static void message_handler_req_lib_votequorum_qdisk_register (void *conn,
  165. const void *message);
  166. static void message_handler_req_lib_votequorum_qdisk_unregister (void *conn,
  167. const void *message);
  168. static void message_handler_req_lib_votequorum_qdisk_poll (void *conn,
  169. const void *message);
  170. static void message_handler_req_lib_votequorum_qdisk_getinfo (void *conn,
  171. const void *message);
  172. static void message_handler_req_lib_votequorum_setstate (void *conn,
  173. const void *message);
  174. static void message_handler_req_lib_votequorum_leaving (void *conn,
  175. const void *message);
  176. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  177. const void *msg);
  178. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  179. const void *msg);
  180. static int quorum_exec_send_nodeinfo(void);
  181. static int quorum_exec_send_reconfigure(int param, int nodeid, int value);
  182. static void exec_votequorum_nodeinfo_endian_convert (void *msg);
  183. static void exec_votequorum_reconfigure_endian_convert (void *msg);
  184. static void add_votequorum_config_notification(void);
  185. static void recalculate_quorum(int allow_decrease, int by_current_nodes);
  186. /*
  187. * Library Handler Definition
  188. */
  189. static struct corosync_lib_handler quorum_lib_service[] =
  190. {
  191. { /* 0 */
  192. .lib_handler_fn = message_handler_req_lib_votequorum_getinfo,
  193. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  194. },
  195. { /* 1 */
  196. .lib_handler_fn = message_handler_req_lib_votequorum_setexpected,
  197. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  198. },
  199. { /* 2 */
  200. .lib_handler_fn = message_handler_req_lib_votequorum_setvotes,
  201. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  202. },
  203. { /* 3 */
  204. .lib_handler_fn = message_handler_req_lib_votequorum_qdisk_register,
  205. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  206. },
  207. { /* 4 */
  208. .lib_handler_fn = message_handler_req_lib_votequorum_qdisk_unregister,
  209. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  210. },
  211. { /* 5 */
  212. .lib_handler_fn = message_handler_req_lib_votequorum_qdisk_poll,
  213. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  214. },
  215. { /* 6 */
  216. .lib_handler_fn = message_handler_req_lib_votequorum_qdisk_getinfo,
  217. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  218. },
  219. { /* 7 */
  220. .lib_handler_fn = message_handler_req_lib_votequorum_setstate,
  221. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  222. },
  223. { /* 8 */
  224. .lib_handler_fn = message_handler_req_lib_votequorum_leaving,
  225. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  226. },
  227. { /* 9 */
  228. .lib_handler_fn = message_handler_req_lib_votequorum_trackstart,
  229. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  230. },
  231. { /* 10 */
  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. /* Load the library-servicing part of this module */
  337. api->service_link_and_init(api, "corosync_votequorum_iface", 0);
  338. LEAVE();
  339. }
  340. struct req_exec_quorum_nodeinfo {
  341. struct qb_ipc_request_header header __attribute__((aligned(8)));
  342. unsigned int first_trans;
  343. unsigned int votes;
  344. unsigned int expected_votes;
  345. unsigned int major_version; /* Not backwards compatible */
  346. unsigned int minor_version; /* Backwards compatible */
  347. unsigned int patch_version; /* Backwards/forwards compatible */
  348. unsigned int config_version;
  349. unsigned int flags;
  350. unsigned int wait_for_all;
  351. } __attribute__((packed));
  352. /* Parameters for RECONFIG command */
  353. #define RECONFIG_PARAM_EXPECTED_VOTES 1
  354. #define RECONFIG_PARAM_NODE_VOTES 2
  355. #define RECONFIG_PARAM_LEAVING 3
  356. struct req_exec_quorum_reconfigure {
  357. struct qb_ipc_request_header header __attribute__((aligned(8)));
  358. unsigned int param;
  359. unsigned int nodeid;
  360. unsigned int value;
  361. };
  362. static void read_quorum_config(void)
  363. {
  364. uint8_t value = 0;
  365. int cluster_members = 0;
  366. struct list_head *tmp;
  367. log_printf(LOGSYS_LEVEL_INFO, "Reading configuration\n");
  368. if (icmap_get_uint32("quorum.expected_votes", &us->expected_votes) != CS_OK) {
  369. us->expected_votes = DEFAULT_EXPECTED;
  370. }
  371. if (icmap_get_uint32("quorum.votes", &us->votes) != CS_OK) {
  372. us->votes = 1;
  373. }
  374. if (icmap_get_uint32("quorum.quorumdev_poll", &quorumdev_poll) != CS_OK) {
  375. quorumdev_poll = DEFAULT_QDEV_POLL;
  376. }
  377. if (icmap_get_uint32("quorum.leaving_timeout", &leaving_timeout) != CS_OK) {
  378. leaving_timeout = DEFAULT_LEAVE_TMO;
  379. }
  380. value = 0;
  381. icmap_get_uint8("quorum.two_node", &value);
  382. if (value)
  383. quorum_flags |= VOTEQUORUM_FLAG_FEATURE_TWONODE;
  384. else
  385. quorum_flags &= ~VOTEQUORUM_FLAG_FEATURE_TWONODE;
  386. /*
  387. * two_node mode is invalid if there are more than 2 nodes in the cluster!
  388. */
  389. list_iterate(tmp, &cluster_members_list) {
  390. cluster_members++;
  391. }
  392. if (quorum_flags & VOTEQUORUM_FLAG_FEATURE_TWONODE && cluster_members > 2) {
  393. log_printf(LOGSYS_LEVEL_WARNING, "quorum.two_node was set but there are more than 2 nodes in the cluster. It will be ignored.");
  394. quorum_flags &= ~VOTEQUORUM_FLAG_FEATURE_TWONODE;
  395. }
  396. }
  397. static int votequorum_exec_init_fn (struct corosync_api_v1 *api)
  398. {
  399. #ifdef COROSYNC_SOLARIS
  400. logsys_subsys_init();
  401. #endif
  402. ENTER();
  403. corosync_api = api;
  404. list_init(&cluster_members_list);
  405. list_init(&trackers_list);
  406. /* Allocate a cluster_node for us */
  407. us = allocate_node(corosync_api->totem_nodeid_get());
  408. if (!us)
  409. return (1);
  410. us->flags |= NODE_FLAGS_US;
  411. us->state = NODESTATE_MEMBER;
  412. us->expected_votes = DEFAULT_EXPECTED;
  413. us->votes = 1;
  414. time(&us->join_time);
  415. read_quorum_config();
  416. recalculate_quorum(0, 0);
  417. /* Listen for changes */
  418. add_votequorum_config_notification();
  419. /* Start us off with one node */
  420. quorum_exec_send_nodeinfo();
  421. LEAVE();
  422. return (0);
  423. }
  424. static int quorum_lib_exit_fn (void *conn)
  425. {
  426. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  427. ENTER();
  428. if (quorum_pd->tracking_enabled) {
  429. list_del (&quorum_pd->list);
  430. list_init (&quorum_pd->list);
  431. }
  432. LEAVE();
  433. return (0);
  434. }
  435. static int send_quorum_notification(void *conn, uint64_t context)
  436. {
  437. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  438. struct list_head *tmp;
  439. struct cluster_node *node;
  440. int cluster_members = 0;
  441. int i = 0;
  442. int size;
  443. char *buf;
  444. ENTER();
  445. list_iterate(tmp, &cluster_members_list) {
  446. node = list_entry(tmp, struct cluster_node, list);
  447. cluster_members++;
  448. }
  449. if (quorum_device)
  450. cluster_members++;
  451. size = sizeof(struct res_lib_votequorum_notification) + sizeof(struct votequorum_node) * cluster_members;
  452. buf = alloca(size);
  453. if (!buf) {
  454. LEAVE();
  455. return -1;
  456. }
  457. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)buf;
  458. res_lib_votequorum_notification->quorate = cluster_is_quorate;
  459. res_lib_votequorum_notification->node_list_entries = cluster_members;
  460. res_lib_votequorum_notification->context = context;
  461. list_iterate(tmp, &cluster_members_list) {
  462. node = list_entry(tmp, struct cluster_node, list);
  463. res_lib_votequorum_notification->node_list[i].nodeid = node->node_id;
  464. res_lib_votequorum_notification->node_list[i++].state = node->state;
  465. }
  466. if (quorum_device) {
  467. res_lib_votequorum_notification->node_list[i].nodeid = 0;
  468. res_lib_votequorum_notification->node_list[i++].state = quorum_device->state | 0x80;
  469. }
  470. res_lib_votequorum_notification->header.id = MESSAGE_RES_VOTEQUORUM_NOTIFICATION;
  471. res_lib_votequorum_notification->header.size = size;
  472. res_lib_votequorum_notification->header.error = CS_OK;
  473. /* Send it to all interested parties */
  474. if (conn) {
  475. int ret = corosync_api->ipc_dispatch_send(conn, buf, size);
  476. LEAVE();
  477. return ret;
  478. }
  479. else {
  480. struct quorum_pd *qpd;
  481. list_iterate(tmp, &trackers_list) {
  482. qpd = list_entry(tmp, struct quorum_pd, list);
  483. res_lib_votequorum_notification->context = qpd->tracking_context;
  484. corosync_api->ipc_dispatch_send(qpd->conn, buf, size);
  485. }
  486. }
  487. LEAVE();
  488. return 0;
  489. }
  490. static void send_expectedvotes_notification(void)
  491. {
  492. struct res_lib_votequorum_expectedvotes_notification res_lib_votequorum_expectedvotes_notification;
  493. struct quorum_pd *qpd;
  494. struct list_head *tmp;
  495. log_printf(LOGSYS_LEVEL_DEBUG, "Sending expected votes callback\n");
  496. res_lib_votequorum_expectedvotes_notification.header.id = MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION;
  497. res_lib_votequorum_expectedvotes_notification.header.size = sizeof(res_lib_votequorum_expectedvotes_notification);
  498. res_lib_votequorum_expectedvotes_notification.header.error = CS_OK;
  499. res_lib_votequorum_expectedvotes_notification.expected_votes = us->expected_votes;
  500. list_iterate(tmp, &trackers_list) {
  501. qpd = list_entry(tmp, struct quorum_pd, list);
  502. res_lib_votequorum_expectedvotes_notification.context = qpd->tracking_context;
  503. corosync_api->ipc_dispatch_send(qpd->conn, &res_lib_votequorum_expectedvotes_notification,
  504. sizeof(struct res_lib_votequorum_expectedvotes_notification));
  505. }
  506. }
  507. static void get_lowest_node_id(void)
  508. {
  509. struct cluster_node *node = NULL;
  510. struct list_head *tmp;
  511. lowest_node_id = us->node_id;
  512. list_iterate(tmp, &cluster_members_list) {
  513. node = list_entry(tmp, struct cluster_node, list);
  514. if (node->node_id < lowest_node_id)
  515. lowest_node_id = node->node_id;
  516. }
  517. log_printf(LOGSYS_LEVEL_DEBUG, "lowest node id: %d us: %d\n", lowest_node_id, us->node_id);
  518. }
  519. static int check_low_node_id_partition(void)
  520. {
  521. struct cluster_node *node = NULL;
  522. struct list_head *tmp;
  523. int found = 0;
  524. list_iterate(tmp, &cluster_members_list) {
  525. node = list_entry(tmp, struct cluster_node, list);
  526. if (node->state == NODESTATE_MEMBER) {
  527. if (node->node_id == lowest_node_id) {
  528. found = 1;
  529. }
  530. }
  531. }
  532. return found;
  533. }
  534. static void set_quorate(int total_votes)
  535. {
  536. int quorate;
  537. ENTER();
  538. /*
  539. * wait for all nodes to show up before granting quorum
  540. */
  541. if (wait_for_all) {
  542. if (total_votes != us->expected_votes) {
  543. log_printf(LOGSYS_LEVEL_NOTICE,
  544. "Waiting for all cluster members. "
  545. "Current votes: %d expected_votes: %d\n",
  546. total_votes, us->expected_votes);
  547. cluster_is_quorate = 0;
  548. return;
  549. }
  550. wait_for_all = 0;
  551. get_lowest_node_id();
  552. }
  553. if (quorum > total_votes) {
  554. quorate = 0;
  555. }
  556. else {
  557. quorate = 1;
  558. }
  559. if ((auto_tie_breaker) &&
  560. (total_votes == (us->expected_votes / 2)) &&
  561. (check_low_node_id_partition() == 1)) {
  562. quorate = 1;
  563. }
  564. if (cluster_is_quorate && !quorate)
  565. log_printf(LOGSYS_LEVEL_INFO, "quorum lost, blocking activity\n");
  566. if (!cluster_is_quorate && quorate)
  567. log_printf(LOGSYS_LEVEL_INFO, "quorum regained, resuming activity\n");
  568. cluster_is_quorate = quorate;
  569. set_quorum(quorum_members, quorum_members_entries, quorate, &quorum_ringid);
  570. ENTER();
  571. }
  572. static int calculate_quorum(int allow_decrease, int max_expected, unsigned int *ret_total_votes)
  573. {
  574. struct list_head *nodelist;
  575. struct cluster_node *node;
  576. unsigned int total_votes = 0;
  577. unsigned int highest_expected = 0;
  578. unsigned int newquorum, q1, q2;
  579. unsigned int total_nodes = 0;
  580. ENTER();
  581. list_iterate(nodelist, &cluster_members_list) {
  582. node = list_entry(nodelist, struct cluster_node, list);
  583. log_printf(LOGSYS_LEVEL_DEBUG, "node %x state=%d, votes=%d, expected=%d\n",
  584. node->node_id, node->state, node->votes, node->expected_votes);
  585. if (node->state == NODESTATE_MEMBER) {
  586. if (max_expected)
  587. node->expected_votes = max_expected;
  588. else
  589. highest_expected = max(highest_expected, node->expected_votes);
  590. total_votes += node->votes;
  591. total_nodes++;
  592. }
  593. }
  594. if (quorum_device && quorum_device->state == NODESTATE_MEMBER)
  595. total_votes += quorum_device->votes;
  596. if (max_expected > 0)
  597. highest_expected = max_expected;
  598. /* This quorum calculation is taken from the OpenVMS Cluster Systems
  599. * manual, but, then, you guessed that didn't you */
  600. q1 = (highest_expected + 2) / 2;
  601. q2 = (total_votes + 2) / 2;
  602. newquorum = max(q1, q2);
  603. /* Normally quorum never decreases but the system administrator can
  604. * force it down by setting expected votes to a maximum value */
  605. if (!allow_decrease)
  606. newquorum = max(quorum, newquorum);
  607. /* The special two_node mode allows each of the two nodes to retain
  608. * quorum if the other fails. Only one of the two should live past
  609. * fencing (as both nodes try to fence each other in split-brain.)
  610. * Also: if there are more than two nodes, force us inquorate to avoid
  611. * any damage or confusion.
  612. */
  613. if ((quorum_flags & VOTEQUORUM_FLAG_FEATURE_TWONODE) && total_nodes <= 2)
  614. newquorum = 1;
  615. if (ret_total_votes)
  616. *ret_total_votes = total_votes;
  617. LEAVE();
  618. return newquorum;
  619. }
  620. /* Recalculate cluster quorum, set quorate and notify changes */
  621. static void recalculate_quorum(int allow_decrease, int by_current_nodes)
  622. {
  623. unsigned int total_votes = 0;
  624. int cluster_members = 0;
  625. struct list_head *nodelist;
  626. struct cluster_node *node;
  627. ENTER();
  628. list_iterate(nodelist, &cluster_members_list) {
  629. node = list_entry(nodelist, struct cluster_node, list);
  630. if (node->state == NODESTATE_MEMBER) {
  631. if (by_current_nodes)
  632. cluster_members++;
  633. total_votes += node->votes;
  634. }
  635. }
  636. /* Keep expected_votes at the highest number of votes in the cluster */
  637. log_printf(LOGSYS_LEVEL_DEBUG, "total_votes=%d, expected_votes=%d\n", total_votes, us->expected_votes);
  638. if (total_votes > us->expected_votes) {
  639. us->expected_votes = total_votes;
  640. send_expectedvotes_notification();
  641. }
  642. quorum = calculate_quorum(allow_decrease, cluster_members, &total_votes);
  643. set_quorate(total_votes);
  644. send_quorum_notification(NULL, 0L);
  645. LEAVE();
  646. }
  647. static void node_add_ordered(struct cluster_node *newnode)
  648. {
  649. struct cluster_node *node = NULL;
  650. struct list_head *tmp;
  651. struct list_head *newlist = &newnode->list;
  652. list_iterate(tmp, &cluster_members_list) {
  653. node = list_entry(tmp, struct cluster_node, list);
  654. if (newnode->node_id < node->node_id)
  655. break;
  656. }
  657. if (!node)
  658. list_add(&newnode->list, &cluster_members_list);
  659. else {
  660. newlist->prev = tmp->prev;
  661. newlist->next = tmp;
  662. tmp->prev->next = newlist;
  663. tmp->prev = newlist;
  664. }
  665. }
  666. static struct cluster_node *allocate_node(int nodeid)
  667. {
  668. struct cluster_node *cl;
  669. cl = malloc(sizeof(struct cluster_node));
  670. if (cl) {
  671. memset(cl, 0, sizeof(struct cluster_node));
  672. cl->node_id = nodeid;
  673. if (nodeid)
  674. node_add_ordered(cl);
  675. }
  676. return cl;
  677. }
  678. static struct cluster_node *find_node_by_nodeid(int nodeid)
  679. {
  680. struct cluster_node *node;
  681. struct list_head *tmp;
  682. if (nodeid == NODEID_US)
  683. return us;
  684. if (nodeid == NODEID_QDEVICE)
  685. return quorum_device;
  686. list_iterate(tmp, &cluster_members_list) {
  687. node = list_entry(tmp, struct cluster_node, list);
  688. if (node->node_id == nodeid)
  689. return node;
  690. }
  691. return NULL;
  692. }
  693. static int quorum_exec_send_nodeinfo()
  694. {
  695. struct req_exec_quorum_nodeinfo req_exec_quorum_nodeinfo;
  696. struct iovec iov[1];
  697. int ret;
  698. ENTER();
  699. req_exec_quorum_nodeinfo.expected_votes = us->expected_votes;
  700. req_exec_quorum_nodeinfo.votes = us->votes;
  701. req_exec_quorum_nodeinfo.major_version = VOTEQUORUM_MAJOR_VERSION;
  702. req_exec_quorum_nodeinfo.minor_version = VOTEQUORUM_MINOR_VERSION;
  703. req_exec_quorum_nodeinfo.patch_version = VOTEQUORUM_PATCH_VERSION;
  704. req_exec_quorum_nodeinfo.flags = us->flags;
  705. req_exec_quorum_nodeinfo.first_trans = first_trans;
  706. req_exec_quorum_nodeinfo.wait_for_all = wait_for_all;
  707. req_exec_quorum_nodeinfo.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO);
  708. req_exec_quorum_nodeinfo.header.size = sizeof(req_exec_quorum_nodeinfo);
  709. iov[0].iov_base = (void *)&req_exec_quorum_nodeinfo;
  710. iov[0].iov_len = sizeof(req_exec_quorum_nodeinfo);
  711. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  712. LEAVE();
  713. return ret;
  714. }
  715. static int quorum_exec_send_reconfigure(int param, int nodeid, int value)
  716. {
  717. struct req_exec_quorum_reconfigure req_exec_quorum_reconfigure;
  718. struct iovec iov[1];
  719. int ret;
  720. ENTER();
  721. req_exec_quorum_reconfigure.param = param;
  722. req_exec_quorum_reconfigure.nodeid = nodeid;
  723. req_exec_quorum_reconfigure.value = value;
  724. req_exec_quorum_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE);
  725. req_exec_quorum_reconfigure.header.size = sizeof(req_exec_quorum_reconfigure);
  726. iov[0].iov_base = (void *)&req_exec_quorum_reconfigure;
  727. iov[0].iov_len = sizeof(req_exec_quorum_reconfigure);
  728. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  729. LEAVE();
  730. return ret;
  731. }
  732. static void lms_timer_fn(void *arg)
  733. {
  734. ENTER();
  735. last_man_standing_timer_set = 0;
  736. if (cluster_is_quorate) {
  737. recalculate_quorum(1,1);
  738. }
  739. LEAVE();
  740. }
  741. static void quorum_confchg_fn (
  742. enum totem_configuration_type configuration_type,
  743. const unsigned int *member_list, size_t member_list_entries,
  744. const unsigned int *left_list, size_t left_list_entries,
  745. const unsigned int *joined_list, size_t joined_list_entries,
  746. const struct memb_ring_id *ring_id)
  747. {
  748. int i;
  749. int leaving = 0;
  750. struct cluster_node *node;
  751. ENTER();
  752. if (member_list_entries > 1)
  753. first_trans = 0;
  754. if (left_list_entries) {
  755. for (i = 0; i< left_list_entries; i++) {
  756. node = find_node_by_nodeid(left_list[i]);
  757. if (node) {
  758. if (node->state == NODESTATE_LEAVING)
  759. leaving = 1;
  760. node->state = NODESTATE_DEAD;
  761. node->flags |= NODE_FLAGS_BEENDOWN;
  762. }
  763. }
  764. }
  765. if (last_man_standing) {
  766. if ((member_list_entries >= quorum) && (left_list_entries)) {
  767. if (last_man_standing_timer_set) {
  768. corosync_api->timer_delete(last_man_standing_timer);
  769. last_man_standing_timer_set = 0;
  770. }
  771. corosync_api->timer_add_duration((unsigned long long)last_man_standing_window*1000000, NULL, lms_timer_fn, &last_man_standing_timer);
  772. last_man_standing_timer_set = 1;
  773. }
  774. }
  775. if (member_list_entries) {
  776. memcpy(quorum_members, member_list, sizeof(unsigned int) * member_list_entries);
  777. quorum_members_entries = member_list_entries;
  778. if (quorum_device) {
  779. quorum_members[quorum_members_entries++] = 0;
  780. }
  781. quorum_exec_send_nodeinfo();
  782. }
  783. if (left_list_entries)
  784. recalculate_quorum(leaving, leaving);
  785. memcpy(&quorum_ringid, ring_id, sizeof(*ring_id));
  786. set_quorum(quorum_members, quorum_members_entries, cluster_is_quorate, &quorum_ringid);
  787. LEAVE();
  788. }
  789. static void exec_votequorum_nodeinfo_endian_convert (void *msg)
  790. {
  791. struct req_exec_quorum_nodeinfo *nodeinfo = msg;
  792. nodeinfo->votes = swab32(nodeinfo->votes);
  793. nodeinfo->expected_votes = swab32(nodeinfo->expected_votes);
  794. nodeinfo->major_version = swab32(nodeinfo->major_version);
  795. nodeinfo->minor_version = swab32(nodeinfo->minor_version);
  796. nodeinfo->patch_version = swab32(nodeinfo->patch_version);
  797. nodeinfo->config_version = swab32(nodeinfo->config_version);
  798. nodeinfo->flags = swab32(nodeinfo->flags);
  799. nodeinfo->wait_for_all = swab32(nodeinfo->wait_for_all);
  800. }
  801. static void exec_votequorum_reconfigure_endian_convert (void *msg)
  802. {
  803. struct req_exec_quorum_reconfigure *reconfigure = msg;
  804. reconfigure->nodeid = swab32(reconfigure->nodeid);
  805. reconfigure->value = swab32(reconfigure->value);
  806. }
  807. static void message_handler_req_exec_votequorum_nodeinfo (
  808. const void *message,
  809. unsigned int nodeid)
  810. {
  811. const struct req_exec_quorum_nodeinfo *req_exec_quorum_nodeinfo = message;
  812. struct cluster_node *node;
  813. int old_votes;
  814. int old_expected;
  815. nodestate_t old_state;
  816. int new_node = 0;
  817. ENTER();
  818. log_printf(LOGSYS_LEVEL_DEBUG, "got nodeinfo message from cluster node %d\n", nodeid);
  819. node = find_node_by_nodeid(nodeid);
  820. if (!node) {
  821. node = allocate_node(nodeid);
  822. new_node = 1;
  823. }
  824. if (!node) {
  825. corosync_api->error_memory_failure();
  826. return;
  827. }
  828. old_votes = node->votes;
  829. old_expected = node->expected_votes;
  830. old_state = node->state;
  831. /* Update node state */
  832. node->votes = req_exec_quorum_nodeinfo->votes;
  833. node->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  834. node->state = NODESTATE_MEMBER;
  835. log_printf(LOGSYS_LEVEL_DEBUG, "nodeinfo message: votes: %d, expected: %d wfa: %d\n", req_exec_quorum_nodeinfo->votes, req_exec_quorum_nodeinfo->expected_votes, req_exec_quorum_nodeinfo->wait_for_all);
  836. if ((last_man_standing) && (req_exec_quorum_nodeinfo->votes > 1)) {
  837. log_printf(LOGSYS_LEVEL_WARNING, "Last Man Standing feature is supported only when all"
  838. "cluster nodes votes are set to 1. Disabling LMS.");
  839. last_man_standing = 0;
  840. if (last_man_standing_timer_set) {
  841. corosync_api->timer_delete(last_man_standing_timer);
  842. last_man_standing_timer_set = 0;
  843. }
  844. }
  845. node->flags &= ~NODE_FLAGS_BEENDOWN;
  846. if (new_node || req_exec_quorum_nodeinfo->first_trans ||
  847. old_votes != node->votes || old_expected != node->expected_votes || old_state != node->state)
  848. recalculate_quorum(0, 0);
  849. if (!nodeid) {
  850. free(node);
  851. }
  852. if ((wait_for_all) && (!req_exec_quorum_nodeinfo->wait_for_all)) {
  853. wait_for_all = 0;
  854. }
  855. LEAVE();
  856. }
  857. static void message_handler_req_exec_votequorum_reconfigure (
  858. const void *message,
  859. unsigned int nodeid)
  860. {
  861. const struct req_exec_quorum_reconfigure *req_exec_quorum_reconfigure = message;
  862. struct cluster_node *node;
  863. struct list_head *nodelist;
  864. log_printf(LOGSYS_LEVEL_DEBUG, "got reconfigure message from cluster node %d\n", nodeid);
  865. node = find_node_by_nodeid(req_exec_quorum_reconfigure->nodeid);
  866. if (!node)
  867. return;
  868. switch(req_exec_quorum_reconfigure->param)
  869. {
  870. case RECONFIG_PARAM_EXPECTED_VOTES:
  871. list_iterate(nodelist, &cluster_members_list) {
  872. node = list_entry(nodelist, struct cluster_node, list);
  873. if (node->state == NODESTATE_MEMBER &&
  874. node->expected_votes > req_exec_quorum_reconfigure->value) {
  875. node->expected_votes = req_exec_quorum_reconfigure->value;
  876. }
  877. }
  878. send_expectedvotes_notification();
  879. recalculate_quorum(1, 0); /* Allow decrease */
  880. break;
  881. case RECONFIG_PARAM_NODE_VOTES:
  882. node->votes = req_exec_quorum_reconfigure->value;
  883. recalculate_quorum(1, 0); /* Allow decrease */
  884. break;
  885. case RECONFIG_PARAM_LEAVING:
  886. if (req_exec_quorum_reconfigure->value == 1 && node->state == NODESTATE_MEMBER)
  887. node->state = NODESTATE_LEAVING;
  888. if (req_exec_quorum_reconfigure->value == 0 && node->state == NODESTATE_LEAVING)
  889. node->state = NODESTATE_MEMBER;
  890. break;
  891. }
  892. }
  893. static int quorum_lib_init_fn (void *conn)
  894. {
  895. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  896. ENTER();
  897. list_init (&pd->list);
  898. pd->conn = conn;
  899. LEAVE();
  900. return (0);
  901. }
  902. /*
  903. * Someone called votequorum_leave AGES ago!
  904. * Assume they forgot to shut down the node.
  905. */
  906. static void leaving_timer_fn(void *arg)
  907. {
  908. ENTER();
  909. if (us->state == NODESTATE_LEAVING)
  910. us->state = NODESTATE_MEMBER;
  911. /* Tell everyone else we made a mistake */
  912. quorum_exec_send_reconfigure(RECONFIG_PARAM_LEAVING, us->node_id, 0);
  913. LEAVE();
  914. }
  915. /* Message from the library */
  916. static void message_handler_req_lib_votequorum_getinfo (void *conn, const void *message)
  917. {
  918. const struct req_lib_votequorum_getinfo *req_lib_votequorum_getinfo = message;
  919. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  920. struct cluster_node *node;
  921. unsigned int highest_expected = 0;
  922. unsigned int total_votes = 0;
  923. cs_error_t error = CS_OK;
  924. log_printf(LOGSYS_LEVEL_DEBUG, "got getinfo request on %p for node %d\n", conn, req_lib_votequorum_getinfo->nodeid);
  925. node = find_node_by_nodeid(req_lib_votequorum_getinfo->nodeid);
  926. if (node) {
  927. struct cluster_node *iternode;
  928. struct list_head *nodelist;
  929. list_iterate(nodelist, &cluster_members_list) {
  930. iternode = list_entry(nodelist, struct cluster_node, list);
  931. if (iternode->state == NODESTATE_MEMBER) {
  932. highest_expected =
  933. max(highest_expected, iternode->expected_votes);
  934. total_votes += iternode->votes;
  935. }
  936. }
  937. if (quorum_device && quorum_device->state == NODESTATE_MEMBER) {
  938. total_votes += quorum_device->votes;
  939. }
  940. res_lib_votequorum_getinfo.votes = us->votes;
  941. res_lib_votequorum_getinfo.expected_votes = us->expected_votes;
  942. res_lib_votequorum_getinfo.highest_expected = highest_expected;
  943. res_lib_votequorum_getinfo.quorum = quorum;
  944. res_lib_votequorum_getinfo.total_votes = total_votes;
  945. res_lib_votequorum_getinfo.flags = 0;
  946. res_lib_votequorum_getinfo.nodeid = node->node_id;
  947. if (us->flags & NODE_FLAGS_HASSTATE)
  948. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_HASSTATE;
  949. if (quorum_flags & VOTEQUORUM_FLAG_FEATURE_TWONODE)
  950. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_TWONODE;
  951. if (cluster_is_quorate)
  952. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_QUORATE;
  953. }
  954. else {
  955. error = CS_ERR_NOT_EXIST;
  956. }
  957. res_lib_votequorum_getinfo.header.size = sizeof(res_lib_votequorum_getinfo);
  958. res_lib_votequorum_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  959. res_lib_votequorum_getinfo.header.error = error;
  960. corosync_api->ipc_response_send(conn, &res_lib_votequorum_getinfo, sizeof(res_lib_votequorum_getinfo));
  961. log_printf(LOGSYS_LEVEL_DEBUG, "getinfo response error: %d\n", error);
  962. }
  963. /* Message from the library */
  964. static void message_handler_req_lib_votequorum_setexpected (void *conn, const void *message)
  965. {
  966. const struct req_lib_votequorum_setexpected *req_lib_votequorum_setexpected = message;
  967. struct res_lib_votequorum_status res_lib_votequorum_status;
  968. cs_error_t error = CS_OK;
  969. unsigned int newquorum;
  970. unsigned int total_votes;
  971. ENTER();
  972. /* Validate new expected votes */
  973. newquorum = calculate_quorum(1, req_lib_votequorum_setexpected->expected_votes, &total_votes);
  974. if (newquorum < total_votes / 2
  975. || newquorum > total_votes) {
  976. error = CS_ERR_INVALID_PARAM;
  977. goto error_exit;
  978. }
  979. quorum_exec_send_reconfigure(RECONFIG_PARAM_EXPECTED_VOTES, us->node_id, req_lib_votequorum_setexpected->expected_votes);
  980. /* send status */
  981. error_exit:
  982. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  983. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  984. res_lib_votequorum_status.header.error = error;
  985. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  986. LEAVE();
  987. }
  988. /* Message from the library */
  989. static void message_handler_req_lib_votequorum_setvotes (void *conn, const void *message)
  990. {
  991. const struct req_lib_votequorum_setvotes *req_lib_votequorum_setvotes = message;
  992. struct res_lib_votequorum_status res_lib_votequorum_status;
  993. struct cluster_node *node;
  994. unsigned int newquorum;
  995. unsigned int total_votes;
  996. unsigned int saved_votes;
  997. cs_error_t error = CS_OK;
  998. unsigned int nodeid;
  999. ENTER();
  1000. nodeid = req_lib_votequorum_setvotes->nodeid;
  1001. node = find_node_by_nodeid(nodeid);
  1002. if (!node) {
  1003. error = CS_ERR_NAME_NOT_FOUND;
  1004. goto error_exit;
  1005. }
  1006. /* Check votes is valid */
  1007. saved_votes = node->votes;
  1008. node->votes = req_lib_votequorum_setvotes->votes;
  1009. newquorum = calculate_quorum(1, 0, &total_votes);
  1010. if (newquorum < total_votes / 2 || newquorum > total_votes) {
  1011. node->votes = saved_votes;
  1012. error = CS_ERR_INVALID_PARAM;
  1013. goto error_exit;
  1014. }
  1015. if (!nodeid)
  1016. nodeid = corosync_api->totem_nodeid_get();
  1017. quorum_exec_send_reconfigure(RECONFIG_PARAM_NODE_VOTES, nodeid,
  1018. req_lib_votequorum_setvotes->votes);
  1019. error_exit:
  1020. /* send status */
  1021. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1022. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1023. res_lib_votequorum_status.header.error = error;
  1024. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1025. LEAVE();
  1026. }
  1027. static void message_handler_req_lib_votequorum_leaving (void *conn, const void *message)
  1028. {
  1029. struct res_lib_votequorum_status res_lib_votequorum_status;
  1030. cs_error_t error = CS_OK;
  1031. ENTER();
  1032. quorum_exec_send_reconfigure(RECONFIG_PARAM_LEAVING, us->node_id, 1);
  1033. /*
  1034. * If we don't shut down in a sensible amount of time then cancel the
  1035. * leave status.
  1036. */
  1037. if (leaving_timeout)
  1038. corosync_api->timer_add_duration((unsigned long long)leaving_timeout*1000000, NULL,
  1039. leaving_timer_fn, &leaving_timer);
  1040. /* send status */
  1041. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1042. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1043. res_lib_votequorum_status.header.error = error;
  1044. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1045. LEAVE();
  1046. }
  1047. static void quorum_device_timer_fn(void *arg)
  1048. {
  1049. ENTER();
  1050. if (!quorum_device || quorum_device->state == NODESTATE_DEAD)
  1051. return;
  1052. if ( (quorum_device->last_hello / QB_TIME_NS_IN_SEC) + quorumdev_poll/1000 <
  1053. (qb_util_nano_current_get () / QB_TIME_NS_IN_SEC)) {
  1054. quorum_device->state = NODESTATE_DEAD;
  1055. log_printf(LOGSYS_LEVEL_INFO, "lost contact with quorum device\n");
  1056. recalculate_quorum(0, 0);
  1057. }
  1058. else {
  1059. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1060. quorum_device_timer_fn, &quorum_device_timer);
  1061. }
  1062. LEAVE();
  1063. }
  1064. static void message_handler_req_lib_votequorum_qdisk_register (void *conn,
  1065. const void *message)
  1066. {
  1067. const struct req_lib_votequorum_qdisk_register
  1068. *req_lib_votequorum_qdisk_register = message;
  1069. struct res_lib_votequorum_status res_lib_votequorum_status;
  1070. cs_error_t error = CS_OK;
  1071. ENTER();
  1072. if (quorum_device) {
  1073. error = CS_ERR_EXIST;
  1074. }
  1075. else {
  1076. quorum_device = allocate_node(0);
  1077. quorum_device->state = NODESTATE_DEAD;
  1078. quorum_device->votes = req_lib_votequorum_qdisk_register->votes;
  1079. strcpy(quorum_device_name, req_lib_votequorum_qdisk_register->name);
  1080. list_add(&quorum_device->list, &cluster_members_list);
  1081. }
  1082. /* send status */
  1083. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1084. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1085. res_lib_votequorum_status.header.error = error;
  1086. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1087. LEAVE();
  1088. }
  1089. static void message_handler_req_lib_votequorum_qdisk_unregister (void *conn,
  1090. const void *message)
  1091. {
  1092. struct res_lib_votequorum_status res_lib_votequorum_status;
  1093. cs_error_t error = CS_OK;
  1094. ENTER();
  1095. if (quorum_device) {
  1096. struct cluster_node *node = quorum_device;
  1097. quorum_device = NULL;
  1098. list_del(&node->list);
  1099. free(node);
  1100. recalculate_quorum(0, 0);
  1101. }
  1102. else {
  1103. error = CS_ERR_NOT_EXIST;
  1104. }
  1105. /* send status */
  1106. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1107. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1108. res_lib_votequorum_status.header.error = error;
  1109. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1110. LEAVE();
  1111. }
  1112. static void message_handler_req_lib_votequorum_qdisk_poll (void *conn,
  1113. const void *message)
  1114. {
  1115. const struct req_lib_votequorum_qdisk_poll
  1116. *req_lib_votequorum_qdisk_poll = message;
  1117. struct res_lib_votequorum_status res_lib_votequorum_status;
  1118. cs_error_t error = CS_OK;
  1119. ENTER();
  1120. if (quorum_device) {
  1121. if (req_lib_votequorum_qdisk_poll->state) {
  1122. quorum_device->last_hello = qb_util_nano_current_get ();
  1123. if (quorum_device->state == NODESTATE_DEAD) {
  1124. quorum_device->state = NODESTATE_MEMBER;
  1125. recalculate_quorum(0, 0);
  1126. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1127. quorum_device_timer_fn, &quorum_device_timer);
  1128. }
  1129. }
  1130. else {
  1131. if (quorum_device->state == NODESTATE_MEMBER) {
  1132. quorum_device->state = NODESTATE_DEAD;
  1133. recalculate_quorum(0, 0);
  1134. corosync_api->timer_delete(quorum_device_timer);
  1135. }
  1136. }
  1137. }
  1138. else {
  1139. error = CS_ERR_NOT_EXIST;
  1140. }
  1141. /* send status */
  1142. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1143. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1144. res_lib_votequorum_status.header.error = error;
  1145. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1146. LEAVE();
  1147. }
  1148. static void message_handler_req_lib_votequorum_qdisk_getinfo (void *conn,
  1149. const void *message)
  1150. {
  1151. struct res_lib_votequorum_qdisk_getinfo res_lib_votequorum_qdisk_getinfo;
  1152. cs_error_t error = CS_OK;
  1153. ENTER();
  1154. if (quorum_device) {
  1155. log_printf(LOGSYS_LEVEL_DEBUG, "got qdisk_getinfo state %d\n", quorum_device->state);
  1156. res_lib_votequorum_qdisk_getinfo.votes = quorum_device->votes;
  1157. if (quorum_device->state == NODESTATE_MEMBER)
  1158. res_lib_votequorum_qdisk_getinfo.state = 1;
  1159. else
  1160. res_lib_votequorum_qdisk_getinfo.state = 0;
  1161. strcpy(res_lib_votequorum_qdisk_getinfo.name, quorum_device_name);
  1162. }
  1163. else {
  1164. error = CS_ERR_NOT_EXIST;
  1165. }
  1166. /* send status */
  1167. res_lib_votequorum_qdisk_getinfo.header.size = sizeof(res_lib_votequorum_qdisk_getinfo);
  1168. res_lib_votequorum_qdisk_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1169. res_lib_votequorum_qdisk_getinfo.header.error = error;
  1170. corosync_api->ipc_response_send(conn, &res_lib_votequorum_qdisk_getinfo, sizeof(res_lib_votequorum_qdisk_getinfo));
  1171. LEAVE();
  1172. }
  1173. static void message_handler_req_lib_votequorum_setstate (void *conn,
  1174. const void *message)
  1175. {
  1176. struct res_lib_votequorum_status res_lib_votequorum_status;
  1177. cs_error_t error = CS_OK;
  1178. ENTER();
  1179. us->flags |= NODE_FLAGS_HASSTATE;
  1180. /* send status */
  1181. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1182. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1183. res_lib_votequorum_status.header.error = error;
  1184. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1185. LEAVE();
  1186. }
  1187. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  1188. const void *msg)
  1189. {
  1190. const struct req_lib_votequorum_trackstart
  1191. *req_lib_votequorum_trackstart = msg;
  1192. struct res_lib_votequorum_status res_lib_votequorum_status;
  1193. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1194. ENTER();
  1195. /*
  1196. * If an immediate listing of the current cluster membership
  1197. * is requested, generate membership list
  1198. */
  1199. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  1200. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  1201. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p\n", conn);
  1202. send_quorum_notification(conn, req_lib_votequorum_trackstart->context);
  1203. }
  1204. /*
  1205. * Record requests for tracking
  1206. */
  1207. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  1208. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  1209. quorum_pd->track_flags = req_lib_votequorum_trackstart->track_flags;
  1210. quorum_pd->tracking_enabled = 1;
  1211. quorum_pd->tracking_context = req_lib_votequorum_trackstart->context;
  1212. list_add (&quorum_pd->list, &trackers_list);
  1213. }
  1214. /* Send status */
  1215. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1216. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1217. res_lib_votequorum_status.header.error = CS_OK;
  1218. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1219. LEAVE();
  1220. }
  1221. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  1222. const void *msg)
  1223. {
  1224. struct res_lib_votequorum_status res_lib_votequorum_status;
  1225. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1226. int error = CS_OK;
  1227. ENTER();
  1228. if (quorum_pd->tracking_enabled) {
  1229. error = CS_OK;
  1230. quorum_pd->tracking_enabled = 0;
  1231. list_del (&quorum_pd->list);
  1232. list_init (&quorum_pd->list);
  1233. } else {
  1234. error = CS_ERR_NOT_EXIST;
  1235. }
  1236. /* send status */
  1237. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1238. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1239. res_lib_votequorum_status.header.error = error;
  1240. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1241. LEAVE();
  1242. }
  1243. static void reread_config(void)
  1244. {
  1245. unsigned int old_votes;
  1246. unsigned int old_expected;
  1247. old_votes = us->votes;
  1248. old_expected = us->expected_votes;
  1249. /*
  1250. * Reload the configuration
  1251. */
  1252. read_quorum_config();
  1253. /*
  1254. * Check for fundamental changes that we need to propogate
  1255. */
  1256. if (old_votes != us->votes) {
  1257. quorum_exec_send_reconfigure(RECONFIG_PARAM_NODE_VOTES, us->node_id, us->votes);
  1258. }
  1259. if (old_expected != us->expected_votes) {
  1260. quorum_exec_send_reconfigure(RECONFIG_PARAM_EXPECTED_VOTES, us->node_id, us->expected_votes);
  1261. }
  1262. }
  1263. static void key_change_quorum(
  1264. int32_t event,
  1265. const char *key_name,
  1266. struct icmap_notify_value new_val,
  1267. struct icmap_notify_value old_val,
  1268. void *user_data)
  1269. {
  1270. reread_config();
  1271. }
  1272. static void add_votequorum_config_notification(void)
  1273. {
  1274. icmap_track_t icmap_track;
  1275. icmap_track_add("quorum.",
  1276. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  1277. key_change_quorum,
  1278. NULL,
  1279. &icmap_track);
  1280. }