votequorum.c 49 KB

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