votequorum.c 45 KB

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