votequorum.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. /*
  2. * Copyright (c) 2009-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Authors: Christine Caulfield (ccaulfie@redhat.com)
  7. * Fabio M. Di Nitto (fdinitto@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <sys/types.h>
  37. #include <stdint.h>
  38. #include <qb/qbipc_common.h>
  39. #include <qb/qbdefs.h>
  40. #include <qb/qbutil.h>
  41. #include <corosync/corodefs.h>
  42. #include <corosync/list.h>
  43. #include <corosync/logsys.h>
  44. #include <corosync/coroapi.h>
  45. #include <corosync/engine/quorum.h>
  46. #include <corosync/icmap.h>
  47. #include <corosync/ipc_votequorum.h>
  48. #include "service.h"
  49. LOGSYS_DECLARE_SUBSYS ("VOTEQ");
  50. /*
  51. * interface with corosync
  52. */
  53. static struct corosync_api_v1 *corosync_api;
  54. /*
  55. * votequorum global config vars
  56. */
  57. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  58. #define DEFAULT_QDEV_POLL 10000
  59. static unsigned int quorumdev_poll = DEFAULT_QDEV_POLL;
  60. static char quorum_device_name[VOTEQUORUM_MAX_QDEVICE_NAME_LEN];
  61. #endif
  62. static uint8_t two_node = 0;
  63. static uint8_t wait_for_all = 0;
  64. static uint8_t wait_for_all_status = 0;
  65. static uint8_t auto_tie_breaker = 0;
  66. static int lowest_node_id = -1;
  67. #define DEFAULT_LMS_WIN 10000
  68. static uint8_t last_man_standing = 0;
  69. static uint32_t last_man_standing_window = DEFAULT_LMS_WIN;
  70. static uint8_t leave_remove = 0;
  71. static uint32_t ev_barrier = 0;
  72. /*
  73. * votequorum_exec defines/structs/forward definitions
  74. */
  75. struct req_exec_quorum_nodeinfo {
  76. struct qb_ipc_request_header header __attribute__((aligned(8)));
  77. uint32_t votes;
  78. uint32_t expected_votes;
  79. uint16_t flags;
  80. uint8_t quorate;
  81. uint8_t wait_for_all_status;
  82. uint8_t first_trans;
  83. uint8_t _pad0;
  84. uint8_t _pad1;
  85. uint8_t _pad2;
  86. } __attribute__((packed));
  87. struct req_exec_quorum_reconfigure {
  88. struct qb_ipc_request_header header __attribute__((aligned(8)));
  89. uint32_t nodeid;
  90. uint32_t value;
  91. uint8_t param;
  92. uint8_t _pad0;
  93. uint8_t _pad1;
  94. uint8_t _pad2;
  95. } __attribute__((packed));
  96. /*
  97. * votequorum_exec onwire version (via totem)
  98. */
  99. #include "votequorum.h"
  100. /*
  101. * votequorum_exec onwire messages (via totem)
  102. */
  103. #define MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO 0
  104. #define MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE 1
  105. static void votequorum_exec_send_expectedvotes_notification(void);
  106. static int votequorum_exec_send_quorum_notification(void *conn, uint64_t context);
  107. #define VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES 1
  108. #define VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES 2
  109. static int votequorum_exec_send_reconfigure(uint8_t param, unsigned int nodeid, uint32_t value);
  110. /*
  111. * votequorum internal node status/view
  112. */
  113. #define NODE_FLAGS_LEAVING 1
  114. #define NODEID_US 0
  115. #define NODEID_QDEVICE UINT32_MAX
  116. typedef enum {
  117. NODESTATE_MEMBER=1,
  118. NODESTATE_DEAD,
  119. NODESTATE_LEAVING
  120. } nodestate_t;
  121. struct cluster_node {
  122. int node_id;
  123. nodestate_t state;
  124. uint32_t votes;
  125. uint32_t expected_votes;
  126. uint16_t flags;
  127. unsigned long long int last_hello; /* Only used for quorum devices */
  128. struct list_head list;
  129. };
  130. /*
  131. * votequorum internal quorum status
  132. */
  133. static uint8_t quorum;
  134. static uint8_t cluster_is_quorate;
  135. static uint8_t first_trans = 1;
  136. /*
  137. * votequorum membership data
  138. */
  139. static struct cluster_node *us;
  140. static struct cluster_node *quorum_device = NULL;
  141. static struct list_head cluster_members_list;
  142. static unsigned int quorum_members[PROCESSOR_COUNT_MAX+1];
  143. static int quorum_members_entries = 0;
  144. static struct memb_ring_id quorum_ringid;
  145. /*
  146. * votequorum tracking
  147. */
  148. struct quorum_pd {
  149. unsigned char track_flags;
  150. int tracking_enabled;
  151. uint64_t tracking_context;
  152. struct list_head list;
  153. void *conn;
  154. };
  155. static struct list_head trackers_list;
  156. /*
  157. * votequorum timers
  158. */
  159. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  160. static corosync_timer_handle_t quorum_device_timer;
  161. #endif
  162. static corosync_timer_handle_t last_man_standing_timer;
  163. static int last_man_standing_timer_set = 0;
  164. /*
  165. * Service Interfaces required by service_message_handler struct
  166. */
  167. static void votequorum_confchg_fn (
  168. enum totem_configuration_type configuration_type,
  169. const unsigned int *member_list, size_t member_list_entries,
  170. const unsigned int *left_list, size_t left_list_entries,
  171. const unsigned int *joined_list, size_t joined_list_entries,
  172. const struct memb_ring_id *ring_id);
  173. static quorum_set_quorate_fn_t quorum_callback;
  174. /*
  175. * votequorum_exec handler and definitions
  176. */
  177. static char *votequorum_exec_init_fn (struct corosync_api_v1 *api);
  178. static int votequorum_exec_exit_fn (void);
  179. static void message_handler_req_exec_votequorum_nodeinfo (
  180. const void *message,
  181. unsigned int nodeid);
  182. static void exec_votequorum_nodeinfo_endian_convert (void *message);
  183. static void message_handler_req_exec_votequorum_reconfigure (
  184. const void *message,
  185. unsigned int nodeid);
  186. static void exec_votequorum_reconfigure_endian_convert (void *message);
  187. static struct corosync_exec_handler votequorum_exec_engine[] =
  188. {
  189. { /* 0 */
  190. .exec_handler_fn = message_handler_req_exec_votequorum_nodeinfo,
  191. .exec_endian_convert_fn = exec_votequorum_nodeinfo_endian_convert
  192. },
  193. { /* 1 */
  194. .exec_handler_fn = message_handler_req_exec_votequorum_reconfigure,
  195. .exec_endian_convert_fn = exec_votequorum_reconfigure_endian_convert
  196. },
  197. };
  198. /*
  199. * Library Handler and Functions Definitions
  200. */
  201. static int quorum_lib_init_fn (void *conn);
  202. static int quorum_lib_exit_fn (void *conn);
  203. static void message_handler_req_lib_votequorum_getinfo (void *conn,
  204. const void *message);
  205. static void message_handler_req_lib_votequorum_setexpected (void *conn,
  206. const void *message);
  207. static void message_handler_req_lib_votequorum_setvotes (void *conn,
  208. const void *message);
  209. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  210. const void *message);
  211. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  212. const void *message);
  213. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  214. static void message_handler_req_lib_votequorum_qdevice_register (void *conn,
  215. const void *message);
  216. static void message_handler_req_lib_votequorum_qdevice_unregister (void *conn,
  217. const void *message);
  218. static void message_handler_req_lib_votequorum_qdevice_poll (void *conn,
  219. const void *message);
  220. static void message_handler_req_lib_votequorum_qdevice_getinfo (void *conn,
  221. const void *message);
  222. #endif
  223. static struct corosync_lib_handler quorum_lib_service[] =
  224. {
  225. { /* 0 */
  226. .lib_handler_fn = message_handler_req_lib_votequorum_getinfo,
  227. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  228. },
  229. { /* 1 */
  230. .lib_handler_fn = message_handler_req_lib_votequorum_setexpected,
  231. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  232. },
  233. { /* 2 */
  234. .lib_handler_fn = message_handler_req_lib_votequorum_setvotes,
  235. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  236. },
  237. { /* 3 */
  238. .lib_handler_fn = message_handler_req_lib_votequorum_trackstart,
  239. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  240. },
  241. { /* 4 */
  242. .lib_handler_fn = message_handler_req_lib_votequorum_trackstop,
  243. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  244. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  245. },
  246. { /* 5 */
  247. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_register,
  248. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  249. },
  250. { /* 6 */
  251. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_unregister,
  252. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  253. },
  254. { /* 7 */
  255. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_poll,
  256. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  257. },
  258. { /* 8 */
  259. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_getinfo,
  260. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  261. #endif
  262. }
  263. };
  264. static struct corosync_service_engine votequorum_service_engine = {
  265. .name = "corosync vote quorum service v1.0",
  266. .id = VOTEQUORUM_SERVICE,
  267. .priority = 2,
  268. .private_data_size = sizeof (struct quorum_pd),
  269. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  270. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED,
  271. .lib_init_fn = quorum_lib_init_fn,
  272. .lib_exit_fn = quorum_lib_exit_fn,
  273. .lib_engine = quorum_lib_service,
  274. .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler),
  275. .exec_init_fn = votequorum_exec_init_fn,
  276. .exec_exit_fn = votequorum_exec_exit_fn,
  277. .exec_engine = votequorum_exec_engine,
  278. .exec_engine_count = sizeof (votequorum_exec_engine) / sizeof (struct corosync_exec_handler),
  279. .confchg_fn = votequorum_confchg_fn,
  280. .sync_mode = CS_SYNC_V1
  281. };
  282. struct corosync_service_engine *votequorum_get_service_engine_ver0 (void)
  283. {
  284. return (&votequorum_service_engine);
  285. }
  286. static struct default_service votequorum_service[] = {
  287. {
  288. .name = "corosync_votequorum",
  289. .ver = 0,
  290. .loader = votequorum_get_service_engine_ver0
  291. },
  292. };
  293. /*
  294. * common/utility macros/functions
  295. */
  296. #define max(a,b) (((a) > (b)) ? (a) : (b))
  297. #define list_iterate(v, head) \
  298. for (v = (head)->next; v != head; v = v->next)
  299. static void node_add_ordered(struct cluster_node *newnode)
  300. {
  301. struct cluster_node *node = NULL;
  302. struct list_head *tmp;
  303. struct list_head *newlist = &newnode->list;
  304. ENTER();
  305. list_iterate(tmp, &cluster_members_list) {
  306. node = list_entry(tmp, struct cluster_node, list);
  307. if (newnode->node_id < node->node_id) {
  308. break;
  309. }
  310. }
  311. if (!node) {
  312. list_add(&newnode->list, &cluster_members_list);
  313. } else {
  314. newlist->prev = tmp->prev;
  315. newlist->next = tmp;
  316. tmp->prev->next = newlist;
  317. tmp->prev = newlist;
  318. }
  319. LEAVE();
  320. }
  321. static struct cluster_node *allocate_node(unsigned int nodeid)
  322. {
  323. struct cluster_node *cl;
  324. ENTER();
  325. cl = malloc(sizeof(struct cluster_node));
  326. if (cl) {
  327. memset(cl, 0, sizeof(struct cluster_node));
  328. cl->node_id = nodeid;
  329. if (nodeid) {
  330. node_add_ordered(cl);
  331. }
  332. }
  333. LEAVE();
  334. return cl;
  335. }
  336. static struct cluster_node *find_node_by_nodeid(unsigned int nodeid)
  337. {
  338. struct cluster_node *node;
  339. struct list_head *tmp;
  340. ENTER();
  341. if (nodeid == NODEID_US) {
  342. LEAVE();
  343. return us;
  344. }
  345. if (nodeid == NODEID_QDEVICE) {
  346. LEAVE();
  347. return quorum_device;
  348. }
  349. list_iterate(tmp, &cluster_members_list) {
  350. node = list_entry(tmp, struct cluster_node, list);
  351. if (node->node_id == nodeid) {
  352. LEAVE();
  353. return node;
  354. }
  355. }
  356. LEAVE();
  357. return NULL;
  358. }
  359. static void get_lowest_node_id(void)
  360. {
  361. struct cluster_node *node = NULL;
  362. struct list_head *tmp;
  363. ENTER();
  364. lowest_node_id = us->node_id;
  365. list_iterate(tmp, &cluster_members_list) {
  366. node = list_entry(tmp, struct cluster_node, list);
  367. if ((node->state == NODESTATE_MEMBER) &&
  368. (node->node_id < lowest_node_id)) {
  369. lowest_node_id = node->node_id;
  370. }
  371. }
  372. log_printf(LOGSYS_LEVEL_DEBUG, "lowest node id: %d us: %d", lowest_node_id, us->node_id);
  373. icmap_set_uint32("runtime.votequorum.lowest_node_id", lowest_node_id);
  374. LEAVE();
  375. }
  376. static int check_low_node_id_partition(void)
  377. {
  378. struct cluster_node *node = NULL;
  379. struct list_head *tmp;
  380. int found = 0;
  381. ENTER();
  382. list_iterate(tmp, &cluster_members_list) {
  383. node = list_entry(tmp, struct cluster_node, list);
  384. if ((node->state == NODESTATE_MEMBER) &&
  385. (node->node_id == lowest_node_id)) {
  386. found = 1;
  387. }
  388. }
  389. LEAVE();
  390. return found;
  391. }
  392. static void update_wait_for_all_status(uint8_t wfa_status)
  393. {
  394. wait_for_all_status = wfa_status;
  395. icmap_set_uint8("runtime.votequorum.wait_for_all_status",
  396. wait_for_all_status);
  397. }
  398. static void update_two_node(void)
  399. {
  400. icmap_set_uint8("runtime.votequorum.two_node", two_node);
  401. }
  402. static void update_ev_barrier(uint32_t expected_votes)
  403. {
  404. ev_barrier = expected_votes;
  405. icmap_set_uint32("runtime.votequorum.ev_barrier", ev_barrier);
  406. }
  407. /*
  408. * quorum calculation core bits
  409. */
  410. static int calculate_quorum(int allow_decrease, unsigned int max_expected, unsigned int *ret_total_votes)
  411. {
  412. struct list_head *nodelist;
  413. struct cluster_node *node;
  414. unsigned int total_votes = 0;
  415. unsigned int highest_expected = 0;
  416. unsigned int newquorum, q1, q2;
  417. unsigned int total_nodes = 0;
  418. ENTER();
  419. if ((leave_remove) && (allow_decrease) && (max_expected)) {
  420. max_expected = max(ev_barrier, max_expected);
  421. }
  422. list_iterate(nodelist, &cluster_members_list) {
  423. node = list_entry(nodelist, struct cluster_node, list);
  424. log_printf(LOGSYS_LEVEL_DEBUG, "node %u state=%d, votes=%u, expected=%u",
  425. node->node_id, node->state, node->votes, node->expected_votes);
  426. if (node->state == NODESTATE_MEMBER) {
  427. if (max_expected) {
  428. node->expected_votes = max_expected;
  429. } else {
  430. highest_expected = max(highest_expected, node->expected_votes);
  431. }
  432. total_votes += node->votes;
  433. total_nodes++;
  434. }
  435. }
  436. if (quorum_device && quorum_device->state == NODESTATE_MEMBER) {
  437. total_votes += quorum_device->votes;
  438. }
  439. if (max_expected > 0) {
  440. highest_expected = max_expected;
  441. }
  442. /*
  443. * This quorum calculation is taken from the OpenVMS Cluster Systems
  444. * manual, but, then, you guessed that didn't you
  445. */
  446. q1 = (highest_expected + 2) / 2;
  447. q2 = (total_votes + 2) / 2;
  448. newquorum = max(q1, q2);
  449. /*
  450. * Normally quorum never decreases but the system administrator can
  451. * force it down by setting expected votes to a maximum value
  452. */
  453. if (!allow_decrease) {
  454. newquorum = max(quorum, newquorum);
  455. }
  456. /*
  457. * The special two_node mode allows each of the two nodes to retain
  458. * quorum if the other fails. Only one of the two should live past
  459. * fencing (as both nodes try to fence each other in split-brain.)
  460. * Also: if there are more than two nodes, force us inquorate to avoid
  461. * any damage or confusion.
  462. */
  463. if (two_node && total_nodes <= 2) {
  464. newquorum = 1;
  465. }
  466. if (ret_total_votes) {
  467. *ret_total_votes = total_votes;
  468. }
  469. LEAVE();
  470. return newquorum;
  471. }
  472. static void are_we_quorate(unsigned int total_votes)
  473. {
  474. int quorate;
  475. int quorum_change = 0;
  476. ENTER();
  477. /*
  478. * wait for all nodes to show up before granting quorum
  479. */
  480. if ((wait_for_all) && (wait_for_all_status)) {
  481. if (total_votes != us->expected_votes) {
  482. log_printf(LOGSYS_LEVEL_NOTICE,
  483. "Waiting for all cluster members. "
  484. "Current votes: %d expected_votes: %d",
  485. total_votes, us->expected_votes);
  486. cluster_is_quorate = 0;
  487. return;
  488. }
  489. update_wait_for_all_status(0);
  490. }
  491. if (quorum > total_votes) {
  492. quorate = 0;
  493. } else {
  494. quorate = 1;
  495. get_lowest_node_id();
  496. }
  497. if ((auto_tie_breaker) &&
  498. (total_votes == (us->expected_votes / 2)) &&
  499. (check_low_node_id_partition() == 1)) {
  500. quorate = 1;
  501. }
  502. if (cluster_is_quorate && !quorate) {
  503. quorum_change = 1;
  504. log_printf(LOGSYS_LEVEL_DEBUG, "quorum lost, blocking activity");
  505. }
  506. if (!cluster_is_quorate && quorate) {
  507. quorum_change = 1;
  508. log_printf(LOGSYS_LEVEL_DEBUG, "quorum regained, resuming activity");
  509. }
  510. cluster_is_quorate = quorate;
  511. if (wait_for_all) {
  512. if (quorate) {
  513. update_wait_for_all_status(0);
  514. } else {
  515. update_wait_for_all_status(1);
  516. }
  517. }
  518. if (quorum_change) {
  519. quorum_callback(quorum_members, quorum_members_entries,
  520. cluster_is_quorate, &quorum_ringid);
  521. }
  522. LEAVE();
  523. }
  524. /* Recalculate cluster quorum, set quorate and notify changes */
  525. static void recalculate_quorum(int allow_decrease, int by_current_nodes)
  526. {
  527. unsigned int total_votes = 0;
  528. int cluster_members = 0;
  529. struct list_head *nodelist;
  530. struct cluster_node *node;
  531. ENTER();
  532. list_iterate(nodelist, &cluster_members_list) {
  533. node = list_entry(nodelist, struct cluster_node, list);
  534. if (node->state == NODESTATE_MEMBER) {
  535. if (by_current_nodes) {
  536. cluster_members++;
  537. }
  538. total_votes += node->votes;
  539. }
  540. }
  541. /*
  542. * Keep expected_votes at the highest number of votes in the cluster
  543. */
  544. log_printf(LOGSYS_LEVEL_DEBUG, "total_votes=%d, expected_votes=%d", total_votes, us->expected_votes);
  545. if (total_votes > us->expected_votes) {
  546. us->expected_votes = total_votes;
  547. votequorum_exec_send_expectedvotes_notification();
  548. }
  549. quorum = calculate_quorum(allow_decrease, cluster_members, &total_votes);
  550. are_we_quorate(total_votes);
  551. votequorum_exec_send_quorum_notification(NULL, 0L);
  552. LEAVE();
  553. }
  554. /*
  555. * configuration bits and pieces
  556. */
  557. static int votequorum_read_nodelist_configuration(uint32_t *votes,
  558. uint32_t *expected_votes)
  559. {
  560. icmap_iter_t iter;
  561. const char *iter_key;
  562. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  563. uint32_t our_pos, node_pos;
  564. uint32_t nodelist_expected_votes = 0;
  565. uint32_t node_votes = 0;
  566. int res = 0;
  567. ENTER();
  568. if (icmap_get_uint32("nodelist.local_node_pos", &our_pos) != CS_OK) {
  569. log_printf(LOGSYS_LEVEL_DEBUG,
  570. "No nodelist defined or our node is not in the nodelist");
  571. return -1;
  572. }
  573. iter = icmap_iter_init("nodelist.node.");
  574. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  575. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  576. if (res != 2) {
  577. continue;
  578. }
  579. if (strcmp(tmp_key, "ring0_addr") != 0) {
  580. continue;
  581. }
  582. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.quorum_votes", node_pos);
  583. if (icmap_get_uint32(tmp_key, &node_votes) != CS_OK) {
  584. node_votes = 1;
  585. }
  586. nodelist_expected_votes = nodelist_expected_votes + node_votes;
  587. if (node_pos == our_pos) {
  588. *votes = node_votes;
  589. }
  590. }
  591. *expected_votes = nodelist_expected_votes;
  592. icmap_iter_finalize(iter);
  593. LEAVE();
  594. return 0;
  595. }
  596. /*
  597. * votequorum_readconfig_static is executed before
  598. * votequorum_readconfig_dynamic
  599. */
  600. static char *votequorum_readconfig_static(void)
  601. {
  602. uint32_t node_votes, node_expected_votes, expected_votes;
  603. ENTER();
  604. log_printf(LOGSYS_LEVEL_DEBUG, "Reading static configuration");
  605. icmap_get_uint8("quorum.two_node", &two_node);
  606. update_two_node();
  607. if (two_node) {
  608. wait_for_all = 1;
  609. }
  610. icmap_get_uint8("quorum.leave_remove", &leave_remove);
  611. icmap_get_uint8("quorum.wait_for_all", &wait_for_all);
  612. icmap_get_uint8("quorum.auto_tie_breaker", &auto_tie_breaker);
  613. icmap_get_uint8("quorum.last_man_standing", &last_man_standing);
  614. icmap_get_uint32("quorum.last_man_standing_window", &last_man_standing_window);
  615. if ((votequorum_read_nodelist_configuration(&node_votes, &node_expected_votes)) &&
  616. (icmap_get_uint32("quorum.expected_votes", &expected_votes) != CS_OK)) {
  617. return ((char *)"configuration error: nodelist or quorum.expected_votes must be configured!");
  618. }
  619. if (wait_for_all) {
  620. update_wait_for_all_status(1);
  621. }
  622. LEAVE();
  623. return (NULL);
  624. }
  625. static void votequorum_readconfig_dynamic(void)
  626. {
  627. int cluster_members = 0;
  628. struct list_head *tmp;
  629. ENTER();
  630. log_printf(LOGSYS_LEVEL_DEBUG, "Reading dynamic configuration");
  631. if (votequorum_read_nodelist_configuration(&us->votes, &us->expected_votes)) {
  632. us->votes = 1;
  633. icmap_get_uint32("quorum.votes", &us->votes);
  634. }
  635. icmap_get_uint32("quorum.expected_votes", &us->expected_votes);
  636. update_ev_barrier(us->expected_votes);
  637. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  638. if (icmap_get_uint32("quorum.quorumdev_poll", &quorumdev_poll) != CS_OK) {
  639. quorumdev_poll = DEFAULT_QDEV_POLL;
  640. }
  641. #endif
  642. icmap_get_uint8("quorum.two_node", &two_node);
  643. update_two_node();
  644. /*
  645. * two_node mode is invalid if there are more than 2 nodes in the cluster!
  646. */
  647. list_iterate(tmp, &cluster_members_list) {
  648. cluster_members++;
  649. }
  650. if (two_node && cluster_members > 2) {
  651. log_printf(LOGSYS_LEVEL_WARNING, "quorum.two_node was set but there are more than 2 nodes in the cluster. It will be ignored.");
  652. two_node = 0;
  653. update_two_node();
  654. }
  655. LEAVE();
  656. }
  657. static void votequorum_refresh_config(
  658. int32_t event,
  659. const char *key_name,
  660. struct icmap_notify_value new_val,
  661. struct icmap_notify_value old_val,
  662. void *user_data)
  663. {
  664. unsigned int old_votes;
  665. unsigned int old_expected;
  666. ENTER();
  667. old_votes = us->votes;
  668. old_expected = us->expected_votes;
  669. /*
  670. * Reload the configuration
  671. */
  672. votequorum_readconfig_dynamic();
  673. /*
  674. * Check for fundamental changes that we need to propogate
  675. */
  676. if (old_votes != us->votes) {
  677. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, us->node_id, us->votes);
  678. }
  679. if (old_expected != us->expected_votes) {
  680. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES, us->node_id, us->expected_votes);
  681. }
  682. LEAVE();
  683. }
  684. static void votequorum_exec_add_config_notification(void)
  685. {
  686. icmap_track_t icmap_track_nodelist = NULL;
  687. icmap_track_t icmap_track_quorum = NULL;
  688. ENTER();
  689. icmap_track_add("nodelist.",
  690. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  691. votequorum_refresh_config,
  692. NULL,
  693. &icmap_track_nodelist);
  694. icmap_track_add("quorum.",
  695. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  696. votequorum_refresh_config,
  697. NULL,
  698. &icmap_track_quorum);
  699. LEAVE();
  700. }
  701. /*
  702. * votequorum_exec core
  703. */
  704. static int votequorum_exec_send_reconfigure(uint8_t param, unsigned int nodeid, uint32_t value)
  705. {
  706. struct req_exec_quorum_reconfigure req_exec_quorum_reconfigure;
  707. struct iovec iov[1];
  708. int ret;
  709. ENTER();
  710. req_exec_quorum_reconfigure.nodeid = nodeid;
  711. req_exec_quorum_reconfigure.value = value;
  712. req_exec_quorum_reconfigure.param = param;
  713. req_exec_quorum_reconfigure._pad0 = 0;
  714. req_exec_quorum_reconfigure._pad1 = 0;
  715. req_exec_quorum_reconfigure._pad2 = 0;
  716. req_exec_quorum_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE);
  717. req_exec_quorum_reconfigure.header.size = sizeof(req_exec_quorum_reconfigure);
  718. iov[0].iov_base = (void *)&req_exec_quorum_reconfigure;
  719. iov[0].iov_len = sizeof(req_exec_quorum_reconfigure);
  720. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  721. LEAVE();
  722. return ret;
  723. }
  724. static int votequorum_exec_send_nodeinfo(void)
  725. {
  726. struct req_exec_quorum_nodeinfo req_exec_quorum_nodeinfo;
  727. struct iovec iov[1];
  728. int ret;
  729. ENTER();
  730. req_exec_quorum_nodeinfo.votes = us->votes;
  731. req_exec_quorum_nodeinfo.expected_votes = us->expected_votes;
  732. req_exec_quorum_nodeinfo.flags = us->flags;
  733. req_exec_quorum_nodeinfo.quorate = cluster_is_quorate;
  734. req_exec_quorum_nodeinfo.wait_for_all_status = wait_for_all_status;
  735. req_exec_quorum_nodeinfo.first_trans = first_trans;
  736. req_exec_quorum_nodeinfo._pad0 = 0;
  737. req_exec_quorum_nodeinfo._pad1 = 0;
  738. req_exec_quorum_nodeinfo._pad2 = 0;
  739. req_exec_quorum_nodeinfo.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO);
  740. req_exec_quorum_nodeinfo.header.size = sizeof(req_exec_quorum_nodeinfo);
  741. iov[0].iov_base = (void *)&req_exec_quorum_nodeinfo;
  742. iov[0].iov_len = sizeof(req_exec_quorum_nodeinfo);
  743. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  744. LEAVE();
  745. return ret;
  746. }
  747. static int votequorum_exec_send_quorum_notification(void *conn, uint64_t context)
  748. {
  749. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  750. struct list_head *tmp;
  751. struct cluster_node *node;
  752. int cluster_members = 0;
  753. int i = 0;
  754. int size;
  755. char *buf;
  756. ENTER();
  757. list_iterate(tmp, &cluster_members_list) {
  758. node = list_entry(tmp, struct cluster_node, list);
  759. cluster_members++;
  760. }
  761. if (quorum_device) {
  762. cluster_members++;
  763. }
  764. size = sizeof(struct res_lib_votequorum_notification) + sizeof(struct votequorum_node) * cluster_members;
  765. buf = alloca(size);
  766. if (!buf) {
  767. LEAVE();
  768. return -1;
  769. }
  770. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)buf;
  771. res_lib_votequorum_notification->quorate = cluster_is_quorate;
  772. res_lib_votequorum_notification->node_list_entries = cluster_members;
  773. res_lib_votequorum_notification->context = context;
  774. list_iterate(tmp, &cluster_members_list) {
  775. node = list_entry(tmp, struct cluster_node, list);
  776. res_lib_votequorum_notification->node_list[i].nodeid = node->node_id;
  777. res_lib_votequorum_notification->node_list[i++].state = node->state;
  778. }
  779. if (quorum_device) {
  780. res_lib_votequorum_notification->node_list[i].nodeid = 0;
  781. res_lib_votequorum_notification->node_list[i++].state = quorum_device->state | 0x80;
  782. }
  783. res_lib_votequorum_notification->header.id = MESSAGE_RES_VOTEQUORUM_NOTIFICATION;
  784. res_lib_votequorum_notification->header.size = size;
  785. res_lib_votequorum_notification->header.error = CS_OK;
  786. /* Send it to all interested parties */
  787. if (conn) {
  788. int ret = corosync_api->ipc_dispatch_send(conn, buf, size);
  789. LEAVE();
  790. return ret;
  791. } else {
  792. struct quorum_pd *qpd;
  793. list_iterate(tmp, &trackers_list) {
  794. qpd = list_entry(tmp, struct quorum_pd, list);
  795. res_lib_votequorum_notification->context = qpd->tracking_context;
  796. corosync_api->ipc_dispatch_send(qpd->conn, buf, size);
  797. }
  798. }
  799. LEAVE();
  800. return 0;
  801. }
  802. static void votequorum_exec_send_expectedvotes_notification(void)
  803. {
  804. struct res_lib_votequorum_expectedvotes_notification res_lib_votequorum_expectedvotes_notification;
  805. struct quorum_pd *qpd;
  806. struct list_head *tmp;
  807. ENTER();
  808. log_printf(LOGSYS_LEVEL_DEBUG, "Sending expected votes callback");
  809. res_lib_votequorum_expectedvotes_notification.header.id = MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION;
  810. res_lib_votequorum_expectedvotes_notification.header.size = sizeof(res_lib_votequorum_expectedvotes_notification);
  811. res_lib_votequorum_expectedvotes_notification.header.error = CS_OK;
  812. res_lib_votequorum_expectedvotes_notification.expected_votes = us->expected_votes;
  813. list_iterate(tmp, &trackers_list) {
  814. qpd = list_entry(tmp, struct quorum_pd, list);
  815. res_lib_votequorum_expectedvotes_notification.context = qpd->tracking_context;
  816. corosync_api->ipc_dispatch_send(qpd->conn, &res_lib_votequorum_expectedvotes_notification,
  817. sizeof(struct res_lib_votequorum_expectedvotes_notification));
  818. }
  819. LEAVE();
  820. }
  821. static void exec_votequorum_nodeinfo_endian_convert (void *message)
  822. {
  823. struct req_exec_quorum_nodeinfo *nodeinfo = message;
  824. ENTER();
  825. nodeinfo->votes = swab32(nodeinfo->votes);
  826. nodeinfo->expected_votes = swab32(nodeinfo->expected_votes);
  827. nodeinfo->flags = swab16(nodeinfo->flags);
  828. LEAVE();
  829. }
  830. static void message_handler_req_exec_votequorum_nodeinfo (
  831. const void *message,
  832. unsigned int nodeid)
  833. {
  834. const struct req_exec_quorum_nodeinfo *req_exec_quorum_nodeinfo = message;
  835. struct cluster_node *node;
  836. int old_votes;
  837. int old_expected;
  838. uint16_t old_flags;
  839. nodestate_t old_state;
  840. int new_node = 0;
  841. int allow_downgrade = 0;
  842. int by_node = 0;
  843. ENTER();
  844. log_printf(LOGSYS_LEVEL_DEBUG, "got nodeinfo message from cluster node %u", nodeid);
  845. log_printf(LOGSYS_LEVEL_DEBUG, "nodeinfo message[%u]: votes: %d, expected: %d wfa: %d quorate: %d flags: %d",
  846. nodeid,
  847. req_exec_quorum_nodeinfo->votes,
  848. req_exec_quorum_nodeinfo->expected_votes,
  849. req_exec_quorum_nodeinfo->wait_for_all_status,
  850. req_exec_quorum_nodeinfo->quorate,
  851. req_exec_quorum_nodeinfo->flags);
  852. node = find_node_by_nodeid(nodeid);
  853. if (!node) {
  854. node = allocate_node(nodeid);
  855. new_node = 1;
  856. }
  857. if (!node) {
  858. corosync_api->error_memory_failure();
  859. LEAVE();
  860. return;
  861. }
  862. old_votes = node->votes;
  863. old_expected = node->expected_votes;
  864. old_state = node->state;
  865. old_flags = node->flags;
  866. /* Update node state */
  867. node->votes = req_exec_quorum_nodeinfo->votes;
  868. node->flags = req_exec_quorum_nodeinfo->flags;
  869. if (node->flags & NODE_FLAGS_LEAVING) {
  870. node->state = NODESTATE_LEAVING;
  871. allow_downgrade = 1;
  872. by_node = 1;
  873. } else {
  874. node->state = NODESTATE_MEMBER;
  875. }
  876. if ((!cluster_is_quorate) &&
  877. (req_exec_quorum_nodeinfo->quorate)) {
  878. allow_downgrade = 1;
  879. us->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  880. }
  881. if (req_exec_quorum_nodeinfo->quorate) {
  882. node->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  883. } else {
  884. node->expected_votes = us->expected_votes;
  885. }
  886. if ((last_man_standing) && (req_exec_quorum_nodeinfo->votes > 1)) {
  887. log_printf(LOGSYS_LEVEL_WARNING, "Last Man Standing feature is supported only when all"
  888. "cluster nodes votes are set to 1. Disabling LMS.");
  889. last_man_standing = 0;
  890. if (last_man_standing_timer_set) {
  891. corosync_api->timer_delete(last_man_standing_timer);
  892. last_man_standing_timer_set = 0;
  893. }
  894. }
  895. if (new_node ||
  896. req_exec_quorum_nodeinfo->first_trans ||
  897. old_votes != node->votes ||
  898. old_expected != node->expected_votes ||
  899. old_flags != node->flags ||
  900. old_state != node->state) {
  901. recalculate_quorum(allow_downgrade, by_node);
  902. }
  903. if (!nodeid) {
  904. free(node);
  905. }
  906. if ((wait_for_all) &&
  907. (!req_exec_quorum_nodeinfo->wait_for_all_status) &&
  908. (req_exec_quorum_nodeinfo->quorate)) {
  909. update_wait_for_all_status(0);
  910. }
  911. LEAVE();
  912. }
  913. static void exec_votequorum_reconfigure_endian_convert (void *message)
  914. {
  915. struct req_exec_quorum_reconfigure *reconfigure = message;
  916. ENTER();
  917. reconfigure->nodeid = swab32(reconfigure->nodeid);
  918. reconfigure->value = swab32(reconfigure->value);
  919. LEAVE();
  920. }
  921. static void message_handler_req_exec_votequorum_reconfigure (
  922. const void *message,
  923. unsigned int nodeid)
  924. {
  925. const struct req_exec_quorum_reconfigure *req_exec_quorum_reconfigure = message;
  926. struct cluster_node *node;
  927. struct list_head *nodelist;
  928. ENTER();
  929. log_printf(LOGSYS_LEVEL_DEBUG, "got reconfigure message from cluster node %u", nodeid);
  930. node = find_node_by_nodeid(req_exec_quorum_reconfigure->nodeid);
  931. if (!node) {
  932. LEAVE();
  933. return;
  934. }
  935. switch(req_exec_quorum_reconfigure->param)
  936. {
  937. case VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES:
  938. list_iterate(nodelist, &cluster_members_list) {
  939. node = list_entry(nodelist, struct cluster_node, list);
  940. if (node->state == NODESTATE_MEMBER &&
  941. node->expected_votes > req_exec_quorum_reconfigure->value) {
  942. node->expected_votes = req_exec_quorum_reconfigure->value;
  943. }
  944. }
  945. votequorum_exec_send_expectedvotes_notification();
  946. update_ev_barrier(req_exec_quorum_reconfigure->value);
  947. recalculate_quorum(1, 0); /* Allow decrease */
  948. break;
  949. case VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES:
  950. node->votes = req_exec_quorum_reconfigure->value;
  951. recalculate_quorum(1, 0); /* Allow decrease */
  952. break;
  953. }
  954. LEAVE();
  955. }
  956. static int votequorum_exec_exit_fn (void)
  957. {
  958. int ret = 0;
  959. ENTER();
  960. if (leave_remove) {
  961. us->flags |= NODE_FLAGS_LEAVING;
  962. ret = votequorum_exec_send_nodeinfo();
  963. }
  964. LEAVE();
  965. return ret;
  966. }
  967. static char *votequorum_exec_init_fn (struct corosync_api_v1 *api)
  968. {
  969. #ifdef COROSYNC_SOLARIS
  970. logsys_subsys_init();
  971. #endif
  972. ENTER();
  973. list_init(&cluster_members_list);
  974. list_init(&trackers_list);
  975. /*
  976. * Allocate a cluster_node for us
  977. */
  978. us = allocate_node(corosync_api->totem_nodeid_get());
  979. if (!us) {
  980. LEAVE();
  981. return ((char *)"Could not allocate node.");
  982. }
  983. icmap_set_uint32("runtime.votequorum.this_node_id", us->node_id);
  984. us->state = NODESTATE_MEMBER;
  985. us->votes = 1;
  986. votequorum_readconfig_dynamic();
  987. recalculate_quorum(0, 0);
  988. /*
  989. * Listen for changes
  990. */
  991. votequorum_exec_add_config_notification();
  992. /*
  993. * Start us off with one node
  994. */
  995. votequorum_exec_send_nodeinfo();
  996. LEAVE();
  997. return (NULL);
  998. }
  999. /*
  1000. * votequorum service core
  1001. */
  1002. static void votequorum_last_man_standing_timer_fn(void *arg)
  1003. {
  1004. ENTER();
  1005. last_man_standing_timer_set = 0;
  1006. if (cluster_is_quorate) {
  1007. recalculate_quorum(1,1);
  1008. }
  1009. LEAVE();
  1010. }
  1011. static void votequorum_confchg_fn (
  1012. enum totem_configuration_type configuration_type,
  1013. const unsigned int *member_list, size_t member_list_entries,
  1014. const unsigned int *left_list, size_t left_list_entries,
  1015. const unsigned int *joined_list, size_t joined_list_entries,
  1016. const struct memb_ring_id *ring_id)
  1017. {
  1018. int i;
  1019. struct cluster_node *node;
  1020. ENTER();
  1021. if (member_list_entries > 1) {
  1022. first_trans = 0;
  1023. }
  1024. if (left_list_entries) {
  1025. for (i = 0; i< left_list_entries; i++) {
  1026. node = find_node_by_nodeid(left_list[i]);
  1027. if (node) {
  1028. node->state = NODESTATE_DEAD;
  1029. }
  1030. }
  1031. }
  1032. if (last_man_standing) {
  1033. if (((member_list_entries >= quorum) && (left_list_entries)) ||
  1034. ((member_list_entries <= quorum) && (auto_tie_breaker) && (check_low_node_id_partition() == 1))) {
  1035. if (last_man_standing_timer_set) {
  1036. corosync_api->timer_delete(last_man_standing_timer);
  1037. last_man_standing_timer_set = 0;
  1038. }
  1039. corosync_api->timer_add_duration((unsigned long long)last_man_standing_window*1000000,
  1040. NULL, votequorum_last_man_standing_timer_fn,
  1041. &last_man_standing_timer);
  1042. last_man_standing_timer_set = 1;
  1043. }
  1044. }
  1045. if (member_list_entries) {
  1046. memcpy(quorum_members, member_list, sizeof(unsigned int) * member_list_entries);
  1047. quorum_members_entries = member_list_entries;
  1048. if (quorum_device) {
  1049. quorum_members[quorum_members_entries++] = 0;
  1050. }
  1051. votequorum_exec_send_nodeinfo();
  1052. }
  1053. if (left_list_entries) {
  1054. recalculate_quorum(0, 0);
  1055. }
  1056. memcpy(&quorum_ringid, ring_id, sizeof(*ring_id));
  1057. if (configuration_type == TOTEM_CONFIGURATION_REGULAR) {
  1058. quorum_callback(quorum_members, quorum_members_entries,
  1059. cluster_is_quorate, &quorum_ringid);
  1060. }
  1061. LEAVE();
  1062. }
  1063. char *votequorum_init(struct corosync_api_v1 *api,
  1064. quorum_set_quorate_fn_t q_set_quorate_fn)
  1065. {
  1066. char *error;
  1067. ENTER();
  1068. if (q_set_quorate_fn == NULL) {
  1069. return ((char *)"Quorate function not set");
  1070. }
  1071. corosync_api = api;
  1072. quorum_callback = q_set_quorate_fn;
  1073. error = votequorum_readconfig_static();
  1074. if (error) {
  1075. return (error);
  1076. }
  1077. error = corosync_service_link_and_init(corosync_api,
  1078. &votequorum_service[0]);
  1079. if (error) {
  1080. return (error);
  1081. }
  1082. LEAVE();
  1083. return (NULL);
  1084. }
  1085. /*
  1086. * Library Handler init/fini
  1087. */
  1088. static int quorum_lib_init_fn (void *conn)
  1089. {
  1090. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1091. ENTER();
  1092. list_init (&pd->list);
  1093. pd->conn = conn;
  1094. LEAVE();
  1095. return (0);
  1096. }
  1097. static int quorum_lib_exit_fn (void *conn)
  1098. {
  1099. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1100. ENTER();
  1101. if (quorum_pd->tracking_enabled) {
  1102. list_del (&quorum_pd->list);
  1103. list_init (&quorum_pd->list);
  1104. }
  1105. LEAVE();
  1106. return (0);
  1107. }
  1108. /*
  1109. * library internal functions
  1110. */
  1111. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  1112. static void quorum_device_timer_fn(void *arg)
  1113. {
  1114. ENTER();
  1115. if (!quorum_device || quorum_device->state == NODESTATE_DEAD) {
  1116. LEAVE();
  1117. return;
  1118. }
  1119. if ((quorum_device->last_hello / QB_TIME_NS_IN_SEC) + quorumdev_poll/1000 <
  1120. (qb_util_nano_current_get () / QB_TIME_NS_IN_SEC)) {
  1121. quorum_device->state = NODESTATE_DEAD;
  1122. log_printf(LOGSYS_LEVEL_INFO, "lost contact with quorum device");
  1123. recalculate_quorum(0, 0);
  1124. } else {
  1125. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1126. quorum_device_timer_fn, &quorum_device_timer);
  1127. }
  1128. LEAVE();
  1129. }
  1130. #endif
  1131. /*
  1132. * Library Handler Functions
  1133. */
  1134. static void message_handler_req_lib_votequorum_getinfo (void *conn, const void *message)
  1135. {
  1136. const struct req_lib_votequorum_getinfo *req_lib_votequorum_getinfo = message;
  1137. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  1138. struct cluster_node *node;
  1139. unsigned int highest_expected = 0;
  1140. unsigned int total_votes = 0;
  1141. cs_error_t error = CS_OK;
  1142. ENTER();
  1143. log_printf(LOGSYS_LEVEL_DEBUG, "got getinfo request on %p for node %u", conn, req_lib_votequorum_getinfo->nodeid);
  1144. node = find_node_by_nodeid(req_lib_votequorum_getinfo->nodeid);
  1145. if (node) {
  1146. struct cluster_node *iternode;
  1147. struct list_head *nodelist;
  1148. list_iterate(nodelist, &cluster_members_list) {
  1149. iternode = list_entry(nodelist, struct cluster_node, list);
  1150. if (iternode->state == NODESTATE_MEMBER) {
  1151. highest_expected =
  1152. max(highest_expected, iternode->expected_votes);
  1153. total_votes += iternode->votes;
  1154. }
  1155. }
  1156. if (quorum_device && quorum_device->state == NODESTATE_MEMBER) {
  1157. total_votes += quorum_device->votes;
  1158. }
  1159. res_lib_votequorum_getinfo.votes = us->votes;
  1160. res_lib_votequorum_getinfo.expected_votes = us->expected_votes;
  1161. res_lib_votequorum_getinfo.highest_expected = highest_expected;
  1162. res_lib_votequorum_getinfo.quorum = quorum;
  1163. res_lib_votequorum_getinfo.total_votes = total_votes;
  1164. res_lib_votequorum_getinfo.flags = 0;
  1165. res_lib_votequorum_getinfo.nodeid = node->node_id;
  1166. if (two_node) {
  1167. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_TWONODE;
  1168. }
  1169. if (cluster_is_quorate) {
  1170. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_QUORATE;
  1171. }
  1172. if (wait_for_all) {
  1173. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_WAIT_FOR_ALL;
  1174. }
  1175. if (last_man_standing) {
  1176. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_LAST_MAN_STANDING;
  1177. }
  1178. if (auto_tie_breaker) {
  1179. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_AUTO_TIE_BREAKER;
  1180. }
  1181. if (leave_remove) {
  1182. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_LEAVE_REMOVE;
  1183. }
  1184. } else {
  1185. error = CS_ERR_NOT_EXIST;
  1186. }
  1187. res_lib_votequorum_getinfo.header.size = sizeof(res_lib_votequorum_getinfo);
  1188. res_lib_votequorum_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1189. res_lib_votequorum_getinfo.header.error = error;
  1190. corosync_api->ipc_response_send(conn, &res_lib_votequorum_getinfo, sizeof(res_lib_votequorum_getinfo));
  1191. log_printf(LOGSYS_LEVEL_DEBUG, "getinfo response error: %d", error);
  1192. LEAVE();
  1193. }
  1194. static void message_handler_req_lib_votequorum_setexpected (void *conn, const void *message)
  1195. {
  1196. const struct req_lib_votequorum_setexpected *req_lib_votequorum_setexpected = message;
  1197. struct res_lib_votequorum_status res_lib_votequorum_status;
  1198. cs_error_t error = CS_OK;
  1199. unsigned int newquorum;
  1200. unsigned int total_votes;
  1201. ENTER();
  1202. /*
  1203. * Validate new expected votes
  1204. */
  1205. newquorum = calculate_quorum(1, req_lib_votequorum_setexpected->expected_votes, &total_votes);
  1206. if (newquorum < total_votes / 2 ||
  1207. newquorum > total_votes) {
  1208. error = CS_ERR_INVALID_PARAM;
  1209. goto error_exit;
  1210. }
  1211. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES, us->node_id,
  1212. req_lib_votequorum_setexpected->expected_votes);
  1213. error_exit:
  1214. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1215. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1216. res_lib_votequorum_status.header.error = error;
  1217. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1218. LEAVE();
  1219. }
  1220. static void message_handler_req_lib_votequorum_setvotes (void *conn, const void *message)
  1221. {
  1222. const struct req_lib_votequorum_setvotes *req_lib_votequorum_setvotes = message;
  1223. struct res_lib_votequorum_status res_lib_votequorum_status;
  1224. struct cluster_node *node;
  1225. unsigned int newquorum;
  1226. unsigned int total_votes;
  1227. unsigned int saved_votes;
  1228. cs_error_t error = CS_OK;
  1229. unsigned int nodeid;
  1230. ENTER();
  1231. nodeid = req_lib_votequorum_setvotes->nodeid;
  1232. node = find_node_by_nodeid(nodeid);
  1233. if (!node) {
  1234. error = CS_ERR_NAME_NOT_FOUND;
  1235. goto error_exit;
  1236. }
  1237. /*
  1238. * Check votes is valid
  1239. */
  1240. saved_votes = node->votes;
  1241. node->votes = req_lib_votequorum_setvotes->votes;
  1242. newquorum = calculate_quorum(1, 0, &total_votes);
  1243. if (newquorum < total_votes / 2 ||
  1244. newquorum > total_votes) {
  1245. node->votes = saved_votes;
  1246. error = CS_ERR_INVALID_PARAM;
  1247. goto error_exit;
  1248. }
  1249. if (!nodeid) {
  1250. nodeid = corosync_api->totem_nodeid_get();
  1251. }
  1252. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, nodeid,
  1253. req_lib_votequorum_setvotes->votes);
  1254. error_exit:
  1255. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1256. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1257. res_lib_votequorum_status.header.error = error;
  1258. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1259. LEAVE();
  1260. }
  1261. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  1262. const void *message)
  1263. {
  1264. const struct req_lib_votequorum_trackstart *req_lib_votequorum_trackstart = message;
  1265. struct res_lib_votequorum_status res_lib_votequorum_status;
  1266. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1267. ENTER();
  1268. /*
  1269. * If an immediate listing of the current cluster membership
  1270. * is requested, generate membership list
  1271. */
  1272. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  1273. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  1274. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p", conn);
  1275. votequorum_exec_send_quorum_notification(conn, req_lib_votequorum_trackstart->context);
  1276. }
  1277. /*
  1278. * Record requests for tracking
  1279. */
  1280. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  1281. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  1282. quorum_pd->track_flags = req_lib_votequorum_trackstart->track_flags;
  1283. quorum_pd->tracking_enabled = 1;
  1284. quorum_pd->tracking_context = req_lib_votequorum_trackstart->context;
  1285. list_add (&quorum_pd->list, &trackers_list);
  1286. }
  1287. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1288. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1289. res_lib_votequorum_status.header.error = CS_OK;
  1290. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1291. LEAVE();
  1292. }
  1293. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  1294. const void *message)
  1295. {
  1296. struct res_lib_votequorum_status res_lib_votequorum_status;
  1297. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1298. int error = CS_OK;
  1299. ENTER();
  1300. if (quorum_pd->tracking_enabled) {
  1301. error = CS_OK;
  1302. quorum_pd->tracking_enabled = 0;
  1303. list_del (&quorum_pd->list);
  1304. list_init (&quorum_pd->list);
  1305. } else {
  1306. error = CS_ERR_NOT_EXIST;
  1307. }
  1308. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1309. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1310. res_lib_votequorum_status.header.error = error;
  1311. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1312. LEAVE();
  1313. }
  1314. #ifdef EXPERIMENTAL_QUORUM_DEVICE_API
  1315. static void message_handler_req_lib_votequorum_qdevice_register (void *conn,
  1316. const void *message)
  1317. {
  1318. const struct req_lib_votequorum_qdevice_register *req_lib_votequorum_qdevice_register = message;
  1319. struct res_lib_votequorum_status res_lib_votequorum_status;
  1320. cs_error_t error = CS_OK;
  1321. ENTER();
  1322. if (quorum_device) {
  1323. error = CS_ERR_EXIST;
  1324. } else {
  1325. quorum_device = allocate_node(0);
  1326. quorum_device->state = NODESTATE_DEAD;
  1327. quorum_device->votes = req_lib_votequorum_qdevice_register->votes;
  1328. strcpy(quorum_device_name, req_lib_votequorum_qdevice_register->name);
  1329. list_add(&quorum_device->list, &cluster_members_list);
  1330. }
  1331. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1332. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1333. res_lib_votequorum_status.header.error = error;
  1334. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1335. LEAVE();
  1336. }
  1337. static void message_handler_req_lib_votequorum_qdevice_unregister (void *conn,
  1338. const void *message)
  1339. {
  1340. struct res_lib_votequorum_status res_lib_votequorum_status;
  1341. cs_error_t error = CS_OK;
  1342. ENTER();
  1343. if (quorum_device) {
  1344. struct cluster_node *node = quorum_device;
  1345. quorum_device = NULL;
  1346. list_del(&node->list);
  1347. free(node);
  1348. recalculate_quorum(0, 0);
  1349. } else {
  1350. error = CS_ERR_NOT_EXIST;
  1351. }
  1352. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1353. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1354. res_lib_votequorum_status.header.error = error;
  1355. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1356. LEAVE();
  1357. }
  1358. static void message_handler_req_lib_votequorum_qdevice_poll (void *conn,
  1359. const void *message)
  1360. {
  1361. const struct req_lib_votequorum_qdevice_poll *req_lib_votequorum_qdevice_poll = message;
  1362. struct res_lib_votequorum_status res_lib_votequorum_status;
  1363. cs_error_t error = CS_OK;
  1364. ENTER();
  1365. if (quorum_device) {
  1366. if (req_lib_votequorum_qdevice_poll->state) {
  1367. quorum_device->last_hello = qb_util_nano_current_get ();
  1368. if (quorum_device->state == NODESTATE_DEAD) {
  1369. quorum_device->state = NODESTATE_MEMBER;
  1370. recalculate_quorum(0, 0);
  1371. corosync_api->timer_add_duration((unsigned long long)quorumdev_poll*1000000, quorum_device,
  1372. quorum_device_timer_fn, &quorum_device_timer);
  1373. }
  1374. } else {
  1375. if (quorum_device->state == NODESTATE_MEMBER) {
  1376. quorum_device->state = NODESTATE_DEAD;
  1377. recalculate_quorum(0, 0);
  1378. corosync_api->timer_delete(quorum_device_timer);
  1379. }
  1380. }
  1381. } else {
  1382. error = CS_ERR_NOT_EXIST;
  1383. }
  1384. /*
  1385. * send status
  1386. */
  1387. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1388. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1389. res_lib_votequorum_status.header.error = error;
  1390. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1391. LEAVE();
  1392. }
  1393. static void message_handler_req_lib_votequorum_qdevice_getinfo (void *conn,
  1394. const void *message)
  1395. {
  1396. struct res_lib_votequorum_qdevice_getinfo res_lib_votequorum_qdevice_getinfo;
  1397. cs_error_t error = CS_OK;
  1398. ENTER();
  1399. if (quorum_device) {
  1400. log_printf(LOGSYS_LEVEL_DEBUG, "got qdevice_getinfo state %d", quorum_device->state);
  1401. res_lib_votequorum_qdevice_getinfo.votes = quorum_device->votes;
  1402. if (quorum_device->state == NODESTATE_MEMBER) {
  1403. res_lib_votequorum_qdevice_getinfo.state = 1;
  1404. } else {
  1405. res_lib_votequorum_qdevice_getinfo.state = 0;
  1406. }
  1407. strcpy(res_lib_votequorum_qdevice_getinfo.name, quorum_device_name);
  1408. } else {
  1409. error = CS_ERR_NOT_EXIST;
  1410. }
  1411. res_lib_votequorum_qdevice_getinfo.header.size = sizeof(res_lib_votequorum_qdevice_getinfo);
  1412. res_lib_votequorum_qdevice_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1413. res_lib_votequorum_qdevice_getinfo.header.error = error;
  1414. corosync_api->ipc_response_send(conn, &res_lib_votequorum_qdevice_getinfo, sizeof(res_lib_votequorum_qdevice_getinfo));
  1415. LEAVE();
  1416. }
  1417. #endif