votequorum.c 44 KB

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