votequorum.c 47 KB

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