votequorum.c 50 KB

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