votequorum.c 43 KB

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