votequorum.c 49 KB

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