votequorum.c 50 KB

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