4
0

votequorum.c 47 KB

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