votequorum.c 43 KB

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