votequorum.c 43 KB

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