4
0

votequorum.c 86 KB

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