votequorum.c 49 KB

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