votequorum.c 70 KB

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