votequorum.c 50 KB

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