votequorum.c 46 KB

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