votequorum.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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.two_node", &two_node);
  606. if (two_node) {
  607. wait_for_all = 1;
  608. }
  609. icmap_get_uint8("quorum.wait_for_all", &wait_for_all);
  610. icmap_get_uint8("quorum.auto_tie_breaker", &auto_tie_breaker);
  611. icmap_get_uint8("quorum.last_man_standing", &last_man_standing);
  612. icmap_get_uint32("quorum.last_man_standing_window", &last_man_standing_window);
  613. if ((votequorum_read_nodelist_configuration(&node_votes, &node_expected_votes)) &&
  614. (icmap_get_uint32("quorum.expected_votes", &expected_votes) != CS_OK)) {
  615. log_printf(LOGSYS_LEVEL_CRIT,
  616. "configuration error: nodelist or quorum.expected_votes must be configured!");
  617. return -1;
  618. }
  619. if ((auto_tie_breaker) && (lowest_node_id == -1)) {
  620. wait_for_all = 1;
  621. }
  622. if (wait_for_all) {
  623. wait_for_all_status = 1;
  624. }
  625. LEAVE();
  626. return 0;
  627. }
  628. static void votequorum_readconfig_dynamic(void)
  629. {
  630. int cluster_members = 0;
  631. struct list_head *tmp;
  632. uint32_t expected_votes = DEFAULT_EXPECTED;
  633. int have_nodelist = 1;
  634. ENTER();
  635. log_printf(LOGSYS_LEVEL_DEBUG, "Reading dynamic configuration");
  636. if (votequorum_read_nodelist_configuration(&us->votes, &us->expected_votes)) {
  637. have_nodelist = 0;
  638. us->expected_votes = DEFAULT_EXPECTED;
  639. us->votes = 1;
  640. icmap_get_uint32("quorum.votes", &us->votes);
  641. }
  642. if (icmap_get_uint32("quorum.expected_votes", &expected_votes) == CS_OK) {
  643. if (have_nodelist) {
  644. us->expected_votes = max(us->expected_votes, expected_votes);
  645. } else {
  646. us->expected_votes = expected_votes;
  647. }
  648. }
  649. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  650. if (icmap_get_uint32("quorum.quorumdev_poll", &quorumdev_poll) != CS_OK) {
  651. quorumdev_poll = DEFAULT_QDEV_POLL;
  652. }
  653. #endif
  654. icmap_get_uint8("quorum.two_node", &two_node);
  655. /*
  656. * two_node mode is invalid if there are more than 2 nodes in the cluster!
  657. */
  658. list_iterate(tmp, &cluster_members_list) {
  659. cluster_members++;
  660. }
  661. if (two_node && cluster_members > 2) {
  662. log_printf(LOGSYS_LEVEL_WARNING, "quorum.two_node was set but there are more than 2 nodes in the cluster. It will be ignored.");
  663. two_node = 0;
  664. }
  665. LEAVE();
  666. }
  667. static void votequorum_refresh_config(
  668. int32_t event,
  669. const char *key_name,
  670. struct icmap_notify_value new_val,
  671. struct icmap_notify_value old_val,
  672. void *user_data)
  673. {
  674. unsigned int old_votes;
  675. unsigned int old_expected;
  676. ENTER();
  677. old_votes = us->votes;
  678. old_expected = us->expected_votes;
  679. /*
  680. * Reload the configuration
  681. */
  682. votequorum_readconfig_dynamic();
  683. /*
  684. * Check for fundamental changes that we need to propogate
  685. */
  686. if (old_votes != us->votes) {
  687. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, us->node_id, us->votes);
  688. }
  689. if (old_expected != us->expected_votes) {
  690. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES, us->node_id, us->expected_votes);
  691. }
  692. LEAVE();
  693. }
  694. static void votequorum_exec_add_config_notification(void)
  695. {
  696. icmap_track_t icmap_track_nodelist = NULL;
  697. icmap_track_t icmap_track_quorum = NULL;
  698. ENTER();
  699. icmap_track_add("nodelist.",
  700. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  701. votequorum_refresh_config,
  702. NULL,
  703. &icmap_track_nodelist);
  704. icmap_track_add("quorum.",
  705. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  706. votequorum_refresh_config,
  707. NULL,
  708. &icmap_track_quorum);
  709. LEAVE();
  710. }
  711. /*
  712. * votequorum_exec core
  713. */
  714. static int votequorum_exec_send_reconfigure(uint8_t param, unsigned int nodeid, uint32_t value)
  715. {
  716. struct req_exec_quorum_reconfigure req_exec_quorum_reconfigure;
  717. struct iovec iov[1];
  718. int ret;
  719. ENTER();
  720. req_exec_quorum_reconfigure.nodeid = nodeid;
  721. req_exec_quorum_reconfigure.value = value;
  722. req_exec_quorum_reconfigure.param = param;
  723. req_exec_quorum_reconfigure._pad0 = 0;
  724. req_exec_quorum_reconfigure._pad1 = 0;
  725. req_exec_quorum_reconfigure._pad2 = 0;
  726. req_exec_quorum_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE);
  727. req_exec_quorum_reconfigure.header.size = sizeof(req_exec_quorum_reconfigure);
  728. iov[0].iov_base = (void *)&req_exec_quorum_reconfigure;
  729. iov[0].iov_len = sizeof(req_exec_quorum_reconfigure);
  730. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  731. LEAVE();
  732. return ret;
  733. }
  734. static int votequorum_exec_send_nodeinfo(void)
  735. {
  736. struct req_exec_quorum_nodeinfo req_exec_quorum_nodeinfo;
  737. struct iovec iov[1];
  738. int ret;
  739. ENTER();
  740. req_exec_quorum_nodeinfo.votes = us->votes;
  741. req_exec_quorum_nodeinfo.expected_votes = us->expected_votes;
  742. req_exec_quorum_nodeinfo.flags = us->flags;
  743. req_exec_quorum_nodeinfo.quorate = cluster_is_quorate;
  744. req_exec_quorum_nodeinfo.wait_for_all_status = wait_for_all_status;
  745. req_exec_quorum_nodeinfo.first_trans = first_trans;
  746. req_exec_quorum_nodeinfo._pad0 = 0;
  747. req_exec_quorum_nodeinfo._pad1 = 0;
  748. req_exec_quorum_nodeinfo._pad2 = 0;
  749. req_exec_quorum_nodeinfo.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO);
  750. req_exec_quorum_nodeinfo.header.size = sizeof(req_exec_quorum_nodeinfo);
  751. iov[0].iov_base = (void *)&req_exec_quorum_nodeinfo;
  752. iov[0].iov_len = sizeof(req_exec_quorum_nodeinfo);
  753. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  754. LEAVE();
  755. return ret;
  756. }
  757. static int votequorum_exec_send_quorum_notification(void *conn, uint64_t context)
  758. {
  759. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  760. struct list_head *tmp;
  761. struct cluster_node *node;
  762. int cluster_members = 0;
  763. int i = 0;
  764. int size;
  765. char *buf;
  766. ENTER();
  767. list_iterate(tmp, &cluster_members_list) {
  768. node = list_entry(tmp, struct cluster_node, list);
  769. cluster_members++;
  770. }
  771. if (quorum_device) {
  772. cluster_members++;
  773. }
  774. size = sizeof(struct res_lib_votequorum_notification) + sizeof(struct votequorum_node) * cluster_members;
  775. buf = alloca(size);
  776. if (!buf) {
  777. LEAVE();
  778. return -1;
  779. }
  780. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)buf;
  781. res_lib_votequorum_notification->quorate = cluster_is_quorate;
  782. res_lib_votequorum_notification->node_list_entries = cluster_members;
  783. res_lib_votequorum_notification->context = context;
  784. list_iterate(tmp, &cluster_members_list) {
  785. node = list_entry(tmp, struct cluster_node, list);
  786. res_lib_votequorum_notification->node_list[i].nodeid = node->node_id;
  787. res_lib_votequorum_notification->node_list[i++].state = node->state;
  788. }
  789. if (quorum_device) {
  790. res_lib_votequorum_notification->node_list[i].nodeid = 0;
  791. res_lib_votequorum_notification->node_list[i++].state = quorum_device->state | 0x80;
  792. }
  793. res_lib_votequorum_notification->header.id = MESSAGE_RES_VOTEQUORUM_NOTIFICATION;
  794. res_lib_votequorum_notification->header.size = size;
  795. res_lib_votequorum_notification->header.error = CS_OK;
  796. /* Send it to all interested parties */
  797. if (conn) {
  798. int ret = corosync_api->ipc_dispatch_send(conn, buf, size);
  799. LEAVE();
  800. return ret;
  801. } else {
  802. struct quorum_pd *qpd;
  803. list_iterate(tmp, &trackers_list) {
  804. qpd = list_entry(tmp, struct quorum_pd, list);
  805. res_lib_votequorum_notification->context = qpd->tracking_context;
  806. corosync_api->ipc_dispatch_send(qpd->conn, buf, size);
  807. }
  808. }
  809. LEAVE();
  810. return 0;
  811. }
  812. static void votequorum_exec_send_expectedvotes_notification(void)
  813. {
  814. struct res_lib_votequorum_expectedvotes_notification res_lib_votequorum_expectedvotes_notification;
  815. struct quorum_pd *qpd;
  816. struct list_head *tmp;
  817. ENTER();
  818. log_printf(LOGSYS_LEVEL_DEBUG, "Sending expected votes callback");
  819. res_lib_votequorum_expectedvotes_notification.header.id = MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION;
  820. res_lib_votequorum_expectedvotes_notification.header.size = sizeof(res_lib_votequorum_expectedvotes_notification);
  821. res_lib_votequorum_expectedvotes_notification.header.error = CS_OK;
  822. res_lib_votequorum_expectedvotes_notification.expected_votes = us->expected_votes;
  823. list_iterate(tmp, &trackers_list) {
  824. qpd = list_entry(tmp, struct quorum_pd, list);
  825. res_lib_votequorum_expectedvotes_notification.context = qpd->tracking_context;
  826. corosync_api->ipc_dispatch_send(qpd->conn, &res_lib_votequorum_expectedvotes_notification,
  827. sizeof(struct res_lib_votequorum_expectedvotes_notification));
  828. }
  829. LEAVE();
  830. }
  831. static void exec_votequorum_nodeinfo_endian_convert (void *message)
  832. {
  833. struct req_exec_quorum_nodeinfo *nodeinfo = message;
  834. ENTER();
  835. nodeinfo->votes = swab32(nodeinfo->votes);
  836. nodeinfo->expected_votes = swab32(nodeinfo->expected_votes);
  837. nodeinfo->flags = swab16(nodeinfo->flags);
  838. LEAVE();
  839. }
  840. static void message_handler_req_exec_votequorum_nodeinfo (
  841. const void *message,
  842. unsigned int nodeid)
  843. {
  844. const struct req_exec_quorum_nodeinfo *req_exec_quorum_nodeinfo = message;
  845. struct cluster_node *node;
  846. int old_votes;
  847. int old_expected;
  848. nodestate_t old_state;
  849. int new_node = 0;
  850. ENTER();
  851. log_printf(LOGSYS_LEVEL_DEBUG, "got nodeinfo message from cluster node %u", nodeid);
  852. node = find_node_by_nodeid(nodeid);
  853. if (!node) {
  854. node = allocate_node(nodeid);
  855. new_node = 1;
  856. }
  857. if (!node) {
  858. corosync_api->error_memory_failure();
  859. LEAVE();
  860. return;
  861. }
  862. old_votes = node->votes;
  863. old_expected = node->expected_votes;
  864. old_state = node->state;
  865. /* Update node state */
  866. node->votes = req_exec_quorum_nodeinfo->votes;
  867. node->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  868. node->state = NODESTATE_MEMBER;
  869. log_printf(LOGSYS_LEVEL_DEBUG, "nodeinfo message: votes: %d, expected: %d wfa: %d quorate: %d",
  870. req_exec_quorum_nodeinfo->votes,
  871. req_exec_quorum_nodeinfo->expected_votes,
  872. req_exec_quorum_nodeinfo->wait_for_all_status,
  873. req_exec_quorum_nodeinfo->quorate);
  874. if ((last_man_standing) && (req_exec_quorum_nodeinfo->votes > 1)) {
  875. log_printf(LOGSYS_LEVEL_WARNING, "Last Man Standing feature is supported only when all"
  876. "cluster nodes votes are set to 1. Disabling LMS.");
  877. last_man_standing = 0;
  878. if (last_man_standing_timer_set) {
  879. corosync_api->timer_delete(last_man_standing_timer);
  880. last_man_standing_timer_set = 0;
  881. }
  882. }
  883. node->flags &= ~NODE_FLAGS_BEENDOWN;
  884. if (new_node ||
  885. req_exec_quorum_nodeinfo->first_trans ||
  886. old_votes != node->votes ||
  887. old_expected != node->expected_votes ||
  888. old_state != node->state) {
  889. recalculate_quorum(0, 0);
  890. }
  891. if (!nodeid) {
  892. free(node);
  893. }
  894. if ((wait_for_all) &&
  895. (!req_exec_quorum_nodeinfo->wait_for_all_status) &&
  896. (req_exec_quorum_nodeinfo->quorate)) {
  897. wait_for_all_status = 0;
  898. }
  899. LEAVE();
  900. }
  901. static void exec_votequorum_reconfigure_endian_convert (void *message)
  902. {
  903. struct req_exec_quorum_reconfigure *reconfigure = message;
  904. ENTER();
  905. reconfigure->nodeid = swab32(reconfigure->nodeid);
  906. reconfigure->value = swab32(reconfigure->value);
  907. LEAVE();
  908. }
  909. static void message_handler_req_exec_votequorum_reconfigure (
  910. const void *message,
  911. unsigned int nodeid)
  912. {
  913. const struct req_exec_quorum_reconfigure *req_exec_quorum_reconfigure = message;
  914. struct cluster_node *node;
  915. struct list_head *nodelist;
  916. ENTER();
  917. log_printf(LOGSYS_LEVEL_DEBUG, "got reconfigure message from cluster node %u", nodeid);
  918. node = find_node_by_nodeid(req_exec_quorum_reconfigure->nodeid);
  919. if (!node) {
  920. LEAVE();
  921. return;
  922. }
  923. switch(req_exec_quorum_reconfigure->param)
  924. {
  925. case VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES:
  926. list_iterate(nodelist, &cluster_members_list) {
  927. node = list_entry(nodelist, struct cluster_node, list);
  928. if (node->state == NODESTATE_MEMBER &&
  929. node->expected_votes > req_exec_quorum_reconfigure->value) {
  930. node->expected_votes = req_exec_quorum_reconfigure->value;
  931. }
  932. }
  933. votequorum_exec_send_expectedvotes_notification();
  934. recalculate_quorum(1, 0); /* Allow decrease */
  935. break;
  936. case VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES:
  937. node->votes = req_exec_quorum_reconfigure->value;
  938. recalculate_quorum(1, 0); /* Allow decrease */
  939. break;
  940. }
  941. LEAVE();
  942. }
  943. static int votequorum_exec_init_fn (struct corosync_api_v1 *api)
  944. {
  945. #ifdef COROSYNC_SOLARIS
  946. logsys_subsys_init();
  947. #endif
  948. ENTER();
  949. list_init(&cluster_members_list);
  950. list_init(&trackers_list);
  951. /*
  952. * Allocate a cluster_node for us
  953. */
  954. us = allocate_node(corosync_api->totem_nodeid_get());
  955. if (!us) {
  956. LEAVE();
  957. return (1);
  958. }
  959. us->flags |= NODE_FLAGS_US;
  960. us->state = NODESTATE_MEMBER;
  961. us->expected_votes = DEFAULT_EXPECTED;
  962. us->votes = 1;
  963. votequorum_readconfig_dynamic();
  964. recalculate_quorum(0, 0);
  965. /*
  966. * Listen for changes
  967. */
  968. votequorum_exec_add_config_notification();
  969. /*
  970. * Start us off with one node
  971. */
  972. votequorum_exec_send_nodeinfo();
  973. LEAVE();
  974. return (0);
  975. }
  976. /*
  977. * votequorum service core
  978. */
  979. static void votequorum_last_man_standing_timer_fn(void *arg)
  980. {
  981. ENTER();
  982. last_man_standing_timer_set = 0;
  983. if (cluster_is_quorate) {
  984. recalculate_quorum(1,1);
  985. }
  986. LEAVE();
  987. }
  988. static void votequorum_confchg_fn (
  989. enum totem_configuration_type configuration_type,
  990. const unsigned int *member_list, size_t member_list_entries,
  991. const unsigned int *left_list, size_t left_list_entries,
  992. const unsigned int *joined_list, size_t joined_list_entries,
  993. const struct memb_ring_id *ring_id)
  994. {
  995. int i;
  996. int leaving = 0;
  997. struct cluster_node *node;
  998. ENTER();
  999. if (member_list_entries > 1) {
  1000. first_trans = 0;
  1001. }
  1002. if (left_list_entries) {
  1003. for (i = 0; i< left_list_entries; i++) {
  1004. node = find_node_by_nodeid(left_list[i]);
  1005. if (node) {
  1006. if (node->state == NODESTATE_LEAVING) {
  1007. leaving = 1;
  1008. }
  1009. node->state = NODESTATE_DEAD;
  1010. node->flags |= NODE_FLAGS_BEENDOWN;
  1011. }
  1012. }
  1013. }
  1014. if (last_man_standing) {
  1015. if (((member_list_entries >= quorum) && (left_list_entries)) ||
  1016. ((member_list_entries <= quorum) && (auto_tie_breaker) && (check_low_node_id_partition() == 1))) {
  1017. if (last_man_standing_timer_set) {
  1018. corosync_api->timer_delete(last_man_standing_timer);
  1019. last_man_standing_timer_set = 0;
  1020. }
  1021. corosync_api->timer_add_duration((unsigned long long)last_man_standing_window*1000000,
  1022. NULL, votequorum_last_man_standing_timer_fn,
  1023. &last_man_standing_timer);
  1024. last_man_standing_timer_set = 1;
  1025. }
  1026. }
  1027. if (member_list_entries) {
  1028. memcpy(quorum_members, member_list, sizeof(unsigned int) * member_list_entries);
  1029. quorum_members_entries = member_list_entries;
  1030. if (quorum_device) {
  1031. quorum_members[quorum_members_entries++] = 0;
  1032. }
  1033. votequorum_exec_send_nodeinfo();
  1034. }
  1035. if (left_list_entries) {
  1036. recalculate_quorum(leaving, leaving);
  1037. }
  1038. memcpy(&quorum_ringid, ring_id, sizeof(*ring_id));
  1039. if (configuration_type == TOTEM_CONFIGURATION_REGULAR) {
  1040. quorum_callback(quorum_members, quorum_members_entries,
  1041. cluster_is_quorate, &quorum_ringid);
  1042. }
  1043. LEAVE();
  1044. }
  1045. cs_error_t votequorum_init(struct corosync_api_v1 *api,
  1046. quorum_set_quorate_fn_t q_set_quorate_fn)
  1047. {
  1048. ENTER();
  1049. if ((!api) || (!q_set_quorate_fn)) {
  1050. return CS_ERR_INVALID_PARAM;
  1051. }
  1052. corosync_api = api;
  1053. quorum_callback = q_set_quorate_fn;
  1054. if (votequorum_readconfig_static()) {
  1055. return CS_ERR_INVALID_PARAM;
  1056. }
  1057. corosync_service_link_and_init(corosync_api,
  1058. &votequorum_service[0]);
  1059. LEAVE();
  1060. return CS_OK;
  1061. }
  1062. /*
  1063. * Library Handler init/fini
  1064. */
  1065. static int quorum_lib_init_fn (void *conn)
  1066. {
  1067. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1068. ENTER();
  1069. list_init (&pd->list);
  1070. pd->conn = conn;
  1071. LEAVE();
  1072. return (0);
  1073. }
  1074. static int quorum_lib_exit_fn (void *conn)
  1075. {
  1076. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1077. ENTER();
  1078. if (quorum_pd->tracking_enabled) {
  1079. list_del (&quorum_pd->list);
  1080. list_init (&quorum_pd->list);
  1081. }
  1082. LEAVE();
  1083. return (0);
  1084. }
  1085. /*
  1086. * library internal functions
  1087. */
  1088. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  1089. static void quorum_device_timer_fn(void *arg)
  1090. {
  1091. ENTER();
  1092. if (!quorum_device || quorum_device->state == NODESTATE_DEAD) {
  1093. LEAVE();
  1094. return;
  1095. }
  1096. if ((quorum_device->last_hello / QB_TIME_NS_IN_SEC) + quorumdev_poll/1000 <
  1097. (qb_util_nano_current_get () / QB_TIME_NS_IN_SEC)) {
  1098. quorum_device->state = NODESTATE_DEAD;
  1099. log_printf(LOGSYS_LEVEL_INFO, "lost contact with quorum device");
  1100. recalculate_quorum(0, 0);
  1101. } else {
  1102. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1103. quorum_device_timer_fn, &quorum_device_timer);
  1104. }
  1105. LEAVE();
  1106. }
  1107. #endif
  1108. /*
  1109. * Library Handler Functions
  1110. */
  1111. static void message_handler_req_lib_votequorum_getinfo (void *conn, const void *message)
  1112. {
  1113. const struct req_lib_votequorum_getinfo *req_lib_votequorum_getinfo = message;
  1114. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  1115. struct cluster_node *node;
  1116. unsigned int highest_expected = 0;
  1117. unsigned int total_votes = 0;
  1118. cs_error_t error = CS_OK;
  1119. ENTER();
  1120. log_printf(LOGSYS_LEVEL_DEBUG, "got getinfo request on %p for node %u", conn, req_lib_votequorum_getinfo->nodeid);
  1121. node = find_node_by_nodeid(req_lib_votequorum_getinfo->nodeid);
  1122. if (node) {
  1123. struct cluster_node *iternode;
  1124. struct list_head *nodelist;
  1125. list_iterate(nodelist, &cluster_members_list) {
  1126. iternode = list_entry(nodelist, struct cluster_node, list);
  1127. if (iternode->state == NODESTATE_MEMBER) {
  1128. highest_expected =
  1129. max(highest_expected, iternode->expected_votes);
  1130. total_votes += iternode->votes;
  1131. }
  1132. }
  1133. if (quorum_device && quorum_device->state == NODESTATE_MEMBER) {
  1134. total_votes += quorum_device->votes;
  1135. }
  1136. res_lib_votequorum_getinfo.votes = us->votes;
  1137. res_lib_votequorum_getinfo.expected_votes = us->expected_votes;
  1138. res_lib_votequorum_getinfo.highest_expected = highest_expected;
  1139. res_lib_votequorum_getinfo.quorum = quorum;
  1140. res_lib_votequorum_getinfo.total_votes = total_votes;
  1141. res_lib_votequorum_getinfo.flags = 0;
  1142. res_lib_votequorum_getinfo.nodeid = node->node_id;
  1143. if (two_node) {
  1144. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_TWONODE;
  1145. }
  1146. if (cluster_is_quorate) {
  1147. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_QUORATE;
  1148. }
  1149. if (wait_for_all) {
  1150. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_WAIT_FOR_ALL;
  1151. }
  1152. if (last_man_standing) {
  1153. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_LAST_MAN_STANDING;
  1154. }
  1155. if (auto_tie_breaker) {
  1156. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_AUTO_TIE_BREAKER;
  1157. }
  1158. } else {
  1159. error = CS_ERR_NOT_EXIST;
  1160. }
  1161. res_lib_votequorum_getinfo.header.size = sizeof(res_lib_votequorum_getinfo);
  1162. res_lib_votequorum_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1163. res_lib_votequorum_getinfo.header.error = error;
  1164. corosync_api->ipc_response_send(conn, &res_lib_votequorum_getinfo, sizeof(res_lib_votequorum_getinfo));
  1165. log_printf(LOGSYS_LEVEL_DEBUG, "getinfo response error: %d", error);
  1166. LEAVE();
  1167. }
  1168. static void message_handler_req_lib_votequorum_setexpected (void *conn, const void *message)
  1169. {
  1170. const struct req_lib_votequorum_setexpected *req_lib_votequorum_setexpected = message;
  1171. struct res_lib_votequorum_status res_lib_votequorum_status;
  1172. cs_error_t error = CS_OK;
  1173. unsigned int newquorum;
  1174. unsigned int total_votes;
  1175. ENTER();
  1176. /*
  1177. * Validate new expected votes
  1178. */
  1179. newquorum = calculate_quorum(1, req_lib_votequorum_setexpected->expected_votes, &total_votes);
  1180. if (newquorum < total_votes / 2 ||
  1181. newquorum > total_votes) {
  1182. error = CS_ERR_INVALID_PARAM;
  1183. goto error_exit;
  1184. }
  1185. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES, us->node_id,
  1186. req_lib_votequorum_setexpected->expected_votes);
  1187. error_exit:
  1188. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1189. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1190. res_lib_votequorum_status.header.error = error;
  1191. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1192. LEAVE();
  1193. }
  1194. static void message_handler_req_lib_votequorum_setvotes (void *conn, const void *message)
  1195. {
  1196. const struct req_lib_votequorum_setvotes *req_lib_votequorum_setvotes = message;
  1197. struct res_lib_votequorum_status res_lib_votequorum_status;
  1198. struct cluster_node *node;
  1199. unsigned int newquorum;
  1200. unsigned int total_votes;
  1201. unsigned int saved_votes;
  1202. cs_error_t error = CS_OK;
  1203. unsigned int nodeid;
  1204. ENTER();
  1205. nodeid = req_lib_votequorum_setvotes->nodeid;
  1206. node = find_node_by_nodeid(nodeid);
  1207. if (!node) {
  1208. error = CS_ERR_NAME_NOT_FOUND;
  1209. goto error_exit;
  1210. }
  1211. /*
  1212. * Check votes is valid
  1213. */
  1214. saved_votes = node->votes;
  1215. node->votes = req_lib_votequorum_setvotes->votes;
  1216. newquorum = calculate_quorum(1, 0, &total_votes);
  1217. if (newquorum < total_votes / 2 ||
  1218. newquorum > total_votes) {
  1219. node->votes = saved_votes;
  1220. error = CS_ERR_INVALID_PARAM;
  1221. goto error_exit;
  1222. }
  1223. if (!nodeid) {
  1224. nodeid = corosync_api->totem_nodeid_get();
  1225. }
  1226. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, nodeid,
  1227. req_lib_votequorum_setvotes->votes);
  1228. error_exit:
  1229. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1230. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1231. res_lib_votequorum_status.header.error = error;
  1232. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1233. LEAVE();
  1234. }
  1235. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  1236. const void *message)
  1237. {
  1238. const struct req_lib_votequorum_trackstart *req_lib_votequorum_trackstart = message;
  1239. struct res_lib_votequorum_status res_lib_votequorum_status;
  1240. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1241. ENTER();
  1242. /*
  1243. * If an immediate listing of the current cluster membership
  1244. * is requested, generate membership list
  1245. */
  1246. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  1247. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  1248. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p", conn);
  1249. votequorum_exec_send_quorum_notification(conn, req_lib_votequorum_trackstart->context);
  1250. }
  1251. /*
  1252. * Record requests for tracking
  1253. */
  1254. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  1255. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  1256. quorum_pd->track_flags = req_lib_votequorum_trackstart->track_flags;
  1257. quorum_pd->tracking_enabled = 1;
  1258. quorum_pd->tracking_context = req_lib_votequorum_trackstart->context;
  1259. list_add (&quorum_pd->list, &trackers_list);
  1260. }
  1261. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1262. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1263. res_lib_votequorum_status.header.error = CS_OK;
  1264. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1265. LEAVE();
  1266. }
  1267. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  1268. const void *message)
  1269. {
  1270. struct res_lib_votequorum_status res_lib_votequorum_status;
  1271. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1272. int error = CS_OK;
  1273. ENTER();
  1274. if (quorum_pd->tracking_enabled) {
  1275. error = CS_OK;
  1276. quorum_pd->tracking_enabled = 0;
  1277. list_del (&quorum_pd->list);
  1278. list_init (&quorum_pd->list);
  1279. } else {
  1280. error = CS_ERR_NOT_EXIST;
  1281. }
  1282. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1283. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1284. res_lib_votequorum_status.header.error = error;
  1285. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1286. LEAVE();
  1287. }
  1288. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  1289. static void message_handler_req_lib_votequorum_qdisk_register (void *conn,
  1290. const void *message)
  1291. {
  1292. const struct req_lib_votequorum_qdisk_register *req_lib_votequorum_qdisk_register = message;
  1293. struct res_lib_votequorum_status res_lib_votequorum_status;
  1294. cs_error_t error = CS_OK;
  1295. ENTER();
  1296. if (quorum_device) {
  1297. error = CS_ERR_EXIST;
  1298. } else {
  1299. quorum_device = allocate_node(0);
  1300. quorum_device->state = NODESTATE_DEAD;
  1301. quorum_device->votes = req_lib_votequorum_qdisk_register->votes;
  1302. strcpy(quorum_device_name, req_lib_votequorum_qdisk_register->name);
  1303. list_add(&quorum_device->list, &cluster_members_list);
  1304. }
  1305. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1306. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1307. res_lib_votequorum_status.header.error = error;
  1308. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1309. LEAVE();
  1310. }
  1311. static void message_handler_req_lib_votequorum_qdisk_unregister (void *conn,
  1312. const void *message)
  1313. {
  1314. struct res_lib_votequorum_status res_lib_votequorum_status;
  1315. cs_error_t error = CS_OK;
  1316. ENTER();
  1317. if (quorum_device) {
  1318. struct cluster_node *node = quorum_device;
  1319. quorum_device = NULL;
  1320. list_del(&node->list);
  1321. free(node);
  1322. recalculate_quorum(0, 0);
  1323. } else {
  1324. error = CS_ERR_NOT_EXIST;
  1325. }
  1326. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1327. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1328. res_lib_votequorum_status.header.error = error;
  1329. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1330. LEAVE();
  1331. }
  1332. static void message_handler_req_lib_votequorum_qdisk_poll (void *conn,
  1333. const void *message)
  1334. {
  1335. const struct req_lib_votequorum_qdisk_poll *req_lib_votequorum_qdisk_poll = message;
  1336. struct res_lib_votequorum_status res_lib_votequorum_status;
  1337. cs_error_t error = CS_OK;
  1338. ENTER();
  1339. if (quorum_device) {
  1340. if (req_lib_votequorum_qdisk_poll->state) {
  1341. quorum_device->last_hello = qb_util_nano_current_get ();
  1342. if (quorum_device->state == NODESTATE_DEAD) {
  1343. quorum_device->state = NODESTATE_MEMBER;
  1344. recalculate_quorum(0, 0);
  1345. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1346. quorum_device_timer_fn, &quorum_device_timer);
  1347. }
  1348. } else {
  1349. if (quorum_device->state == NODESTATE_MEMBER) {
  1350. quorum_device->state = NODESTATE_DEAD;
  1351. recalculate_quorum(0, 0);
  1352. corosync_api->timer_delete(quorum_device_timer);
  1353. }
  1354. }
  1355. } else {
  1356. error = CS_ERR_NOT_EXIST;
  1357. }
  1358. /*
  1359. * send status
  1360. */
  1361. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1362. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1363. res_lib_votequorum_status.header.error = error;
  1364. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1365. LEAVE();
  1366. }
  1367. static void message_handler_req_lib_votequorum_qdisk_getinfo (void *conn,
  1368. const void *message)
  1369. {
  1370. struct res_lib_votequorum_qdisk_getinfo res_lib_votequorum_qdisk_getinfo;
  1371. cs_error_t error = CS_OK;
  1372. ENTER();
  1373. if (quorum_device) {
  1374. log_printf(LOGSYS_LEVEL_DEBUG, "got qdisk_getinfo state %d", quorum_device->state);
  1375. res_lib_votequorum_qdisk_getinfo.votes = quorum_device->votes;
  1376. if (quorum_device->state == NODESTATE_MEMBER) {
  1377. res_lib_votequorum_qdisk_getinfo.state = 1;
  1378. } else {
  1379. res_lib_votequorum_qdisk_getinfo.state = 0;
  1380. }
  1381. strcpy(res_lib_votequorum_qdisk_getinfo.name, quorum_device_name);
  1382. } else {
  1383. error = CS_ERR_NOT_EXIST;
  1384. }
  1385. res_lib_votequorum_qdisk_getinfo.header.size = sizeof(res_lib_votequorum_qdisk_getinfo);
  1386. res_lib_votequorum_qdisk_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1387. res_lib_votequorum_qdisk_getinfo.header.error = error;
  1388. corosync_api->ipc_response_send(conn, &res_lib_votequorum_qdisk_getinfo, sizeof(res_lib_votequorum_qdisk_getinfo));
  1389. LEAVE();
  1390. }
  1391. #endif