votequorum.c 86 KB

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