votequorum.c 43 KB

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