votequorum.c 46 KB

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