votequorum.c 44 KB

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