votequorum.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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_flags;
  109. #define VOTEQUORUM_FLAG_FEATURE_TWONODE 1
  110. static int quorum;
  111. static int cluster_is_quorate;
  112. static int first_trans = 1;
  113. static unsigned int quorumdev_poll = DEFAULT_QDEV_POLL;
  114. static unsigned int leaving_timeout = DEFAULT_LEAVE_TMO;
  115. static uint8_t wait_for_all = 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. /* Load the library-servicing part of this module */
  344. api->service_link_and_init(api, "corosync_votequorum_iface", 0);
  345. LEAVE();
  346. }
  347. struct req_exec_quorum_nodeinfo {
  348. struct qb_ipc_request_header header __attribute__((aligned(8)));
  349. unsigned int first_trans;
  350. unsigned int votes;
  351. unsigned int expected_votes;
  352. unsigned int major_version; /* Not backwards compatible */
  353. unsigned int minor_version; /* Backwards compatible */
  354. unsigned int patch_version; /* Backwards/forwards compatible */
  355. unsigned int config_version;
  356. unsigned int flags;
  357. unsigned int wait_for_all;
  358. } __attribute__((packed));
  359. /*
  360. * Parameters for RECONFIG command
  361. */
  362. #define RECONFIG_PARAM_EXPECTED_VOTES 1
  363. #define RECONFIG_PARAM_NODE_VOTES 2
  364. #define RECONFIG_PARAM_LEAVING 3
  365. struct req_exec_quorum_reconfigure {
  366. struct qb_ipc_request_header header __attribute__((aligned(8)));
  367. unsigned int param;
  368. unsigned int nodeid;
  369. unsigned int value;
  370. };
  371. static void read_quorum_config(void)
  372. {
  373. uint8_t value = 0;
  374. int cluster_members = 0;
  375. struct list_head *tmp;
  376. ENTER();
  377. log_printf(LOGSYS_LEVEL_DEBUG, "Reading configuration\n");
  378. if (icmap_get_uint32("quorum.expected_votes", &us->expected_votes) != CS_OK) {
  379. us->expected_votes = DEFAULT_EXPECTED;
  380. }
  381. if (icmap_get_uint32("quorum.votes", &us->votes) != CS_OK) {
  382. us->votes = 1;
  383. }
  384. if (icmap_get_uint32("quorum.quorumdev_poll", &quorumdev_poll) != CS_OK) {
  385. quorumdev_poll = DEFAULT_QDEV_POLL;
  386. }
  387. if (icmap_get_uint32("quorum.leaving_timeout", &leaving_timeout) != CS_OK) {
  388. leaving_timeout = DEFAULT_LEAVE_TMO;
  389. }
  390. value = 0;
  391. icmap_get_uint8("quorum.two_node", &value);
  392. if (value) {
  393. quorum_flags |= VOTEQUORUM_FLAG_FEATURE_TWONODE;
  394. } else {
  395. quorum_flags &= ~VOTEQUORUM_FLAG_FEATURE_TWONODE;
  396. }
  397. /*
  398. * two_node mode is invalid if there are more than 2 nodes in the cluster!
  399. */
  400. list_iterate(tmp, &cluster_members_list) {
  401. cluster_members++;
  402. }
  403. if (quorum_flags & VOTEQUORUM_FLAG_FEATURE_TWONODE && cluster_members > 2) {
  404. log_printf(LOGSYS_LEVEL_WARNING, "quorum.two_node was set but there are more than 2 nodes in the cluster. It will be ignored.");
  405. quorum_flags &= ~VOTEQUORUM_FLAG_FEATURE_TWONODE;
  406. }
  407. LEAVE();
  408. }
  409. static int votequorum_exec_init_fn (struct corosync_api_v1 *api)
  410. {
  411. #ifdef COROSYNC_SOLARIS
  412. logsys_subsys_init();
  413. #endif
  414. ENTER();
  415. corosync_api = api;
  416. list_init(&cluster_members_list);
  417. list_init(&trackers_list);
  418. /*
  419. * Allocate a cluster_node for us
  420. */
  421. us = allocate_node(corosync_api->totem_nodeid_get());
  422. if (!us) {
  423. LEAVE();
  424. return (1);
  425. }
  426. us->flags |= NODE_FLAGS_US;
  427. us->state = NODESTATE_MEMBER;
  428. us->expected_votes = DEFAULT_EXPECTED;
  429. us->votes = 1;
  430. time(&us->join_time);
  431. read_quorum_config();
  432. recalculate_quorum(0, 0);
  433. /*
  434. * Listen for changes
  435. */
  436. add_votequorum_config_notification();
  437. /*
  438. * Start us off with one node
  439. */
  440. quorum_exec_send_nodeinfo();
  441. LEAVE();
  442. return (0);
  443. }
  444. static int quorum_lib_exit_fn (void *conn)
  445. {
  446. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  447. ENTER();
  448. if (quorum_pd->tracking_enabled) {
  449. list_del (&quorum_pd->list);
  450. list_init (&quorum_pd->list);
  451. }
  452. LEAVE();
  453. return (0);
  454. }
  455. static int send_quorum_notification(void *conn, uint64_t context)
  456. {
  457. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  458. struct list_head *tmp;
  459. struct cluster_node *node;
  460. int cluster_members = 0;
  461. int i = 0;
  462. int size;
  463. char *buf;
  464. ENTER();
  465. list_iterate(tmp, &cluster_members_list) {
  466. node = list_entry(tmp, struct cluster_node, list);
  467. cluster_members++;
  468. }
  469. if (quorum_device) {
  470. cluster_members++;
  471. }
  472. size = sizeof(struct res_lib_votequorum_notification) + sizeof(struct votequorum_node) * cluster_members;
  473. buf = alloca(size);
  474. if (!buf) {
  475. LEAVE();
  476. return -1;
  477. }
  478. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)buf;
  479. res_lib_votequorum_notification->quorate = cluster_is_quorate;
  480. res_lib_votequorum_notification->node_list_entries = cluster_members;
  481. res_lib_votequorum_notification->context = context;
  482. list_iterate(tmp, &cluster_members_list) {
  483. node = list_entry(tmp, struct cluster_node, list);
  484. res_lib_votequorum_notification->node_list[i].nodeid = node->node_id;
  485. res_lib_votequorum_notification->node_list[i++].state = node->state;
  486. }
  487. if (quorum_device) {
  488. res_lib_votequorum_notification->node_list[i].nodeid = 0;
  489. res_lib_votequorum_notification->node_list[i++].state = quorum_device->state | 0x80;
  490. }
  491. res_lib_votequorum_notification->header.id = MESSAGE_RES_VOTEQUORUM_NOTIFICATION;
  492. res_lib_votequorum_notification->header.size = size;
  493. res_lib_votequorum_notification->header.error = CS_OK;
  494. /* Send it to all interested parties */
  495. if (conn) {
  496. int ret = corosync_api->ipc_dispatch_send(conn, buf, size);
  497. LEAVE();
  498. return ret;
  499. } else {
  500. struct quorum_pd *qpd;
  501. list_iterate(tmp, &trackers_list) {
  502. qpd = list_entry(tmp, struct quorum_pd, list);
  503. res_lib_votequorum_notification->context = qpd->tracking_context;
  504. corosync_api->ipc_dispatch_send(qpd->conn, buf, size);
  505. }
  506. }
  507. LEAVE();
  508. return 0;
  509. }
  510. static void send_expectedvotes_notification(void)
  511. {
  512. struct res_lib_votequorum_expectedvotes_notification res_lib_votequorum_expectedvotes_notification;
  513. struct quorum_pd *qpd;
  514. struct list_head *tmp;
  515. ENTER();
  516. log_printf(LOGSYS_LEVEL_DEBUG, "Sending expected votes callback\n");
  517. res_lib_votequorum_expectedvotes_notification.header.id = MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION;
  518. res_lib_votequorum_expectedvotes_notification.header.size = sizeof(res_lib_votequorum_expectedvotes_notification);
  519. res_lib_votequorum_expectedvotes_notification.header.error = CS_OK;
  520. res_lib_votequorum_expectedvotes_notification.expected_votes = us->expected_votes;
  521. list_iterate(tmp, &trackers_list) {
  522. qpd = list_entry(tmp, struct quorum_pd, list);
  523. res_lib_votequorum_expectedvotes_notification.context = qpd->tracking_context;
  524. corosync_api->ipc_dispatch_send(qpd->conn, &res_lib_votequorum_expectedvotes_notification,
  525. sizeof(struct res_lib_votequorum_expectedvotes_notification));
  526. }
  527. LEAVE();
  528. }
  529. static void get_lowest_node_id(void)
  530. {
  531. struct cluster_node *node = NULL;
  532. struct list_head *tmp;
  533. ENTER();
  534. lowest_node_id = us->node_id;
  535. list_iterate(tmp, &cluster_members_list) {
  536. node = list_entry(tmp, struct cluster_node, list);
  537. if (node->node_id < lowest_node_id) {
  538. lowest_node_id = node->node_id;
  539. }
  540. }
  541. log_printf(LOGSYS_LEVEL_DEBUG, "lowest node id: %d us: %d\n", lowest_node_id, us->node_id);
  542. LEAVE();
  543. }
  544. static int check_low_node_id_partition(void)
  545. {
  546. struct cluster_node *node = NULL;
  547. struct list_head *tmp;
  548. int found = 0;
  549. ENTER();
  550. list_iterate(tmp, &cluster_members_list) {
  551. node = list_entry(tmp, struct cluster_node, list);
  552. if (node->state == NODESTATE_MEMBER) {
  553. if (node->node_id == lowest_node_id) {
  554. found = 1;
  555. }
  556. }
  557. }
  558. LEAVE();
  559. return found;
  560. }
  561. static void set_quorate(int total_votes)
  562. {
  563. int quorate;
  564. ENTER();
  565. /*
  566. * wait for all nodes to show up before granting quorum
  567. */
  568. if (wait_for_all) {
  569. if (total_votes != us->expected_votes) {
  570. log_printf(LOGSYS_LEVEL_NOTICE,
  571. "Waiting for all cluster members. "
  572. "Current votes: %d expected_votes: %d\n",
  573. total_votes, us->expected_votes);
  574. cluster_is_quorate = 0;
  575. return;
  576. }
  577. wait_for_all = 0;
  578. get_lowest_node_id();
  579. }
  580. if (quorum > total_votes) {
  581. quorate = 0;
  582. } else {
  583. quorate = 1;
  584. }
  585. if ((auto_tie_breaker) &&
  586. (total_votes == (us->expected_votes / 2)) &&
  587. (check_low_node_id_partition() == 1)) {
  588. quorate = 1;
  589. }
  590. if (cluster_is_quorate && !quorate) {
  591. log_printf(LOGSYS_LEVEL_INFO, "quorum lost, blocking activity\n");
  592. }
  593. if (!cluster_is_quorate && quorate) {
  594. log_printf(LOGSYS_LEVEL_INFO, "quorum regained, resuming activity\n");
  595. }
  596. cluster_is_quorate = quorate;
  597. LEAVE();
  598. }
  599. static int calculate_quorum(int allow_decrease, int max_expected, unsigned int *ret_total_votes)
  600. {
  601. struct list_head *nodelist;
  602. struct cluster_node *node;
  603. unsigned int total_votes = 0;
  604. unsigned int highest_expected = 0;
  605. unsigned int newquorum, q1, q2;
  606. unsigned int total_nodes = 0;
  607. ENTER();
  608. list_iterate(nodelist, &cluster_members_list) {
  609. node = list_entry(nodelist, struct cluster_node, list);
  610. log_printf(LOGSYS_LEVEL_DEBUG, "node %x state=%d, votes=%d, expected=%d\n",
  611. node->node_id, node->state, node->votes, node->expected_votes);
  612. if (node->state == NODESTATE_MEMBER) {
  613. if (max_expected) {
  614. node->expected_votes = max_expected;
  615. } else {
  616. highest_expected = max(highest_expected, node->expected_votes);
  617. }
  618. total_votes += node->votes;
  619. total_nodes++;
  620. }
  621. }
  622. if (quorum_device && quorum_device->state == NODESTATE_MEMBER) {
  623. total_votes += quorum_device->votes;
  624. }
  625. if (max_expected > 0) {
  626. highest_expected = max_expected;
  627. }
  628. /*
  629. * This quorum calculation is taken from the OpenVMS Cluster Systems
  630. * manual, but, then, you guessed that didn't you
  631. */
  632. q1 = (highest_expected + 2) / 2;
  633. q2 = (total_votes + 2) / 2;
  634. newquorum = max(q1, q2);
  635. /*
  636. * Normally quorum never decreases but the system administrator can
  637. * force it down by setting expected votes to a maximum value
  638. */
  639. if (!allow_decrease) {
  640. newquorum = max(quorum, newquorum);
  641. }
  642. /*
  643. * The special two_node mode allows each of the two nodes to retain
  644. * quorum if the other fails. Only one of the two should live past
  645. * fencing (as both nodes try to fence each other in split-brain.)
  646. * Also: if there are more than two nodes, force us inquorate to avoid
  647. * any damage or confusion.
  648. */
  649. if ((quorum_flags & VOTEQUORUM_FLAG_FEATURE_TWONODE) && total_nodes <= 2) {
  650. newquorum = 1;
  651. }
  652. if (ret_total_votes) {
  653. *ret_total_votes = total_votes;
  654. }
  655. LEAVE();
  656. return newquorum;
  657. }
  658. /* Recalculate cluster quorum, set quorate and notify changes */
  659. static void recalculate_quorum(int allow_decrease, int by_current_nodes)
  660. {
  661. unsigned int total_votes = 0;
  662. int cluster_members = 0;
  663. struct list_head *nodelist;
  664. struct cluster_node *node;
  665. ENTER();
  666. list_iterate(nodelist, &cluster_members_list) {
  667. node = list_entry(nodelist, struct cluster_node, list);
  668. if (node->state == NODESTATE_MEMBER) {
  669. if (by_current_nodes) {
  670. cluster_members++;
  671. }
  672. total_votes += node->votes;
  673. }
  674. }
  675. /*
  676. * Keep expected_votes at the highest number of votes in the cluster
  677. */
  678. log_printf(LOGSYS_LEVEL_DEBUG, "total_votes=%d, expected_votes=%d\n", total_votes, us->expected_votes);
  679. if (total_votes > us->expected_votes) {
  680. us->expected_votes = total_votes;
  681. send_expectedvotes_notification();
  682. }
  683. quorum = calculate_quorum(allow_decrease, cluster_members, &total_votes);
  684. set_quorate(total_votes);
  685. send_quorum_notification(NULL, 0L);
  686. LEAVE();
  687. }
  688. static void node_add_ordered(struct cluster_node *newnode)
  689. {
  690. struct cluster_node *node = NULL;
  691. struct list_head *tmp;
  692. struct list_head *newlist = &newnode->list;
  693. ENTER();
  694. list_iterate(tmp, &cluster_members_list) {
  695. node = list_entry(tmp, struct cluster_node, list);
  696. if (newnode->node_id < node->node_id) {
  697. break;
  698. }
  699. }
  700. if (!node) {
  701. list_add(&newnode->list, &cluster_members_list);
  702. } else {
  703. newlist->prev = tmp->prev;
  704. newlist->next = tmp;
  705. tmp->prev->next = newlist;
  706. tmp->prev = newlist;
  707. }
  708. LEAVE();
  709. }
  710. static struct cluster_node *allocate_node(int nodeid)
  711. {
  712. struct cluster_node *cl;
  713. ENTER();
  714. cl = malloc(sizeof(struct cluster_node));
  715. if (cl) {
  716. memset(cl, 0, sizeof(struct cluster_node));
  717. cl->node_id = nodeid;
  718. if (nodeid) {
  719. node_add_ordered(cl);
  720. }
  721. }
  722. LEAVE();
  723. return cl;
  724. }
  725. static struct cluster_node *find_node_by_nodeid(int nodeid)
  726. {
  727. struct cluster_node *node;
  728. struct list_head *tmp;
  729. ENTER();
  730. if (nodeid == NODEID_US) {
  731. LEAVE();
  732. return us;
  733. }
  734. if (nodeid == NODEID_QDEVICE) {
  735. LEAVE();
  736. return quorum_device;
  737. }
  738. list_iterate(tmp, &cluster_members_list) {
  739. node = list_entry(tmp, struct cluster_node, list);
  740. if (node->node_id == nodeid) {
  741. LEAVE();
  742. return node;
  743. }
  744. }
  745. LEAVE();
  746. return NULL;
  747. }
  748. static int quorum_exec_send_nodeinfo()
  749. {
  750. struct req_exec_quorum_nodeinfo req_exec_quorum_nodeinfo;
  751. struct iovec iov[1];
  752. int ret;
  753. ENTER();
  754. req_exec_quorum_nodeinfo.expected_votes = us->expected_votes;
  755. req_exec_quorum_nodeinfo.votes = us->votes;
  756. req_exec_quorum_nodeinfo.major_version = VOTEQUORUM_MAJOR_VERSION;
  757. req_exec_quorum_nodeinfo.minor_version = VOTEQUORUM_MINOR_VERSION;
  758. req_exec_quorum_nodeinfo.patch_version = VOTEQUORUM_PATCH_VERSION;
  759. req_exec_quorum_nodeinfo.flags = us->flags;
  760. req_exec_quorum_nodeinfo.first_trans = first_trans;
  761. req_exec_quorum_nodeinfo.wait_for_all = wait_for_all;
  762. req_exec_quorum_nodeinfo.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO);
  763. req_exec_quorum_nodeinfo.header.size = sizeof(req_exec_quorum_nodeinfo);
  764. iov[0].iov_base = (void *)&req_exec_quorum_nodeinfo;
  765. iov[0].iov_len = sizeof(req_exec_quorum_nodeinfo);
  766. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  767. LEAVE();
  768. return ret;
  769. }
  770. static int quorum_exec_send_reconfigure(int param, int nodeid, int value)
  771. {
  772. struct req_exec_quorum_reconfigure req_exec_quorum_reconfigure;
  773. struct iovec iov[1];
  774. int ret;
  775. ENTER();
  776. req_exec_quorum_reconfigure.param = param;
  777. req_exec_quorum_reconfigure.nodeid = nodeid;
  778. req_exec_quorum_reconfigure.value = value;
  779. req_exec_quorum_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE);
  780. req_exec_quorum_reconfigure.header.size = sizeof(req_exec_quorum_reconfigure);
  781. iov[0].iov_base = (void *)&req_exec_quorum_reconfigure;
  782. iov[0].iov_len = sizeof(req_exec_quorum_reconfigure);
  783. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  784. LEAVE();
  785. return ret;
  786. }
  787. static void lms_timer_fn(void *arg)
  788. {
  789. ENTER();
  790. last_man_standing_timer_set = 0;
  791. if (cluster_is_quorate) {
  792. recalculate_quorum(1,1);
  793. }
  794. LEAVE();
  795. }
  796. static void quorum_confchg_fn (
  797. enum totem_configuration_type configuration_type,
  798. const unsigned int *member_list, size_t member_list_entries,
  799. const unsigned int *left_list, size_t left_list_entries,
  800. const unsigned int *joined_list, size_t joined_list_entries,
  801. const struct memb_ring_id *ring_id)
  802. {
  803. int i;
  804. int leaving = 0;
  805. struct cluster_node *node;
  806. ENTER();
  807. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  808. return;
  809. }
  810. if (member_list_entries > 1) {
  811. first_trans = 0;
  812. }
  813. if (left_list_entries) {
  814. for (i = 0; i< left_list_entries; i++) {
  815. node = find_node_by_nodeid(left_list[i]);
  816. if (node) {
  817. if (node->state == NODESTATE_LEAVING) {
  818. leaving = 1;
  819. }
  820. node->state = NODESTATE_DEAD;
  821. node->flags |= NODE_FLAGS_BEENDOWN;
  822. }
  823. }
  824. }
  825. if (last_man_standing) {
  826. if ((member_list_entries >= quorum) && (left_list_entries)) {
  827. if (last_man_standing_timer_set) {
  828. corosync_api->timer_delete(last_man_standing_timer);
  829. last_man_standing_timer_set = 0;
  830. }
  831. corosync_api->timer_add_duration((unsigned long long)last_man_standing_window*1000000, NULL, lms_timer_fn, &last_man_standing_timer);
  832. last_man_standing_timer_set = 1;
  833. }
  834. }
  835. if (member_list_entries) {
  836. memcpy(quorum_members, member_list, sizeof(unsigned int) * member_list_entries);
  837. quorum_members_entries = member_list_entries;
  838. if (quorum_device) {
  839. quorum_members[quorum_members_entries++] = 0;
  840. }
  841. quorum_exec_send_nodeinfo();
  842. }
  843. if (left_list_entries) {
  844. recalculate_quorum(leaving, leaving);
  845. }
  846. memcpy(&quorum_ringid, ring_id, sizeof(*ring_id));
  847. set_quorum(quorum_members, quorum_members_entries, cluster_is_quorate, &quorum_ringid);
  848. LEAVE();
  849. }
  850. static void exec_votequorum_nodeinfo_endian_convert (void *message)
  851. {
  852. struct req_exec_quorum_nodeinfo *nodeinfo = message;
  853. ENTER();
  854. nodeinfo->votes = swab32(nodeinfo->votes);
  855. nodeinfo->expected_votes = swab32(nodeinfo->expected_votes);
  856. nodeinfo->major_version = swab32(nodeinfo->major_version);
  857. nodeinfo->minor_version = swab32(nodeinfo->minor_version);
  858. nodeinfo->patch_version = swab32(nodeinfo->patch_version);
  859. nodeinfo->config_version = swab32(nodeinfo->config_version);
  860. nodeinfo->flags = swab32(nodeinfo->flags);
  861. nodeinfo->wait_for_all = swab32(nodeinfo->wait_for_all);
  862. LEAVE();
  863. }
  864. static void exec_votequorum_reconfigure_endian_convert (void *message)
  865. {
  866. struct req_exec_quorum_reconfigure *reconfigure = message;
  867. ENTER();
  868. reconfigure->nodeid = swab32(reconfigure->nodeid);
  869. reconfigure->value = swab32(reconfigure->value);
  870. LEAVE();
  871. }
  872. static void message_handler_req_exec_votequorum_nodeinfo (
  873. const void *message,
  874. unsigned int nodeid)
  875. {
  876. const struct req_exec_quorum_nodeinfo *req_exec_quorum_nodeinfo = message;
  877. struct cluster_node *node;
  878. int old_votes;
  879. int old_expected;
  880. nodestate_t old_state;
  881. int new_node = 0;
  882. ENTER();
  883. log_printf(LOGSYS_LEVEL_DEBUG, "got nodeinfo message from cluster node %d\n", nodeid);
  884. node = find_node_by_nodeid(nodeid);
  885. if (!node) {
  886. node = allocate_node(nodeid);
  887. new_node = 1;
  888. }
  889. if (!node) {
  890. corosync_api->error_memory_failure();
  891. LEAVE();
  892. return;
  893. }
  894. old_votes = node->votes;
  895. old_expected = node->expected_votes;
  896. old_state = node->state;
  897. /* Update node state */
  898. node->votes = req_exec_quorum_nodeinfo->votes;
  899. node->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  900. node->state = NODESTATE_MEMBER;
  901. log_printf(LOGSYS_LEVEL_DEBUG, "nodeinfo message: votes: %d, expected: %d wfa: %d\n",
  902. req_exec_quorum_nodeinfo->votes,
  903. req_exec_quorum_nodeinfo->expected_votes,
  904. req_exec_quorum_nodeinfo->wait_for_all);
  905. if ((last_man_standing) && (req_exec_quorum_nodeinfo->votes > 1)) {
  906. log_printf(LOGSYS_LEVEL_WARNING, "Last Man Standing feature is supported only when all"
  907. "cluster nodes votes are set to 1. Disabling LMS.");
  908. last_man_standing = 0;
  909. if (last_man_standing_timer_set) {
  910. corosync_api->timer_delete(last_man_standing_timer);
  911. last_man_standing_timer_set = 0;
  912. }
  913. }
  914. node->flags &= ~NODE_FLAGS_BEENDOWN;
  915. if (new_node ||
  916. req_exec_quorum_nodeinfo->first_trans ||
  917. old_votes != node->votes ||
  918. old_expected != node->expected_votes ||
  919. old_state != node->state) {
  920. recalculate_quorum(0, 0);
  921. }
  922. if (!nodeid) {
  923. free(node);
  924. }
  925. if ((wait_for_all) && (!req_exec_quorum_nodeinfo->wait_for_all)) {
  926. wait_for_all = 0;
  927. }
  928. LEAVE();
  929. }
  930. static void message_handler_req_exec_votequorum_reconfigure (
  931. const void *message,
  932. unsigned int nodeid)
  933. {
  934. const struct req_exec_quorum_reconfigure *req_exec_quorum_reconfigure = message;
  935. struct cluster_node *node;
  936. struct list_head *nodelist;
  937. ENTER();
  938. log_printf(LOGSYS_LEVEL_DEBUG, "got reconfigure message from cluster node %d\n", nodeid);
  939. node = find_node_by_nodeid(req_exec_quorum_reconfigure->nodeid);
  940. if (!node) {
  941. LEAVE();
  942. return;
  943. }
  944. switch(req_exec_quorum_reconfigure->param)
  945. {
  946. case RECONFIG_PARAM_EXPECTED_VOTES:
  947. list_iterate(nodelist, &cluster_members_list) {
  948. node = list_entry(nodelist, struct cluster_node, list);
  949. if (node->state == NODESTATE_MEMBER &&
  950. node->expected_votes > req_exec_quorum_reconfigure->value) {
  951. node->expected_votes = req_exec_quorum_reconfigure->value;
  952. }
  953. }
  954. send_expectedvotes_notification();
  955. recalculate_quorum(1, 0); /* Allow decrease */
  956. break;
  957. case RECONFIG_PARAM_NODE_VOTES:
  958. node->votes = req_exec_quorum_reconfigure->value;
  959. recalculate_quorum(1, 0); /* Allow decrease */
  960. break;
  961. case RECONFIG_PARAM_LEAVING:
  962. if (req_exec_quorum_reconfigure->value == 1 && node->state == NODESTATE_MEMBER) {
  963. node->state = NODESTATE_LEAVING;
  964. }
  965. if (req_exec_quorum_reconfigure->value == 0 && node->state == NODESTATE_LEAVING) {
  966. node->state = NODESTATE_MEMBER;
  967. }
  968. break;
  969. }
  970. LEAVE();
  971. }
  972. static int quorum_lib_init_fn (void *conn)
  973. {
  974. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  975. ENTER();
  976. list_init (&pd->list);
  977. pd->conn = conn;
  978. LEAVE();
  979. return (0);
  980. }
  981. /*
  982. * Someone called votequorum_leave AGES ago!
  983. * Assume they forgot to shut down the node.
  984. */
  985. static void leaving_timer_fn(void *arg)
  986. {
  987. ENTER();
  988. if (us->state == NODESTATE_LEAVING) {
  989. us->state = NODESTATE_MEMBER;
  990. }
  991. /*
  992. * Tell everyone else we made a mistake
  993. */
  994. quorum_exec_send_reconfigure(RECONFIG_PARAM_LEAVING, us->node_id, 0);
  995. LEAVE();
  996. }
  997. /*
  998. * Message from the library
  999. */
  1000. static void message_handler_req_lib_votequorum_getinfo (void *conn, const void *message)
  1001. {
  1002. const struct req_lib_votequorum_getinfo *req_lib_votequorum_getinfo = message;
  1003. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  1004. struct cluster_node *node;
  1005. unsigned int highest_expected = 0;
  1006. unsigned int total_votes = 0;
  1007. cs_error_t error = CS_OK;
  1008. ENTER();
  1009. log_printf(LOGSYS_LEVEL_DEBUG, "got getinfo request on %p for node %d\n", conn, req_lib_votequorum_getinfo->nodeid);
  1010. node = find_node_by_nodeid(req_lib_votequorum_getinfo->nodeid);
  1011. if (node) {
  1012. struct cluster_node *iternode;
  1013. struct list_head *nodelist;
  1014. list_iterate(nodelist, &cluster_members_list) {
  1015. iternode = list_entry(nodelist, struct cluster_node, list);
  1016. if (iternode->state == NODESTATE_MEMBER) {
  1017. highest_expected =
  1018. max(highest_expected, iternode->expected_votes);
  1019. total_votes += iternode->votes;
  1020. }
  1021. }
  1022. if (quorum_device && quorum_device->state == NODESTATE_MEMBER) {
  1023. total_votes += quorum_device->votes;
  1024. }
  1025. res_lib_votequorum_getinfo.votes = us->votes;
  1026. res_lib_votequorum_getinfo.expected_votes = us->expected_votes;
  1027. res_lib_votequorum_getinfo.highest_expected = highest_expected;
  1028. res_lib_votequorum_getinfo.quorum = quorum;
  1029. res_lib_votequorum_getinfo.total_votes = total_votes;
  1030. res_lib_votequorum_getinfo.flags = 0;
  1031. res_lib_votequorum_getinfo.nodeid = node->node_id;
  1032. if (us->flags & NODE_FLAGS_HASSTATE) {
  1033. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_HASSTATE;
  1034. }
  1035. if (quorum_flags & VOTEQUORUM_FLAG_FEATURE_TWONODE) {
  1036. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_TWONODE;
  1037. }
  1038. if (cluster_is_quorate) {
  1039. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_QUORATE;
  1040. }
  1041. } else {
  1042. error = CS_ERR_NOT_EXIST;
  1043. }
  1044. res_lib_votequorum_getinfo.header.size = sizeof(res_lib_votequorum_getinfo);
  1045. res_lib_votequorum_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1046. res_lib_votequorum_getinfo.header.error = error;
  1047. corosync_api->ipc_response_send(conn, &res_lib_votequorum_getinfo, sizeof(res_lib_votequorum_getinfo));
  1048. log_printf(LOGSYS_LEVEL_DEBUG, "getinfo response error: %d\n", error);
  1049. LEAVE();
  1050. }
  1051. /*
  1052. * Message from the library
  1053. */
  1054. static void message_handler_req_lib_votequorum_setexpected (void *conn, const void *message)
  1055. {
  1056. const struct req_lib_votequorum_setexpected *req_lib_votequorum_setexpected = message;
  1057. struct res_lib_votequorum_status res_lib_votequorum_status;
  1058. cs_error_t error = CS_OK;
  1059. unsigned int newquorum;
  1060. unsigned int total_votes;
  1061. ENTER();
  1062. /*
  1063. * Validate new expected votes
  1064. */
  1065. newquorum = calculate_quorum(1, req_lib_votequorum_setexpected->expected_votes, &total_votes);
  1066. if (newquorum < total_votes / 2 ||
  1067. newquorum > total_votes) {
  1068. error = CS_ERR_INVALID_PARAM;
  1069. goto error_exit;
  1070. }
  1071. quorum_exec_send_reconfigure(RECONFIG_PARAM_EXPECTED_VOTES, us->node_id,
  1072. req_lib_votequorum_setexpected->expected_votes);
  1073. /*
  1074. * send status
  1075. */
  1076. error_exit:
  1077. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1078. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1079. res_lib_votequorum_status.header.error = error;
  1080. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1081. LEAVE();
  1082. }
  1083. /*
  1084. * Message from the library
  1085. */
  1086. static void message_handler_req_lib_votequorum_setvotes (void *conn, const void *message)
  1087. {
  1088. const struct req_lib_votequorum_setvotes *req_lib_votequorum_setvotes = message;
  1089. struct res_lib_votequorum_status res_lib_votequorum_status;
  1090. struct cluster_node *node;
  1091. unsigned int newquorum;
  1092. unsigned int total_votes;
  1093. unsigned int saved_votes;
  1094. cs_error_t error = CS_OK;
  1095. unsigned int nodeid;
  1096. ENTER();
  1097. nodeid = req_lib_votequorum_setvotes->nodeid;
  1098. node = find_node_by_nodeid(nodeid);
  1099. if (!node) {
  1100. error = CS_ERR_NAME_NOT_FOUND;
  1101. goto error_exit;
  1102. }
  1103. /*
  1104. * Check votes is valid
  1105. */
  1106. saved_votes = node->votes;
  1107. node->votes = req_lib_votequorum_setvotes->votes;
  1108. newquorum = calculate_quorum(1, 0, &total_votes);
  1109. if (newquorum < total_votes / 2 ||
  1110. newquorum > total_votes) {
  1111. node->votes = saved_votes;
  1112. error = CS_ERR_INVALID_PARAM;
  1113. goto error_exit;
  1114. }
  1115. if (!nodeid) {
  1116. nodeid = corosync_api->totem_nodeid_get();
  1117. }
  1118. quorum_exec_send_reconfigure(RECONFIG_PARAM_NODE_VOTES, nodeid,
  1119. req_lib_votequorum_setvotes->votes);
  1120. /*
  1121. * send status
  1122. */
  1123. error_exit:
  1124. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1125. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1126. res_lib_votequorum_status.header.error = error;
  1127. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1128. LEAVE();
  1129. }
  1130. static void message_handler_req_lib_votequorum_leaving (void *conn, const void *message)
  1131. {
  1132. struct res_lib_votequorum_status res_lib_votequorum_status;
  1133. cs_error_t error = CS_OK;
  1134. ENTER();
  1135. quorum_exec_send_reconfigure(RECONFIG_PARAM_LEAVING, us->node_id, 1);
  1136. /*
  1137. * If we don't shut down in a sensible amount of time then cancel the
  1138. * leave status.
  1139. */
  1140. if (leaving_timeout) {
  1141. corosync_api->timer_add_duration((unsigned long long)leaving_timeout*1000000, NULL,
  1142. leaving_timer_fn, &leaving_timer);
  1143. }
  1144. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1145. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1146. res_lib_votequorum_status.header.error = error;
  1147. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1148. LEAVE();
  1149. }
  1150. static void quorum_device_timer_fn(void *arg)
  1151. {
  1152. ENTER();
  1153. if (!quorum_device || quorum_device->state == NODESTATE_DEAD) {
  1154. LEAVE();
  1155. return;
  1156. }
  1157. if ((quorum_device->last_hello / QB_TIME_NS_IN_SEC) + quorumdev_poll/1000 <
  1158. (qb_util_nano_current_get () / QB_TIME_NS_IN_SEC)) {
  1159. quorum_device->state = NODESTATE_DEAD;
  1160. log_printf(LOGSYS_LEVEL_INFO, "lost contact with quorum device\n");
  1161. recalculate_quorum(0, 0);
  1162. } else {
  1163. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1164. quorum_device_timer_fn, &quorum_device_timer);
  1165. }
  1166. LEAVE();
  1167. }
  1168. static void message_handler_req_lib_votequorum_qdisk_register (void *conn,
  1169. const void *message)
  1170. {
  1171. const struct req_lib_votequorum_qdisk_register *req_lib_votequorum_qdisk_register = message;
  1172. struct res_lib_votequorum_status res_lib_votequorum_status;
  1173. cs_error_t error = CS_OK;
  1174. ENTER();
  1175. if (quorum_device) {
  1176. error = CS_ERR_EXIST;
  1177. } else {
  1178. quorum_device = allocate_node(0);
  1179. quorum_device->state = NODESTATE_DEAD;
  1180. quorum_device->votes = req_lib_votequorum_qdisk_register->votes;
  1181. strcpy(quorum_device_name, req_lib_votequorum_qdisk_register->name);
  1182. list_add(&quorum_device->list, &cluster_members_list);
  1183. }
  1184. /*
  1185. * send status
  1186. */
  1187. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1188. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1189. res_lib_votequorum_status.header.error = error;
  1190. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1191. LEAVE();
  1192. }
  1193. static void message_handler_req_lib_votequorum_qdisk_unregister (void *conn,
  1194. const void *message)
  1195. {
  1196. struct res_lib_votequorum_status res_lib_votequorum_status;
  1197. cs_error_t error = CS_OK;
  1198. ENTER();
  1199. if (quorum_device) {
  1200. struct cluster_node *node = quorum_device;
  1201. quorum_device = NULL;
  1202. list_del(&node->list);
  1203. free(node);
  1204. recalculate_quorum(0, 0);
  1205. } else {
  1206. error = CS_ERR_NOT_EXIST;
  1207. }
  1208. /*
  1209. * send status
  1210. */
  1211. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1212. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1213. res_lib_votequorum_status.header.error = error;
  1214. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1215. LEAVE();
  1216. }
  1217. static void message_handler_req_lib_votequorum_qdisk_poll (void *conn,
  1218. const void *message)
  1219. {
  1220. const struct req_lib_votequorum_qdisk_poll *req_lib_votequorum_qdisk_poll = message;
  1221. struct res_lib_votequorum_status res_lib_votequorum_status;
  1222. cs_error_t error = CS_OK;
  1223. ENTER();
  1224. if (quorum_device) {
  1225. if (req_lib_votequorum_qdisk_poll->state) {
  1226. quorum_device->last_hello = qb_util_nano_current_get ();
  1227. if (quorum_device->state == NODESTATE_DEAD) {
  1228. quorum_device->state = NODESTATE_MEMBER;
  1229. recalculate_quorum(0, 0);
  1230. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1231. quorum_device_timer_fn, &quorum_device_timer);
  1232. }
  1233. } else {
  1234. if (quorum_device->state == NODESTATE_MEMBER) {
  1235. quorum_device->state = NODESTATE_DEAD;
  1236. recalculate_quorum(0, 0);
  1237. corosync_api->timer_delete(quorum_device_timer);
  1238. }
  1239. }
  1240. } else {
  1241. error = CS_ERR_NOT_EXIST;
  1242. }
  1243. /*
  1244. * send status
  1245. */
  1246. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1247. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1248. res_lib_votequorum_status.header.error = error;
  1249. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1250. LEAVE();
  1251. }
  1252. static void message_handler_req_lib_votequorum_qdisk_getinfo (void *conn,
  1253. const void *message)
  1254. {
  1255. struct res_lib_votequorum_qdisk_getinfo res_lib_votequorum_qdisk_getinfo;
  1256. cs_error_t error = CS_OK;
  1257. ENTER();
  1258. if (quorum_device) {
  1259. log_printf(LOGSYS_LEVEL_DEBUG, "got qdisk_getinfo state %d\n", quorum_device->state);
  1260. res_lib_votequorum_qdisk_getinfo.votes = quorum_device->votes;
  1261. if (quorum_device->state == NODESTATE_MEMBER) {
  1262. res_lib_votequorum_qdisk_getinfo.state = 1;
  1263. } else {
  1264. res_lib_votequorum_qdisk_getinfo.state = 0;
  1265. }
  1266. strcpy(res_lib_votequorum_qdisk_getinfo.name, quorum_device_name);
  1267. } else {
  1268. error = CS_ERR_NOT_EXIST;
  1269. }
  1270. /*
  1271. * send status
  1272. */
  1273. res_lib_votequorum_qdisk_getinfo.header.size = sizeof(res_lib_votequorum_qdisk_getinfo);
  1274. res_lib_votequorum_qdisk_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1275. res_lib_votequorum_qdisk_getinfo.header.error = error;
  1276. corosync_api->ipc_response_send(conn, &res_lib_votequorum_qdisk_getinfo, sizeof(res_lib_votequorum_qdisk_getinfo));
  1277. LEAVE();
  1278. }
  1279. static void message_handler_req_lib_votequorum_setstate (void *conn,
  1280. const void *message)
  1281. {
  1282. struct res_lib_votequorum_status res_lib_votequorum_status;
  1283. cs_error_t error = CS_OK;
  1284. ENTER();
  1285. us->flags |= NODE_FLAGS_HASSTATE;
  1286. /*
  1287. * send status
  1288. */
  1289. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1290. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1291. res_lib_votequorum_status.header.error = error;
  1292. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1293. LEAVE();
  1294. }
  1295. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  1296. const void *message)
  1297. {
  1298. const struct req_lib_votequorum_trackstart *req_lib_votequorum_trackstart = message;
  1299. struct res_lib_votequorum_status res_lib_votequorum_status;
  1300. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1301. ENTER();
  1302. /*
  1303. * If an immediate listing of the current cluster membership
  1304. * is requested, generate membership list
  1305. */
  1306. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  1307. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  1308. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p\n", conn);
  1309. send_quorum_notification(conn, req_lib_votequorum_trackstart->context);
  1310. }
  1311. /*
  1312. * Record requests for tracking
  1313. */
  1314. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  1315. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  1316. quorum_pd->track_flags = req_lib_votequorum_trackstart->track_flags;
  1317. quorum_pd->tracking_enabled = 1;
  1318. quorum_pd->tracking_context = req_lib_votequorum_trackstart->context;
  1319. list_add (&quorum_pd->list, &trackers_list);
  1320. }
  1321. /*
  1322. * Send status
  1323. */
  1324. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1325. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1326. res_lib_votequorum_status.header.error = CS_OK;
  1327. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1328. LEAVE();
  1329. }
  1330. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  1331. const void *message)
  1332. {
  1333. struct res_lib_votequorum_status res_lib_votequorum_status;
  1334. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1335. int error = CS_OK;
  1336. ENTER();
  1337. if (quorum_pd->tracking_enabled) {
  1338. error = CS_OK;
  1339. quorum_pd->tracking_enabled = 0;
  1340. list_del (&quorum_pd->list);
  1341. list_init (&quorum_pd->list);
  1342. } else {
  1343. error = CS_ERR_NOT_EXIST;
  1344. }
  1345. /*
  1346. * send status
  1347. */
  1348. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1349. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1350. res_lib_votequorum_status.header.error = error;
  1351. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1352. LEAVE();
  1353. }
  1354. static void reread_config(void)
  1355. {
  1356. unsigned int old_votes;
  1357. unsigned int old_expected;
  1358. ENTER();
  1359. old_votes = us->votes;
  1360. old_expected = us->expected_votes;
  1361. /*
  1362. * Reload the configuration
  1363. */
  1364. read_quorum_config();
  1365. /*
  1366. * Check for fundamental changes that we need to propogate
  1367. */
  1368. if (old_votes != us->votes) {
  1369. quorum_exec_send_reconfigure(RECONFIG_PARAM_NODE_VOTES, us->node_id, us->votes);
  1370. }
  1371. if (old_expected != us->expected_votes) {
  1372. quorum_exec_send_reconfigure(RECONFIG_PARAM_EXPECTED_VOTES, us->node_id, us->expected_votes);
  1373. }
  1374. LEAVE();
  1375. }
  1376. static void key_change_quorum(
  1377. int32_t event,
  1378. const char *key_name,
  1379. struct icmap_notify_value new_val,
  1380. struct icmap_notify_value old_val,
  1381. void *user_data)
  1382. {
  1383. ENTER();
  1384. reread_config();
  1385. LEAVE();
  1386. }
  1387. static void add_votequorum_config_notification(void)
  1388. {
  1389. icmap_track_t icmap_track;
  1390. ENTER();
  1391. icmap_track_add("quorum.",
  1392. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  1393. key_change_quorum,
  1394. NULL,
  1395. &icmap_track);
  1396. LEAVE();
  1397. }