votequorum.c 48 KB

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