votequorum.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346
  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 "quorum.h"
  40. #include <corosync/corodefs.h>
  41. #include <corosync/list.h>
  42. #include <corosync/logsys.h>
  43. #include <corosync/coroapi.h>
  44. #include <corosync/icmap.h>
  45. #include <corosync/ipc_votequorum.h>
  46. #include "service.h"
  47. LOGSYS_DECLARE_SUBSYS ("VOTEQ");
  48. /*
  49. * interface with corosync
  50. */
  51. static struct corosync_api_v1 *corosync_api;
  52. /*
  53. * votequorum global config vars
  54. */
  55. #define DEFAULT_QDEVICE_TIMEOUT 10000
  56. static char qdevice_name[VOTEQUORUM_MAX_QDEVICE_NAME_LEN];
  57. static struct cluster_node *qdevice = NULL;
  58. static unsigned int qdevice_timeout = DEFAULT_QDEVICE_TIMEOUT;
  59. static uint8_t qdevice_can_operate = 1;
  60. static uint8_t qdevice_is_registered = 0;
  61. static void *qdevice_reg_conn = NULL;
  62. static uint8_t two_node = 0;
  63. static uint8_t wait_for_all = 0;
  64. static uint8_t wait_for_all_status = 0;
  65. static uint8_t auto_tie_breaker = 0;
  66. static int lowest_node_id = -1;
  67. #define DEFAULT_LMS_WIN 10000
  68. static uint8_t last_man_standing = 0;
  69. static uint32_t last_man_standing_window = DEFAULT_LMS_WIN;
  70. static uint8_t leave_remove = 0;
  71. static uint32_t ev_barrier = 0;
  72. /*
  73. * votequorum_exec defines/structs/forward definitions
  74. */
  75. struct req_exec_quorum_nodeinfo {
  76. struct qb_ipc_request_header header __attribute__((aligned(8)));
  77. uint32_t nodeid;
  78. uint32_t votes;
  79. uint32_t expected_votes;
  80. uint32_t flags;
  81. } __attribute__((packed));
  82. struct req_exec_quorum_reconfigure {
  83. struct qb_ipc_request_header header __attribute__((aligned(8)));
  84. uint32_t nodeid;
  85. uint32_t value;
  86. uint8_t param;
  87. uint8_t _pad0;
  88. uint8_t _pad1;
  89. uint8_t _pad2;
  90. } __attribute__((packed));
  91. struct req_exec_quorum_qdevice_reg {
  92. struct qb_ipc_request_header header __attribute__((aligned(8)));
  93. uint32_t operation;
  94. char qdevice_name[VOTEQUORUM_MAX_QDEVICE_NAME_LEN];
  95. } __attribute__((packed));
  96. struct req_exec_quorum_qdevice_reconfigure {
  97. struct qb_ipc_request_header header __attribute__((aligned(8)));
  98. char oldname[VOTEQUORUM_MAX_QDEVICE_NAME_LEN];
  99. char newname[VOTEQUORUM_MAX_QDEVICE_NAME_LEN];
  100. } __attribute__((packed));
  101. /*
  102. * votequorum_exec onwire version (via totem)
  103. */
  104. #include "votequorum.h"
  105. /*
  106. * votequorum_exec onwire messages (via totem)
  107. */
  108. #define MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO 0
  109. #define MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE 1
  110. #define MESSAGE_REQ_EXEC_VOTEQUORUM_QDEVICE_REG 2
  111. #define MESSAGE_REQ_EXEC_VOTEQUORUM_QDEVICE_RECONFIGURE 3
  112. static void votequorum_exec_send_expectedvotes_notification(void);
  113. static int votequorum_exec_send_quorum_notification(void *conn, uint64_t context);
  114. #define VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES 1
  115. #define VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES 2
  116. static int votequorum_exec_send_reconfigure(uint8_t param, unsigned int nodeid, uint32_t value);
  117. /*
  118. * used by req_exec_quorum_qdevice_reg
  119. */
  120. #define VOTEQUORUM_QDEVICE_OPERATION_UNREGISTER 0
  121. #define VOTEQUORUM_QDEVICE_OPERATION_REGISTER 1
  122. /*
  123. * votequorum internal node status/view
  124. */
  125. #define NODE_FLAGS_QUORATE 1
  126. #define NODE_FLAGS_LEAVING 2
  127. #define NODE_FLAGS_WFASTATUS 4
  128. #define NODE_FLAGS_FIRST 8
  129. #define NODE_FLAGS_QDEVICE 16
  130. #define NODE_FLAGS_QDEVICE_STATE 32
  131. #define NODEID_QDEVICE 0
  132. typedef enum {
  133. NODESTATE_MEMBER=1,
  134. NODESTATE_DEAD,
  135. NODESTATE_LEAVING
  136. } nodestate_t;
  137. struct cluster_node {
  138. int node_id;
  139. nodestate_t state;
  140. uint32_t votes;
  141. uint32_t expected_votes;
  142. uint32_t flags;
  143. struct list_head list;
  144. };
  145. /*
  146. * votequorum internal quorum status
  147. */
  148. static uint8_t quorum;
  149. static uint8_t cluster_is_quorate;
  150. /*
  151. * votequorum membership data
  152. */
  153. static struct cluster_node *us;
  154. static struct list_head cluster_members_list;
  155. static unsigned int quorum_members[PROCESSOR_COUNT_MAX+1];
  156. static int quorum_members_entries = 0;
  157. static struct memb_ring_id quorum_ringid;
  158. /*
  159. * votequorum tracking
  160. */
  161. struct quorum_pd {
  162. unsigned char track_flags;
  163. int tracking_enabled;
  164. uint64_t tracking_context;
  165. struct list_head list;
  166. void *conn;
  167. };
  168. static struct list_head trackers_list;
  169. /*
  170. * votequorum timers
  171. */
  172. static corosync_timer_handle_t qdevice_timer;
  173. static int qdevice_timer_set = 0;
  174. static corosync_timer_handle_t last_man_standing_timer;
  175. static int last_man_standing_timer_set = 0;
  176. /*
  177. * Service Interfaces required by service_message_handler struct
  178. */
  179. static void votequorum_confchg_fn (
  180. enum totem_configuration_type configuration_type,
  181. const unsigned int *member_list, size_t member_list_entries,
  182. const unsigned int *left_list, size_t left_list_entries,
  183. const unsigned int *joined_list, size_t joined_list_entries,
  184. const struct memb_ring_id *ring_id);
  185. static quorum_set_quorate_fn_t quorum_callback;
  186. /*
  187. * votequorum_exec handler and definitions
  188. */
  189. static char *votequorum_exec_init_fn (struct corosync_api_v1 *api);
  190. static int votequorum_exec_exit_fn (void);
  191. static int votequorum_exec_send_nodeinfo(uint32_t nodeid);
  192. static void message_handler_req_exec_votequorum_nodeinfo (
  193. const void *message,
  194. unsigned int nodeid);
  195. static void exec_votequorum_nodeinfo_endian_convert (void *message);
  196. static void message_handler_req_exec_votequorum_reconfigure (
  197. const void *message,
  198. unsigned int nodeid);
  199. static void exec_votequorum_reconfigure_endian_convert (void *message);
  200. static void message_handler_req_exec_votequorum_qdevice_reg (
  201. const void *message,
  202. unsigned int nodeid);
  203. static void exec_votequorum_qdevice_reg_endian_convert (void *message);
  204. static void message_handler_req_exec_votequorum_qdevice_reconfigure (
  205. const void *message,
  206. unsigned int nodeid);
  207. static void exec_votequorum_qdevice_reconfigure_endian_convert (void *message);
  208. static struct corosync_exec_handler votequorum_exec_engine[] =
  209. {
  210. { /* 0 */
  211. .exec_handler_fn = message_handler_req_exec_votequorum_nodeinfo,
  212. .exec_endian_convert_fn = exec_votequorum_nodeinfo_endian_convert
  213. },
  214. { /* 1 */
  215. .exec_handler_fn = message_handler_req_exec_votequorum_reconfigure,
  216. .exec_endian_convert_fn = exec_votequorum_reconfigure_endian_convert
  217. },
  218. { /* 2 */
  219. .exec_handler_fn = message_handler_req_exec_votequorum_qdevice_reg,
  220. .exec_endian_convert_fn = exec_votequorum_qdevice_reg_endian_convert
  221. },
  222. { /* 3 */
  223. .exec_handler_fn = message_handler_req_exec_votequorum_qdevice_reconfigure,
  224. .exec_endian_convert_fn = exec_votequorum_qdevice_reconfigure_endian_convert
  225. },
  226. };
  227. /*
  228. * Library Handler and Functions Definitions
  229. */
  230. static int quorum_lib_init_fn (void *conn);
  231. static int quorum_lib_exit_fn (void *conn);
  232. static void message_handler_req_lib_votequorum_getinfo (void *conn,
  233. const void *message);
  234. static void message_handler_req_lib_votequorum_setexpected (void *conn,
  235. const void *message);
  236. static void message_handler_req_lib_votequorum_setvotes (void *conn,
  237. const void *message);
  238. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  239. const void *message);
  240. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  241. const void *message);
  242. static void message_handler_req_lib_votequorum_qdevice_register (void *conn,
  243. const void *message);
  244. static void message_handler_req_lib_votequorum_qdevice_unregister (void *conn,
  245. const void *message);
  246. static void message_handler_req_lib_votequorum_qdevice_update (void *conn,
  247. const void *message);
  248. static void message_handler_req_lib_votequorum_qdevice_poll (void *conn,
  249. const void *message);
  250. static void message_handler_req_lib_votequorum_qdevice_getinfo (void *conn,
  251. const void *message);
  252. static struct corosync_lib_handler quorum_lib_service[] =
  253. {
  254. { /* 0 */
  255. .lib_handler_fn = message_handler_req_lib_votequorum_getinfo,
  256. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  257. },
  258. { /* 1 */
  259. .lib_handler_fn = message_handler_req_lib_votequorum_setexpected,
  260. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  261. },
  262. { /* 2 */
  263. .lib_handler_fn = message_handler_req_lib_votequorum_setvotes,
  264. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  265. },
  266. { /* 3 */
  267. .lib_handler_fn = message_handler_req_lib_votequorum_trackstart,
  268. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  269. },
  270. { /* 4 */
  271. .lib_handler_fn = message_handler_req_lib_votequorum_trackstop,
  272. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  273. },
  274. { /* 5 */
  275. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_register,
  276. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  277. },
  278. { /* 6 */
  279. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_unregister,
  280. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  281. },
  282. { /* 7 */
  283. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_update,
  284. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  285. },
  286. { /* 8 */
  287. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_poll,
  288. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  289. },
  290. { /* 9 */
  291. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_getinfo,
  292. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  293. }
  294. };
  295. static struct corosync_service_engine votequorum_service_engine = {
  296. .name = "corosync vote quorum service v1.0",
  297. .id = VOTEQUORUM_SERVICE,
  298. .priority = 2,
  299. .private_data_size = sizeof (struct quorum_pd),
  300. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  301. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED,
  302. .lib_init_fn = quorum_lib_init_fn,
  303. .lib_exit_fn = quorum_lib_exit_fn,
  304. .lib_engine = quorum_lib_service,
  305. .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler),
  306. .exec_init_fn = votequorum_exec_init_fn,
  307. .exec_exit_fn = votequorum_exec_exit_fn,
  308. .exec_engine = votequorum_exec_engine,
  309. .exec_engine_count = sizeof (votequorum_exec_engine) / sizeof (struct corosync_exec_handler),
  310. .confchg_fn = votequorum_confchg_fn,
  311. .sync_mode = CS_SYNC_V1
  312. };
  313. struct corosync_service_engine *votequorum_get_service_engine_ver0 (void)
  314. {
  315. return (&votequorum_service_engine);
  316. }
  317. static struct default_service votequorum_service[] = {
  318. {
  319. .name = "corosync_votequorum",
  320. .ver = 0,
  321. .loader = votequorum_get_service_engine_ver0
  322. },
  323. };
  324. /*
  325. * common/utility macros/functions
  326. */
  327. #define max(a,b) (((a) > (b)) ? (a) : (b))
  328. #define list_iterate(v, head) \
  329. for (v = (head)->next; v != head; v = v->next)
  330. static void node_add_ordered(struct cluster_node *newnode)
  331. {
  332. struct cluster_node *node = NULL;
  333. struct list_head *tmp;
  334. struct list_head *newlist = &newnode->list;
  335. ENTER();
  336. list_iterate(tmp, &cluster_members_list) {
  337. node = list_entry(tmp, struct cluster_node, list);
  338. if (newnode->node_id < node->node_id) {
  339. break;
  340. }
  341. }
  342. if (!node) {
  343. list_add(&newnode->list, &cluster_members_list);
  344. } else {
  345. newlist->prev = tmp->prev;
  346. newlist->next = tmp;
  347. tmp->prev->next = newlist;
  348. tmp->prev = newlist;
  349. }
  350. LEAVE();
  351. }
  352. static struct cluster_node *allocate_node(unsigned int nodeid)
  353. {
  354. struct cluster_node *cl;
  355. ENTER();
  356. cl = malloc(sizeof(struct cluster_node));
  357. if (cl) {
  358. memset(cl, 0, sizeof(struct cluster_node));
  359. cl->node_id = nodeid;
  360. if (nodeid != NODEID_QDEVICE) {
  361. node_add_ordered(cl);
  362. }
  363. }
  364. LEAVE();
  365. return cl;
  366. }
  367. static struct cluster_node *find_node_by_nodeid(unsigned int nodeid)
  368. {
  369. struct cluster_node *node;
  370. struct list_head *tmp;
  371. ENTER();
  372. if (nodeid == us->node_id) {
  373. LEAVE();
  374. return us;
  375. }
  376. if (nodeid == NODEID_QDEVICE) {
  377. LEAVE();
  378. return qdevice;
  379. }
  380. list_iterate(tmp, &cluster_members_list) {
  381. node = list_entry(tmp, struct cluster_node, list);
  382. if (node->node_id == nodeid) {
  383. LEAVE();
  384. return node;
  385. }
  386. }
  387. LEAVE();
  388. return NULL;
  389. }
  390. static void get_lowest_node_id(void)
  391. {
  392. struct cluster_node *node = NULL;
  393. struct list_head *tmp;
  394. ENTER();
  395. lowest_node_id = us->node_id;
  396. list_iterate(tmp, &cluster_members_list) {
  397. node = list_entry(tmp, struct cluster_node, list);
  398. if ((node->state == NODESTATE_MEMBER) &&
  399. (node->node_id < lowest_node_id)) {
  400. lowest_node_id = node->node_id;
  401. }
  402. }
  403. log_printf(LOGSYS_LEVEL_DEBUG, "lowest node id: %d us: %d", lowest_node_id, us->node_id);
  404. icmap_set_uint32("runtime.votequorum.lowest_node_id", lowest_node_id);
  405. LEAVE();
  406. }
  407. static int check_low_node_id_partition(void)
  408. {
  409. struct cluster_node *node = NULL;
  410. struct list_head *tmp;
  411. int found = 0;
  412. ENTER();
  413. list_iterate(tmp, &cluster_members_list) {
  414. node = list_entry(tmp, struct cluster_node, list);
  415. if ((node->state == NODESTATE_MEMBER) &&
  416. (node->node_id == lowest_node_id)) {
  417. found = 1;
  418. }
  419. }
  420. LEAVE();
  421. return found;
  422. }
  423. static void update_wait_for_all_status(uint8_t wfa_status)
  424. {
  425. wait_for_all_status = wfa_status;
  426. if (wait_for_all_status) {
  427. us->flags |= NODE_FLAGS_WFASTATUS;
  428. } else {
  429. us->flags &= ~NODE_FLAGS_WFASTATUS;
  430. }
  431. icmap_set_uint8("runtime.votequorum.wait_for_all_status",
  432. wait_for_all_status);
  433. }
  434. static void update_two_node(void)
  435. {
  436. icmap_set_uint8("runtime.votequorum.two_node", two_node);
  437. }
  438. static void update_ev_barrier(uint32_t expected_votes)
  439. {
  440. ev_barrier = expected_votes;
  441. icmap_set_uint32("runtime.votequorum.ev_barrier", ev_barrier);
  442. }
  443. static void update_qdevice_can_operate(uint8_t status)
  444. {
  445. qdevice_can_operate = status;
  446. icmap_set_uint8("runtime.votequorum.qdevice_can_operate", qdevice_can_operate);
  447. }
  448. /*
  449. * quorum calculation core bits
  450. */
  451. static int calculate_quorum(int allow_decrease, unsigned int max_expected, unsigned int *ret_total_votes)
  452. {
  453. struct list_head *nodelist;
  454. struct cluster_node *node;
  455. unsigned int total_votes = 0;
  456. unsigned int highest_expected = 0;
  457. unsigned int newquorum, q1, q2;
  458. unsigned int total_nodes = 0;
  459. ENTER();
  460. if ((leave_remove) && (allow_decrease) && (max_expected)) {
  461. max_expected = max(ev_barrier, max_expected);
  462. }
  463. list_iterate(nodelist, &cluster_members_list) {
  464. node = list_entry(nodelist, struct cluster_node, list);
  465. log_printf(LOGSYS_LEVEL_DEBUG, "node %u state=%d, votes=%u, expected=%u",
  466. node->node_id, node->state, node->votes, node->expected_votes);
  467. if (node->state == NODESTATE_MEMBER) {
  468. if (max_expected) {
  469. node->expected_votes = max_expected;
  470. } else {
  471. highest_expected = max(highest_expected, node->expected_votes);
  472. }
  473. total_votes += node->votes;
  474. total_nodes++;
  475. }
  476. }
  477. if (qdevice_is_registered) {
  478. log_printf(LOGSYS_LEVEL_DEBUG, "node %u state=%d, votes=%u",
  479. qdevice->node_id, qdevice->state, qdevice->votes);
  480. if (qdevice->state == NODESTATE_MEMBER) {
  481. total_votes += qdevice->votes;
  482. total_nodes++;
  483. }
  484. }
  485. if (max_expected > 0) {
  486. highest_expected = max_expected;
  487. }
  488. /*
  489. * This quorum calculation is taken from the OpenVMS Cluster Systems
  490. * manual, but, then, you guessed that didn't you
  491. */
  492. q1 = (highest_expected + 2) / 2;
  493. q2 = (total_votes + 2) / 2;
  494. newquorum = max(q1, q2);
  495. /*
  496. * Normally quorum never decreases but the system administrator can
  497. * force it down by setting expected votes to a maximum value
  498. */
  499. if (!allow_decrease) {
  500. newquorum = max(quorum, newquorum);
  501. }
  502. /*
  503. * The special two_node mode allows each of the two nodes to retain
  504. * quorum if the other fails. Only one of the two should live past
  505. * fencing (as both nodes try to fence each other in split-brain.)
  506. * Also: if there are more than two nodes, force us inquorate to avoid
  507. * any damage or confusion.
  508. */
  509. if (two_node && total_nodes <= 2) {
  510. newquorum = 1;
  511. }
  512. if (ret_total_votes) {
  513. *ret_total_votes = total_votes;
  514. }
  515. LEAVE();
  516. return newquorum;
  517. }
  518. static void are_we_quorate(unsigned int total_votes)
  519. {
  520. int quorate;
  521. int quorum_change = 0;
  522. ENTER();
  523. /*
  524. * wait for all nodes to show up before granting quorum
  525. */
  526. if ((wait_for_all) && (wait_for_all_status)) {
  527. if (total_votes != us->expected_votes) {
  528. log_printf(LOGSYS_LEVEL_NOTICE,
  529. "Waiting for all cluster members. "
  530. "Current votes: %d expected_votes: %d",
  531. total_votes, us->expected_votes);
  532. cluster_is_quorate = 0;
  533. return;
  534. }
  535. update_wait_for_all_status(0);
  536. }
  537. if (quorum > total_votes) {
  538. quorate = 0;
  539. } else {
  540. quorate = 1;
  541. get_lowest_node_id();
  542. }
  543. if ((auto_tie_breaker) &&
  544. (total_votes == (us->expected_votes / 2)) &&
  545. (check_low_node_id_partition() == 1)) {
  546. quorate = 1;
  547. }
  548. if (cluster_is_quorate && !quorate) {
  549. quorum_change = 1;
  550. log_printf(LOGSYS_LEVEL_DEBUG, "quorum lost, blocking activity");
  551. }
  552. if (!cluster_is_quorate && quorate) {
  553. quorum_change = 1;
  554. log_printf(LOGSYS_LEVEL_DEBUG, "quorum regained, resuming activity");
  555. }
  556. cluster_is_quorate = quorate;
  557. if (cluster_is_quorate) {
  558. us->flags |= NODE_FLAGS_QUORATE;
  559. } else {
  560. us->flags &= ~NODE_FLAGS_QUORATE;
  561. }
  562. if (wait_for_all) {
  563. if (quorate) {
  564. update_wait_for_all_status(0);
  565. } else {
  566. update_wait_for_all_status(1);
  567. }
  568. }
  569. if (quorum_change) {
  570. quorum_callback(quorum_members, quorum_members_entries,
  571. cluster_is_quorate, &quorum_ringid);
  572. }
  573. LEAVE();
  574. }
  575. /*
  576. * Recalculate cluster quorum, set quorate and notify changes
  577. */
  578. static void recalculate_quorum(int allow_decrease, int by_current_nodes)
  579. {
  580. unsigned int total_votes = 0;
  581. int cluster_members = 0;
  582. struct list_head *nodelist;
  583. struct cluster_node *node;
  584. ENTER();
  585. list_iterate(nodelist, &cluster_members_list) {
  586. node = list_entry(nodelist, struct cluster_node, list);
  587. if (node->state == NODESTATE_MEMBER) {
  588. if (by_current_nodes) {
  589. cluster_members++;
  590. }
  591. total_votes += node->votes;
  592. }
  593. }
  594. if (qdevice->votes) {
  595. total_votes += qdevice->votes;
  596. if (by_current_nodes) {
  597. cluster_members++;
  598. }
  599. }
  600. /*
  601. * Keep expected_votes at the highest number of votes in the cluster
  602. */
  603. log_printf(LOGSYS_LEVEL_DEBUG, "total_votes=%d, expected_votes=%d", total_votes, us->expected_votes);
  604. if (total_votes > us->expected_votes) {
  605. us->expected_votes = total_votes;
  606. votequorum_exec_send_expectedvotes_notification();
  607. }
  608. quorum = calculate_quorum(allow_decrease, cluster_members, &total_votes);
  609. are_we_quorate(total_votes);
  610. votequorum_exec_send_quorum_notification(NULL, 0L);
  611. LEAVE();
  612. }
  613. /*
  614. * configuration bits and pieces
  615. */
  616. static int votequorum_read_nodelist_configuration(uint32_t *votes,
  617. uint32_t *nodes,
  618. uint32_t *expected_votes)
  619. {
  620. icmap_iter_t iter;
  621. const char *iter_key;
  622. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  623. uint32_t our_pos, node_pos;
  624. uint32_t nodecount = 0;
  625. uint32_t nodelist_expected_votes = 0;
  626. uint32_t node_votes = 0;
  627. int res = 0;
  628. ENTER();
  629. if (icmap_get_uint32("nodelist.local_node_pos", &our_pos) != CS_OK) {
  630. log_printf(LOGSYS_LEVEL_DEBUG,
  631. "No nodelist defined or our node is not in the nodelist");
  632. return 0;
  633. }
  634. iter = icmap_iter_init("nodelist.node.");
  635. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  636. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  637. if (res != 2) {
  638. continue;
  639. }
  640. if (strcmp(tmp_key, "ring0_addr") != 0) {
  641. continue;
  642. }
  643. nodecount++;
  644. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.quorum_votes", node_pos);
  645. if (icmap_get_uint32(tmp_key, &node_votes) != CS_OK) {
  646. node_votes = 1;
  647. }
  648. nodelist_expected_votes = nodelist_expected_votes + node_votes;
  649. if (node_pos == our_pos) {
  650. *votes = node_votes;
  651. }
  652. }
  653. *expected_votes = nodelist_expected_votes;
  654. *nodes = nodecount;
  655. icmap_iter_finalize(iter);
  656. LEAVE();
  657. return 1;
  658. }
  659. static int votequorum_qdevice_is_configured(uint32_t *qdevice_votes)
  660. {
  661. char *qdevice_model = NULL;
  662. ENTER();
  663. if ((icmap_get_string("quorum.device.model", &qdevice_model) == CS_OK) &&
  664. (strlen(qdevice_model))) {
  665. free(qdevice_model);
  666. if (icmap_get_uint32("quorum.device.votes", qdevice_votes) != CS_OK) {
  667. *qdevice_votes = -1;
  668. }
  669. if (icmap_get_uint32("quorum.device.timeout", &qdevice_timeout) != CS_OK) {
  670. qdevice_timeout = DEFAULT_QDEVICE_TIMEOUT;
  671. }
  672. update_qdevice_can_operate(1);
  673. return 1;
  674. }
  675. LEAVE();
  676. return 0;
  677. }
  678. /*
  679. * votequorum_readconfig_static is executed before
  680. * votequorum_readconfig_dynamic
  681. *
  682. * errors from _static are fatal
  683. * errors from _dynamic need to be handled at runtime without self-destruction
  684. *
  685. * TODO: static/dynamic shares a lot of checks _and_
  686. * there are probably more options we can change at runtime
  687. * than we allow now. Review this in 2.1
  688. */
  689. static char *votequorum_readconfig_static(void)
  690. {
  691. uint32_t node_votes = 0, qdevice_votes = 0;
  692. uint32_t node_expected_votes = 0, expected_votes = 0;
  693. uint32_t node_count = 0;
  694. int have_nodelist, have_qdevice;
  695. ENTER();
  696. log_printf(LOGSYS_LEVEL_DEBUG, "Reading static configuration");
  697. /*
  698. * gather basic data here
  699. */
  700. icmap_get_uint32("quorum.expected_votes", &expected_votes);
  701. have_nodelist = votequorum_read_nodelist_configuration(&node_votes, &node_count, &node_expected_votes);
  702. have_qdevice = votequorum_qdevice_is_configured(&qdevice_votes);
  703. icmap_get_uint8("quorum.two_node", &two_node);
  704. update_two_node();
  705. /*
  706. * do basic config verification
  707. */
  708. if ((!have_nodelist) && (!expected_votes)) {
  709. return ((char *)"configuration error: nodelist or quorum.expected_votes must be configured!");
  710. }
  711. if ((two_node) && (have_qdevice)) {
  712. return ((char *)"configuration error: two_node and quorum device cannot be configured at the same time!");
  713. }
  714. if ((expected_votes) && (have_qdevice) && (qdevice_votes == -1)) {
  715. return ((char *)"configuration error: quorum.device.votes must be specified when quorum.expected_votes is set");
  716. }
  717. if ((have_qdevice) &&
  718. (qdevice_votes == -1) &&
  719. (have_nodelist) &&
  720. (node_count != node_expected_votes)) {
  721. return ((char *)"configuration error: quorum.device.votes must be specified when not all nodes votes 1");
  722. }
  723. if ((qdevice_votes > 0) && (expected_votes)) {
  724. int delta = expected_votes - qdevice_votes;
  725. if (delta < 2) {
  726. return ((char *)"configuration error: quorum.device.votes is too high or expected_votes is too low");
  727. }
  728. }
  729. /*
  730. * Enable special features
  731. */
  732. if (two_node) {
  733. wait_for_all = 1;
  734. }
  735. icmap_get_uint8("quorum.leave_remove", &leave_remove);
  736. icmap_get_uint8("quorum.wait_for_all", &wait_for_all);
  737. icmap_get_uint8("quorum.auto_tie_breaker", &auto_tie_breaker);
  738. icmap_get_uint8("quorum.last_man_standing", &last_man_standing);
  739. icmap_get_uint32("quorum.last_man_standing_window", &last_man_standing_window);
  740. if ((have_qdevice) && (last_man_standing)) {
  741. return ((char *)"configuration error: quorum device is not compatible with last_man_standing feature");
  742. }
  743. if ((have_qdevice) && (auto_tie_breaker)) {
  744. return ((char *)"configuration error: quorum device is not compatible with auto_tie_breaker feature");
  745. }
  746. LEAVE();
  747. return (NULL);
  748. }
  749. static void votequorum_readconfig_dynamic(void)
  750. {
  751. uint32_t node_votes = 0, qdevice_votes = 0;
  752. uint32_t node_expected_votes = 0, expected_votes = 0;
  753. uint32_t node_count = 0;
  754. int have_nodelist, have_qdevice;
  755. ENTER();
  756. log_printf(LOGSYS_LEVEL_DEBUG, "Reading dynamic configuration");
  757. /*
  758. * gather basic data here
  759. * TODO: make it common with static!!!
  760. */
  761. icmap_get_uint32("quorum.expected_votes", &expected_votes);
  762. have_nodelist = votequorum_read_nodelist_configuration(&node_votes, &node_count, &node_expected_votes);
  763. have_qdevice = votequorum_qdevice_is_configured(&qdevice_votes);
  764. icmap_get_uint8("quorum.two_node", &two_node);
  765. /*
  766. * do config verification and enablement
  767. */
  768. if ((!have_nodelist) && (!expected_votes)) {
  769. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: nodelist or quorum.expected_votes must be configured!");
  770. log_printf(LOGSYS_LEVEL_CRIT, "will continue with current runtime data");
  771. goto out;
  772. }
  773. /*
  774. * two_node and qdevice are not compatible in the same config.
  775. * try to make an educated guess of what to do
  776. */
  777. if ((two_node) && (have_qdevice)) {
  778. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: two_node and quorum device cannot be configured at the same time!");
  779. if (qdevice_is_registered) {
  780. log_printf(LOGSYS_LEVEL_CRIT, "quorum device is registered, disabling two_node");
  781. two_node = 0;
  782. } else {
  783. log_printf(LOGSYS_LEVEL_CRIT, "quorum device is not registered, allowing two_node");
  784. update_qdevice_can_operate(0);
  785. }
  786. }
  787. /*
  788. * quorum device is not compatible with last_man_standing and auto_tie_breaker
  789. * neither lms or atb can be set at runtime, so there is no need to check for
  790. * runtime incompatibilities, but qdevice can be configured _after_ LMS and ATB have
  791. * been enabled at startup.
  792. */
  793. if ((have_qdevice) && (last_man_standing)) {
  794. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device is not compatible with last_man_standing");
  795. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  796. update_qdevice_can_operate(0);
  797. }
  798. if ((have_qdevice) && (auto_tie_breaker)) {
  799. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device is not compatible with auto_tie_breaker");
  800. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  801. update_qdevice_can_operate(0);
  802. }
  803. /*
  804. * if user specifies quorum.expected_votes + quorum.device but NOT the device.votes
  805. * we don't know what the quorum device should vote.
  806. */
  807. if ((expected_votes) && (have_qdevice) && (qdevice_votes == -1)) {
  808. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device.votes must be specified when quorum.expected_votes is set");
  809. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  810. update_qdevice_can_operate(0);
  811. }
  812. /*
  813. * if user specifies a node list with uneven votes and no device.votes
  814. * we cannot autocalculate the votes
  815. */
  816. if ((have_qdevice) &&
  817. (qdevice_votes == -1) &&
  818. (have_nodelist) &&
  819. (node_count != node_expected_votes)) {
  820. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device.votes must be specified when not all nodes votes 1");
  821. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  822. update_qdevice_can_operate(0);
  823. }
  824. /*
  825. * validate quorum device votes vs expected_votes
  826. */
  827. if ((qdevice_votes > 0) && (expected_votes)) {
  828. int delta = expected_votes - qdevice_votes;
  829. if (delta < 2) {
  830. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device.votes is too high or expected_votes is too low");
  831. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  832. update_qdevice_can_operate(0);
  833. }
  834. }
  835. /*
  836. * automatically calculate device votes and adjust expected_votes from nodelist
  837. */
  838. if ((have_qdevice) &&
  839. (qdevice_votes == -1) &&
  840. (!expected_votes) &&
  841. (have_nodelist) &&
  842. (node_count == node_expected_votes)) {
  843. qdevice_votes = node_expected_votes - 1;
  844. node_expected_votes = node_expected_votes + qdevice_votes;
  845. }
  846. /*
  847. * set this node votes and expected_votes
  848. */
  849. if (have_nodelist) {
  850. us->votes = node_votes;
  851. us->expected_votes = node_expected_votes;
  852. } else {
  853. us->votes = 1;
  854. icmap_get_uint32("quorum.votes", &us->votes);
  855. }
  856. if (expected_votes) {
  857. us->expected_votes = expected_votes;
  858. }
  859. /*
  860. * set qdevice votes
  861. */
  862. if (!have_qdevice) {
  863. qdevice->votes = 0;
  864. }
  865. if (qdevice_votes != -1) {
  866. qdevice->votes = qdevice_votes;
  867. }
  868. update_ev_barrier(us->expected_votes);
  869. update_two_node();
  870. out:
  871. LEAVE();
  872. }
  873. static void votequorum_refresh_config(
  874. int32_t event,
  875. const char *key_name,
  876. struct icmap_notify_value new_val,
  877. struct icmap_notify_value old_val,
  878. void *user_data)
  879. {
  880. unsigned int old_votes;
  881. unsigned int old_expected;
  882. unsigned int old_qdevice_votes;
  883. ENTER();
  884. old_votes = us->votes;
  885. old_expected = us->expected_votes;
  886. old_qdevice_votes = qdevice->votes;
  887. /*
  888. * Reload the configuration
  889. */
  890. votequorum_readconfig_dynamic();
  891. /*
  892. * Check for fundamental changes that we need to propogate
  893. */
  894. if (old_votes != us->votes) {
  895. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, us->node_id, us->votes);
  896. }
  897. if (old_expected != us->expected_votes) {
  898. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES, us->node_id, us->expected_votes);
  899. }
  900. if (old_qdevice_votes != qdevice->votes) {
  901. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, NODEID_QDEVICE, qdevice->votes);
  902. }
  903. LEAVE();
  904. }
  905. static void votequorum_exec_add_config_notification(void)
  906. {
  907. icmap_track_t icmap_track_nodelist = NULL;
  908. icmap_track_t icmap_track_quorum = NULL;
  909. ENTER();
  910. icmap_track_add("nodelist.",
  911. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  912. votequorum_refresh_config,
  913. NULL,
  914. &icmap_track_nodelist);
  915. icmap_track_add("quorum.",
  916. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  917. votequorum_refresh_config,
  918. NULL,
  919. &icmap_track_quorum);
  920. LEAVE();
  921. }
  922. /*
  923. * votequorum_exec core
  924. */
  925. static int votequorum_exec_send_reconfigure(uint8_t param, unsigned int nodeid, uint32_t value)
  926. {
  927. struct req_exec_quorum_reconfigure req_exec_quorum_reconfigure;
  928. struct iovec iov[1];
  929. int ret;
  930. ENTER();
  931. req_exec_quorum_reconfigure.nodeid = nodeid;
  932. req_exec_quorum_reconfigure.value = value;
  933. req_exec_quorum_reconfigure.param = param;
  934. req_exec_quorum_reconfigure._pad0 = 0;
  935. req_exec_quorum_reconfigure._pad1 = 0;
  936. req_exec_quorum_reconfigure._pad2 = 0;
  937. req_exec_quorum_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE);
  938. req_exec_quorum_reconfigure.header.size = sizeof(req_exec_quorum_reconfigure);
  939. iov[0].iov_base = (void *)&req_exec_quorum_reconfigure;
  940. iov[0].iov_len = sizeof(req_exec_quorum_reconfigure);
  941. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  942. LEAVE();
  943. return ret;
  944. }
  945. static int votequorum_exec_send_nodeinfo(uint32_t nodeid)
  946. {
  947. struct req_exec_quorum_nodeinfo req_exec_quorum_nodeinfo;
  948. struct iovec iov[1];
  949. struct cluster_node *node;
  950. int ret;
  951. ENTER();
  952. node = find_node_by_nodeid(nodeid);
  953. if (!node) {
  954. return -1;
  955. }
  956. req_exec_quorum_nodeinfo.nodeid = nodeid;
  957. req_exec_quorum_nodeinfo.votes = node->votes;
  958. req_exec_quorum_nodeinfo.expected_votes = node->expected_votes;
  959. req_exec_quorum_nodeinfo.flags = node->flags;
  960. req_exec_quorum_nodeinfo.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO);
  961. req_exec_quorum_nodeinfo.header.size = sizeof(req_exec_quorum_nodeinfo);
  962. iov[0].iov_base = (void *)&req_exec_quorum_nodeinfo;
  963. iov[0].iov_len = sizeof(req_exec_quorum_nodeinfo);
  964. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  965. LEAVE();
  966. return ret;
  967. }
  968. static int votequorum_exec_send_qdevice_reconfigure(const char *oldname, const char *newname)
  969. {
  970. struct req_exec_quorum_qdevice_reconfigure req_exec_quorum_qdevice_reconfigure;
  971. struct iovec iov[1];
  972. int ret;
  973. ENTER();
  974. req_exec_quorum_qdevice_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_QDEVICE_RECONFIGURE);
  975. req_exec_quorum_qdevice_reconfigure.header.size = sizeof(req_exec_quorum_qdevice_reconfigure);
  976. strcpy(req_exec_quorum_qdevice_reconfigure.oldname, oldname);
  977. strcpy(req_exec_quorum_qdevice_reconfigure.newname, newname);
  978. iov[0].iov_base = (void *)&req_exec_quorum_qdevice_reconfigure;
  979. iov[0].iov_len = sizeof(req_exec_quorum_qdevice_reconfigure);
  980. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  981. LEAVE();
  982. return ret;
  983. }
  984. static int votequorum_exec_send_qdevice_reg(uint32_t operation, const char *qdevice_name_req)
  985. {
  986. struct req_exec_quorum_qdevice_reg req_exec_quorum_qdevice_reg;
  987. struct iovec iov[1];
  988. int ret;
  989. ENTER();
  990. req_exec_quorum_qdevice_reg.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_QDEVICE_REG);
  991. req_exec_quorum_qdevice_reg.header.size = sizeof(req_exec_quorum_qdevice_reg);
  992. req_exec_quorum_qdevice_reg.operation = operation;
  993. strcpy(req_exec_quorum_qdevice_reg.qdevice_name, qdevice_name_req);
  994. iov[0].iov_base = (void *)&req_exec_quorum_qdevice_reg;
  995. iov[0].iov_len = sizeof(req_exec_quorum_qdevice_reg);
  996. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  997. LEAVE();
  998. return ret;
  999. }
  1000. static int votequorum_exec_send_quorum_notification(void *conn, uint64_t context)
  1001. {
  1002. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  1003. struct list_head *tmp;
  1004. struct cluster_node *node;
  1005. int cluster_members = 0;
  1006. int i = 0;
  1007. int size;
  1008. char *buf;
  1009. ENTER();
  1010. list_iterate(tmp, &cluster_members_list) {
  1011. node = list_entry(tmp, struct cluster_node, list);
  1012. cluster_members++;
  1013. }
  1014. if (qdevice_is_registered) {
  1015. cluster_members++;
  1016. }
  1017. size = sizeof(struct res_lib_votequorum_notification) + sizeof(struct votequorum_node) * cluster_members;
  1018. buf = alloca(size);
  1019. if (!buf) {
  1020. LEAVE();
  1021. return -1;
  1022. }
  1023. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)buf;
  1024. res_lib_votequorum_notification->quorate = cluster_is_quorate;
  1025. res_lib_votequorum_notification->node_list_entries = cluster_members;
  1026. res_lib_votequorum_notification->context = context;
  1027. list_iterate(tmp, &cluster_members_list) {
  1028. node = list_entry(tmp, struct cluster_node, list);
  1029. res_lib_votequorum_notification->node_list[i].nodeid = node->node_id;
  1030. res_lib_votequorum_notification->node_list[i++].state = node->state;
  1031. }
  1032. if (qdevice_is_registered) {
  1033. res_lib_votequorum_notification->node_list[i].nodeid = NODEID_QDEVICE;
  1034. res_lib_votequorum_notification->node_list[i++].state = qdevice->state;
  1035. }
  1036. res_lib_votequorum_notification->header.id = MESSAGE_RES_VOTEQUORUM_NOTIFICATION;
  1037. res_lib_votequorum_notification->header.size = size;
  1038. res_lib_votequorum_notification->header.error = CS_OK;
  1039. /* Send it to all interested parties */
  1040. if (conn) {
  1041. int ret = corosync_api->ipc_dispatch_send(conn, buf, size);
  1042. LEAVE();
  1043. return ret;
  1044. } else {
  1045. struct quorum_pd *qpd;
  1046. list_iterate(tmp, &trackers_list) {
  1047. qpd = list_entry(tmp, struct quorum_pd, list);
  1048. res_lib_votequorum_notification->context = qpd->tracking_context;
  1049. corosync_api->ipc_dispatch_send(qpd->conn, buf, size);
  1050. }
  1051. }
  1052. LEAVE();
  1053. return 0;
  1054. }
  1055. static void votequorum_exec_send_expectedvotes_notification(void)
  1056. {
  1057. struct res_lib_votequorum_expectedvotes_notification res_lib_votequorum_expectedvotes_notification;
  1058. struct quorum_pd *qpd;
  1059. struct list_head *tmp;
  1060. ENTER();
  1061. log_printf(LOGSYS_LEVEL_DEBUG, "Sending expected votes callback");
  1062. res_lib_votequorum_expectedvotes_notification.header.id = MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION;
  1063. res_lib_votequorum_expectedvotes_notification.header.size = sizeof(res_lib_votequorum_expectedvotes_notification);
  1064. res_lib_votequorum_expectedvotes_notification.header.error = CS_OK;
  1065. res_lib_votequorum_expectedvotes_notification.expected_votes = us->expected_votes;
  1066. list_iterate(tmp, &trackers_list) {
  1067. qpd = list_entry(tmp, struct quorum_pd, list);
  1068. res_lib_votequorum_expectedvotes_notification.context = qpd->tracking_context;
  1069. corosync_api->ipc_dispatch_send(qpd->conn, &res_lib_votequorum_expectedvotes_notification,
  1070. sizeof(struct res_lib_votequorum_expectedvotes_notification));
  1071. }
  1072. LEAVE();
  1073. }
  1074. static void exec_votequorum_qdevice_reconfigure_endian_convert (void *message)
  1075. {
  1076. ENTER();
  1077. LEAVE();
  1078. }
  1079. static void message_handler_req_exec_votequorum_qdevice_reconfigure (
  1080. const void *message,
  1081. unsigned int nodeid)
  1082. {
  1083. const struct req_exec_quorum_qdevice_reconfigure *req_exec_quorum_qdevice_reconfigure = message;
  1084. ENTER();
  1085. log_printf(LOGSYS_LEVEL_DEBUG, "Received qdevice name change req from node %u [from: %s to: %s]",
  1086. nodeid,
  1087. req_exec_quorum_qdevice_reconfigure->oldname,
  1088. req_exec_quorum_qdevice_reconfigure->newname);
  1089. if (!strcmp(req_exec_quorum_qdevice_reconfigure->oldname, qdevice_name)) {
  1090. log_printf(LOGSYS_LEVEL_DEBUG, "Allowing qdevice rename");
  1091. memset(qdevice_name, 0, VOTEQUORUM_MAX_QDEVICE_NAME_LEN);
  1092. strcpy(qdevice_name, req_exec_quorum_qdevice_reconfigure->newname);
  1093. /*
  1094. * TODO: notify qdevices about name change?
  1095. * this is not relevant for now and can wait later on since
  1096. * qdevices are local only and libvotequorum is not final
  1097. */
  1098. }
  1099. LEAVE();
  1100. }
  1101. static void exec_votequorum_qdevice_reg_endian_convert (void *message)
  1102. {
  1103. struct req_exec_quorum_qdevice_reg *req_exec_quorum_qdevice_reg = message;
  1104. ENTER();
  1105. req_exec_quorum_qdevice_reg->operation = swab32(req_exec_quorum_qdevice_reg->operation);
  1106. LEAVE();
  1107. }
  1108. static void message_handler_req_exec_votequorum_qdevice_reg (
  1109. const void *message,
  1110. unsigned int nodeid)
  1111. {
  1112. const struct req_exec_quorum_qdevice_reg *req_exec_quorum_qdevice_reg = message;
  1113. struct res_lib_votequorum_status res_lib_votequorum_status;
  1114. int wipe_qdevice_name = 1;
  1115. struct cluster_node *node = NULL;
  1116. struct list_head *tmp;
  1117. cs_error_t error = CS_OK;
  1118. ENTER();
  1119. log_printf(LOGSYS_LEVEL_DEBUG, "Received qdevice op %u req from node %u [%s]",
  1120. req_exec_quorum_qdevice_reg->operation,
  1121. nodeid, req_exec_quorum_qdevice_reg->qdevice_name);
  1122. switch(req_exec_quorum_qdevice_reg->operation)
  1123. {
  1124. case VOTEQUORUM_QDEVICE_OPERATION_REGISTER:
  1125. if (nodeid != us->node_id) {
  1126. if (!strlen(qdevice_name)) {
  1127. log_printf(LOGSYS_LEVEL_DEBUG, "Remote qdevice name recorded");
  1128. strcpy(qdevice_name, req_exec_quorum_qdevice_reg->qdevice_name);
  1129. }
  1130. LEAVE();
  1131. return;
  1132. }
  1133. /*
  1134. * this should NEVER happen
  1135. */
  1136. if (!qdevice_reg_conn) {
  1137. log_printf(LOGSYS_LEVEL_WARNING, "Unable to determine origin of the qdevice register call!");
  1138. LEAVE();
  1139. return;
  1140. }
  1141. /*
  1142. * registering our own device in this case
  1143. */
  1144. if (!strlen(qdevice_name)) {
  1145. strcpy(qdevice_name, req_exec_quorum_qdevice_reg->qdevice_name);
  1146. }
  1147. /*
  1148. * check if it is our device or something else
  1149. */
  1150. if ((!strncmp(req_exec_quorum_qdevice_reg->qdevice_name,
  1151. qdevice_name, VOTEQUORUM_MAX_QDEVICE_NAME_LEN))) {
  1152. qdevice_is_registered = 1;
  1153. us->flags |= NODE_FLAGS_QDEVICE;
  1154. votequorum_exec_send_nodeinfo(NODEID_QDEVICE);
  1155. votequorum_exec_send_nodeinfo(us->node_id);
  1156. } else {
  1157. log_printf(LOGSYS_LEVEL_WARNING,
  1158. "A new qdevice with different name (new: %s old: %s) is trying to register!",
  1159. req_exec_quorum_qdevice_reg->qdevice_name, qdevice_name);
  1160. error = CS_ERR_EXIST;
  1161. }
  1162. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1163. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1164. res_lib_votequorum_status.header.error = error;
  1165. corosync_api->ipc_response_send(qdevice_reg_conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1166. qdevice_reg_conn = NULL;
  1167. break;
  1168. case VOTEQUORUM_QDEVICE_OPERATION_UNREGISTER:
  1169. list_iterate(tmp, &cluster_members_list) {
  1170. node = list_entry(tmp, struct cluster_node, list);
  1171. if ((node->state == NODESTATE_MEMBER) &&
  1172. (node->flags & NODE_FLAGS_QDEVICE)) {
  1173. wipe_qdevice_name = 0;
  1174. }
  1175. }
  1176. if (wipe_qdevice_name) {
  1177. memset(qdevice_name, 0, VOTEQUORUM_MAX_QDEVICE_NAME_LEN);
  1178. }
  1179. break;
  1180. }
  1181. LEAVE();
  1182. }
  1183. static void exec_votequorum_nodeinfo_endian_convert (void *message)
  1184. {
  1185. struct req_exec_quorum_nodeinfo *nodeinfo = message;
  1186. ENTER();
  1187. nodeinfo->nodeid = swab32(nodeinfo->nodeid);
  1188. nodeinfo->votes = swab32(nodeinfo->votes);
  1189. nodeinfo->expected_votes = swab32(nodeinfo->expected_votes);
  1190. nodeinfo->flags = swab32(nodeinfo->flags);
  1191. LEAVE();
  1192. }
  1193. static void message_handler_req_exec_votequorum_nodeinfo (
  1194. const void *message,
  1195. unsigned int sender_nodeid)
  1196. {
  1197. const struct req_exec_quorum_nodeinfo *req_exec_quorum_nodeinfo = message;
  1198. struct cluster_node *node = NULL;
  1199. int old_votes;
  1200. int old_expected;
  1201. uint16_t old_flags;
  1202. nodestate_t old_state;
  1203. int new_node = 0;
  1204. int allow_downgrade = 0;
  1205. int by_node = 0;
  1206. unsigned int nodeid = req_exec_quorum_nodeinfo->nodeid;
  1207. ENTER();
  1208. log_printf(LOGSYS_LEVEL_DEBUG, "got nodeinfo message from cluster node %u", sender_nodeid);
  1209. log_printf(LOGSYS_LEVEL_DEBUG, "nodeinfo message[%u]: votes: %d, expected: %d flags: %d",
  1210. nodeid,
  1211. req_exec_quorum_nodeinfo->votes,
  1212. req_exec_quorum_nodeinfo->expected_votes,
  1213. req_exec_quorum_nodeinfo->flags);
  1214. node = find_node_by_nodeid(nodeid);
  1215. if (!node) {
  1216. node = allocate_node(nodeid);
  1217. new_node = 1;
  1218. }
  1219. if (!node) {
  1220. corosync_api->error_memory_failure();
  1221. LEAVE();
  1222. return;
  1223. }
  1224. if (new_node) {
  1225. old_votes = 0;
  1226. old_expected = 0;
  1227. old_state = NODESTATE_DEAD;
  1228. old_flags = 0;
  1229. } else {
  1230. old_votes = node->votes;
  1231. old_expected = node->expected_votes;
  1232. old_state = node->state;
  1233. old_flags = node->flags;
  1234. }
  1235. /* Update node state */
  1236. node->flags = req_exec_quorum_nodeinfo->flags;
  1237. if (nodeid != NODEID_QDEVICE) {
  1238. node->votes = req_exec_quorum_nodeinfo->votes;
  1239. } else {
  1240. if ((!cluster_is_quorate) &&
  1241. (req_exec_quorum_nodeinfo->flags & NODE_FLAGS_QUORATE)) {
  1242. node->votes = req_exec_quorum_nodeinfo->votes;
  1243. } else {
  1244. node->votes = max(node->votes, req_exec_quorum_nodeinfo->votes);
  1245. }
  1246. }
  1247. if (node->flags & NODE_FLAGS_LEAVING) {
  1248. node->state = NODESTATE_LEAVING;
  1249. allow_downgrade = 1;
  1250. by_node = 1;
  1251. } else {
  1252. if (nodeid != NODEID_QDEVICE) {
  1253. node->state = NODESTATE_MEMBER;
  1254. } else {
  1255. /*
  1256. * qdevice status is only local to the node
  1257. */
  1258. node->state = old_state;
  1259. }
  1260. }
  1261. if (nodeid != NODEID_QDEVICE) {
  1262. if ((!cluster_is_quorate) &&
  1263. (req_exec_quorum_nodeinfo->flags & NODE_FLAGS_QUORATE)) {
  1264. allow_downgrade = 1;
  1265. us->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  1266. }
  1267. if (req_exec_quorum_nodeinfo->flags & NODE_FLAGS_QUORATE) {
  1268. node->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  1269. } else {
  1270. node->expected_votes = us->expected_votes;
  1271. }
  1272. if ((last_man_standing) && (req_exec_quorum_nodeinfo->votes > 1)) {
  1273. log_printf(LOGSYS_LEVEL_WARNING, "Last Man Standing feature is supported only when all"
  1274. "cluster nodes votes are set to 1. Disabling LMS.");
  1275. last_man_standing = 0;
  1276. if (last_man_standing_timer_set) {
  1277. corosync_api->timer_delete(last_man_standing_timer);
  1278. last_man_standing_timer_set = 0;
  1279. }
  1280. }
  1281. }
  1282. if (new_node ||
  1283. nodeid == NODEID_QDEVICE ||
  1284. req_exec_quorum_nodeinfo->flags & NODE_FLAGS_FIRST ||
  1285. old_votes != node->votes ||
  1286. old_expected != node->expected_votes ||
  1287. old_flags != node->flags ||
  1288. old_state != node->state) {
  1289. recalculate_quorum(allow_downgrade, by_node);
  1290. }
  1291. if ((wait_for_all) &&
  1292. (!(req_exec_quorum_nodeinfo->flags & NODE_FLAGS_WFASTATUS)) &&
  1293. (req_exec_quorum_nodeinfo->flags & NODE_FLAGS_QUORATE)) {
  1294. update_wait_for_all_status(0);
  1295. }
  1296. LEAVE();
  1297. }
  1298. static void exec_votequorum_reconfigure_endian_convert (void *message)
  1299. {
  1300. struct req_exec_quorum_reconfigure *reconfigure = message;
  1301. ENTER();
  1302. reconfigure->nodeid = swab32(reconfigure->nodeid);
  1303. reconfigure->value = swab32(reconfigure->value);
  1304. LEAVE();
  1305. }
  1306. static void message_handler_req_exec_votequorum_reconfigure (
  1307. const void *message,
  1308. unsigned int nodeid)
  1309. {
  1310. const struct req_exec_quorum_reconfigure *req_exec_quorum_reconfigure = message;
  1311. struct cluster_node *node;
  1312. struct list_head *nodelist;
  1313. ENTER();
  1314. log_printf(LOGSYS_LEVEL_DEBUG, "got reconfigure message from cluster node %u for %u",
  1315. nodeid, req_exec_quorum_reconfigure->nodeid);
  1316. node = find_node_by_nodeid(req_exec_quorum_reconfigure->nodeid);
  1317. if (!node) {
  1318. LEAVE();
  1319. return;
  1320. }
  1321. switch(req_exec_quorum_reconfigure->param)
  1322. {
  1323. case VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES:
  1324. list_iterate(nodelist, &cluster_members_list) {
  1325. node = list_entry(nodelist, struct cluster_node, list);
  1326. if (node->state == NODESTATE_MEMBER) {
  1327. node->expected_votes = req_exec_quorum_reconfigure->value;
  1328. }
  1329. }
  1330. votequorum_exec_send_expectedvotes_notification();
  1331. update_ev_barrier(req_exec_quorum_reconfigure->value);
  1332. recalculate_quorum(1, 0); /* Allow decrease */
  1333. break;
  1334. case VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES:
  1335. node->votes = req_exec_quorum_reconfigure->value;
  1336. recalculate_quorum(1, 0); /* Allow decrease */
  1337. break;
  1338. }
  1339. LEAVE();
  1340. }
  1341. static int votequorum_exec_exit_fn (void)
  1342. {
  1343. int ret = 0;
  1344. ENTER();
  1345. if (leave_remove) {
  1346. us->flags |= NODE_FLAGS_LEAVING;
  1347. ret = votequorum_exec_send_nodeinfo(us->node_id);
  1348. }
  1349. LEAVE();
  1350. return ret;
  1351. }
  1352. static char *votequorum_exec_init_fn (struct corosync_api_v1 *api)
  1353. {
  1354. #ifdef COROSYNC_SOLARIS
  1355. logsys_subsys_init();
  1356. #endif
  1357. ENTER();
  1358. list_init(&cluster_members_list);
  1359. list_init(&trackers_list);
  1360. /*
  1361. * Allocate a cluster_node for us
  1362. */
  1363. us = allocate_node(corosync_api->totem_nodeid_get());
  1364. if (!us) {
  1365. LEAVE();
  1366. return ((char *)"Could not allocate node.");
  1367. }
  1368. icmap_set_uint32("runtime.votequorum.this_node_id", us->node_id);
  1369. us->state = NODESTATE_MEMBER;
  1370. us->votes = 1;
  1371. us->flags |= NODE_FLAGS_FIRST;
  1372. if (wait_for_all) {
  1373. update_wait_for_all_status(1);
  1374. }
  1375. qdevice = allocate_node(NODEID_QDEVICE);
  1376. if (!qdevice) {
  1377. LEAVE();
  1378. return ((char *)"Could not allocate node.");
  1379. }
  1380. qdevice->state = NODESTATE_DEAD;
  1381. qdevice->votes = 0;
  1382. memset(qdevice_name, 0, VOTEQUORUM_MAX_QDEVICE_NAME_LEN);
  1383. votequorum_readconfig_dynamic();
  1384. recalculate_quorum(0, 0);
  1385. /*
  1386. * Listen for changes
  1387. */
  1388. votequorum_exec_add_config_notification();
  1389. /*
  1390. * Start us off with one node
  1391. */
  1392. votequorum_exec_send_nodeinfo(us->node_id);
  1393. LEAVE();
  1394. return (NULL);
  1395. }
  1396. /*
  1397. * votequorum service core
  1398. */
  1399. static void votequorum_last_man_standing_timer_fn(void *arg)
  1400. {
  1401. ENTER();
  1402. last_man_standing_timer_set = 0;
  1403. if (cluster_is_quorate) {
  1404. recalculate_quorum(1,1);
  1405. }
  1406. LEAVE();
  1407. }
  1408. static void votequorum_confchg_fn (
  1409. enum totem_configuration_type configuration_type,
  1410. const unsigned int *member_list, size_t member_list_entries,
  1411. const unsigned int *left_list, size_t left_list_entries,
  1412. const unsigned int *joined_list, size_t joined_list_entries,
  1413. const struct memb_ring_id *ring_id)
  1414. {
  1415. int i;
  1416. struct cluster_node *node;
  1417. ENTER();
  1418. if (member_list_entries > 1) {
  1419. us->flags &= ~NODE_FLAGS_FIRST;
  1420. }
  1421. if (left_list_entries) {
  1422. for (i = 0; i< left_list_entries; i++) {
  1423. node = find_node_by_nodeid(left_list[i]);
  1424. if (node) {
  1425. node->state = NODESTATE_DEAD;
  1426. }
  1427. }
  1428. }
  1429. if (last_man_standing) {
  1430. if (((member_list_entries >= quorum) && (left_list_entries)) ||
  1431. ((member_list_entries <= quorum) && (auto_tie_breaker) && (check_low_node_id_partition() == 1))) {
  1432. if (last_man_standing_timer_set) {
  1433. corosync_api->timer_delete(last_man_standing_timer);
  1434. last_man_standing_timer_set = 0;
  1435. }
  1436. corosync_api->timer_add_duration((unsigned long long)last_man_standing_window*1000000,
  1437. NULL, votequorum_last_man_standing_timer_fn,
  1438. &last_man_standing_timer);
  1439. last_man_standing_timer_set = 1;
  1440. }
  1441. }
  1442. if (member_list_entries) {
  1443. memcpy(quorum_members, member_list, sizeof(unsigned int) * member_list_entries);
  1444. quorum_members_entries = member_list_entries;
  1445. votequorum_exec_send_nodeinfo(us->node_id);
  1446. votequorum_exec_send_nodeinfo(NODEID_QDEVICE);
  1447. if (strlen(qdevice_name)) {
  1448. votequorum_exec_send_qdevice_reg(VOTEQUORUM_QDEVICE_OPERATION_REGISTER,
  1449. qdevice_name);
  1450. }
  1451. }
  1452. memcpy(&quorum_ringid, ring_id, sizeof(*ring_id));
  1453. if (left_list_entries) {
  1454. recalculate_quorum(0, 0);
  1455. }
  1456. if (configuration_type == TOTEM_CONFIGURATION_REGULAR) {
  1457. quorum_callback(quorum_members, quorum_members_entries,
  1458. cluster_is_quorate, &quorum_ringid);
  1459. }
  1460. LEAVE();
  1461. }
  1462. char *votequorum_init(struct corosync_api_v1 *api,
  1463. quorum_set_quorate_fn_t q_set_quorate_fn)
  1464. {
  1465. char *error;
  1466. ENTER();
  1467. if (q_set_quorate_fn == NULL) {
  1468. return ((char *)"Quorate function not set");
  1469. }
  1470. corosync_api = api;
  1471. quorum_callback = q_set_quorate_fn;
  1472. error = votequorum_readconfig_static();
  1473. if (error) {
  1474. return (error);
  1475. }
  1476. error = corosync_service_link_and_init(corosync_api,
  1477. &votequorum_service[0]);
  1478. if (error) {
  1479. return (error);
  1480. }
  1481. LEAVE();
  1482. return (NULL);
  1483. }
  1484. /*
  1485. * Library Handler init/fini
  1486. */
  1487. static int quorum_lib_init_fn (void *conn)
  1488. {
  1489. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1490. ENTER();
  1491. list_init (&pd->list);
  1492. pd->conn = conn;
  1493. LEAVE();
  1494. return (0);
  1495. }
  1496. static int quorum_lib_exit_fn (void *conn)
  1497. {
  1498. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1499. ENTER();
  1500. if (quorum_pd->tracking_enabled) {
  1501. list_del (&quorum_pd->list);
  1502. list_init (&quorum_pd->list);
  1503. }
  1504. LEAVE();
  1505. return (0);
  1506. }
  1507. /*
  1508. * library internal functions
  1509. */
  1510. static void qdevice_timer_fn(void *arg)
  1511. {
  1512. ENTER();
  1513. if ((!qdevice_is_registered) ||
  1514. (qdevice->state == NODESTATE_DEAD) ||
  1515. (!qdevice_timer_set)) {
  1516. LEAVE();
  1517. return;
  1518. }
  1519. qdevice->state = NODESTATE_DEAD;
  1520. us->flags &= ~NODE_FLAGS_QDEVICE_STATE;
  1521. log_printf(LOGSYS_LEVEL_INFO, "lost contact with quorum device %s", qdevice_name);
  1522. votequorum_exec_send_nodeinfo(us->node_id);
  1523. qdevice_timer_set = 0;
  1524. LEAVE();
  1525. }
  1526. /*
  1527. * Library Handler Functions
  1528. */
  1529. static void message_handler_req_lib_votequorum_getinfo (void *conn, const void *message)
  1530. {
  1531. const struct req_lib_votequorum_getinfo *req_lib_votequorum_getinfo = message;
  1532. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  1533. struct cluster_node *node;
  1534. unsigned int highest_expected = 0;
  1535. unsigned int total_votes = 0;
  1536. cs_error_t error = CS_OK;
  1537. ENTER();
  1538. log_printf(LOGSYS_LEVEL_DEBUG, "got getinfo request on %p for node %u", conn, req_lib_votequorum_getinfo->nodeid);
  1539. node = find_node_by_nodeid(req_lib_votequorum_getinfo->nodeid);
  1540. if (node) {
  1541. struct cluster_node *iternode;
  1542. struct list_head *nodelist;
  1543. list_iterate(nodelist, &cluster_members_list) {
  1544. iternode = list_entry(nodelist, struct cluster_node, list);
  1545. if (iternode->state == NODESTATE_MEMBER) {
  1546. highest_expected =
  1547. max(highest_expected, iternode->expected_votes);
  1548. total_votes += iternode->votes;
  1549. }
  1550. }
  1551. if (((qdevice_is_registered) && (qdevice->state == NODESTATE_MEMBER)) ||
  1552. ((node->flags & NODE_FLAGS_QDEVICE) && (node->flags & NODE_FLAGS_QDEVICE_STATE))) {
  1553. total_votes += qdevice->votes;
  1554. }
  1555. res_lib_votequorum_getinfo.state = node->state;
  1556. res_lib_votequorum_getinfo.votes = node->votes;
  1557. res_lib_votequorum_getinfo.expected_votes = node->expected_votes;
  1558. res_lib_votequorum_getinfo.highest_expected = highest_expected;
  1559. res_lib_votequorum_getinfo.quorum = quorum;
  1560. res_lib_votequorum_getinfo.total_votes = total_votes;
  1561. res_lib_votequorum_getinfo.flags = 0;
  1562. res_lib_votequorum_getinfo.nodeid = node->node_id;
  1563. if (two_node) {
  1564. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_TWONODE;
  1565. }
  1566. if (cluster_is_quorate) {
  1567. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_FLAG_QUORATE;
  1568. }
  1569. if (wait_for_all) {
  1570. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_WAIT_FOR_ALL;
  1571. }
  1572. if (last_man_standing) {
  1573. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_LAST_MAN_STANDING;
  1574. }
  1575. if (auto_tie_breaker) {
  1576. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_AUTO_TIE_BREAKER;
  1577. }
  1578. if (leave_remove) {
  1579. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_LEAVE_REMOVE;
  1580. }
  1581. if (node->flags & NODE_FLAGS_QDEVICE) {
  1582. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_QDEVICE;
  1583. }
  1584. } else {
  1585. error = CS_ERR_NOT_EXIST;
  1586. }
  1587. res_lib_votequorum_getinfo.header.size = sizeof(res_lib_votequorum_getinfo);
  1588. res_lib_votequorum_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1589. res_lib_votequorum_getinfo.header.error = error;
  1590. corosync_api->ipc_response_send(conn, &res_lib_votequorum_getinfo, sizeof(res_lib_votequorum_getinfo));
  1591. log_printf(LOGSYS_LEVEL_DEBUG, "getinfo response error: %d", error);
  1592. LEAVE();
  1593. }
  1594. static void message_handler_req_lib_votequorum_setexpected (void *conn, const void *message)
  1595. {
  1596. const struct req_lib_votequorum_setexpected *req_lib_votequorum_setexpected = message;
  1597. struct res_lib_votequorum_status res_lib_votequorum_status;
  1598. cs_error_t error = CS_OK;
  1599. unsigned int newquorum;
  1600. unsigned int total_votes;
  1601. uint8_t leave_remove_status = 0;
  1602. ENTER();
  1603. leave_remove_status = leave_remove;
  1604. leave_remove = 0;
  1605. /*
  1606. * Validate new expected votes
  1607. */
  1608. newquorum = calculate_quorum(1, req_lib_votequorum_setexpected->expected_votes, &total_votes);
  1609. leave_remove = leave_remove_status;
  1610. if (newquorum < total_votes / 2 ||
  1611. newquorum > total_votes) {
  1612. error = CS_ERR_INVALID_PARAM;
  1613. goto error_exit;
  1614. }
  1615. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES, us->node_id,
  1616. req_lib_votequorum_setexpected->expected_votes);
  1617. error_exit:
  1618. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1619. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1620. res_lib_votequorum_status.header.error = error;
  1621. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1622. LEAVE();
  1623. }
  1624. static void message_handler_req_lib_votequorum_setvotes (void *conn, const void *message)
  1625. {
  1626. const struct req_lib_votequorum_setvotes *req_lib_votequorum_setvotes = message;
  1627. struct res_lib_votequorum_status res_lib_votequorum_status;
  1628. struct cluster_node *node;
  1629. unsigned int newquorum;
  1630. unsigned int total_votes;
  1631. unsigned int saved_votes;
  1632. cs_error_t error = CS_OK;
  1633. unsigned int nodeid;
  1634. ENTER();
  1635. nodeid = req_lib_votequorum_setvotes->nodeid;
  1636. node = find_node_by_nodeid(nodeid);
  1637. if (!node) {
  1638. error = CS_ERR_NAME_NOT_FOUND;
  1639. goto error_exit;
  1640. }
  1641. /*
  1642. * Check votes is valid
  1643. */
  1644. saved_votes = node->votes;
  1645. node->votes = req_lib_votequorum_setvotes->votes;
  1646. newquorum = calculate_quorum(1, 0, &total_votes);
  1647. if (newquorum < total_votes / 2 ||
  1648. newquorum > total_votes) {
  1649. node->votes = saved_votes;
  1650. error = CS_ERR_INVALID_PARAM;
  1651. goto error_exit;
  1652. }
  1653. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, nodeid,
  1654. req_lib_votequorum_setvotes->votes);
  1655. error_exit:
  1656. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1657. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1658. res_lib_votequorum_status.header.error = error;
  1659. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1660. LEAVE();
  1661. }
  1662. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  1663. const void *message)
  1664. {
  1665. const struct req_lib_votequorum_trackstart *req_lib_votequorum_trackstart = message;
  1666. struct res_lib_votequorum_status res_lib_votequorum_status;
  1667. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1668. ENTER();
  1669. /*
  1670. * If an immediate listing of the current cluster membership
  1671. * is requested, generate membership list
  1672. */
  1673. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  1674. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  1675. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p", conn);
  1676. votequorum_exec_send_quorum_notification(conn, req_lib_votequorum_trackstart->context);
  1677. }
  1678. /*
  1679. * Record requests for tracking
  1680. */
  1681. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  1682. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  1683. quorum_pd->track_flags = req_lib_votequorum_trackstart->track_flags;
  1684. quorum_pd->tracking_enabled = 1;
  1685. quorum_pd->tracking_context = req_lib_votequorum_trackstart->context;
  1686. list_add (&quorum_pd->list, &trackers_list);
  1687. }
  1688. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1689. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1690. res_lib_votequorum_status.header.error = CS_OK;
  1691. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1692. LEAVE();
  1693. }
  1694. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  1695. const void *message)
  1696. {
  1697. struct res_lib_votequorum_status res_lib_votequorum_status;
  1698. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1699. int error = CS_OK;
  1700. ENTER();
  1701. if (quorum_pd->tracking_enabled) {
  1702. error = CS_OK;
  1703. quorum_pd->tracking_enabled = 0;
  1704. list_del (&quorum_pd->list);
  1705. list_init (&quorum_pd->list);
  1706. } else {
  1707. error = CS_ERR_NOT_EXIST;
  1708. }
  1709. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1710. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1711. res_lib_votequorum_status.header.error = error;
  1712. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1713. LEAVE();
  1714. }
  1715. static void message_handler_req_lib_votequorum_qdevice_register (void *conn,
  1716. const void *message)
  1717. {
  1718. const struct req_lib_votequorum_qdevice_register *req_lib_votequorum_qdevice_register = message;
  1719. struct res_lib_votequorum_status res_lib_votequorum_status;
  1720. cs_error_t error = CS_OK;
  1721. ENTER();
  1722. if (!qdevice_can_operate) {
  1723. log_printf(LOGSYS_LEVEL_INFO, "Registration of quorum device is disabled by incorrect corosync.conf. See logs for more information");
  1724. error = CS_ERR_ACCESS;
  1725. goto out;
  1726. }
  1727. if (qdevice_is_registered) {
  1728. if ((!strncmp(req_lib_votequorum_qdevice_register->name,
  1729. qdevice_name, VOTEQUORUM_MAX_QDEVICE_NAME_LEN))) {
  1730. goto out;
  1731. } else {
  1732. log_printf(LOGSYS_LEVEL_WARNING,
  1733. "A new qdevice with different name (new: %s old: %s) is trying to re-register!",
  1734. req_lib_votequorum_qdevice_register->name, qdevice_name);
  1735. error = CS_ERR_EXIST;
  1736. goto out;
  1737. }
  1738. } else {
  1739. if (qdevice_reg_conn != NULL) {
  1740. log_printf(LOGSYS_LEVEL_WARNING,
  1741. "Registration request already in progress");
  1742. error = CS_ERR_TRY_AGAIN;
  1743. goto out;
  1744. }
  1745. qdevice_reg_conn = conn;
  1746. if (votequorum_exec_send_qdevice_reg(VOTEQUORUM_QDEVICE_OPERATION_REGISTER,
  1747. req_lib_votequorum_qdevice_register->name) != 0) {
  1748. log_printf(LOGSYS_LEVEL_WARNING,
  1749. "Unable to send qdevice registration request to cluster");
  1750. error = CS_ERR_TRY_AGAIN;
  1751. qdevice_reg_conn = NULL;
  1752. } else {
  1753. LEAVE();
  1754. return;
  1755. }
  1756. }
  1757. out:
  1758. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1759. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1760. res_lib_votequorum_status.header.error = error;
  1761. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1762. LEAVE();
  1763. }
  1764. static void message_handler_req_lib_votequorum_qdevice_unregister (void *conn,
  1765. const void *message)
  1766. {
  1767. const struct req_lib_votequorum_qdevice_unregister *req_lib_votequorum_qdevice_unregister = message;
  1768. struct res_lib_votequorum_status res_lib_votequorum_status;
  1769. cs_error_t error = CS_OK;
  1770. ENTER();
  1771. if (qdevice_is_registered) {
  1772. if (strncmp(req_lib_votequorum_qdevice_unregister->name, qdevice_name, VOTEQUORUM_MAX_QDEVICE_NAME_LEN)) {
  1773. error = CS_ERR_INVALID_PARAM;
  1774. goto out;
  1775. }
  1776. if (qdevice_timer_set) {
  1777. corosync_api->timer_delete(qdevice_timer);
  1778. qdevice_timer_set = 0;
  1779. }
  1780. qdevice_is_registered = 0;
  1781. us->flags &= ~NODE_FLAGS_QDEVICE;
  1782. us->flags &= ~NODE_FLAGS_QDEVICE_STATE;
  1783. qdevice->state = NODESTATE_DEAD;
  1784. votequorum_exec_send_nodeinfo(us->node_id);
  1785. votequorum_exec_send_qdevice_reg(VOTEQUORUM_QDEVICE_OPERATION_UNREGISTER,
  1786. req_lib_votequorum_qdevice_unregister->name);
  1787. } else {
  1788. error = CS_ERR_NOT_EXIST;
  1789. }
  1790. out:
  1791. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1792. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1793. res_lib_votequorum_status.header.error = error;
  1794. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1795. LEAVE();
  1796. }
  1797. static void message_handler_req_lib_votequorum_qdevice_update (void *conn,
  1798. const void *message)
  1799. {
  1800. const struct req_lib_votequorum_qdevice_update *req_lib_votequorum_qdevice_update = message;
  1801. struct res_lib_votequorum_status res_lib_votequorum_status;
  1802. cs_error_t error = CS_OK;
  1803. ENTER();
  1804. if (qdevice_is_registered) {
  1805. if (strncmp(req_lib_votequorum_qdevice_update->oldname, qdevice_name, VOTEQUORUM_MAX_QDEVICE_NAME_LEN)) {
  1806. error = CS_ERR_INVALID_PARAM;
  1807. goto out;
  1808. }
  1809. votequorum_exec_send_qdevice_reconfigure(req_lib_votequorum_qdevice_update->oldname,
  1810. req_lib_votequorum_qdevice_update->newname);
  1811. } else {
  1812. error = CS_ERR_NOT_EXIST;
  1813. }
  1814. out:
  1815. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1816. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1817. res_lib_votequorum_status.header.error = error;
  1818. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1819. LEAVE();
  1820. }
  1821. static void message_handler_req_lib_votequorum_qdevice_poll (void *conn,
  1822. const void *message)
  1823. {
  1824. const struct req_lib_votequorum_qdevice_poll *req_lib_votequorum_qdevice_poll = message;
  1825. struct res_lib_votequorum_status res_lib_votequorum_status;
  1826. cs_error_t error = CS_OK;
  1827. ENTER();
  1828. if (!qdevice_can_operate) {
  1829. error = CS_ERR_ACCESS;
  1830. goto out;
  1831. }
  1832. if (qdevice_is_registered) {
  1833. if (strncmp(req_lib_votequorum_qdevice_poll->name, qdevice_name, VOTEQUORUM_MAX_QDEVICE_NAME_LEN)) {
  1834. error = CS_ERR_INVALID_PARAM;
  1835. goto out;
  1836. }
  1837. if (qdevice_timer_set) {
  1838. corosync_api->timer_delete(qdevice_timer);
  1839. qdevice_timer_set = 0;
  1840. }
  1841. if (req_lib_votequorum_qdevice_poll->state) {
  1842. if (qdevice->state == NODESTATE_DEAD) {
  1843. qdevice->state = NODESTATE_MEMBER;
  1844. us->flags |= NODE_FLAGS_QDEVICE_STATE;
  1845. votequorum_exec_send_nodeinfo(us->node_id);
  1846. }
  1847. } else {
  1848. if (qdevice->state == NODESTATE_MEMBER) {
  1849. qdevice->state = NODESTATE_DEAD;
  1850. us->flags &= ~NODE_FLAGS_QDEVICE_STATE;
  1851. votequorum_exec_send_nodeinfo(us->node_id);
  1852. }
  1853. }
  1854. corosync_api->timer_add_duration((unsigned long long)qdevice_timeout*1000000, qdevice,
  1855. qdevice_timer_fn, &qdevice_timer);
  1856. qdevice_timer_set = 1;
  1857. } else {
  1858. error = CS_ERR_NOT_EXIST;
  1859. }
  1860. out:
  1861. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1862. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1863. res_lib_votequorum_status.header.error = error;
  1864. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1865. LEAVE();
  1866. }
  1867. static void message_handler_req_lib_votequorum_qdevice_getinfo (void *conn,
  1868. const void *message)
  1869. {
  1870. const struct req_lib_votequorum_qdevice_getinfo *req_lib_votequorum_qdevice_getinfo = message;
  1871. struct res_lib_votequorum_qdevice_getinfo res_lib_votequorum_qdevice_getinfo;
  1872. cs_error_t error = CS_OK;
  1873. struct cluster_node *node;
  1874. uint32_t nodeid = req_lib_votequorum_qdevice_getinfo->nodeid;
  1875. ENTER();
  1876. if ((nodeid != us->node_id) &&
  1877. (nodeid != NODEID_QDEVICE)) {
  1878. node = find_node_by_nodeid(req_lib_votequorum_qdevice_getinfo->nodeid);
  1879. if ((node) &&
  1880. (node->flags & NODE_FLAGS_QDEVICE)) {
  1881. int state = 0;
  1882. if (node->flags & NODE_FLAGS_QDEVICE_STATE) {
  1883. state = 1;
  1884. }
  1885. log_printf(LOGSYS_LEVEL_DEBUG, "got qdevice_getinfo node %u state %d", nodeid, state);
  1886. res_lib_votequorum_qdevice_getinfo.votes = qdevice->votes;
  1887. if (state) {
  1888. res_lib_votequorum_qdevice_getinfo.state = 1;
  1889. } else {
  1890. res_lib_votequorum_qdevice_getinfo.state = 0;
  1891. }
  1892. strcpy(res_lib_votequorum_qdevice_getinfo.name, qdevice_name);
  1893. } else {
  1894. error = CS_ERR_NOT_EXIST;
  1895. }
  1896. } else {
  1897. if (qdevice_is_registered) {
  1898. log_printf(LOGSYS_LEVEL_DEBUG, "got qdevice_getinfo node %u state %d", us->node_id, qdevice->state);
  1899. res_lib_votequorum_qdevice_getinfo.votes = qdevice->votes;
  1900. if (qdevice->state == NODESTATE_MEMBER) {
  1901. res_lib_votequorum_qdevice_getinfo.state = 1;
  1902. } else {
  1903. res_lib_votequorum_qdevice_getinfo.state = 0;
  1904. }
  1905. strcpy(res_lib_votequorum_qdevice_getinfo.name, qdevice_name);
  1906. } else {
  1907. error = CS_ERR_NOT_EXIST;
  1908. }
  1909. }
  1910. res_lib_votequorum_qdevice_getinfo.header.size = sizeof(res_lib_votequorum_qdevice_getinfo);
  1911. res_lib_votequorum_qdevice_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  1912. res_lib_votequorum_qdevice_getinfo.header.error = error;
  1913. corosync_api->ipc_response_send(conn, &res_lib_votequorum_qdevice_getinfo, sizeof(res_lib_votequorum_qdevice_getinfo));
  1914. LEAVE();
  1915. }