4
0

votequorum.c 75 KB

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