votequorum.c 44 KB

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