votequorum.c 78 KB

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