votequorum.c 47 KB

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