votequorum.c 49 KB

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