votequorum.c 44 KB

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