votequorum.c 50 KB

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