4
0

votequorum.c 86 KB

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