votequorum.c 75 KB

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