votequorum.c 46 KB

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