votequorum.c 49 KB

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