votequorum.c 79 KB

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