votequorum.c 49 KB

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