votequorum.c 43 KB

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