votequorum.c 44 KB

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