votequorum.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874
  1. /*
  2. * Copyright (c) 2009-2014 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 <sys/stat.h>
  38. #include <fcntl.h>
  39. #include <stdint.h>
  40. #include <unistd.h>
  41. #include <qb/qbipc_common.h>
  42. #include "quorum.h"
  43. #include <corosync/corodefs.h>
  44. #include <corosync/list.h>
  45. #include <corosync/logsys.h>
  46. #include <corosync/coroapi.h>
  47. #include <corosync/icmap.h>
  48. #include <corosync/votequorum.h>
  49. #include <corosync/ipc_votequorum.h>
  50. #include "service.h"
  51. #include "util.h"
  52. LOGSYS_DECLARE_SUBSYS ("VOTEQ");
  53. /*
  54. * interface with corosync
  55. */
  56. static struct corosync_api_v1 *corosync_api;
  57. /*
  58. * votequorum global config vars
  59. */
  60. static char qdevice_name[VOTEQUORUM_QDEVICE_MAX_NAME_LEN];
  61. static struct cluster_node *qdevice = NULL;
  62. static unsigned int qdevice_timeout = VOTEQUORUM_QDEVICE_DEFAULT_TIMEOUT;
  63. static uint8_t qdevice_can_operate = 1;
  64. static void *qdevice_reg_conn = NULL;
  65. static uint8_t qdevice_master_wins = 0;
  66. static uint8_t two_node = 0;
  67. static uint8_t wait_for_all = 0;
  68. static uint8_t wait_for_all_status = 0;
  69. static enum {ATB_NONE, ATB_LOWEST, ATB_HIGHEST, ATB_LIST} auto_tie_breaker = ATB_NONE;
  70. static int lowest_node_id = -1;
  71. static int highest_node_id = -1;
  72. #define DEFAULT_LMS_WIN 10000
  73. static uint8_t last_man_standing = 0;
  74. static uint32_t last_man_standing_window = DEFAULT_LMS_WIN;
  75. static uint8_t allow_downscale = 0;
  76. static uint32_t ev_barrier = 0;
  77. static uint8_t ev_tracking = 0;
  78. static uint32_t ev_tracking_barrier = 0;
  79. static int ev_tracking_fd = -1;
  80. /*
  81. * votequorum_exec defines/structs/forward definitions
  82. */
  83. struct req_exec_quorum_nodeinfo {
  84. struct qb_ipc_request_header header __attribute__((aligned(8)));
  85. uint32_t nodeid;
  86. uint32_t votes;
  87. uint32_t expected_votes;
  88. uint32_t flags;
  89. } __attribute__((packed));
  90. struct req_exec_quorum_reconfigure {
  91. struct qb_ipc_request_header header __attribute__((aligned(8)));
  92. uint32_t nodeid;
  93. uint32_t value;
  94. uint8_t param;
  95. uint8_t _pad0;
  96. uint8_t _pad1;
  97. uint8_t _pad2;
  98. } __attribute__((packed));
  99. struct req_exec_quorum_qdevice_reg {
  100. struct qb_ipc_request_header header __attribute__((aligned(8)));
  101. uint32_t operation;
  102. char qdevice_name[VOTEQUORUM_QDEVICE_MAX_NAME_LEN];
  103. } __attribute__((packed));
  104. struct req_exec_quorum_qdevice_reconfigure {
  105. struct qb_ipc_request_header header __attribute__((aligned(8)));
  106. char oldname[VOTEQUORUM_QDEVICE_MAX_NAME_LEN];
  107. char newname[VOTEQUORUM_QDEVICE_MAX_NAME_LEN];
  108. } __attribute__((packed));
  109. /*
  110. * votequorum_exec onwire version (via totem)
  111. */
  112. #include "votequorum.h"
  113. /*
  114. * votequorum_exec onwire messages (via totem)
  115. */
  116. #define MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO 0
  117. #define MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE 1
  118. #define MESSAGE_REQ_EXEC_VOTEQUORUM_QDEVICE_REG 2
  119. #define MESSAGE_REQ_EXEC_VOTEQUORUM_QDEVICE_RECONFIGURE 3
  120. static void votequorum_exec_send_expectedvotes_notification(void);
  121. static int votequorum_exec_send_quorum_notification(void *conn, uint64_t context);
  122. #define VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES 1
  123. #define VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES 2
  124. static int votequorum_exec_send_reconfigure(uint8_t param, unsigned int nodeid, uint32_t value);
  125. /*
  126. * used by req_exec_quorum_qdevice_reg
  127. */
  128. #define VOTEQUORUM_QDEVICE_OPERATION_UNREGISTER 0
  129. #define VOTEQUORUM_QDEVICE_OPERATION_REGISTER 1
  130. /*
  131. * votequorum internal node status/view
  132. */
  133. #define NODE_FLAGS_QUORATE 1
  134. #define NODE_FLAGS_LEAVING 2
  135. #define NODE_FLAGS_WFASTATUS 4
  136. #define NODE_FLAGS_FIRST 8
  137. #define NODE_FLAGS_QDEVICE_REGISTERED 16
  138. #define NODE_FLAGS_QDEVICE_ALIVE 32
  139. #define NODE_FLAGS_QDEVICE_CAST_VOTE 64
  140. #define NODE_FLAGS_QDEVICE_MASTER_WINS 128
  141. typedef enum {
  142. NODESTATE_MEMBER=1,
  143. NODESTATE_DEAD,
  144. NODESTATE_LEAVING
  145. } nodestate_t;
  146. struct cluster_node {
  147. int node_id;
  148. nodestate_t state;
  149. uint32_t votes;
  150. uint32_t expected_votes;
  151. uint32_t flags;
  152. struct list_head list;
  153. };
  154. /*
  155. * votequorum internal quorum status
  156. */
  157. static uint8_t quorum;
  158. static uint8_t cluster_is_quorate;
  159. /*
  160. * votequorum membership data
  161. */
  162. static struct cluster_node *us;
  163. static struct list_head cluster_members_list;
  164. static unsigned int quorum_members[PROCESSOR_COUNT_MAX];
  165. static unsigned int previous_quorum_members[PROCESSOR_COUNT_MAX];
  166. static unsigned int atb_nodelist[PROCESSOR_COUNT_MAX];
  167. static int quorum_members_entries = 0;
  168. static int previous_quorum_members_entries = 0;
  169. static int atb_nodelist_entries = 0;
  170. static struct memb_ring_id quorum_ringid;
  171. /*
  172. * pre allocate all cluster_nodes + one for qdevice
  173. */
  174. static struct cluster_node cluster_nodes[PROCESSOR_COUNT_MAX+2];
  175. static int cluster_nodes_entries = 0;
  176. /*
  177. * votequorum tracking
  178. */
  179. struct quorum_pd {
  180. unsigned char track_flags;
  181. int tracking_enabled;
  182. uint64_t tracking_context;
  183. struct list_head list;
  184. void *conn;
  185. };
  186. static struct list_head trackers_list;
  187. /*
  188. * votequorum timers
  189. */
  190. static corosync_timer_handle_t qdevice_timer;
  191. static int qdevice_timer_set = 0;
  192. static corosync_timer_handle_t last_man_standing_timer;
  193. static int last_man_standing_timer_set = 0;
  194. static int sync_nodeinfo_sent = 0;
  195. static int sync_wait_for_poll_or_timeout = 0;
  196. /*
  197. * Service Interfaces required by service_message_handler struct
  198. */
  199. static int sync_in_progress = 0;
  200. static void votequorum_sync_init (
  201. const unsigned int *trans_list,
  202. size_t trans_list_entries,
  203. const unsigned int *member_list,
  204. size_t member_list_entries,
  205. const struct memb_ring_id *ring_id);
  206. static int votequorum_sync_process (void);
  207. static void votequorum_sync_activate (void);
  208. static void votequorum_sync_abort (void);
  209. static quorum_set_quorate_fn_t quorum_callback;
  210. /*
  211. * votequorum_exec handler and definitions
  212. */
  213. static char *votequorum_exec_init_fn (struct corosync_api_v1 *api);
  214. static int votequorum_exec_exit_fn (void);
  215. static int votequorum_exec_send_nodeinfo(uint32_t nodeid);
  216. static void message_handler_req_exec_votequorum_nodeinfo (
  217. const void *message,
  218. unsigned int nodeid);
  219. static void exec_votequorum_nodeinfo_endian_convert (void *message);
  220. static void message_handler_req_exec_votequorum_reconfigure (
  221. const void *message,
  222. unsigned int nodeid);
  223. static void exec_votequorum_reconfigure_endian_convert (void *message);
  224. static void message_handler_req_exec_votequorum_qdevice_reg (
  225. const void *message,
  226. unsigned int nodeid);
  227. static void exec_votequorum_qdevice_reg_endian_convert (void *message);
  228. static void message_handler_req_exec_votequorum_qdevice_reconfigure (
  229. const void *message,
  230. unsigned int nodeid);
  231. static void exec_votequorum_qdevice_reconfigure_endian_convert (void *message);
  232. static struct corosync_exec_handler votequorum_exec_engine[] =
  233. {
  234. { /* 0 */
  235. .exec_handler_fn = message_handler_req_exec_votequorum_nodeinfo,
  236. .exec_endian_convert_fn = exec_votequorum_nodeinfo_endian_convert
  237. },
  238. { /* 1 */
  239. .exec_handler_fn = message_handler_req_exec_votequorum_reconfigure,
  240. .exec_endian_convert_fn = exec_votequorum_reconfigure_endian_convert
  241. },
  242. { /* 2 */
  243. .exec_handler_fn = message_handler_req_exec_votequorum_qdevice_reg,
  244. .exec_endian_convert_fn = exec_votequorum_qdevice_reg_endian_convert
  245. },
  246. { /* 3 */
  247. .exec_handler_fn = message_handler_req_exec_votequorum_qdevice_reconfigure,
  248. .exec_endian_convert_fn = exec_votequorum_qdevice_reconfigure_endian_convert
  249. },
  250. };
  251. /*
  252. * Library Handler and Functions Definitions
  253. */
  254. static int quorum_lib_init_fn (void *conn);
  255. static int quorum_lib_exit_fn (void *conn);
  256. static void qdevice_timer_fn(void *arg);
  257. static void message_handler_req_lib_votequorum_getinfo (void *conn,
  258. const void *message);
  259. static void message_handler_req_lib_votequorum_setexpected (void *conn,
  260. const void *message);
  261. static void message_handler_req_lib_votequorum_setvotes (void *conn,
  262. const void *message);
  263. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  264. const void *message);
  265. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  266. const void *message);
  267. static void message_handler_req_lib_votequorum_qdevice_register (void *conn,
  268. const void *message);
  269. static void message_handler_req_lib_votequorum_qdevice_unregister (void *conn,
  270. const void *message);
  271. static void message_handler_req_lib_votequorum_qdevice_update (void *conn,
  272. const void *message);
  273. static void message_handler_req_lib_votequorum_qdevice_poll (void *conn,
  274. const void *message);
  275. static void message_handler_req_lib_votequorum_qdevice_master_wins (void *conn,
  276. const void *message);
  277. static struct corosync_lib_handler quorum_lib_service[] =
  278. {
  279. { /* 0 */
  280. .lib_handler_fn = message_handler_req_lib_votequorum_getinfo,
  281. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  282. },
  283. { /* 1 */
  284. .lib_handler_fn = message_handler_req_lib_votequorum_setexpected,
  285. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  286. },
  287. { /* 2 */
  288. .lib_handler_fn = message_handler_req_lib_votequorum_setvotes,
  289. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  290. },
  291. { /* 3 */
  292. .lib_handler_fn = message_handler_req_lib_votequorum_trackstart,
  293. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  294. },
  295. { /* 4 */
  296. .lib_handler_fn = message_handler_req_lib_votequorum_trackstop,
  297. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  298. },
  299. { /* 5 */
  300. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_register,
  301. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  302. },
  303. { /* 6 */
  304. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_unregister,
  305. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  306. },
  307. { /* 7 */
  308. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_update,
  309. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  310. },
  311. { /* 8 */
  312. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_poll,
  313. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  314. },
  315. { /* 9 */
  316. .lib_handler_fn = message_handler_req_lib_votequorum_qdevice_master_wins,
  317. .flow_control = COROSYNC_LIB_FLOW_CONTROL_NOT_REQUIRED
  318. }
  319. };
  320. static struct corosync_service_engine votequorum_service_engine = {
  321. .name = "corosync vote quorum service v1.0",
  322. .id = VOTEQUORUM_SERVICE,
  323. .priority = 2,
  324. .private_data_size = sizeof (struct quorum_pd),
  325. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  326. .flow_control = COROSYNC_LIB_FLOW_CONTROL_REQUIRED,
  327. .lib_init_fn = quorum_lib_init_fn,
  328. .lib_exit_fn = quorum_lib_exit_fn,
  329. .lib_engine = quorum_lib_service,
  330. .lib_engine_count = sizeof (quorum_lib_service) / sizeof (struct corosync_lib_handler),
  331. .exec_init_fn = votequorum_exec_init_fn,
  332. .exec_exit_fn = votequorum_exec_exit_fn,
  333. .exec_engine = votequorum_exec_engine,
  334. .exec_engine_count = sizeof (votequorum_exec_engine) / sizeof (struct corosync_exec_handler),
  335. .sync_init = votequorum_sync_init,
  336. .sync_process = votequorum_sync_process,
  337. .sync_activate = votequorum_sync_activate,
  338. .sync_abort = votequorum_sync_abort
  339. };
  340. struct corosync_service_engine *votequorum_get_service_engine_ver0 (void)
  341. {
  342. return (&votequorum_service_engine);
  343. }
  344. static struct default_service votequorum_service[] = {
  345. {
  346. .name = "corosync_votequorum",
  347. .ver = 0,
  348. .loader = votequorum_get_service_engine_ver0
  349. },
  350. };
  351. /*
  352. * common/utility macros/functions
  353. */
  354. #define max(a,b) (((a) > (b)) ? (a) : (b))
  355. #define list_iterate(v, head) \
  356. for (v = (head)->next; v != head; v = v->next)
  357. static void node_add_ordered(struct cluster_node *newnode)
  358. {
  359. struct cluster_node *node = NULL;
  360. struct list_head *tmp;
  361. struct list_head *newlist = &newnode->list;
  362. ENTER();
  363. list_iterate(tmp, &cluster_members_list) {
  364. node = list_entry(tmp, struct cluster_node, list);
  365. if (newnode->node_id < node->node_id) {
  366. break;
  367. }
  368. }
  369. if (!node) {
  370. list_add(&newnode->list, &cluster_members_list);
  371. } else {
  372. newlist->prev = tmp->prev;
  373. newlist->next = tmp;
  374. tmp->prev->next = newlist;
  375. tmp->prev = newlist;
  376. }
  377. LEAVE();
  378. }
  379. static struct cluster_node *allocate_node(unsigned int nodeid)
  380. {
  381. struct cluster_node *cl = NULL;
  382. struct list_head *tmp;
  383. ENTER();
  384. if (cluster_nodes_entries <= PROCESSOR_COUNT_MAX + 1) {
  385. cl = (struct cluster_node *)&cluster_nodes[cluster_nodes_entries];
  386. cluster_nodes_entries++;
  387. } else {
  388. list_iterate(tmp, &cluster_members_list) {
  389. cl = list_entry(tmp, struct cluster_node, list);
  390. if (cl->state == NODESTATE_DEAD) {
  391. break;
  392. }
  393. }
  394. /*
  395. * this should never happen
  396. */
  397. if (!cl) {
  398. log_printf(LOGSYS_LEVEL_CRIT, "Unable to find memory for node %u data!!", nodeid);
  399. goto out;
  400. }
  401. list_del(tmp);
  402. }
  403. memset(cl, 0, sizeof(struct cluster_node));
  404. cl->node_id = nodeid;
  405. if (nodeid != VOTEQUORUM_QDEVICE_NODEID) {
  406. node_add_ordered(cl);
  407. }
  408. out:
  409. LEAVE();
  410. return cl;
  411. }
  412. static struct cluster_node *find_node_by_nodeid(unsigned int nodeid)
  413. {
  414. struct cluster_node *node;
  415. struct list_head *tmp;
  416. ENTER();
  417. if (nodeid == us->node_id) {
  418. LEAVE();
  419. return us;
  420. }
  421. if (nodeid == VOTEQUORUM_QDEVICE_NODEID) {
  422. LEAVE();
  423. return qdevice;
  424. }
  425. list_iterate(tmp, &cluster_members_list) {
  426. node = list_entry(tmp, struct cluster_node, list);
  427. if (node->node_id == nodeid) {
  428. LEAVE();
  429. return node;
  430. }
  431. }
  432. LEAVE();
  433. return NULL;
  434. }
  435. static void get_lowest_node_id(void)
  436. {
  437. struct cluster_node *node = NULL;
  438. struct list_head *tmp;
  439. ENTER();
  440. lowest_node_id = us->node_id;
  441. list_iterate(tmp, &cluster_members_list) {
  442. node = list_entry(tmp, struct cluster_node, list);
  443. if ((node->state == NODESTATE_MEMBER) &&
  444. (node->node_id < lowest_node_id)) {
  445. lowest_node_id = node->node_id;
  446. }
  447. }
  448. log_printf(LOGSYS_LEVEL_DEBUG, "lowest node id: %d us: %d", lowest_node_id, us->node_id);
  449. icmap_set_uint32("runtime.votequorum.lowest_node_id", lowest_node_id);
  450. LEAVE();
  451. }
  452. static void get_highest_node_id(void)
  453. {
  454. struct cluster_node *node = NULL;
  455. struct list_head *tmp;
  456. ENTER();
  457. highest_node_id = us->node_id;
  458. list_iterate(tmp, &cluster_members_list) {
  459. node = list_entry(tmp, struct cluster_node, list);
  460. if ((node->state == NODESTATE_MEMBER) &&
  461. (node->node_id > highest_node_id)) {
  462. highest_node_id = node->node_id;
  463. }
  464. }
  465. log_printf(LOGSYS_LEVEL_DEBUG, "highest node id: %d us: %d", highest_node_id, us->node_id);
  466. icmap_set_uint32("runtime.votequorum.highest_node_id", highest_node_id);
  467. LEAVE();
  468. }
  469. static int check_low_node_id_partition(void)
  470. {
  471. struct cluster_node *node = NULL;
  472. struct list_head *tmp;
  473. int found = 0;
  474. ENTER();
  475. list_iterate(tmp, &cluster_members_list) {
  476. node = list_entry(tmp, struct cluster_node, list);
  477. if ((node->state == NODESTATE_MEMBER) &&
  478. (node->node_id == lowest_node_id)) {
  479. found = 1;
  480. }
  481. }
  482. LEAVE();
  483. return found;
  484. }
  485. static int check_high_node_id_partition(void)
  486. {
  487. struct cluster_node *node = NULL;
  488. struct list_head *tmp;
  489. int found = 0;
  490. ENTER();
  491. list_iterate(tmp, &cluster_members_list) {
  492. node = list_entry(tmp, struct cluster_node, list);
  493. if ((node->state == NODESTATE_MEMBER) &&
  494. (node->node_id == highest_node_id)) {
  495. found = 1;
  496. }
  497. }
  498. LEAVE();
  499. return found;
  500. }
  501. static int is_in_nodelist(int nodeid, unsigned int *members, int entries)
  502. {
  503. int i;
  504. ENTER();
  505. for (i=0; i<entries; i++) {
  506. if (nodeid == members[i]) {
  507. LEAVE();
  508. return 1;
  509. }
  510. }
  511. LEAVE();
  512. return 0;
  513. }
  514. /*
  515. * The algorithm for a list of time-breaker nodes is:
  516. * travel the list of nodes in the auto_tie_breaker list,
  517. * if the node IS in our current partition, check if the
  518. * nodes earlier in the atb list are in the 'previous' partition;
  519. * If none are found then we are safe to be quorate, if any are
  520. * then we cannot be as we don't know if that node is up or down.
  521. * If we don't have a node in the current list we are NOT quorate.
  522. * Obviously if we find the first node in the atb list in our
  523. * partition then we are quorate.
  524. *
  525. * Special cases lowest nodeid, and highest nodeid are handled separately.
  526. */
  527. static int check_auto_tie_breaker(void)
  528. {
  529. int i, j;
  530. int res;
  531. ENTER();
  532. if (auto_tie_breaker == ATB_LOWEST) {
  533. res = check_low_node_id_partition();
  534. log_printf(LOGSYS_LEVEL_DEBUG, "ATB_LOWEST decision: %d", res);
  535. LEAVE();
  536. return res;
  537. }
  538. if (auto_tie_breaker == ATB_HIGHEST) {
  539. res = check_high_node_id_partition();
  540. log_printf(LOGSYS_LEVEL_DEBUG, "ATB_HIGHEST decision: %d", res);
  541. LEAVE();
  542. return res;
  543. }
  544. /* Assume ATB_LIST, we should never be called for ATB_NONE */
  545. for (i=0; i < atb_nodelist_entries; i++) {
  546. if (is_in_nodelist(atb_nodelist[i], quorum_members, quorum_members_entries)) {
  547. /*
  548. * Node is in our partition, if any of its predecessors are
  549. * in the previous quorum partition then it might be in the
  550. * 'other half' (as we've got this far without seeing it here)
  551. * and so we can't be quorate.
  552. */
  553. for (j=0; j<i; j++) {
  554. if (is_in_nodelist(atb_nodelist[j], previous_quorum_members, previous_quorum_members_entries)) {
  555. log_printf(LOGSYS_LEVEL_DEBUG, "ATB_LIST found node %d in previous partition but not here, quorum denied", atb_nodelist[j]);
  556. LEAVE();
  557. return 0;
  558. }
  559. }
  560. /*
  561. * None of the other list nodes were in the previous partition, if there
  562. * are enough votes, we can be quorate
  563. */
  564. log_printf(LOGSYS_LEVEL_DEBUG, "ATB_LIST found node %d in current partition, we can be quorate", atb_nodelist[i]);
  565. LEAVE();
  566. return 1;
  567. }
  568. }
  569. log_printf(LOGSYS_LEVEL_DEBUG, "ATB_LIST found no list nodes in current partition, we cannot be quorate");
  570. LEAVE();
  571. return 0;
  572. }
  573. /*
  574. * atb_string can be either:
  575. * 'lowest'
  576. * 'highest'
  577. * a list of nodeids
  578. */
  579. static void parse_atb_string(char *atb_string)
  580. {
  581. char *ptr;
  582. long num;
  583. ENTER();
  584. auto_tie_breaker = ATB_NONE;
  585. if (!strcmp(atb_string, "lowest"))
  586. auto_tie_breaker = ATB_LOWEST;
  587. if (!strcmp(atb_string, "highest"))
  588. auto_tie_breaker = ATB_HIGHEST;
  589. if (atoi(atb_string)) {
  590. atb_nodelist_entries = 0;
  591. ptr = atb_string;
  592. do {
  593. num = strtol(ptr, &ptr, 10);
  594. if (num) {
  595. log_printf(LOGSYS_LEVEL_DEBUG, "ATB nodelist[%d] = %d", atb_nodelist_entries, num);
  596. atb_nodelist[atb_nodelist_entries++] = num;
  597. }
  598. } while (num);
  599. if (atb_nodelist_entries) {
  600. auto_tie_breaker = ATB_LIST;
  601. }
  602. }
  603. icmap_set_uint32("runtime.votequorum.atb_type", auto_tie_breaker);
  604. log_printf(LOGSYS_LEVEL_DEBUG, "ATB type = %d", auto_tie_breaker);
  605. /* Make sure we got something */
  606. if (auto_tie_breaker == ATB_NONE) {
  607. log_printf(LOGSYS_LEVEL_WARNING, "auto_tie_breaker_nodes is not valid. It must be 'lowest', 'highest' or a space-separated list of node IDs. auto_tie_breaker is disabled");
  608. auto_tie_breaker = ATB_NONE;
  609. }
  610. LEAVE();
  611. }
  612. static int check_qdevice_master(void)
  613. {
  614. struct cluster_node *node = NULL;
  615. struct list_head *tmp;
  616. int found = 0;
  617. ENTER();
  618. list_iterate(tmp, &cluster_members_list) {
  619. node = list_entry(tmp, struct cluster_node, list);
  620. if ((node->state == NODESTATE_MEMBER) &&
  621. (node->flags & NODE_FLAGS_QDEVICE_MASTER_WINS) &&
  622. (node->flags & NODE_FLAGS_QDEVICE_CAST_VOTE)) {
  623. found = 1;
  624. }
  625. }
  626. LEAVE();
  627. return found;
  628. }
  629. static void decode_flags(uint32_t flags)
  630. {
  631. ENTER();
  632. log_printf(LOGSYS_LEVEL_DEBUG,
  633. "flags: quorate: %s Leaving: %s WFA Status: %s First: %s Qdevice: %s QdeviceAlive: %s QdeviceCastVote: %s QdeviceMasterWins: %s",
  634. (flags & NODE_FLAGS_QUORATE)?"Yes":"No",
  635. (flags & NODE_FLAGS_LEAVING)?"Yes":"No",
  636. (flags & NODE_FLAGS_WFASTATUS)?"Yes":"No",
  637. (flags & NODE_FLAGS_FIRST)?"Yes":"No",
  638. (flags & NODE_FLAGS_QDEVICE_REGISTERED)?"Yes":"No",
  639. (flags & NODE_FLAGS_QDEVICE_ALIVE)?"Yes":"No",
  640. (flags & NODE_FLAGS_QDEVICE_CAST_VOTE)?"Yes":"No",
  641. (flags & NODE_FLAGS_QDEVICE_MASTER_WINS)?"Yes":"No");
  642. LEAVE();
  643. }
  644. /*
  645. * load/save are copied almost pristine from totemsrp,c
  646. */
  647. static int load_ev_tracking_barrier(void)
  648. {
  649. int res = 0;
  650. char filename[PATH_MAX];
  651. ENTER();
  652. snprintf(filename, sizeof(filename) - 1, "%s/ev_tracking", get_run_dir());
  653. ev_tracking_fd = open(filename, O_RDWR, 0700);
  654. if (ev_tracking_fd != -1) {
  655. res = read (ev_tracking_fd, &ev_tracking_barrier, sizeof(uint32_t));
  656. if (res == sizeof (uint32_t)) {
  657. LEAVE();
  658. return 0;
  659. }
  660. }
  661. ev_tracking_barrier = 0;
  662. umask(0);
  663. ev_tracking_fd = open (filename, O_CREAT|O_RDWR, 0700);
  664. if (ev_tracking_fd != -1) {
  665. res = write (ev_tracking_fd, &ev_tracking_barrier, sizeof (uint32_t));
  666. if ((res == -1) || (res != sizeof (uint32_t))) {
  667. log_printf(LOGSYS_LEVEL_WARNING,
  668. "Unable to write to %s", filename);
  669. }
  670. LEAVE();
  671. return 0;
  672. }
  673. log_printf(LOGSYS_LEVEL_WARNING,
  674. "Unable to create %s file", filename);
  675. LEAVE();
  676. return -1;
  677. }
  678. static void update_wait_for_all_status(uint8_t wfa_status)
  679. {
  680. ENTER();
  681. wait_for_all_status = wfa_status;
  682. if (wait_for_all_status) {
  683. us->flags |= NODE_FLAGS_WFASTATUS;
  684. } else {
  685. us->flags &= ~NODE_FLAGS_WFASTATUS;
  686. }
  687. icmap_set_uint8("runtime.votequorum.wait_for_all_status",
  688. wait_for_all_status);
  689. LEAVE();
  690. }
  691. static void update_two_node(void)
  692. {
  693. ENTER();
  694. icmap_set_uint8("runtime.votequorum.two_node", two_node);
  695. LEAVE();
  696. }
  697. static void update_ev_barrier(uint32_t expected_votes)
  698. {
  699. ENTER();
  700. ev_barrier = expected_votes;
  701. icmap_set_uint32("runtime.votequorum.ev_barrier", ev_barrier);
  702. LEAVE();
  703. }
  704. static void update_qdevice_can_operate(uint8_t status)
  705. {
  706. ENTER();
  707. qdevice_can_operate = status;
  708. icmap_set_uint8("runtime.votequorum.qdevice_can_operate", qdevice_can_operate);
  709. LEAVE();
  710. }
  711. static void update_qdevice_master_wins(uint8_t allow)
  712. {
  713. ENTER();
  714. qdevice_master_wins = allow;
  715. icmap_set_uint8("runtime.votequorum.qdevice_master_wins", qdevice_master_wins);
  716. LEAVE();
  717. }
  718. static void update_ev_tracking_barrier(uint32_t ev_t_barrier)
  719. {
  720. int res;
  721. ENTER();
  722. ev_tracking_barrier = ev_t_barrier;
  723. icmap_set_uint32("runtime.votequorum.ev_tracking_barrier", ev_tracking_barrier);
  724. if (lseek (ev_tracking_fd, 0, SEEK_SET) != 0) {
  725. log_printf(LOGSYS_LEVEL_WARNING,
  726. "Unable to update ev_tracking_barrier on disk data!!!");
  727. LEAVE();
  728. return;
  729. }
  730. res = write (ev_tracking_fd, &ev_tracking_barrier, sizeof (uint32_t));
  731. if (res != sizeof (uint32_t)) {
  732. log_printf(LOGSYS_LEVEL_WARNING,
  733. "Unable to update ev_tracking_barrier on disk data!!!");
  734. }
  735. fdatasync(ev_tracking_fd);
  736. LEAVE();
  737. }
  738. /*
  739. * quorum calculation core bits
  740. */
  741. static int calculate_quorum(int allow_decrease, unsigned int max_expected, unsigned int *ret_total_votes)
  742. {
  743. struct list_head *nodelist;
  744. struct cluster_node *node;
  745. unsigned int total_votes = 0;
  746. unsigned int highest_expected = 0;
  747. unsigned int newquorum, q1, q2;
  748. unsigned int total_nodes = 0;
  749. ENTER();
  750. if ((allow_downscale) && (allow_decrease) && (max_expected)) {
  751. max_expected = max(ev_barrier, max_expected);
  752. }
  753. list_iterate(nodelist, &cluster_members_list) {
  754. node = list_entry(nodelist, struct cluster_node, list);
  755. log_printf(LOGSYS_LEVEL_DEBUG, "node %u state=%d, votes=%u, expected=%u",
  756. node->node_id, node->state, node->votes, node->expected_votes);
  757. if (node->state == NODESTATE_MEMBER) {
  758. if (max_expected) {
  759. node->expected_votes = max_expected;
  760. } else {
  761. highest_expected = max(highest_expected, node->expected_votes);
  762. }
  763. total_votes += node->votes;
  764. total_nodes++;
  765. }
  766. }
  767. if (us->flags & NODE_FLAGS_QDEVICE_CAST_VOTE) {
  768. log_printf(LOGSYS_LEVEL_DEBUG, "node 0 state=1, votes=%u", qdevice->votes);
  769. total_votes += qdevice->votes;
  770. total_nodes++;
  771. }
  772. if (max_expected > 0) {
  773. highest_expected = max_expected;
  774. }
  775. /*
  776. * This quorum calculation is taken from the OpenVMS Cluster Systems
  777. * manual, but, then, you guessed that didn't you
  778. */
  779. q1 = (highest_expected + 2) / 2;
  780. q2 = (total_votes + 2) / 2;
  781. newquorum = max(q1, q2);
  782. /*
  783. * Normally quorum never decreases but the system administrator can
  784. * force it down by setting expected votes to a maximum value
  785. */
  786. if (!allow_decrease) {
  787. newquorum = max(quorum, newquorum);
  788. }
  789. /*
  790. * The special two_node mode allows each of the two nodes to retain
  791. * quorum if the other fails. Only one of the two should live past
  792. * fencing (as both nodes try to fence each other in split-brain.)
  793. * Also: if there are more than two nodes, force us inquorate to avoid
  794. * any damage or confusion.
  795. */
  796. if (two_node && total_nodes <= 2) {
  797. newquorum = 1;
  798. }
  799. if (ret_total_votes) {
  800. *ret_total_votes = total_votes;
  801. }
  802. LEAVE();
  803. return newquorum;
  804. }
  805. static void are_we_quorate(unsigned int total_votes)
  806. {
  807. int quorate;
  808. int quorum_change = 0;
  809. ENTER();
  810. /*
  811. * wait for all nodes to show up before granting quorum
  812. */
  813. if ((wait_for_all) && (wait_for_all_status)) {
  814. if (total_votes != us->expected_votes) {
  815. log_printf(LOGSYS_LEVEL_NOTICE,
  816. "Waiting for all cluster members. "
  817. "Current votes: %d expected_votes: %d",
  818. total_votes, us->expected_votes);
  819. cluster_is_quorate = 0;
  820. return;
  821. }
  822. update_wait_for_all_status(0);
  823. }
  824. if (quorum > total_votes) {
  825. quorate = 0;
  826. } else {
  827. quorate = 1;
  828. get_lowest_node_id();
  829. get_highest_node_id();
  830. }
  831. if ((auto_tie_breaker != ATB_NONE) &&
  832. (total_votes == (us->expected_votes / 2)) &&
  833. (check_auto_tie_breaker() == 1)) {
  834. quorate = 1;
  835. }
  836. if ((qdevice_master_wins) &&
  837. (!quorate) &&
  838. (check_qdevice_master() == 1)) {
  839. log_printf(LOGSYS_LEVEL_DEBUG, "node is quorate as part of master_wins partition");
  840. quorate = 1;
  841. }
  842. if (cluster_is_quorate && !quorate) {
  843. quorum_change = 1;
  844. log_printf(LOGSYS_LEVEL_DEBUG, "quorum lost, blocking activity");
  845. }
  846. if (!cluster_is_quorate && quorate) {
  847. quorum_change = 1;
  848. log_printf(LOGSYS_LEVEL_DEBUG, "quorum regained, resuming activity");
  849. }
  850. cluster_is_quorate = quorate;
  851. if (cluster_is_quorate) {
  852. us->flags |= NODE_FLAGS_QUORATE;
  853. } else {
  854. us->flags &= ~NODE_FLAGS_QUORATE;
  855. }
  856. if (wait_for_all) {
  857. if (quorate) {
  858. update_wait_for_all_status(0);
  859. } else {
  860. update_wait_for_all_status(1);
  861. }
  862. }
  863. if ((quorum_change) &&
  864. (sync_in_progress == 0)) {
  865. quorum_callback(quorum_members, quorum_members_entries,
  866. cluster_is_quorate, &quorum_ringid);
  867. }
  868. LEAVE();
  869. }
  870. static void get_total_votes(unsigned int *totalvotes, unsigned int *current_members)
  871. {
  872. unsigned int total_votes = 0;
  873. unsigned int cluster_members = 0;
  874. struct list_head *nodelist;
  875. struct cluster_node *node;
  876. ENTER();
  877. list_iterate(nodelist, &cluster_members_list) {
  878. node = list_entry(nodelist, struct cluster_node, list);
  879. if (node->state == NODESTATE_MEMBER) {
  880. cluster_members++;
  881. total_votes += node->votes;
  882. }
  883. }
  884. if (qdevice->votes) {
  885. total_votes += qdevice->votes;
  886. cluster_members++;
  887. }
  888. *totalvotes = total_votes;
  889. *current_members = cluster_members;
  890. LEAVE();
  891. }
  892. /*
  893. * Recalculate cluster quorum, set quorate and notify changes
  894. */
  895. static void recalculate_quorum(int allow_decrease, int by_current_nodes)
  896. {
  897. unsigned int total_votes = 0;
  898. unsigned int cluster_members = 0;
  899. ENTER();
  900. get_total_votes(&total_votes, &cluster_members);
  901. if (!by_current_nodes) {
  902. cluster_members = 0;
  903. }
  904. /*
  905. * Keep expected_votes at the highest number of votes in the cluster
  906. */
  907. log_printf(LOGSYS_LEVEL_DEBUG, "total_votes=%d, expected_votes=%d", total_votes, us->expected_votes);
  908. if (total_votes > us->expected_votes) {
  909. us->expected_votes = total_votes;
  910. votequorum_exec_send_expectedvotes_notification();
  911. }
  912. if ((ev_tracking) &&
  913. (us->expected_votes > ev_tracking_barrier)) {
  914. update_ev_tracking_barrier(us->expected_votes);
  915. }
  916. quorum = calculate_quorum(allow_decrease, cluster_members, &total_votes);
  917. are_we_quorate(total_votes);
  918. votequorum_exec_send_quorum_notification(NULL, 0L);
  919. LEAVE();
  920. }
  921. /*
  922. * configuration bits and pieces
  923. */
  924. static int votequorum_read_nodelist_configuration(uint32_t *votes,
  925. uint32_t *nodes,
  926. uint32_t *expected_votes)
  927. {
  928. icmap_iter_t iter;
  929. const char *iter_key;
  930. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  931. uint32_t our_pos, node_pos;
  932. uint32_t nodecount = 0;
  933. uint32_t nodelist_expected_votes = 0;
  934. uint32_t node_votes = 0;
  935. int res = 0;
  936. ENTER();
  937. if (icmap_get_uint32("nodelist.local_node_pos", &our_pos) != CS_OK) {
  938. log_printf(LOGSYS_LEVEL_DEBUG,
  939. "No nodelist defined or our node is not in the nodelist");
  940. return 0;
  941. }
  942. iter = icmap_iter_init("nodelist.node.");
  943. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  944. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  945. if (res != 2) {
  946. continue;
  947. }
  948. if (strcmp(tmp_key, "ring0_addr") != 0) {
  949. continue;
  950. }
  951. nodecount++;
  952. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.quorum_votes", node_pos);
  953. if (icmap_get_uint32(tmp_key, &node_votes) != CS_OK) {
  954. node_votes = 1;
  955. }
  956. nodelist_expected_votes = nodelist_expected_votes + node_votes;
  957. if (node_pos == our_pos) {
  958. *votes = node_votes;
  959. }
  960. }
  961. *expected_votes = nodelist_expected_votes;
  962. *nodes = nodecount;
  963. icmap_iter_finalize(iter);
  964. LEAVE();
  965. return 1;
  966. }
  967. static int votequorum_qdevice_is_configured(uint32_t *qdevice_votes)
  968. {
  969. char *qdevice_model = NULL;
  970. int ret = 0;
  971. ENTER();
  972. if (icmap_get_string("quorum.device.model", &qdevice_model) == CS_OK) {
  973. if (strlen(qdevice_model)) {
  974. if (icmap_get_uint32("quorum.device.votes", qdevice_votes) != CS_OK) {
  975. *qdevice_votes = -1;
  976. }
  977. if (icmap_get_uint32("quorum.device.timeout", &qdevice_timeout) != CS_OK) {
  978. qdevice_timeout = VOTEQUORUM_QDEVICE_DEFAULT_TIMEOUT;
  979. }
  980. update_qdevice_can_operate(1);
  981. ret = 1;
  982. }
  983. free(qdevice_model);
  984. }
  985. LEAVE();
  986. return ret;
  987. }
  988. #define VOTEQUORUM_READCONFIG_STARTUP 0
  989. #define VOTEQUORUM_READCONFIG_RUNTIME 1
  990. static char *votequorum_readconfig(int runtime)
  991. {
  992. uint32_t node_votes = 0, qdevice_votes = 0;
  993. uint32_t node_expected_votes = 0, expected_votes = 0;
  994. uint32_t node_count = 0;
  995. uint8_t atb = 0;
  996. int have_nodelist, have_qdevice;
  997. char *atb_string = NULL;
  998. char *error = NULL;
  999. ENTER();
  1000. log_printf(LOGSYS_LEVEL_DEBUG, "Reading configuration (runtime: %d)", runtime);
  1001. /*
  1002. * gather basic data here
  1003. */
  1004. icmap_get_uint32("quorum.expected_votes", &expected_votes);
  1005. have_nodelist = votequorum_read_nodelist_configuration(&node_votes, &node_count, &node_expected_votes);
  1006. have_qdevice = votequorum_qdevice_is_configured(&qdevice_votes);
  1007. icmap_get_uint8("quorum.two_node", &two_node);
  1008. /*
  1009. * do config verification and enablement
  1010. */
  1011. if ((!have_nodelist) && (!expected_votes)) {
  1012. if (!runtime) {
  1013. error = (char *)"configuration error: nodelist or quorum.expected_votes must be configured!";
  1014. } else {
  1015. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: nodelist or quorum.expected_votes must be configured!");
  1016. log_printf(LOGSYS_LEVEL_CRIT, "will continue with current runtime data");
  1017. }
  1018. goto out;
  1019. }
  1020. /*
  1021. * two_node and qdevice are not compatible in the same config.
  1022. * try to make an educated guess of what to do
  1023. */
  1024. if ((two_node) && (have_qdevice)) {
  1025. if (!runtime) {
  1026. error = (char *)"configuration error: two_node and quorum device cannot be configured at the same time!";
  1027. goto out;
  1028. } else {
  1029. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: two_node and quorum device cannot be configured at the same time!");
  1030. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  1031. log_printf(LOGSYS_LEVEL_CRIT, "quorum device is registered, disabling two_node");
  1032. two_node = 0;
  1033. } else {
  1034. log_printf(LOGSYS_LEVEL_CRIT, "quorum device is not registered, allowing two_node");
  1035. update_qdevice_can_operate(0);
  1036. }
  1037. }
  1038. }
  1039. /*
  1040. * Enable special features
  1041. */
  1042. if (!runtime) {
  1043. if (two_node) {
  1044. wait_for_all = 1;
  1045. }
  1046. icmap_get_uint8("quorum.allow_downscale", &allow_downscale);
  1047. icmap_get_uint8("quorum.wait_for_all", &wait_for_all);
  1048. icmap_get_uint8("quorum.last_man_standing", &last_man_standing);
  1049. icmap_get_uint32("quorum.last_man_standing_window", &last_man_standing_window);
  1050. icmap_get_uint8("quorum.expected_votes_tracking", &ev_tracking);
  1051. icmap_get_uint8("quorum.auto_tie_breaker", &atb);
  1052. icmap_get_string("quorum.auto_tie_breaker_node", &atb_string);
  1053. if (!atb) {
  1054. auto_tie_breaker = ATB_NONE;
  1055. if (atb_string) {
  1056. log_printf(LOGSYS_LEVEL_WARNING,
  1057. "auto_tie_breaker_node: is meaningless if auto_tie_breaker is set to 0");
  1058. }
  1059. }
  1060. if (atb && atb_string) {
  1061. parse_atb_string(atb_string);
  1062. }
  1063. free(atb_string);
  1064. /* allow_downscale requires ev_tracking */
  1065. if (allow_downscale) {
  1066. ev_tracking = 1;
  1067. }
  1068. if (ev_tracking) {
  1069. if (load_ev_tracking_barrier() < 0) {
  1070. LEAVE();
  1071. return ((char *)"Unable to load ev_tracking file!");
  1072. }
  1073. update_ev_tracking_barrier(ev_tracking_barrier);
  1074. }
  1075. }
  1076. /*
  1077. * quorum device is not compatible with last_man_standing and auto_tie_breaker
  1078. * neither lms or atb can be set at runtime, so there is no need to check for
  1079. * runtime incompatibilities, but qdevice can be configured _after_ LMS and ATB have
  1080. * been enabled at startup.
  1081. */
  1082. if ((have_qdevice) && (last_man_standing)) {
  1083. if (!runtime) {
  1084. error = (char *)"configuration error: quorum.device is not compatible with last_man_standing";
  1085. goto out;
  1086. } else {
  1087. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device is not compatible with last_man_standing");
  1088. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  1089. update_qdevice_can_operate(0);
  1090. }
  1091. }
  1092. if ((have_qdevice) && (auto_tie_breaker != ATB_NONE)) {
  1093. if (!runtime) {
  1094. error = (char *)"configuration error: quorum.device is not compatible with auto_tie_breaker";
  1095. goto out;
  1096. } else {
  1097. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device is not compatible with auto_tie_breaker");
  1098. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  1099. update_qdevice_can_operate(0);
  1100. }
  1101. }
  1102. if ((have_qdevice) && (wait_for_all)) {
  1103. if (!runtime) {
  1104. error = (char *)"configuration error: quorum.device is not compatible with wait_for_all";
  1105. goto out;
  1106. } else {
  1107. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device is not compatible with wait_for_all");
  1108. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  1109. update_qdevice_can_operate(0);
  1110. }
  1111. }
  1112. if ((have_qdevice) && (allow_downscale)) {
  1113. if (!runtime) {
  1114. error = (char *)"configuration error: quorum.device is not compatible with allow_downscale";
  1115. goto out;
  1116. } else {
  1117. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device is not compatible with allow_downscale");
  1118. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  1119. update_qdevice_can_operate(0);
  1120. }
  1121. }
  1122. /*
  1123. * if user specifies quorum.expected_votes + quorum.device but NOT the device.votes
  1124. * we don't know what the quorum device should vote.
  1125. */
  1126. if ((expected_votes) && (have_qdevice) && (qdevice_votes == -1)) {
  1127. if (!runtime) {
  1128. error = (char *)"configuration error: quorum.device.votes must be specified when quorum.expected_votes is set";
  1129. goto out;
  1130. } else {
  1131. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device.votes must be specified when quorum.expected_votes is set");
  1132. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  1133. update_qdevice_can_operate(0);
  1134. }
  1135. }
  1136. /*
  1137. * if user specifies a node list with uneven votes and no device.votes
  1138. * we cannot autocalculate the votes
  1139. */
  1140. if ((have_qdevice) &&
  1141. (qdevice_votes == -1) &&
  1142. (have_nodelist) &&
  1143. (node_count != node_expected_votes)) {
  1144. if (!runtime) {
  1145. error = (char *)"configuration error: quorum.device.votes must be specified when not all nodes votes 1";
  1146. goto out;
  1147. } else {
  1148. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device.votes must be specified when not all nodes votes 1");
  1149. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  1150. update_qdevice_can_operate(0);
  1151. }
  1152. }
  1153. /*
  1154. * validate quorum device votes vs expected_votes
  1155. */
  1156. if ((qdevice_votes > 0) && (expected_votes)) {
  1157. int delta = expected_votes - qdevice_votes;
  1158. if (delta < 2) {
  1159. if (!runtime) {
  1160. error = (char *)"configuration error: quorum.device.votes is too high or expected_votes is too low";
  1161. goto out;
  1162. } else {
  1163. log_printf(LOGSYS_LEVEL_CRIT, "configuration error: quorum.device.votes is too high or expected_votes is too low");
  1164. log_printf(LOGSYS_LEVEL_CRIT, "disabling quorum device operations");
  1165. update_qdevice_can_operate(0);
  1166. }
  1167. }
  1168. }
  1169. /*
  1170. * automatically calculate device votes and adjust expected_votes from nodelist
  1171. */
  1172. if ((have_qdevice) &&
  1173. (qdevice_votes == -1) &&
  1174. (!expected_votes) &&
  1175. (have_nodelist) &&
  1176. (node_count == node_expected_votes)) {
  1177. qdevice_votes = node_expected_votes - 1;
  1178. node_expected_votes = node_expected_votes + qdevice_votes;
  1179. }
  1180. /*
  1181. * set this node votes and expected_votes
  1182. */
  1183. log_printf(LOGSYS_LEVEL_DEBUG, "ev_tracking=%d, ev_tracking_barrier = %d: expected_votes = %d\n", ev_tracking, ev_tracking_barrier, expected_votes);
  1184. if (ev_tracking) {
  1185. expected_votes = ev_tracking_barrier;
  1186. }
  1187. if (have_nodelist) {
  1188. us->votes = node_votes;
  1189. us->expected_votes = node_expected_votes;
  1190. } else {
  1191. us->votes = 1;
  1192. icmap_get_uint32("quorum.votes", &us->votes);
  1193. }
  1194. if (expected_votes) {
  1195. us->expected_votes = expected_votes;
  1196. }
  1197. /*
  1198. * set qdevice votes
  1199. */
  1200. if (!have_qdevice) {
  1201. qdevice->votes = 0;
  1202. }
  1203. if (qdevice_votes != -1) {
  1204. qdevice->votes = qdevice_votes;
  1205. }
  1206. update_ev_barrier(us->expected_votes);
  1207. update_two_node();
  1208. if (wait_for_all) {
  1209. update_wait_for_all_status(1);
  1210. }
  1211. out:
  1212. LEAVE();
  1213. return error;
  1214. }
  1215. static void votequorum_refresh_config(
  1216. int32_t event,
  1217. const char *key_name,
  1218. struct icmap_notify_value new_val,
  1219. struct icmap_notify_value old_val,
  1220. void *user_data)
  1221. {
  1222. int old_votes, old_expected_votes;
  1223. uint8_t reloading;
  1224. ENTER();
  1225. /*
  1226. * If a full reload is in progress then don't do anything until it's done and
  1227. * can reconfigure it all atomically
  1228. */
  1229. if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
  1230. return ;
  1231. }
  1232. old_votes = us->votes;
  1233. old_expected_votes = us->expected_votes;
  1234. /*
  1235. * Reload the configuration
  1236. */
  1237. votequorum_readconfig(VOTEQUORUM_READCONFIG_RUNTIME);
  1238. /*
  1239. * activate new config
  1240. */
  1241. votequorum_exec_send_nodeinfo(us->node_id);
  1242. votequorum_exec_send_nodeinfo(VOTEQUORUM_QDEVICE_NODEID);
  1243. if (us->votes != old_votes) {
  1244. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES,
  1245. us->node_id, us->votes);
  1246. }
  1247. if (us->expected_votes != old_expected_votes) {
  1248. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES,
  1249. us->node_id, us->expected_votes);
  1250. }
  1251. LEAVE();
  1252. }
  1253. static void votequorum_exec_add_config_notification(void)
  1254. {
  1255. icmap_track_t icmap_track_nodelist = NULL;
  1256. icmap_track_t icmap_track_quorum = NULL;
  1257. icmap_track_t icmap_track_reload = NULL;
  1258. ENTER();
  1259. icmap_track_add("nodelist.",
  1260. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  1261. votequorum_refresh_config,
  1262. NULL,
  1263. &icmap_track_nodelist);
  1264. icmap_track_add("quorum.",
  1265. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  1266. votequorum_refresh_config,
  1267. NULL,
  1268. &icmap_track_quorum);
  1269. icmap_track_add("config.totemconfig_reload_in_progress",
  1270. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  1271. votequorum_refresh_config,
  1272. NULL,
  1273. &icmap_track_reload);
  1274. LEAVE();
  1275. }
  1276. /*
  1277. * votequorum_exec core
  1278. */
  1279. static int votequorum_exec_send_reconfigure(uint8_t param, unsigned int nodeid, uint32_t value)
  1280. {
  1281. struct req_exec_quorum_reconfigure req_exec_quorum_reconfigure;
  1282. struct iovec iov[1];
  1283. int ret;
  1284. ENTER();
  1285. req_exec_quorum_reconfigure.nodeid = nodeid;
  1286. req_exec_quorum_reconfigure.value = value;
  1287. req_exec_quorum_reconfigure.param = param;
  1288. req_exec_quorum_reconfigure._pad0 = 0;
  1289. req_exec_quorum_reconfigure._pad1 = 0;
  1290. req_exec_quorum_reconfigure._pad2 = 0;
  1291. req_exec_quorum_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_RECONFIGURE);
  1292. req_exec_quorum_reconfigure.header.size = sizeof(req_exec_quorum_reconfigure);
  1293. iov[0].iov_base = (void *)&req_exec_quorum_reconfigure;
  1294. iov[0].iov_len = sizeof(req_exec_quorum_reconfigure);
  1295. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  1296. LEAVE();
  1297. return ret;
  1298. }
  1299. static int votequorum_exec_send_nodeinfo(uint32_t nodeid)
  1300. {
  1301. struct req_exec_quorum_nodeinfo req_exec_quorum_nodeinfo;
  1302. struct iovec iov[1];
  1303. struct cluster_node *node;
  1304. int ret;
  1305. ENTER();
  1306. node = find_node_by_nodeid(nodeid);
  1307. if (!node) {
  1308. return -1;
  1309. }
  1310. req_exec_quorum_nodeinfo.nodeid = nodeid;
  1311. req_exec_quorum_nodeinfo.votes = node->votes;
  1312. req_exec_quorum_nodeinfo.expected_votes = node->expected_votes;
  1313. req_exec_quorum_nodeinfo.flags = node->flags;
  1314. if (nodeid != VOTEQUORUM_QDEVICE_NODEID) {
  1315. decode_flags(node->flags);
  1316. }
  1317. req_exec_quorum_nodeinfo.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_NODEINFO);
  1318. req_exec_quorum_nodeinfo.header.size = sizeof(req_exec_quorum_nodeinfo);
  1319. iov[0].iov_base = (void *)&req_exec_quorum_nodeinfo;
  1320. iov[0].iov_len = sizeof(req_exec_quorum_nodeinfo);
  1321. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  1322. LEAVE();
  1323. return ret;
  1324. }
  1325. static int votequorum_exec_send_qdevice_reconfigure(const char *oldname, const char *newname)
  1326. {
  1327. struct req_exec_quorum_qdevice_reconfigure req_exec_quorum_qdevice_reconfigure;
  1328. struct iovec iov[1];
  1329. int ret;
  1330. ENTER();
  1331. req_exec_quorum_qdevice_reconfigure.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_QDEVICE_RECONFIGURE);
  1332. req_exec_quorum_qdevice_reconfigure.header.size = sizeof(req_exec_quorum_qdevice_reconfigure);
  1333. strcpy(req_exec_quorum_qdevice_reconfigure.oldname, oldname);
  1334. strcpy(req_exec_quorum_qdevice_reconfigure.newname, newname);
  1335. iov[0].iov_base = (void *)&req_exec_quorum_qdevice_reconfigure;
  1336. iov[0].iov_len = sizeof(req_exec_quorum_qdevice_reconfigure);
  1337. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  1338. LEAVE();
  1339. return ret;
  1340. }
  1341. static int votequorum_exec_send_qdevice_reg(uint32_t operation, const char *qdevice_name_req)
  1342. {
  1343. struct req_exec_quorum_qdevice_reg req_exec_quorum_qdevice_reg;
  1344. struct iovec iov[1];
  1345. int ret;
  1346. ENTER();
  1347. req_exec_quorum_qdevice_reg.header.id = SERVICE_ID_MAKE(VOTEQUORUM_SERVICE, MESSAGE_REQ_EXEC_VOTEQUORUM_QDEVICE_REG);
  1348. req_exec_quorum_qdevice_reg.header.size = sizeof(req_exec_quorum_qdevice_reg);
  1349. req_exec_quorum_qdevice_reg.operation = operation;
  1350. strcpy(req_exec_quorum_qdevice_reg.qdevice_name, qdevice_name_req);
  1351. iov[0].iov_base = (void *)&req_exec_quorum_qdevice_reg;
  1352. iov[0].iov_len = sizeof(req_exec_quorum_qdevice_reg);
  1353. ret = corosync_api->totem_mcast (iov, 1, TOTEM_AGREED);
  1354. LEAVE();
  1355. return ret;
  1356. }
  1357. static int votequorum_exec_send_quorum_notification(void *conn, uint64_t context)
  1358. {
  1359. struct res_lib_votequorum_notification *res_lib_votequorum_notification;
  1360. struct list_head *tmp;
  1361. struct cluster_node *node;
  1362. int cluster_members = 0;
  1363. int i = 0;
  1364. int size;
  1365. char buf[sizeof(struct res_lib_votequorum_notification) + sizeof(struct votequorum_node) * (PROCESSOR_COUNT_MAX + 2)];
  1366. ENTER();
  1367. list_iterate(tmp, &cluster_members_list) {
  1368. node = list_entry(tmp, struct cluster_node, list);
  1369. cluster_members++;
  1370. }
  1371. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  1372. cluster_members++;
  1373. }
  1374. size = sizeof(struct res_lib_votequorum_notification) + sizeof(struct votequorum_node) * cluster_members;
  1375. res_lib_votequorum_notification = (struct res_lib_votequorum_notification *)&buf;
  1376. res_lib_votequorum_notification->quorate = cluster_is_quorate;
  1377. res_lib_votequorum_notification->node_list_entries = cluster_members;
  1378. res_lib_votequorum_notification->ring_id.nodeid = quorum_ringid.rep.nodeid;
  1379. res_lib_votequorum_notification->ring_id.seq = quorum_ringid.seq;
  1380. res_lib_votequorum_notification->context = context;
  1381. list_iterate(tmp, &cluster_members_list) {
  1382. node = list_entry(tmp, struct cluster_node, list);
  1383. res_lib_votequorum_notification->node_list[i].nodeid = node->node_id;
  1384. res_lib_votequorum_notification->node_list[i++].state = node->state;
  1385. }
  1386. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  1387. res_lib_votequorum_notification->node_list[i].nodeid = VOTEQUORUM_QDEVICE_NODEID;
  1388. res_lib_votequorum_notification->node_list[i++].state = qdevice->state;
  1389. }
  1390. res_lib_votequorum_notification->header.id = MESSAGE_RES_VOTEQUORUM_NOTIFICATION;
  1391. res_lib_votequorum_notification->header.size = size;
  1392. res_lib_votequorum_notification->header.error = CS_OK;
  1393. /* Send it to all interested parties */
  1394. if (conn) {
  1395. int ret = corosync_api->ipc_dispatch_send(conn, &buf, size);
  1396. LEAVE();
  1397. return ret;
  1398. } else {
  1399. struct quorum_pd *qpd;
  1400. list_iterate(tmp, &trackers_list) {
  1401. qpd = list_entry(tmp, struct quorum_pd, list);
  1402. res_lib_votequorum_notification->context = qpd->tracking_context;
  1403. corosync_api->ipc_dispatch_send(qpd->conn, &buf, size);
  1404. }
  1405. }
  1406. LEAVE();
  1407. return 0;
  1408. }
  1409. static void votequorum_exec_send_expectedvotes_notification(void)
  1410. {
  1411. struct res_lib_votequorum_expectedvotes_notification res_lib_votequorum_expectedvotes_notification;
  1412. struct quorum_pd *qpd;
  1413. struct list_head *tmp;
  1414. ENTER();
  1415. log_printf(LOGSYS_LEVEL_DEBUG, "Sending expected votes callback");
  1416. res_lib_votequorum_expectedvotes_notification.header.id = MESSAGE_RES_VOTEQUORUM_EXPECTEDVOTES_NOTIFICATION;
  1417. res_lib_votequorum_expectedvotes_notification.header.size = sizeof(res_lib_votequorum_expectedvotes_notification);
  1418. res_lib_votequorum_expectedvotes_notification.header.error = CS_OK;
  1419. res_lib_votequorum_expectedvotes_notification.expected_votes = us->expected_votes;
  1420. list_iterate(tmp, &trackers_list) {
  1421. qpd = list_entry(tmp, struct quorum_pd, list);
  1422. res_lib_votequorum_expectedvotes_notification.context = qpd->tracking_context;
  1423. corosync_api->ipc_dispatch_send(qpd->conn, &res_lib_votequorum_expectedvotes_notification,
  1424. sizeof(struct res_lib_votequorum_expectedvotes_notification));
  1425. }
  1426. LEAVE();
  1427. }
  1428. static void exec_votequorum_qdevice_reconfigure_endian_convert (void *message)
  1429. {
  1430. ENTER();
  1431. LEAVE();
  1432. }
  1433. static void message_handler_req_exec_votequorum_qdevice_reconfigure (
  1434. const void *message,
  1435. unsigned int nodeid)
  1436. {
  1437. const struct req_exec_quorum_qdevice_reconfigure *req_exec_quorum_qdevice_reconfigure = message;
  1438. ENTER();
  1439. log_printf(LOGSYS_LEVEL_DEBUG, "Received qdevice name change req from node %u [from: %s to: %s]",
  1440. nodeid,
  1441. req_exec_quorum_qdevice_reconfigure->oldname,
  1442. req_exec_quorum_qdevice_reconfigure->newname);
  1443. if (!strcmp(req_exec_quorum_qdevice_reconfigure->oldname, qdevice_name)) {
  1444. log_printf(LOGSYS_LEVEL_DEBUG, "Allowing qdevice rename");
  1445. memset(qdevice_name, 0, VOTEQUORUM_QDEVICE_MAX_NAME_LEN);
  1446. strcpy(qdevice_name, req_exec_quorum_qdevice_reconfigure->newname);
  1447. /*
  1448. * TODO: notify qdevices about name change?
  1449. * this is not relevant for now and can wait later on since
  1450. * qdevices are local only and libvotequorum is not final
  1451. */
  1452. }
  1453. LEAVE();
  1454. }
  1455. static void exec_votequorum_qdevice_reg_endian_convert (void *message)
  1456. {
  1457. struct req_exec_quorum_qdevice_reg *req_exec_quorum_qdevice_reg = message;
  1458. ENTER();
  1459. req_exec_quorum_qdevice_reg->operation = swab32(req_exec_quorum_qdevice_reg->operation);
  1460. LEAVE();
  1461. }
  1462. static void message_handler_req_exec_votequorum_qdevice_reg (
  1463. const void *message,
  1464. unsigned int nodeid)
  1465. {
  1466. const struct req_exec_quorum_qdevice_reg *req_exec_quorum_qdevice_reg = message;
  1467. struct res_lib_votequorum_status res_lib_votequorum_status;
  1468. int wipe_qdevice_name = 1;
  1469. struct cluster_node *node = NULL;
  1470. struct list_head *tmp;
  1471. cs_error_t error = CS_OK;
  1472. ENTER();
  1473. log_printf(LOGSYS_LEVEL_DEBUG, "Received qdevice op %u req from node %u [%s]",
  1474. req_exec_quorum_qdevice_reg->operation,
  1475. nodeid, req_exec_quorum_qdevice_reg->qdevice_name);
  1476. switch(req_exec_quorum_qdevice_reg->operation)
  1477. {
  1478. case VOTEQUORUM_QDEVICE_OPERATION_REGISTER:
  1479. if (nodeid != us->node_id) {
  1480. if (!strlen(qdevice_name)) {
  1481. log_printf(LOGSYS_LEVEL_DEBUG, "Remote qdevice name recorded");
  1482. strcpy(qdevice_name, req_exec_quorum_qdevice_reg->qdevice_name);
  1483. }
  1484. LEAVE();
  1485. return;
  1486. }
  1487. /*
  1488. * protect against the case where we broadcast qdevice registration
  1489. * to new memebers, we receive the message back, but there is no registration
  1490. * connection in progress
  1491. */
  1492. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  1493. LEAVE();
  1494. return;
  1495. }
  1496. /*
  1497. * this should NEVER happen
  1498. */
  1499. if (!qdevice_reg_conn) {
  1500. log_printf(LOGSYS_LEVEL_WARNING, "Unable to determine origin of the qdevice register call!");
  1501. LEAVE();
  1502. return;
  1503. }
  1504. /*
  1505. * registering our own device in this case
  1506. */
  1507. if (!strlen(qdevice_name)) {
  1508. strcpy(qdevice_name, req_exec_quorum_qdevice_reg->qdevice_name);
  1509. }
  1510. /*
  1511. * check if it is our device or something else
  1512. */
  1513. if ((!strncmp(req_exec_quorum_qdevice_reg->qdevice_name,
  1514. qdevice_name, VOTEQUORUM_QDEVICE_MAX_NAME_LEN))) {
  1515. us->flags |= NODE_FLAGS_QDEVICE_REGISTERED;
  1516. votequorum_exec_send_nodeinfo(VOTEQUORUM_QDEVICE_NODEID);
  1517. votequorum_exec_send_nodeinfo(us->node_id);
  1518. } else {
  1519. log_printf(LOGSYS_LEVEL_WARNING,
  1520. "A new qdevice with different name (new: %s old: %s) is trying to register!",
  1521. req_exec_quorum_qdevice_reg->qdevice_name, qdevice_name);
  1522. error = CS_ERR_EXIST;
  1523. }
  1524. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  1525. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  1526. res_lib_votequorum_status.header.error = error;
  1527. corosync_api->ipc_response_send(qdevice_reg_conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  1528. qdevice_reg_conn = NULL;
  1529. break;
  1530. case VOTEQUORUM_QDEVICE_OPERATION_UNREGISTER:
  1531. list_iterate(tmp, &cluster_members_list) {
  1532. node = list_entry(tmp, struct cluster_node, list);
  1533. if ((node->state == NODESTATE_MEMBER) &&
  1534. (node->flags & NODE_FLAGS_QDEVICE_REGISTERED)) {
  1535. wipe_qdevice_name = 0;
  1536. }
  1537. }
  1538. if (wipe_qdevice_name) {
  1539. memset(qdevice_name, 0, VOTEQUORUM_QDEVICE_MAX_NAME_LEN);
  1540. }
  1541. break;
  1542. }
  1543. LEAVE();
  1544. }
  1545. static void exec_votequorum_nodeinfo_endian_convert (void *message)
  1546. {
  1547. struct req_exec_quorum_nodeinfo *nodeinfo = message;
  1548. ENTER();
  1549. nodeinfo->nodeid = swab32(nodeinfo->nodeid);
  1550. nodeinfo->votes = swab32(nodeinfo->votes);
  1551. nodeinfo->expected_votes = swab32(nodeinfo->expected_votes);
  1552. nodeinfo->flags = swab32(nodeinfo->flags);
  1553. LEAVE();
  1554. }
  1555. static void message_handler_req_exec_votequorum_nodeinfo (
  1556. const void *message,
  1557. unsigned int sender_nodeid)
  1558. {
  1559. const struct req_exec_quorum_nodeinfo *req_exec_quorum_nodeinfo = message;
  1560. struct cluster_node *node = NULL;
  1561. int old_votes;
  1562. int old_expected;
  1563. uint32_t old_flags;
  1564. nodestate_t old_state;
  1565. int new_node = 0;
  1566. int allow_downgrade = 0;
  1567. int by_node = 0;
  1568. unsigned int nodeid = req_exec_quorum_nodeinfo->nodeid;
  1569. ENTER();
  1570. log_printf(LOGSYS_LEVEL_DEBUG, "got nodeinfo message from cluster node %u", sender_nodeid);
  1571. log_printf(LOGSYS_LEVEL_DEBUG, "nodeinfo message[%u]: votes: %d, expected: %d flags: %d",
  1572. nodeid,
  1573. req_exec_quorum_nodeinfo->votes,
  1574. req_exec_quorum_nodeinfo->expected_votes,
  1575. req_exec_quorum_nodeinfo->flags);
  1576. if (nodeid != VOTEQUORUM_QDEVICE_NODEID) {
  1577. decode_flags(req_exec_quorum_nodeinfo->flags);
  1578. }
  1579. node = find_node_by_nodeid(nodeid);
  1580. if (!node) {
  1581. node = allocate_node(nodeid);
  1582. new_node = 1;
  1583. }
  1584. if (!node) {
  1585. corosync_api->error_memory_failure();
  1586. LEAVE();
  1587. return;
  1588. }
  1589. if (new_node) {
  1590. old_votes = 0;
  1591. old_expected = 0;
  1592. old_state = NODESTATE_DEAD;
  1593. old_flags = 0;
  1594. } else {
  1595. old_votes = node->votes;
  1596. old_expected = node->expected_votes;
  1597. old_state = node->state;
  1598. old_flags = node->flags;
  1599. }
  1600. if (nodeid == VOTEQUORUM_QDEVICE_NODEID) {
  1601. struct cluster_node *sender_node = find_node_by_nodeid(sender_nodeid);
  1602. assert(sender_node != NULL);
  1603. if ((!cluster_is_quorate) &&
  1604. (sender_node->flags & NODE_FLAGS_QUORATE)) {
  1605. node->votes = req_exec_quorum_nodeinfo->votes;
  1606. } else {
  1607. node->votes = max(node->votes, req_exec_quorum_nodeinfo->votes);
  1608. }
  1609. goto recalculate;
  1610. }
  1611. /* Update node state */
  1612. node->flags = req_exec_quorum_nodeinfo->flags;
  1613. node->votes = req_exec_quorum_nodeinfo->votes;
  1614. node->state = NODESTATE_MEMBER;
  1615. if (node->flags & NODE_FLAGS_LEAVING) {
  1616. node->state = NODESTATE_LEAVING;
  1617. allow_downgrade = 1;
  1618. by_node = 1;
  1619. }
  1620. if ((!cluster_is_quorate) &&
  1621. (node->flags & NODE_FLAGS_QUORATE)) {
  1622. allow_downgrade = 1;
  1623. us->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  1624. }
  1625. if (node->flags & NODE_FLAGS_QUORATE || (ev_tracking)) {
  1626. node->expected_votes = req_exec_quorum_nodeinfo->expected_votes;
  1627. } else {
  1628. node->expected_votes = us->expected_votes;
  1629. }
  1630. if ((last_man_standing) && (node->votes > 1)) {
  1631. log_printf(LOGSYS_LEVEL_WARNING, "Last Man Standing feature is supported only when all"
  1632. "cluster nodes votes are set to 1. Disabling LMS.");
  1633. last_man_standing = 0;
  1634. if (last_man_standing_timer_set) {
  1635. corosync_api->timer_delete(last_man_standing_timer);
  1636. last_man_standing_timer_set = 0;
  1637. }
  1638. }
  1639. recalculate:
  1640. if ((new_node) ||
  1641. (nodeid == us->node_id) ||
  1642. (node->flags & NODE_FLAGS_FIRST) ||
  1643. (old_votes != node->votes) ||
  1644. (old_expected != node->expected_votes) ||
  1645. (old_flags != node->flags) ||
  1646. (old_state != node->state)) {
  1647. recalculate_quorum(allow_downgrade, by_node);
  1648. }
  1649. if ((wait_for_all) &&
  1650. (!(node->flags & NODE_FLAGS_WFASTATUS)) &&
  1651. (node->flags & NODE_FLAGS_QUORATE)) {
  1652. update_wait_for_all_status(0);
  1653. }
  1654. LEAVE();
  1655. }
  1656. static void exec_votequorum_reconfigure_endian_convert (void *message)
  1657. {
  1658. struct req_exec_quorum_reconfigure *reconfigure = message;
  1659. ENTER();
  1660. reconfigure->nodeid = swab32(reconfigure->nodeid);
  1661. reconfigure->value = swab32(reconfigure->value);
  1662. LEAVE();
  1663. }
  1664. static void message_handler_req_exec_votequorum_reconfigure (
  1665. const void *message,
  1666. unsigned int nodeid)
  1667. {
  1668. const struct req_exec_quorum_reconfigure *req_exec_quorum_reconfigure = message;
  1669. struct cluster_node *node;
  1670. struct list_head *nodelist;
  1671. ENTER();
  1672. log_printf(LOGSYS_LEVEL_DEBUG, "got reconfigure message from cluster node %u for %u",
  1673. nodeid, req_exec_quorum_reconfigure->nodeid);
  1674. switch(req_exec_quorum_reconfigure->param)
  1675. {
  1676. case VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES:
  1677. list_iterate(nodelist, &cluster_members_list) {
  1678. node = list_entry(nodelist, struct cluster_node, list);
  1679. if (node->state == NODESTATE_MEMBER) {
  1680. node->expected_votes = req_exec_quorum_reconfigure->value;
  1681. }
  1682. }
  1683. votequorum_exec_send_expectedvotes_notification();
  1684. update_ev_barrier(req_exec_quorum_reconfigure->value);
  1685. if (ev_tracking) {
  1686. us->expected_votes = max(us->expected_votes, ev_tracking_barrier);
  1687. }
  1688. recalculate_quorum(1, 0); /* Allow decrease */
  1689. break;
  1690. case VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES:
  1691. node = find_node_by_nodeid(req_exec_quorum_reconfigure->nodeid);
  1692. if (!node) {
  1693. LEAVE();
  1694. return;
  1695. }
  1696. node->votes = req_exec_quorum_reconfigure->value;
  1697. recalculate_quorum(1, 0); /* Allow decrease */
  1698. break;
  1699. }
  1700. LEAVE();
  1701. }
  1702. static int votequorum_exec_exit_fn (void)
  1703. {
  1704. int ret = 0;
  1705. ENTER();
  1706. /*
  1707. * tell the other nodes we are leaving
  1708. */
  1709. if (allow_downscale) {
  1710. us->flags |= NODE_FLAGS_LEAVING;
  1711. ret = votequorum_exec_send_nodeinfo(us->node_id);
  1712. }
  1713. if ((ev_tracking) && (ev_tracking_fd != -1)) {
  1714. close(ev_tracking_fd);
  1715. }
  1716. LEAVE();
  1717. return ret;
  1718. }
  1719. static char *votequorum_exec_init_fn (struct corosync_api_v1 *api)
  1720. {
  1721. char *error = NULL;
  1722. ENTER();
  1723. /*
  1724. * make sure we start clean
  1725. */
  1726. list_init(&cluster_members_list);
  1727. list_init(&trackers_list);
  1728. qdevice = NULL;
  1729. us = NULL;
  1730. memset(cluster_nodes, 0, sizeof(cluster_nodes));
  1731. /*
  1732. * Allocate a cluster_node for qdevice
  1733. */
  1734. qdevice = allocate_node(VOTEQUORUM_QDEVICE_NODEID);
  1735. if (!qdevice) {
  1736. LEAVE();
  1737. return ((char *)"Could not allocate node.");
  1738. }
  1739. qdevice->votes = 0;
  1740. memset(qdevice_name, 0, VOTEQUORUM_QDEVICE_MAX_NAME_LEN);
  1741. /*
  1742. * Allocate a cluster_node for us
  1743. */
  1744. us = allocate_node(corosync_api->totem_nodeid_get());
  1745. if (!us) {
  1746. LEAVE();
  1747. return ((char *)"Could not allocate node.");
  1748. }
  1749. icmap_set_uint32("runtime.votequorum.this_node_id", us->node_id);
  1750. us->state = NODESTATE_MEMBER;
  1751. us->votes = 1;
  1752. us->flags |= NODE_FLAGS_FIRST;
  1753. error = votequorum_readconfig(VOTEQUORUM_READCONFIG_STARTUP);
  1754. if (error) {
  1755. return error;
  1756. }
  1757. recalculate_quorum(0, 0);
  1758. /*
  1759. * Listen for changes
  1760. */
  1761. votequorum_exec_add_config_notification();
  1762. /*
  1763. * Start us off with one node
  1764. */
  1765. votequorum_exec_send_nodeinfo(us->node_id);
  1766. LEAVE();
  1767. return (NULL);
  1768. }
  1769. /*
  1770. * votequorum service core
  1771. */
  1772. static void votequorum_last_man_standing_timer_fn(void *arg)
  1773. {
  1774. ENTER();
  1775. last_man_standing_timer_set = 0;
  1776. if (cluster_is_quorate) {
  1777. recalculate_quorum(1,1);
  1778. }
  1779. LEAVE();
  1780. }
  1781. static void votequorum_sync_init (
  1782. const unsigned int *trans_list, size_t trans_list_entries,
  1783. const unsigned int *member_list, size_t member_list_entries,
  1784. const struct memb_ring_id *ring_id)
  1785. {
  1786. int i, j;
  1787. int found;
  1788. int left_nodes;
  1789. struct cluster_node *node;
  1790. ENTER();
  1791. sync_in_progress = 1;
  1792. sync_nodeinfo_sent = 0;
  1793. sync_wait_for_poll_or_timeout = 0;
  1794. if (member_list_entries > 1) {
  1795. us->flags &= ~NODE_FLAGS_FIRST;
  1796. }
  1797. /*
  1798. * we don't need to track which nodes have left directly,
  1799. * since that info is in the node db, but we need to know
  1800. * if somebody has left for last_man_standing
  1801. */
  1802. left_nodes = 0;
  1803. for (i = 0; i < quorum_members_entries; i++) {
  1804. found = 0;
  1805. for (j = 0; j < member_list_entries; j++) {
  1806. if (quorum_members[i] == member_list[j]) {
  1807. found = 1;
  1808. break;
  1809. }
  1810. }
  1811. if (found == 0) {
  1812. left_nodes = 1;
  1813. node = find_node_by_nodeid(quorum_members[i]);
  1814. if (node) {
  1815. node->state = NODESTATE_DEAD;
  1816. }
  1817. }
  1818. }
  1819. if (last_man_standing) {
  1820. if (((member_list_entries >= quorum) && (left_nodes)) ||
  1821. ((member_list_entries <= quorum) && (auto_tie_breaker != ATB_NONE) && (check_low_node_id_partition() == 1))) {
  1822. if (last_man_standing_timer_set) {
  1823. corosync_api->timer_delete(last_man_standing_timer);
  1824. last_man_standing_timer_set = 0;
  1825. }
  1826. corosync_api->timer_add_duration((unsigned long long)last_man_standing_window*1000000,
  1827. NULL, votequorum_last_man_standing_timer_fn,
  1828. &last_man_standing_timer);
  1829. last_man_standing_timer_set = 1;
  1830. }
  1831. }
  1832. memcpy(previous_quorum_members, quorum_members, sizeof(unsigned int) * quorum_members_entries);
  1833. previous_quorum_members_entries = quorum_members_entries;
  1834. memcpy(quorum_members, member_list, sizeof(unsigned int) * member_list_entries);
  1835. quorum_members_entries = member_list_entries;
  1836. memcpy(&quorum_ringid, ring_id, sizeof(*ring_id));
  1837. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED && us->flags & NODE_FLAGS_QDEVICE_ALIVE) {
  1838. /*
  1839. * Reset poll timer. Sync waiting is interrupted on valid qdevice poll or after timeout
  1840. */
  1841. if (qdevice_timer_set) {
  1842. corosync_api->timer_delete(qdevice_timer);
  1843. }
  1844. corosync_api->timer_add_duration((unsigned long long)qdevice_timeout*1000000, qdevice,
  1845. qdevice_timer_fn, &qdevice_timer);
  1846. qdevice_timer_set = 1;
  1847. sync_wait_for_poll_or_timeout = 1;
  1848. log_printf(LOGSYS_LEVEL_INFO, "waiting for quorum device %s poll (but maximum for %u ms)",
  1849. qdevice_name, qdevice_timeout);
  1850. }
  1851. LEAVE();
  1852. }
  1853. static int votequorum_sync_process (void)
  1854. {
  1855. if (!sync_nodeinfo_sent) {
  1856. votequorum_exec_send_nodeinfo(us->node_id);
  1857. votequorum_exec_send_nodeinfo(VOTEQUORUM_QDEVICE_NODEID);
  1858. if (strlen(qdevice_name)) {
  1859. votequorum_exec_send_qdevice_reg(VOTEQUORUM_QDEVICE_OPERATION_REGISTER,
  1860. qdevice_name);
  1861. }
  1862. sync_nodeinfo_sent = 1;
  1863. }
  1864. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED && sync_wait_for_poll_or_timeout) {
  1865. /*
  1866. * Waiting for qdevice to poll with new ringid or timeout
  1867. */
  1868. return (-1);
  1869. }
  1870. return 0;
  1871. }
  1872. static void votequorum_sync_activate (void)
  1873. {
  1874. recalculate_quorum(0, 0);
  1875. quorum_callback(quorum_members, quorum_members_entries,
  1876. cluster_is_quorate, &quorum_ringid);
  1877. sync_in_progress = 0;
  1878. }
  1879. static void votequorum_sync_abort (void)
  1880. {
  1881. }
  1882. char *votequorum_init(struct corosync_api_v1 *api,
  1883. quorum_set_quorate_fn_t q_set_quorate_fn)
  1884. {
  1885. char *error;
  1886. ENTER();
  1887. if (q_set_quorate_fn == NULL) {
  1888. return ((char *)"Quorate function not set");
  1889. }
  1890. corosync_api = api;
  1891. quorum_callback = q_set_quorate_fn;
  1892. error = corosync_service_link_and_init(corosync_api,
  1893. &votequorum_service[0]);
  1894. if (error) {
  1895. return (error);
  1896. }
  1897. LEAVE();
  1898. return (NULL);
  1899. }
  1900. /*
  1901. * Library Handler init/fini
  1902. */
  1903. static int quorum_lib_init_fn (void *conn)
  1904. {
  1905. struct quorum_pd *pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1906. ENTER();
  1907. list_init (&pd->list);
  1908. pd->conn = conn;
  1909. LEAVE();
  1910. return (0);
  1911. }
  1912. static int quorum_lib_exit_fn (void *conn)
  1913. {
  1914. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  1915. ENTER();
  1916. if (quorum_pd->tracking_enabled) {
  1917. list_del (&quorum_pd->list);
  1918. list_init (&quorum_pd->list);
  1919. }
  1920. LEAVE();
  1921. return (0);
  1922. }
  1923. /*
  1924. * library internal functions
  1925. */
  1926. static void qdevice_timer_fn(void *arg)
  1927. {
  1928. ENTER();
  1929. if ((!(us->flags & NODE_FLAGS_QDEVICE_ALIVE)) ||
  1930. (!qdevice_timer_set)) {
  1931. LEAVE();
  1932. return;
  1933. }
  1934. us->flags &= ~NODE_FLAGS_QDEVICE_ALIVE;
  1935. us->flags &= ~NODE_FLAGS_QDEVICE_CAST_VOTE;
  1936. log_printf(LOGSYS_LEVEL_INFO, "lost contact with quorum device %s", qdevice_name);
  1937. votequorum_exec_send_nodeinfo(us->node_id);
  1938. qdevice_timer_set = 0;
  1939. sync_wait_for_poll_or_timeout = 0;
  1940. LEAVE();
  1941. }
  1942. /*
  1943. * Library Handler Functions
  1944. */
  1945. static void message_handler_req_lib_votequorum_getinfo (void *conn, const void *message)
  1946. {
  1947. const struct req_lib_votequorum_getinfo *req_lib_votequorum_getinfo = message;
  1948. struct res_lib_votequorum_getinfo res_lib_votequorum_getinfo;
  1949. struct cluster_node *node;
  1950. unsigned int highest_expected = 0;
  1951. unsigned int total_votes = 0;
  1952. cs_error_t error = CS_OK;
  1953. uint32_t nodeid = req_lib_votequorum_getinfo->nodeid;
  1954. ENTER();
  1955. log_printf(LOGSYS_LEVEL_DEBUG, "got getinfo request on %p for node %u", conn, req_lib_votequorum_getinfo->nodeid);
  1956. if (nodeid == VOTEQUORUM_QDEVICE_NODEID) {
  1957. nodeid = us->node_id;
  1958. }
  1959. node = find_node_by_nodeid(nodeid);
  1960. if (node) {
  1961. struct cluster_node *iternode;
  1962. struct list_head *nodelist;
  1963. list_iterate(nodelist, &cluster_members_list) {
  1964. iternode = list_entry(nodelist, struct cluster_node, list);
  1965. if (iternode->state == NODESTATE_MEMBER) {
  1966. highest_expected =
  1967. max(highest_expected, iternode->expected_votes);
  1968. total_votes += iternode->votes;
  1969. }
  1970. }
  1971. if (node->flags & NODE_FLAGS_QDEVICE_CAST_VOTE) {
  1972. total_votes += qdevice->votes;
  1973. }
  1974. switch(node->state) {
  1975. case NODESTATE_MEMBER:
  1976. res_lib_votequorum_getinfo.state = VOTEQUORUM_NODESTATE_MEMBER;
  1977. break;
  1978. case NODESTATE_DEAD:
  1979. res_lib_votequorum_getinfo.state = VOTEQUORUM_NODESTATE_DEAD;
  1980. break;
  1981. case NODESTATE_LEAVING:
  1982. res_lib_votequorum_getinfo.state = VOTEQUORUM_NODESTATE_LEAVING;
  1983. break;
  1984. default:
  1985. res_lib_votequorum_getinfo.state = node->state;
  1986. break;
  1987. }
  1988. res_lib_votequorum_getinfo.state = node->state;
  1989. res_lib_votequorum_getinfo.votes = node->votes;
  1990. res_lib_votequorum_getinfo.expected_votes = node->expected_votes;
  1991. res_lib_votequorum_getinfo.highest_expected = highest_expected;
  1992. res_lib_votequorum_getinfo.quorum = quorum;
  1993. res_lib_votequorum_getinfo.total_votes = total_votes;
  1994. res_lib_votequorum_getinfo.flags = 0;
  1995. res_lib_votequorum_getinfo.nodeid = node->node_id;
  1996. if (two_node) {
  1997. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_TWONODE;
  1998. }
  1999. if (cluster_is_quorate) {
  2000. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_QUORATE;
  2001. }
  2002. if (wait_for_all) {
  2003. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_WAIT_FOR_ALL;
  2004. }
  2005. if (last_man_standing) {
  2006. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_LAST_MAN_STANDING;
  2007. }
  2008. if (auto_tie_breaker != ATB_NONE) {
  2009. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_AUTO_TIE_BREAKER;
  2010. }
  2011. if (allow_downscale) {
  2012. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_ALLOW_DOWNSCALE;
  2013. }
  2014. memset(res_lib_votequorum_getinfo.qdevice_name, 0, VOTEQUORUM_QDEVICE_MAX_NAME_LEN);
  2015. strcpy(res_lib_votequorum_getinfo.qdevice_name, qdevice_name);
  2016. res_lib_votequorum_getinfo.qdevice_votes = qdevice->votes;
  2017. if (node->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  2018. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_QDEVICE_REGISTERED;
  2019. }
  2020. if (node->flags & NODE_FLAGS_QDEVICE_ALIVE) {
  2021. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_QDEVICE_ALIVE;
  2022. }
  2023. if (node->flags & NODE_FLAGS_QDEVICE_CAST_VOTE) {
  2024. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_QDEVICE_CAST_VOTE;
  2025. }
  2026. if (node->flags & NODE_FLAGS_QDEVICE_MASTER_WINS) {
  2027. res_lib_votequorum_getinfo.flags |= VOTEQUORUM_INFO_QDEVICE_MASTER_WINS;
  2028. }
  2029. } else {
  2030. error = CS_ERR_NOT_EXIST;
  2031. }
  2032. res_lib_votequorum_getinfo.header.size = sizeof(res_lib_votequorum_getinfo);
  2033. res_lib_votequorum_getinfo.header.id = MESSAGE_RES_VOTEQUORUM_GETINFO;
  2034. res_lib_votequorum_getinfo.header.error = error;
  2035. corosync_api->ipc_response_send(conn, &res_lib_votequorum_getinfo, sizeof(res_lib_votequorum_getinfo));
  2036. log_printf(LOGSYS_LEVEL_DEBUG, "getinfo response error: %d", error);
  2037. LEAVE();
  2038. }
  2039. static void message_handler_req_lib_votequorum_setexpected (void *conn, const void *message)
  2040. {
  2041. const struct req_lib_votequorum_setexpected *req_lib_votequorum_setexpected = message;
  2042. struct res_lib_votequorum_status res_lib_votequorum_status;
  2043. cs_error_t error = CS_OK;
  2044. unsigned int newquorum;
  2045. unsigned int total_votes;
  2046. uint8_t allow_downscale_status = 0;
  2047. ENTER();
  2048. allow_downscale_status = allow_downscale;
  2049. allow_downscale = 0;
  2050. /*
  2051. * Validate new expected votes
  2052. */
  2053. newquorum = calculate_quorum(1, req_lib_votequorum_setexpected->expected_votes, &total_votes);
  2054. allow_downscale = allow_downscale_status;
  2055. if (newquorum < total_votes / 2 ||
  2056. newquorum > total_votes) {
  2057. error = CS_ERR_INVALID_PARAM;
  2058. goto error_exit;
  2059. }
  2060. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_EXPECTED_VOTES, us->node_id,
  2061. req_lib_votequorum_setexpected->expected_votes);
  2062. error_exit:
  2063. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  2064. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  2065. res_lib_votequorum_status.header.error = error;
  2066. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  2067. LEAVE();
  2068. }
  2069. static void message_handler_req_lib_votequorum_setvotes (void *conn, const void *message)
  2070. {
  2071. const struct req_lib_votequorum_setvotes *req_lib_votequorum_setvotes = message;
  2072. struct res_lib_votequorum_status res_lib_votequorum_status;
  2073. struct cluster_node *node;
  2074. unsigned int newquorum;
  2075. unsigned int total_votes;
  2076. unsigned int saved_votes;
  2077. cs_error_t error = CS_OK;
  2078. unsigned int nodeid;
  2079. ENTER();
  2080. nodeid = req_lib_votequorum_setvotes->nodeid;
  2081. node = find_node_by_nodeid(nodeid);
  2082. if (!node) {
  2083. error = CS_ERR_NAME_NOT_FOUND;
  2084. goto error_exit;
  2085. }
  2086. /*
  2087. * Check votes is valid
  2088. */
  2089. saved_votes = node->votes;
  2090. node->votes = req_lib_votequorum_setvotes->votes;
  2091. newquorum = calculate_quorum(1, 0, &total_votes);
  2092. if (newquorum < total_votes / 2 ||
  2093. newquorum > total_votes) {
  2094. node->votes = saved_votes;
  2095. error = CS_ERR_INVALID_PARAM;
  2096. goto error_exit;
  2097. }
  2098. votequorum_exec_send_reconfigure(VOTEQUORUM_RECONFIG_PARAM_NODE_VOTES, nodeid,
  2099. req_lib_votequorum_setvotes->votes);
  2100. error_exit:
  2101. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  2102. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  2103. res_lib_votequorum_status.header.error = error;
  2104. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  2105. LEAVE();
  2106. }
  2107. static void message_handler_req_lib_votequorum_trackstart (void *conn,
  2108. const void *message)
  2109. {
  2110. const struct req_lib_votequorum_trackstart *req_lib_votequorum_trackstart = message;
  2111. struct res_lib_votequorum_status res_lib_votequorum_status;
  2112. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  2113. ENTER();
  2114. /*
  2115. * If an immediate listing of the current cluster membership
  2116. * is requested, generate membership list
  2117. */
  2118. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CURRENT ||
  2119. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES) {
  2120. log_printf(LOGSYS_LEVEL_DEBUG, "sending initial status to %p", conn);
  2121. votequorum_exec_send_quorum_notification(conn, req_lib_votequorum_trackstart->context);
  2122. }
  2123. /*
  2124. * Record requests for tracking
  2125. */
  2126. if (req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES ||
  2127. req_lib_votequorum_trackstart->track_flags & CS_TRACK_CHANGES_ONLY) {
  2128. quorum_pd->track_flags = req_lib_votequorum_trackstart->track_flags;
  2129. quorum_pd->tracking_enabled = 1;
  2130. quorum_pd->tracking_context = req_lib_votequorum_trackstart->context;
  2131. list_add (&quorum_pd->list, &trackers_list);
  2132. }
  2133. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  2134. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  2135. res_lib_votequorum_status.header.error = CS_OK;
  2136. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  2137. LEAVE();
  2138. }
  2139. static void message_handler_req_lib_votequorum_trackstop (void *conn,
  2140. const void *message)
  2141. {
  2142. struct res_lib_votequorum_status res_lib_votequorum_status;
  2143. struct quorum_pd *quorum_pd = (struct quorum_pd *)corosync_api->ipc_private_data_get (conn);
  2144. int error = CS_OK;
  2145. ENTER();
  2146. if (quorum_pd->tracking_enabled) {
  2147. error = CS_OK;
  2148. quorum_pd->tracking_enabled = 0;
  2149. list_del (&quorum_pd->list);
  2150. list_init (&quorum_pd->list);
  2151. } else {
  2152. error = CS_ERR_NOT_EXIST;
  2153. }
  2154. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  2155. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  2156. res_lib_votequorum_status.header.error = error;
  2157. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  2158. LEAVE();
  2159. }
  2160. static void message_handler_req_lib_votequorum_qdevice_register (void *conn,
  2161. const void *message)
  2162. {
  2163. const struct req_lib_votequorum_qdevice_register *req_lib_votequorum_qdevice_register = message;
  2164. struct res_lib_votequorum_status res_lib_votequorum_status;
  2165. cs_error_t error = CS_OK;
  2166. ENTER();
  2167. if (!qdevice_can_operate) {
  2168. log_printf(LOGSYS_LEVEL_INFO, "Registration of quorum device is disabled by incorrect corosync.conf. See logs for more information");
  2169. error = CS_ERR_ACCESS;
  2170. goto out;
  2171. }
  2172. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  2173. if ((!strncmp(req_lib_votequorum_qdevice_register->name,
  2174. qdevice_name, VOTEQUORUM_QDEVICE_MAX_NAME_LEN))) {
  2175. goto out;
  2176. } else {
  2177. log_printf(LOGSYS_LEVEL_WARNING,
  2178. "A new qdevice with different name (new: %s old: %s) is trying to re-register!",
  2179. req_lib_votequorum_qdevice_register->name, qdevice_name);
  2180. error = CS_ERR_EXIST;
  2181. goto out;
  2182. }
  2183. } else {
  2184. if (qdevice_reg_conn != NULL) {
  2185. log_printf(LOGSYS_LEVEL_WARNING,
  2186. "Registration request already in progress");
  2187. error = CS_ERR_TRY_AGAIN;
  2188. goto out;
  2189. }
  2190. qdevice_reg_conn = conn;
  2191. if (votequorum_exec_send_qdevice_reg(VOTEQUORUM_QDEVICE_OPERATION_REGISTER,
  2192. req_lib_votequorum_qdevice_register->name) != 0) {
  2193. log_printf(LOGSYS_LEVEL_WARNING,
  2194. "Unable to send qdevice registration request to cluster");
  2195. error = CS_ERR_TRY_AGAIN;
  2196. qdevice_reg_conn = NULL;
  2197. } else {
  2198. LEAVE();
  2199. return;
  2200. }
  2201. }
  2202. out:
  2203. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  2204. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  2205. res_lib_votequorum_status.header.error = error;
  2206. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  2207. LEAVE();
  2208. }
  2209. static void message_handler_req_lib_votequorum_qdevice_unregister (void *conn,
  2210. const void *message)
  2211. {
  2212. const struct req_lib_votequorum_qdevice_unregister *req_lib_votequorum_qdevice_unregister = message;
  2213. struct res_lib_votequorum_status res_lib_votequorum_status;
  2214. cs_error_t error = CS_OK;
  2215. ENTER();
  2216. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  2217. if (strncmp(req_lib_votequorum_qdevice_unregister->name, qdevice_name, VOTEQUORUM_QDEVICE_MAX_NAME_LEN)) {
  2218. error = CS_ERR_INVALID_PARAM;
  2219. goto out;
  2220. }
  2221. if (qdevice_timer_set) {
  2222. corosync_api->timer_delete(qdevice_timer);
  2223. qdevice_timer_set = 0;
  2224. sync_wait_for_poll_or_timeout = 0;
  2225. }
  2226. us->flags &= ~NODE_FLAGS_QDEVICE_REGISTERED;
  2227. us->flags &= ~NODE_FLAGS_QDEVICE_ALIVE;
  2228. us->flags &= ~NODE_FLAGS_QDEVICE_CAST_VOTE;
  2229. us->flags &= ~NODE_FLAGS_QDEVICE_MASTER_WINS;
  2230. votequorum_exec_send_nodeinfo(us->node_id);
  2231. votequorum_exec_send_qdevice_reg(VOTEQUORUM_QDEVICE_OPERATION_UNREGISTER,
  2232. req_lib_votequorum_qdevice_unregister->name);
  2233. } else {
  2234. error = CS_ERR_NOT_EXIST;
  2235. }
  2236. out:
  2237. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  2238. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  2239. res_lib_votequorum_status.header.error = error;
  2240. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  2241. LEAVE();
  2242. }
  2243. static void message_handler_req_lib_votequorum_qdevice_update (void *conn,
  2244. const void *message)
  2245. {
  2246. const struct req_lib_votequorum_qdevice_update *req_lib_votequorum_qdevice_update = message;
  2247. struct res_lib_votequorum_status res_lib_votequorum_status;
  2248. cs_error_t error = CS_OK;
  2249. ENTER();
  2250. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  2251. if (strncmp(req_lib_votequorum_qdevice_update->oldname, qdevice_name, VOTEQUORUM_QDEVICE_MAX_NAME_LEN)) {
  2252. error = CS_ERR_INVALID_PARAM;
  2253. goto out;
  2254. }
  2255. votequorum_exec_send_qdevice_reconfigure(req_lib_votequorum_qdevice_update->oldname,
  2256. req_lib_votequorum_qdevice_update->newname);
  2257. } else {
  2258. error = CS_ERR_NOT_EXIST;
  2259. }
  2260. out:
  2261. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  2262. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  2263. res_lib_votequorum_status.header.error = error;
  2264. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  2265. LEAVE();
  2266. }
  2267. static void message_handler_req_lib_votequorum_qdevice_poll (void *conn,
  2268. const void *message)
  2269. {
  2270. const struct req_lib_votequorum_qdevice_poll *req_lib_votequorum_qdevice_poll = message;
  2271. struct res_lib_votequorum_status res_lib_votequorum_status;
  2272. cs_error_t error = CS_OK;
  2273. uint32_t oldflags;
  2274. ENTER();
  2275. if (!qdevice_can_operate) {
  2276. error = CS_ERR_ACCESS;
  2277. goto out;
  2278. }
  2279. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  2280. if (!(req_lib_votequorum_qdevice_poll->ring_id.nodeid == quorum_ringid.rep.nodeid &&
  2281. req_lib_votequorum_qdevice_poll->ring_id.seq == quorum_ringid.seq)) {
  2282. log_printf(LOGSYS_LEVEL_DEBUG, "Received poll ring id (%u.%"PRIu64") != last sync "
  2283. "ring id (%u.%"PRIu64"). Ignoring poll call.",
  2284. req_lib_votequorum_qdevice_poll->ring_id.nodeid, req_lib_votequorum_qdevice_poll->ring_id.seq,
  2285. quorum_ringid.rep.nodeid, quorum_ringid.seq);
  2286. error = CS_ERR_MESSAGE_ERROR;
  2287. goto out;
  2288. }
  2289. if (strncmp(req_lib_votequorum_qdevice_poll->name, qdevice_name, VOTEQUORUM_QDEVICE_MAX_NAME_LEN)) {
  2290. error = CS_ERR_INVALID_PARAM;
  2291. goto out;
  2292. }
  2293. if (qdevice_timer_set) {
  2294. corosync_api->timer_delete(qdevice_timer);
  2295. qdevice_timer_set = 0;
  2296. }
  2297. oldflags = us->flags;
  2298. us->flags |= NODE_FLAGS_QDEVICE_ALIVE;
  2299. if (req_lib_votequorum_qdevice_poll->cast_vote) {
  2300. us->flags |= NODE_FLAGS_QDEVICE_CAST_VOTE;
  2301. } else {
  2302. us->flags &= ~NODE_FLAGS_QDEVICE_CAST_VOTE;
  2303. }
  2304. if (us->flags != oldflags) {
  2305. votequorum_exec_send_nodeinfo(us->node_id);
  2306. }
  2307. corosync_api->timer_add_duration((unsigned long long)qdevice_timeout*1000000, qdevice,
  2308. qdevice_timer_fn, &qdevice_timer);
  2309. qdevice_timer_set = 1;
  2310. sync_wait_for_poll_or_timeout = 0;
  2311. } else {
  2312. error = CS_ERR_NOT_EXIST;
  2313. }
  2314. out:
  2315. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  2316. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  2317. res_lib_votequorum_status.header.error = error;
  2318. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  2319. LEAVE();
  2320. }
  2321. static void message_handler_req_lib_votequorum_qdevice_master_wins (void *conn,
  2322. const void *message)
  2323. {
  2324. const struct req_lib_votequorum_qdevice_master_wins *req_lib_votequorum_qdevice_master_wins = message;
  2325. struct res_lib_votequorum_status res_lib_votequorum_status;
  2326. cs_error_t error = CS_OK;
  2327. uint32_t oldflags = us->flags;
  2328. ENTER();
  2329. if (!qdevice_can_operate) {
  2330. error = CS_ERR_ACCESS;
  2331. goto out;
  2332. }
  2333. if (us->flags & NODE_FLAGS_QDEVICE_REGISTERED) {
  2334. if (strncmp(req_lib_votequorum_qdevice_master_wins->name, qdevice_name, VOTEQUORUM_QDEVICE_MAX_NAME_LEN)) {
  2335. error = CS_ERR_INVALID_PARAM;
  2336. goto out;
  2337. }
  2338. if (req_lib_votequorum_qdevice_master_wins->allow) {
  2339. us->flags |= NODE_FLAGS_QDEVICE_MASTER_WINS;
  2340. } else {
  2341. us->flags &= ~NODE_FLAGS_QDEVICE_MASTER_WINS;
  2342. }
  2343. if (us->flags != oldflags) {
  2344. votequorum_exec_send_nodeinfo(us->node_id);
  2345. }
  2346. update_qdevice_master_wins(req_lib_votequorum_qdevice_master_wins->allow);
  2347. } else {
  2348. error = CS_ERR_NOT_EXIST;
  2349. }
  2350. out:
  2351. res_lib_votequorum_status.header.size = sizeof(res_lib_votequorum_status);
  2352. res_lib_votequorum_status.header.id = MESSAGE_RES_VOTEQUORUM_STATUS;
  2353. res_lib_votequorum_status.header.error = error;
  2354. corosync_api->ipc_response_send(conn, &res_lib_votequorum_status, sizeof(res_lib_votequorum_status));
  2355. LEAVE();
  2356. }