votequorum.c 44 KB

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