votequorum.c 49 KB

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