votequorum.c 49 KB

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