votequorum.c 43 KB

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