votequorum.c 75 KB

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