votequorum.c 76 KB

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