heartbeat.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * Copyright (C) 2004, 2005 Oracle. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public
  17. * License along with this program; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 021110-1307, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/module.h>
  25. #include <linux/fs.h>
  26. #include <linux/bio.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/delay.h>
  29. #include <linux/file.h>
  30. #include <linux/kthread.h>
  31. #include <linux/configfs.h>
  32. #include <linux/random.h>
  33. #include <linux/crc32.h>
  34. #include <linux/time.h>
  35. #include <linux/debugfs.h>
  36. #include <linux/slab.h>
  37. #include <linux/bitmap.h>
  38. #include <linux/ktime.h>
  39. #include "heartbeat.h"
  40. #include "tcp.h"
  41. #include "nodemanager.h"
  42. #include "quorum.h"
  43. #include "masklog.h"
  44. /*
  45. * The first heartbeat pass had one global thread that would serialize all hb
  46. * callback calls. This global serializing sem should only be removed once
  47. * we've made sure that all callees can deal with being called concurrently
  48. * from multiple hb region threads.
  49. */
  50. static DECLARE_RWSEM(o2hb_callback_sem);
  51. /*
  52. * multiple hb threads are watching multiple regions. A node is live
  53. * whenever any of the threads sees activity from the node in its region.
  54. */
  55. static DEFINE_SPINLOCK(o2hb_live_lock);
  56. static struct list_head o2hb_live_slots[O2NM_MAX_NODES];
  57. static unsigned long o2hb_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  58. static LIST_HEAD(o2hb_node_events);
  59. static DECLARE_WAIT_QUEUE_HEAD(o2hb_steady_queue);
  60. /*
  61. * In global heartbeat, we maintain a series of region bitmaps.
  62. * - o2hb_region_bitmap allows us to limit the region number to max region.
  63. * - o2hb_live_region_bitmap tracks live regions (seen steady iterations).
  64. * - o2hb_quorum_region_bitmap tracks live regions that have seen all nodes
  65. * heartbeat on it.
  66. * - o2hb_failed_region_bitmap tracks the regions that have seen io timeouts.
  67. */
  68. static unsigned long o2hb_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
  69. static unsigned long o2hb_live_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
  70. static unsigned long o2hb_quorum_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
  71. static unsigned long o2hb_failed_region_bitmap[BITS_TO_LONGS(O2NM_MAX_REGIONS)];
  72. #define O2HB_DB_TYPE_LIVENODES 0
  73. #define O2HB_DB_TYPE_LIVEREGIONS 1
  74. #define O2HB_DB_TYPE_QUORUMREGIONS 2
  75. #define O2HB_DB_TYPE_FAILEDREGIONS 3
  76. #define O2HB_DB_TYPE_REGION_LIVENODES 4
  77. #define O2HB_DB_TYPE_REGION_NUMBER 5
  78. #define O2HB_DB_TYPE_REGION_ELAPSED_TIME 6
  79. #define O2HB_DB_TYPE_REGION_PINNED 7
  80. struct o2hb_debug_buf {
  81. int db_type;
  82. int db_size;
  83. int db_len;
  84. void *db_data;
  85. };
  86. static struct o2hb_debug_buf *o2hb_db_livenodes;
  87. static struct o2hb_debug_buf *o2hb_db_liveregions;
  88. static struct o2hb_debug_buf *o2hb_db_quorumregions;
  89. static struct o2hb_debug_buf *o2hb_db_failedregions;
  90. #define O2HB_DEBUG_DIR "o2hb"
  91. #define O2HB_DEBUG_LIVENODES "livenodes"
  92. #define O2HB_DEBUG_LIVEREGIONS "live_regions"
  93. #define O2HB_DEBUG_QUORUMREGIONS "quorum_regions"
  94. #define O2HB_DEBUG_FAILEDREGIONS "failed_regions"
  95. #define O2HB_DEBUG_REGION_NUMBER "num"
  96. #define O2HB_DEBUG_REGION_ELAPSED_TIME "elapsed_time_in_ms"
  97. #define O2HB_DEBUG_REGION_PINNED "pinned"
  98. static struct dentry *o2hb_debug_dir;
  99. static struct dentry *o2hb_debug_livenodes;
  100. static struct dentry *o2hb_debug_liveregions;
  101. static struct dentry *o2hb_debug_quorumregions;
  102. static struct dentry *o2hb_debug_failedregions;
  103. static LIST_HEAD(o2hb_all_regions);
  104. static struct o2hb_callback {
  105. struct list_head list;
  106. } o2hb_callbacks[O2HB_NUM_CB];
  107. static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type);
  108. #define O2HB_DEFAULT_BLOCK_BITS 9
  109. enum o2hb_heartbeat_modes {
  110. O2HB_HEARTBEAT_LOCAL = 0,
  111. O2HB_HEARTBEAT_GLOBAL,
  112. O2HB_HEARTBEAT_NUM_MODES,
  113. };
  114. char *o2hb_heartbeat_mode_desc[O2HB_HEARTBEAT_NUM_MODES] = {
  115. "local", /* O2HB_HEARTBEAT_LOCAL */
  116. "global", /* O2HB_HEARTBEAT_GLOBAL */
  117. };
  118. unsigned int o2hb_dead_threshold = O2HB_DEFAULT_DEAD_THRESHOLD;
  119. unsigned int o2hb_heartbeat_mode = O2HB_HEARTBEAT_LOCAL;
  120. /*
  121. * o2hb_dependent_users tracks the number of registered callbacks that depend
  122. * on heartbeat. o2net and o2dlm are two entities that register this callback.
  123. * However only o2dlm depends on the heartbeat. It does not want the heartbeat
  124. * to stop while a dlm domain is still active.
  125. */
  126. unsigned int o2hb_dependent_users;
  127. /*
  128. * In global heartbeat mode, all regions are pinned if there are one or more
  129. * dependent users and the quorum region count is <= O2HB_PIN_CUT_OFF. All
  130. * regions are unpinned if the region count exceeds the cut off or the number
  131. * of dependent users falls to zero.
  132. */
  133. #define O2HB_PIN_CUT_OFF 3
  134. /*
  135. * In local heartbeat mode, we assume the dlm domain name to be the same as
  136. * region uuid. This is true for domains created for the file system but not
  137. * necessarily true for userdlm domains. This is a known limitation.
  138. *
  139. * In global heartbeat mode, we pin/unpin all o2hb regions. This solution
  140. * works for both file system and userdlm domains.
  141. */
  142. static int o2hb_region_pin(const char *region_uuid);
  143. static void o2hb_region_unpin(const char *region_uuid);
  144. /* Only sets a new threshold if there are no active regions.
  145. *
  146. * No locking or otherwise interesting code is required for reading
  147. * o2hb_dead_threshold as it can't change once regions are active and
  148. * it's not interesting to anyone until then anyway. */
  149. static void o2hb_dead_threshold_set(unsigned int threshold)
  150. {
  151. if (threshold > O2HB_MIN_DEAD_THRESHOLD) {
  152. spin_lock(&o2hb_live_lock);
  153. if (list_empty(&o2hb_all_regions))
  154. o2hb_dead_threshold = threshold;
  155. spin_unlock(&o2hb_live_lock);
  156. }
  157. }
  158. static int o2hb_global_heartbeat_mode_set(unsigned int hb_mode)
  159. {
  160. int ret = -1;
  161. if (hb_mode < O2HB_HEARTBEAT_NUM_MODES) {
  162. spin_lock(&o2hb_live_lock);
  163. if (list_empty(&o2hb_all_regions)) {
  164. o2hb_heartbeat_mode = hb_mode;
  165. ret = 0;
  166. }
  167. spin_unlock(&o2hb_live_lock);
  168. }
  169. return ret;
  170. }
  171. struct o2hb_node_event {
  172. struct list_head hn_item;
  173. enum o2hb_callback_type hn_event_type;
  174. struct o2nm_node *hn_node;
  175. int hn_node_num;
  176. };
  177. struct o2hb_disk_slot {
  178. struct o2hb_disk_heartbeat_block *ds_raw_block;
  179. u8 ds_node_num;
  180. u64 ds_last_time;
  181. u64 ds_last_generation;
  182. u16 ds_equal_samples;
  183. u16 ds_changed_samples;
  184. struct list_head ds_live_item;
  185. };
  186. /* each thread owns a region.. when we're asked to tear down the region
  187. * we ask the thread to stop, who cleans up the region */
  188. struct o2hb_region {
  189. struct config_item hr_item;
  190. struct list_head hr_all_item;
  191. unsigned hr_unclean_stop:1,
  192. hr_aborted_start:1,
  193. hr_item_pinned:1,
  194. hr_item_dropped:1,
  195. hr_node_deleted:1;
  196. /* protected by the hr_callback_sem */
  197. struct task_struct *hr_task;
  198. unsigned int hr_blocks;
  199. unsigned long long hr_start_block;
  200. unsigned int hr_block_bits;
  201. unsigned int hr_block_bytes;
  202. unsigned int hr_slots_per_page;
  203. unsigned int hr_num_pages;
  204. struct page **hr_slot_data;
  205. struct block_device *hr_bdev;
  206. struct o2hb_disk_slot *hr_slots;
  207. /* live node map of this region */
  208. unsigned long hr_live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  209. unsigned int hr_region_num;
  210. struct dentry *hr_debug_dir;
  211. struct dentry *hr_debug_livenodes;
  212. struct dentry *hr_debug_regnum;
  213. struct dentry *hr_debug_elapsed_time;
  214. struct dentry *hr_debug_pinned;
  215. struct o2hb_debug_buf *hr_db_livenodes;
  216. struct o2hb_debug_buf *hr_db_regnum;
  217. struct o2hb_debug_buf *hr_db_elapsed_time;
  218. struct o2hb_debug_buf *hr_db_pinned;
  219. /* let the person setting up hb wait for it to return until it
  220. * has reached a 'steady' state. This will be fixed when we have
  221. * a more complete api that doesn't lead to this sort of fragility. */
  222. atomic_t hr_steady_iterations;
  223. /* terminate o2hb thread if it does not reach steady state
  224. * (hr_steady_iterations == 0) within hr_unsteady_iterations */
  225. atomic_t hr_unsteady_iterations;
  226. char hr_dev_name[BDEVNAME_SIZE];
  227. unsigned int hr_timeout_ms;
  228. /* randomized as the region goes up and down so that a node
  229. * recognizes a node going up and down in one iteration */
  230. u64 hr_generation;
  231. struct delayed_work hr_write_timeout_work;
  232. unsigned long hr_last_timeout_start;
  233. /* negotiate timer, used to negotiate extending hb timeout. */
  234. struct delayed_work hr_nego_timeout_work;
  235. unsigned long hr_nego_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  236. /* Used during o2hb_check_slot to hold a copy of the block
  237. * being checked because we temporarily have to zero out the
  238. * crc field. */
  239. struct o2hb_disk_heartbeat_block *hr_tmp_block;
  240. /* Message key for negotiate timeout message. */
  241. unsigned int hr_key;
  242. struct list_head hr_handler_list;
  243. /* last hb status, 0 for success, other value for error. */
  244. int hr_last_hb_status;
  245. };
  246. struct o2hb_bio_wait_ctxt {
  247. atomic_t wc_num_reqs;
  248. struct completion wc_io_complete;
  249. int wc_error;
  250. };
  251. #define O2HB_NEGO_TIMEOUT_MS (O2HB_MAX_WRITE_TIMEOUT_MS/2)
  252. enum {
  253. O2HB_NEGO_TIMEOUT_MSG = 1,
  254. O2HB_NEGO_APPROVE_MSG = 2,
  255. };
  256. struct o2hb_nego_msg {
  257. u8 node_num;
  258. };
  259. static void o2hb_write_timeout(struct work_struct *work)
  260. {
  261. int failed, quorum;
  262. struct o2hb_region *reg =
  263. container_of(work, struct o2hb_region,
  264. hr_write_timeout_work.work);
  265. mlog(ML_ERROR, "Heartbeat write timeout to device %s after %u "
  266. "milliseconds\n", reg->hr_dev_name,
  267. jiffies_to_msecs(jiffies - reg->hr_last_timeout_start));
  268. if (o2hb_global_heartbeat_active()) {
  269. spin_lock(&o2hb_live_lock);
  270. if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
  271. set_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
  272. failed = bitmap_weight(o2hb_failed_region_bitmap,
  273. O2NM_MAX_REGIONS);
  274. quorum = bitmap_weight(o2hb_quorum_region_bitmap,
  275. O2NM_MAX_REGIONS);
  276. spin_unlock(&o2hb_live_lock);
  277. mlog(ML_HEARTBEAT, "Number of regions %d, failed regions %d\n",
  278. quorum, failed);
  279. /*
  280. * Fence if the number of failed regions >= half the number
  281. * of quorum regions
  282. */
  283. if ((failed << 1) < quorum)
  284. return;
  285. }
  286. o2quo_disk_timeout();
  287. }
  288. static void o2hb_arm_timeout(struct o2hb_region *reg)
  289. {
  290. /* Arm writeout only after thread reaches steady state */
  291. if (atomic_read(&reg->hr_steady_iterations) != 0)
  292. return;
  293. mlog(ML_HEARTBEAT, "Queue write timeout for %u ms\n",
  294. O2HB_MAX_WRITE_TIMEOUT_MS);
  295. if (o2hb_global_heartbeat_active()) {
  296. spin_lock(&o2hb_live_lock);
  297. clear_bit(reg->hr_region_num, o2hb_failed_region_bitmap);
  298. spin_unlock(&o2hb_live_lock);
  299. }
  300. cancel_delayed_work(&reg->hr_write_timeout_work);
  301. schedule_delayed_work(&reg->hr_write_timeout_work,
  302. msecs_to_jiffies(O2HB_MAX_WRITE_TIMEOUT_MS));
  303. cancel_delayed_work(&reg->hr_nego_timeout_work);
  304. /* negotiate timeout must be less than write timeout. */
  305. schedule_delayed_work(&reg->hr_nego_timeout_work,
  306. msecs_to_jiffies(O2HB_NEGO_TIMEOUT_MS));
  307. memset(reg->hr_nego_node_bitmap, 0, sizeof(reg->hr_nego_node_bitmap));
  308. }
  309. static void o2hb_disarm_timeout(struct o2hb_region *reg)
  310. {
  311. cancel_delayed_work_sync(&reg->hr_write_timeout_work);
  312. cancel_delayed_work_sync(&reg->hr_nego_timeout_work);
  313. }
  314. static int o2hb_send_nego_msg(int key, int type, u8 target)
  315. {
  316. struct o2hb_nego_msg msg;
  317. int status, ret;
  318. msg.node_num = o2nm_this_node();
  319. again:
  320. ret = o2net_send_message(type, key, &msg, sizeof(msg),
  321. target, &status);
  322. if (ret == -EAGAIN || ret == -ENOMEM) {
  323. msleep(100);
  324. goto again;
  325. }
  326. return ret;
  327. }
  328. static void o2hb_nego_timeout(struct work_struct *work)
  329. {
  330. unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  331. int master_node, i, ret;
  332. struct o2hb_region *reg;
  333. reg = container_of(work, struct o2hb_region, hr_nego_timeout_work.work);
  334. /* don't negotiate timeout if last hb failed since it is very
  335. * possible io failed. Should let write timeout fence self.
  336. */
  337. if (reg->hr_last_hb_status)
  338. return;
  339. o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
  340. /* lowest node as master node to make negotiate decision. */
  341. master_node = find_next_bit(live_node_bitmap, O2NM_MAX_NODES, 0);
  342. if (master_node == o2nm_this_node()) {
  343. if (!test_bit(master_node, reg->hr_nego_node_bitmap)) {
  344. printk(KERN_NOTICE "o2hb: node %d hb write hung for %ds on region %s (%s).\n",
  345. o2nm_this_node(), O2HB_NEGO_TIMEOUT_MS/1000,
  346. config_item_name(&reg->hr_item), reg->hr_dev_name);
  347. set_bit(master_node, reg->hr_nego_node_bitmap);
  348. }
  349. if (memcmp(reg->hr_nego_node_bitmap, live_node_bitmap,
  350. sizeof(reg->hr_nego_node_bitmap))) {
  351. /* check negotiate bitmap every second to do timeout
  352. * approve decision.
  353. */
  354. schedule_delayed_work(&reg->hr_nego_timeout_work,
  355. msecs_to_jiffies(1000));
  356. return;
  357. }
  358. printk(KERN_NOTICE "o2hb: all nodes hb write hung, maybe region %s (%s) is down.\n",
  359. config_item_name(&reg->hr_item), reg->hr_dev_name);
  360. /* approve negotiate timeout request. */
  361. o2hb_arm_timeout(reg);
  362. i = -1;
  363. while ((i = find_next_bit(live_node_bitmap,
  364. O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
  365. if (i == master_node)
  366. continue;
  367. mlog(ML_HEARTBEAT, "send NEGO_APPROVE msg to node %d\n", i);
  368. ret = o2hb_send_nego_msg(reg->hr_key,
  369. O2HB_NEGO_APPROVE_MSG, i);
  370. if (ret)
  371. mlog(ML_ERROR, "send NEGO_APPROVE msg to node %d fail %d\n",
  372. i, ret);
  373. }
  374. } else {
  375. /* negotiate timeout with master node. */
  376. printk(KERN_NOTICE "o2hb: node %d hb write hung for %ds on region %s (%s), negotiate timeout with node %d.\n",
  377. o2nm_this_node(), O2HB_NEGO_TIMEOUT_MS/1000, config_item_name(&reg->hr_item),
  378. reg->hr_dev_name, master_node);
  379. ret = o2hb_send_nego_msg(reg->hr_key, O2HB_NEGO_TIMEOUT_MSG,
  380. master_node);
  381. if (ret)
  382. mlog(ML_ERROR, "send NEGO_TIMEOUT msg to node %d fail %d\n",
  383. master_node, ret);
  384. }
  385. }
  386. static int o2hb_nego_timeout_handler(struct o2net_msg *msg, u32 len, void *data,
  387. void **ret_data)
  388. {
  389. struct o2hb_region *reg = data;
  390. struct o2hb_nego_msg *nego_msg;
  391. nego_msg = (struct o2hb_nego_msg *)msg->buf;
  392. printk(KERN_NOTICE "o2hb: receive negotiate timeout message from node %d on region %s (%s).\n",
  393. nego_msg->node_num, config_item_name(&reg->hr_item), reg->hr_dev_name);
  394. if (nego_msg->node_num < O2NM_MAX_NODES)
  395. set_bit(nego_msg->node_num, reg->hr_nego_node_bitmap);
  396. else
  397. mlog(ML_ERROR, "got nego timeout message from bad node.\n");
  398. return 0;
  399. }
  400. static int o2hb_nego_approve_handler(struct o2net_msg *msg, u32 len, void *data,
  401. void **ret_data)
  402. {
  403. struct o2hb_region *reg = data;
  404. printk(KERN_NOTICE "o2hb: negotiate timeout approved by master node on region %s (%s).\n",
  405. config_item_name(&reg->hr_item), reg->hr_dev_name);
  406. o2hb_arm_timeout(reg);
  407. return 0;
  408. }
  409. static inline void o2hb_bio_wait_init(struct o2hb_bio_wait_ctxt *wc)
  410. {
  411. atomic_set(&wc->wc_num_reqs, 1);
  412. init_completion(&wc->wc_io_complete);
  413. wc->wc_error = 0;
  414. }
  415. /* Used in error paths too */
  416. static inline void o2hb_bio_wait_dec(struct o2hb_bio_wait_ctxt *wc,
  417. unsigned int num)
  418. {
  419. /* sadly atomic_sub_and_test() isn't available on all platforms. The
  420. * good news is that the fast path only completes one at a time */
  421. while(num--) {
  422. if (atomic_dec_and_test(&wc->wc_num_reqs)) {
  423. BUG_ON(num > 0);
  424. complete(&wc->wc_io_complete);
  425. }
  426. }
  427. }
  428. static void o2hb_wait_on_io(struct o2hb_bio_wait_ctxt *wc)
  429. {
  430. o2hb_bio_wait_dec(wc, 1);
  431. wait_for_completion(&wc->wc_io_complete);
  432. }
  433. static void o2hb_bio_end_io(struct bio *bio)
  434. {
  435. struct o2hb_bio_wait_ctxt *wc = bio->bi_private;
  436. if (bio->bi_status) {
  437. mlog(ML_ERROR, "IO Error %d\n", bio->bi_status);
  438. wc->wc_error = blk_status_to_errno(bio->bi_status);
  439. }
  440. o2hb_bio_wait_dec(wc, 1);
  441. bio_put(bio);
  442. }
  443. /* Setup a Bio to cover I/O against num_slots slots starting at
  444. * start_slot. */
  445. static struct bio *o2hb_setup_one_bio(struct o2hb_region *reg,
  446. struct o2hb_bio_wait_ctxt *wc,
  447. unsigned int *current_slot,
  448. unsigned int max_slots, int op,
  449. int op_flags)
  450. {
  451. int len, current_page;
  452. unsigned int vec_len, vec_start;
  453. unsigned int bits = reg->hr_block_bits;
  454. unsigned int spp = reg->hr_slots_per_page;
  455. unsigned int cs = *current_slot;
  456. struct bio *bio;
  457. struct page *page;
  458. /* Testing has shown this allocation to take long enough under
  459. * GFP_KERNEL that the local node can get fenced. It would be
  460. * nicest if we could pre-allocate these bios and avoid this
  461. * all together. */
  462. bio = bio_alloc(GFP_ATOMIC, 16);
  463. if (!bio) {
  464. mlog(ML_ERROR, "Could not alloc slots BIO!\n");
  465. bio = ERR_PTR(-ENOMEM);
  466. goto bail;
  467. }
  468. /* Must put everything in 512 byte sectors for the bio... */
  469. bio->bi_iter.bi_sector = (reg->hr_start_block + cs) << (bits - 9);
  470. bio_set_dev(bio, reg->hr_bdev);
  471. bio->bi_private = wc;
  472. bio->bi_end_io = o2hb_bio_end_io;
  473. bio_set_op_attrs(bio, op, op_flags);
  474. vec_start = (cs << bits) % PAGE_SIZE;
  475. while(cs < max_slots) {
  476. current_page = cs / spp;
  477. page = reg->hr_slot_data[current_page];
  478. vec_len = min(PAGE_SIZE - vec_start,
  479. (max_slots-cs) * (PAGE_SIZE/spp) );
  480. mlog(ML_HB_BIO, "page %d, vec_len = %u, vec_start = %u\n",
  481. current_page, vec_len, vec_start);
  482. len = bio_add_page(bio, page, vec_len, vec_start);
  483. if (len != vec_len) {
  484. mlog(ML_ERROR, "Adding page[%d] to bio failed, "
  485. "page %p, len %d, vec_len %u, vec_start %u, "
  486. "bi_sector %llu\n", current_page, page, len,
  487. vec_len, vec_start,
  488. (unsigned long long)bio->bi_iter.bi_sector);
  489. bio_put(bio);
  490. bio = ERR_PTR(-EIO);
  491. return bio;
  492. }
  493. cs += vec_len / (PAGE_SIZE/spp);
  494. vec_start = 0;
  495. }
  496. bail:
  497. *current_slot = cs;
  498. return bio;
  499. }
  500. static int o2hb_read_slots(struct o2hb_region *reg,
  501. unsigned int max_slots)
  502. {
  503. unsigned int current_slot=0;
  504. int status;
  505. struct o2hb_bio_wait_ctxt wc;
  506. struct bio *bio;
  507. o2hb_bio_wait_init(&wc);
  508. while(current_slot < max_slots) {
  509. bio = o2hb_setup_one_bio(reg, &wc, &current_slot, max_slots,
  510. REQ_OP_READ, 0);
  511. if (IS_ERR(bio)) {
  512. status = PTR_ERR(bio);
  513. mlog_errno(status);
  514. goto bail_and_wait;
  515. }
  516. atomic_inc(&wc.wc_num_reqs);
  517. submit_bio(bio);
  518. }
  519. status = 0;
  520. bail_and_wait:
  521. o2hb_wait_on_io(&wc);
  522. if (wc.wc_error && !status)
  523. status = wc.wc_error;
  524. return status;
  525. }
  526. static int o2hb_issue_node_write(struct o2hb_region *reg,
  527. struct o2hb_bio_wait_ctxt *write_wc)
  528. {
  529. int status;
  530. unsigned int slot;
  531. struct bio *bio;
  532. o2hb_bio_wait_init(write_wc);
  533. slot = o2nm_this_node();
  534. bio = o2hb_setup_one_bio(reg, write_wc, &slot, slot+1, REQ_OP_WRITE,
  535. REQ_SYNC);
  536. if (IS_ERR(bio)) {
  537. status = PTR_ERR(bio);
  538. mlog_errno(status);
  539. goto bail;
  540. }
  541. atomic_inc(&write_wc->wc_num_reqs);
  542. submit_bio(bio);
  543. status = 0;
  544. bail:
  545. return status;
  546. }
  547. static u32 o2hb_compute_block_crc_le(struct o2hb_region *reg,
  548. struct o2hb_disk_heartbeat_block *hb_block)
  549. {
  550. __le32 old_cksum;
  551. u32 ret;
  552. /* We want to compute the block crc with a 0 value in the
  553. * hb_cksum field. Save it off here and replace after the
  554. * crc. */
  555. old_cksum = hb_block->hb_cksum;
  556. hb_block->hb_cksum = 0;
  557. ret = crc32_le(0, (unsigned char *) hb_block, reg->hr_block_bytes);
  558. hb_block->hb_cksum = old_cksum;
  559. return ret;
  560. }
  561. static void o2hb_dump_slot(struct o2hb_disk_heartbeat_block *hb_block)
  562. {
  563. mlog(ML_ERROR, "Dump slot information: seq = 0x%llx, node = %u, "
  564. "cksum = 0x%x, generation 0x%llx\n",
  565. (long long)le64_to_cpu(hb_block->hb_seq),
  566. hb_block->hb_node, le32_to_cpu(hb_block->hb_cksum),
  567. (long long)le64_to_cpu(hb_block->hb_generation));
  568. }
  569. static int o2hb_verify_crc(struct o2hb_region *reg,
  570. struct o2hb_disk_heartbeat_block *hb_block)
  571. {
  572. u32 read, computed;
  573. read = le32_to_cpu(hb_block->hb_cksum);
  574. computed = o2hb_compute_block_crc_le(reg, hb_block);
  575. return read == computed;
  576. }
  577. /*
  578. * Compare the slot data with what we wrote in the last iteration.
  579. * If the match fails, print an appropriate error message. This is to
  580. * detect errors like... another node hearting on the same slot,
  581. * flaky device that is losing writes, etc.
  582. * Returns 1 if check succeeds, 0 otherwise.
  583. */
  584. static int o2hb_check_own_slot(struct o2hb_region *reg)
  585. {
  586. struct o2hb_disk_slot *slot;
  587. struct o2hb_disk_heartbeat_block *hb_block;
  588. char *errstr;
  589. slot = &reg->hr_slots[o2nm_this_node()];
  590. /* Don't check on our 1st timestamp */
  591. if (!slot->ds_last_time)
  592. return 0;
  593. hb_block = slot->ds_raw_block;
  594. if (le64_to_cpu(hb_block->hb_seq) == slot->ds_last_time &&
  595. le64_to_cpu(hb_block->hb_generation) == slot->ds_last_generation &&
  596. hb_block->hb_node == slot->ds_node_num)
  597. return 1;
  598. #define ERRSTR1 "Another node is heartbeating on device"
  599. #define ERRSTR2 "Heartbeat generation mismatch on device"
  600. #define ERRSTR3 "Heartbeat sequence mismatch on device"
  601. if (hb_block->hb_node != slot->ds_node_num)
  602. errstr = ERRSTR1;
  603. else if (le64_to_cpu(hb_block->hb_generation) !=
  604. slot->ds_last_generation)
  605. errstr = ERRSTR2;
  606. else
  607. errstr = ERRSTR3;
  608. mlog(ML_ERROR, "%s (%s): expected(%u:0x%llx, 0x%llx), "
  609. "ondisk(%u:0x%llx, 0x%llx)\n", errstr, reg->hr_dev_name,
  610. slot->ds_node_num, (unsigned long long)slot->ds_last_generation,
  611. (unsigned long long)slot->ds_last_time, hb_block->hb_node,
  612. (unsigned long long)le64_to_cpu(hb_block->hb_generation),
  613. (unsigned long long)le64_to_cpu(hb_block->hb_seq));
  614. return 0;
  615. }
  616. static inline void o2hb_prepare_block(struct o2hb_region *reg,
  617. u64 generation)
  618. {
  619. int node_num;
  620. u64 cputime;
  621. struct o2hb_disk_slot *slot;
  622. struct o2hb_disk_heartbeat_block *hb_block;
  623. node_num = o2nm_this_node();
  624. slot = &reg->hr_slots[node_num];
  625. hb_block = (struct o2hb_disk_heartbeat_block *)slot->ds_raw_block;
  626. memset(hb_block, 0, reg->hr_block_bytes);
  627. /* TODO: time stuff */
  628. cputime = ktime_get_real_seconds();
  629. if (!cputime)
  630. cputime = 1;
  631. hb_block->hb_seq = cpu_to_le64(cputime);
  632. hb_block->hb_node = node_num;
  633. hb_block->hb_generation = cpu_to_le64(generation);
  634. hb_block->hb_dead_ms = cpu_to_le32(o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS);
  635. /* This step must always happen last! */
  636. hb_block->hb_cksum = cpu_to_le32(o2hb_compute_block_crc_le(reg,
  637. hb_block));
  638. mlog(ML_HB_BIO, "our node generation = 0x%llx, cksum = 0x%x\n",
  639. (long long)generation,
  640. le32_to_cpu(hb_block->hb_cksum));
  641. }
  642. static void o2hb_fire_callbacks(struct o2hb_callback *hbcall,
  643. struct o2nm_node *node,
  644. int idx)
  645. {
  646. struct o2hb_callback_func *f;
  647. list_for_each_entry(f, &hbcall->list, hc_item) {
  648. mlog(ML_HEARTBEAT, "calling funcs %p\n", f);
  649. (f->hc_func)(node, idx, f->hc_data);
  650. }
  651. }
  652. /* Will run the list in order until we process the passed event */
  653. static void o2hb_run_event_list(struct o2hb_node_event *queued_event)
  654. {
  655. struct o2hb_callback *hbcall;
  656. struct o2hb_node_event *event;
  657. /* Holding callback sem assures we don't alter the callback
  658. * lists when doing this, and serializes ourselves with other
  659. * processes wanting callbacks. */
  660. down_write(&o2hb_callback_sem);
  661. spin_lock(&o2hb_live_lock);
  662. while (!list_empty(&o2hb_node_events)
  663. && !list_empty(&queued_event->hn_item)) {
  664. event = list_entry(o2hb_node_events.next,
  665. struct o2hb_node_event,
  666. hn_item);
  667. list_del_init(&event->hn_item);
  668. spin_unlock(&o2hb_live_lock);
  669. mlog(ML_HEARTBEAT, "Node %s event for %d\n",
  670. event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
  671. event->hn_node_num);
  672. hbcall = hbcall_from_type(event->hn_event_type);
  673. /* We should *never* have gotten on to the list with a
  674. * bad type... This isn't something that we should try
  675. * to recover from. */
  676. BUG_ON(IS_ERR(hbcall));
  677. o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
  678. spin_lock(&o2hb_live_lock);
  679. }
  680. spin_unlock(&o2hb_live_lock);
  681. up_write(&o2hb_callback_sem);
  682. }
  683. static void o2hb_queue_node_event(struct o2hb_node_event *event,
  684. enum o2hb_callback_type type,
  685. struct o2nm_node *node,
  686. int node_num)
  687. {
  688. assert_spin_locked(&o2hb_live_lock);
  689. BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
  690. event->hn_event_type = type;
  691. event->hn_node = node;
  692. event->hn_node_num = node_num;
  693. mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
  694. type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
  695. list_add_tail(&event->hn_item, &o2hb_node_events);
  696. }
  697. static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
  698. {
  699. struct o2hb_node_event event =
  700. { .hn_item = LIST_HEAD_INIT(event.hn_item), };
  701. struct o2nm_node *node;
  702. int queued = 0;
  703. node = o2nm_get_node_by_num(slot->ds_node_num);
  704. if (!node)
  705. return;
  706. spin_lock(&o2hb_live_lock);
  707. if (!list_empty(&slot->ds_live_item)) {
  708. mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
  709. slot->ds_node_num);
  710. list_del_init(&slot->ds_live_item);
  711. if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
  712. clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
  713. o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
  714. slot->ds_node_num);
  715. queued = 1;
  716. }
  717. }
  718. spin_unlock(&o2hb_live_lock);
  719. if (queued)
  720. o2hb_run_event_list(&event);
  721. o2nm_node_put(node);
  722. }
  723. static void o2hb_set_quorum_device(struct o2hb_region *reg)
  724. {
  725. if (!o2hb_global_heartbeat_active())
  726. return;
  727. /* Prevent race with o2hb_heartbeat_group_drop_item() */
  728. if (kthread_should_stop())
  729. return;
  730. /* Tag region as quorum only after thread reaches steady state */
  731. if (atomic_read(&reg->hr_steady_iterations) != 0)
  732. return;
  733. spin_lock(&o2hb_live_lock);
  734. if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
  735. goto unlock;
  736. /*
  737. * A region can be added to the quorum only when it sees all
  738. * live nodes heartbeat on it. In other words, the region has been
  739. * added to all nodes.
  740. */
  741. if (memcmp(reg->hr_live_node_bitmap, o2hb_live_node_bitmap,
  742. sizeof(o2hb_live_node_bitmap)))
  743. goto unlock;
  744. printk(KERN_NOTICE "o2hb: Region %s (%s) is now a quorum device\n",
  745. config_item_name(&reg->hr_item), reg->hr_dev_name);
  746. set_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
  747. /*
  748. * If global heartbeat active, unpin all regions if the
  749. * region count > CUT_OFF
  750. */
  751. if (bitmap_weight(o2hb_quorum_region_bitmap,
  752. O2NM_MAX_REGIONS) > O2HB_PIN_CUT_OFF)
  753. o2hb_region_unpin(NULL);
  754. unlock:
  755. spin_unlock(&o2hb_live_lock);
  756. }
  757. static int o2hb_check_slot(struct o2hb_region *reg,
  758. struct o2hb_disk_slot *slot)
  759. {
  760. int changed = 0, gen_changed = 0;
  761. struct o2hb_node_event event =
  762. { .hn_item = LIST_HEAD_INIT(event.hn_item), };
  763. struct o2nm_node *node;
  764. struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
  765. u64 cputime;
  766. unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
  767. unsigned int slot_dead_ms;
  768. int tmp;
  769. int queued = 0;
  770. memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
  771. /*
  772. * If a node is no longer configured but is still in the livemap, we
  773. * may need to clear that bit from the livemap.
  774. */
  775. node = o2nm_get_node_by_num(slot->ds_node_num);
  776. if (!node) {
  777. spin_lock(&o2hb_live_lock);
  778. tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
  779. spin_unlock(&o2hb_live_lock);
  780. if (!tmp)
  781. return 0;
  782. }
  783. if (!o2hb_verify_crc(reg, hb_block)) {
  784. /* all paths from here will drop o2hb_live_lock for
  785. * us. */
  786. spin_lock(&o2hb_live_lock);
  787. /* Don't print an error on the console in this case -
  788. * a freshly formatted heartbeat area will not have a
  789. * crc set on it. */
  790. if (list_empty(&slot->ds_live_item))
  791. goto out;
  792. /* The node is live but pushed out a bad crc. We
  793. * consider it a transient miss but don't populate any
  794. * other values as they may be junk. */
  795. mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
  796. slot->ds_node_num, reg->hr_dev_name);
  797. o2hb_dump_slot(hb_block);
  798. slot->ds_equal_samples++;
  799. goto fire_callbacks;
  800. }
  801. /* we don't care if these wrap.. the state transitions below
  802. * clear at the right places */
  803. cputime = le64_to_cpu(hb_block->hb_seq);
  804. if (slot->ds_last_time != cputime)
  805. slot->ds_changed_samples++;
  806. else
  807. slot->ds_equal_samples++;
  808. slot->ds_last_time = cputime;
  809. /* The node changed heartbeat generations. We assume this to
  810. * mean it dropped off but came back before we timed out. We
  811. * want to consider it down for the time being but don't want
  812. * to lose any changed_samples state we might build up to
  813. * considering it live again. */
  814. if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
  815. gen_changed = 1;
  816. slot->ds_equal_samples = 0;
  817. mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
  818. "to 0x%llx)\n", slot->ds_node_num,
  819. (long long)slot->ds_last_generation,
  820. (long long)le64_to_cpu(hb_block->hb_generation));
  821. }
  822. slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
  823. mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
  824. "seq %llu last %llu changed %u equal %u\n",
  825. slot->ds_node_num, (long long)slot->ds_last_generation,
  826. le32_to_cpu(hb_block->hb_cksum),
  827. (unsigned long long)le64_to_cpu(hb_block->hb_seq),
  828. (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
  829. slot->ds_equal_samples);
  830. spin_lock(&o2hb_live_lock);
  831. fire_callbacks:
  832. /* dead nodes only come to life after some number of
  833. * changes at any time during their dead time */
  834. if (list_empty(&slot->ds_live_item) &&
  835. slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
  836. mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
  837. slot->ds_node_num, (long long)slot->ds_last_generation);
  838. set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
  839. /* first on the list generates a callback */
  840. if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
  841. mlog(ML_HEARTBEAT, "o2hb: Add node %d to live nodes "
  842. "bitmap\n", slot->ds_node_num);
  843. set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
  844. o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
  845. slot->ds_node_num);
  846. changed = 1;
  847. queued = 1;
  848. }
  849. list_add_tail(&slot->ds_live_item,
  850. &o2hb_live_slots[slot->ds_node_num]);
  851. slot->ds_equal_samples = 0;
  852. /* We want to be sure that all nodes agree on the
  853. * number of milliseconds before a node will be
  854. * considered dead. The self-fencing timeout is
  855. * computed from this value, and a discrepancy might
  856. * result in heartbeat calling a node dead when it
  857. * hasn't self-fenced yet. */
  858. slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
  859. if (slot_dead_ms && slot_dead_ms != dead_ms) {
  860. /* TODO: Perhaps we can fail the region here. */
  861. mlog(ML_ERROR, "Node %d on device %s has a dead count "
  862. "of %u ms, but our count is %u ms.\n"
  863. "Please double check your configuration values "
  864. "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
  865. slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
  866. dead_ms);
  867. }
  868. goto out;
  869. }
  870. /* if the list is dead, we're done.. */
  871. if (list_empty(&slot->ds_live_item))
  872. goto out;
  873. /* live nodes only go dead after enough consequtive missed
  874. * samples.. reset the missed counter whenever we see
  875. * activity */
  876. if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
  877. mlog(ML_HEARTBEAT, "Node %d left my region\n",
  878. slot->ds_node_num);
  879. clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
  880. /* last off the live_slot generates a callback */
  881. list_del_init(&slot->ds_live_item);
  882. if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
  883. mlog(ML_HEARTBEAT, "o2hb: Remove node %d from live "
  884. "nodes bitmap\n", slot->ds_node_num);
  885. clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
  886. /* node can be null */
  887. o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
  888. node, slot->ds_node_num);
  889. changed = 1;
  890. queued = 1;
  891. }
  892. /* We don't clear this because the node is still
  893. * actually writing new blocks. */
  894. if (!gen_changed)
  895. slot->ds_changed_samples = 0;
  896. goto out;
  897. }
  898. if (slot->ds_changed_samples) {
  899. slot->ds_changed_samples = 0;
  900. slot->ds_equal_samples = 0;
  901. }
  902. out:
  903. spin_unlock(&o2hb_live_lock);
  904. if (queued)
  905. o2hb_run_event_list(&event);
  906. if (node)
  907. o2nm_node_put(node);
  908. return changed;
  909. }
  910. static int o2hb_highest_node(unsigned long *nodes, int numbits)
  911. {
  912. return find_last_bit(nodes, numbits);
  913. }
  914. static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
  915. {
  916. int i, ret, highest_node;
  917. int membership_change = 0, own_slot_ok = 0;
  918. unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
  919. unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  920. struct o2hb_bio_wait_ctxt write_wc;
  921. ret = o2nm_configured_node_map(configured_nodes,
  922. sizeof(configured_nodes));
  923. if (ret) {
  924. mlog_errno(ret);
  925. goto bail;
  926. }
  927. /*
  928. * If a node is not configured but is in the livemap, we still need
  929. * to read the slot so as to be able to remove it from the livemap.
  930. */
  931. o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
  932. i = -1;
  933. while ((i = find_next_bit(live_node_bitmap,
  934. O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
  935. set_bit(i, configured_nodes);
  936. }
  937. highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
  938. if (highest_node >= O2NM_MAX_NODES) {
  939. mlog(ML_NOTICE, "o2hb: No configured nodes found!\n");
  940. ret = -EINVAL;
  941. goto bail;
  942. }
  943. /* No sense in reading the slots of nodes that don't exist
  944. * yet. Of course, if the node definitions have holes in them
  945. * then we're reading an empty slot anyway... Consider this
  946. * best-effort. */
  947. ret = o2hb_read_slots(reg, highest_node + 1);
  948. if (ret < 0) {
  949. mlog_errno(ret);
  950. goto bail;
  951. }
  952. /* With an up to date view of the slots, we can check that no
  953. * other node has been improperly configured to heartbeat in
  954. * our slot. */
  955. own_slot_ok = o2hb_check_own_slot(reg);
  956. /* fill in the proper info for our next heartbeat */
  957. o2hb_prepare_block(reg, reg->hr_generation);
  958. ret = o2hb_issue_node_write(reg, &write_wc);
  959. if (ret < 0) {
  960. mlog_errno(ret);
  961. goto bail;
  962. }
  963. i = -1;
  964. while((i = find_next_bit(configured_nodes,
  965. O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
  966. membership_change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
  967. }
  968. /*
  969. * We have to be sure we've advertised ourselves on disk
  970. * before we can go to steady state. This ensures that
  971. * people we find in our steady state have seen us.
  972. */
  973. o2hb_wait_on_io(&write_wc);
  974. if (write_wc.wc_error) {
  975. /* Do not re-arm the write timeout on I/O error - we
  976. * can't be sure that the new block ever made it to
  977. * disk */
  978. mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
  979. write_wc.wc_error, reg->hr_dev_name);
  980. ret = write_wc.wc_error;
  981. goto bail;
  982. }
  983. /* Skip disarming the timeout if own slot has stale/bad data */
  984. if (own_slot_ok) {
  985. o2hb_set_quorum_device(reg);
  986. o2hb_arm_timeout(reg);
  987. reg->hr_last_timeout_start = jiffies;
  988. }
  989. bail:
  990. /* let the person who launched us know when things are steady */
  991. if (atomic_read(&reg->hr_steady_iterations) != 0) {
  992. if (!ret && own_slot_ok && !membership_change) {
  993. if (atomic_dec_and_test(&reg->hr_steady_iterations))
  994. wake_up(&o2hb_steady_queue);
  995. }
  996. }
  997. if (atomic_read(&reg->hr_steady_iterations) != 0) {
  998. if (atomic_dec_and_test(&reg->hr_unsteady_iterations)) {
  999. printk(KERN_NOTICE "o2hb: Unable to stabilize "
  1000. "heartbeart on region %s (%s)\n",
  1001. config_item_name(&reg->hr_item),
  1002. reg->hr_dev_name);
  1003. atomic_set(&reg->hr_steady_iterations, 0);
  1004. reg->hr_aborted_start = 1;
  1005. wake_up(&o2hb_steady_queue);
  1006. ret = -EIO;
  1007. }
  1008. }
  1009. return ret;
  1010. }
  1011. /*
  1012. * we ride the region ref that the region dir holds. before the region
  1013. * dir is removed and drops it ref it will wait to tear down this
  1014. * thread.
  1015. */
  1016. static int o2hb_thread(void *data)
  1017. {
  1018. int i, ret;
  1019. struct o2hb_region *reg = data;
  1020. struct o2hb_bio_wait_ctxt write_wc;
  1021. ktime_t before_hb, after_hb;
  1022. unsigned int elapsed_msec;
  1023. mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
  1024. set_user_nice(current, MIN_NICE);
  1025. /* Pin node */
  1026. ret = o2nm_depend_this_node();
  1027. if (ret) {
  1028. mlog(ML_ERROR, "Node has been deleted, ret = %d\n", ret);
  1029. reg->hr_node_deleted = 1;
  1030. wake_up(&o2hb_steady_queue);
  1031. return 0;
  1032. }
  1033. while (!kthread_should_stop() &&
  1034. !reg->hr_unclean_stop && !reg->hr_aborted_start) {
  1035. /* We track the time spent inside
  1036. * o2hb_do_disk_heartbeat so that we avoid more than
  1037. * hr_timeout_ms between disk writes. On busy systems
  1038. * this should result in a heartbeat which is less
  1039. * likely to time itself out. */
  1040. before_hb = ktime_get_real();
  1041. ret = o2hb_do_disk_heartbeat(reg);
  1042. reg->hr_last_hb_status = ret;
  1043. after_hb = ktime_get_real();
  1044. elapsed_msec = (unsigned int)
  1045. ktime_ms_delta(after_hb, before_hb);
  1046. mlog(ML_HEARTBEAT,
  1047. "start = %lld, end = %lld, msec = %u, ret = %d\n",
  1048. before_hb, after_hb, elapsed_msec, ret);
  1049. if (!kthread_should_stop() &&
  1050. elapsed_msec < reg->hr_timeout_ms) {
  1051. /* the kthread api has blocked signals for us so no
  1052. * need to record the return value. */
  1053. msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
  1054. }
  1055. }
  1056. o2hb_disarm_timeout(reg);
  1057. /* unclean stop is only used in very bad situation */
  1058. for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
  1059. o2hb_shutdown_slot(&reg->hr_slots[i]);
  1060. /* Explicit down notification - avoid forcing the other nodes
  1061. * to timeout on this region when we could just as easily
  1062. * write a clear generation - thus indicating to them that
  1063. * this node has left this region.
  1064. */
  1065. if (!reg->hr_unclean_stop && !reg->hr_aborted_start) {
  1066. o2hb_prepare_block(reg, 0);
  1067. ret = o2hb_issue_node_write(reg, &write_wc);
  1068. if (ret == 0)
  1069. o2hb_wait_on_io(&write_wc);
  1070. else
  1071. mlog_errno(ret);
  1072. }
  1073. /* Unpin node */
  1074. o2nm_undepend_this_node();
  1075. mlog(ML_HEARTBEAT|ML_KTHREAD, "o2hb thread exiting\n");
  1076. return 0;
  1077. }
  1078. #ifdef CONFIG_DEBUG_FS
  1079. static int o2hb_debug_open(struct inode *inode, struct file *file)
  1080. {
  1081. struct o2hb_debug_buf *db = inode->i_private;
  1082. struct o2hb_region *reg;
  1083. unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1084. unsigned long lts;
  1085. char *buf = NULL;
  1086. int i = -1;
  1087. int out = 0;
  1088. /* max_nodes should be the largest bitmap we pass here */
  1089. BUG_ON(sizeof(map) < db->db_size);
  1090. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1091. if (!buf)
  1092. goto bail;
  1093. switch (db->db_type) {
  1094. case O2HB_DB_TYPE_LIVENODES:
  1095. case O2HB_DB_TYPE_LIVEREGIONS:
  1096. case O2HB_DB_TYPE_QUORUMREGIONS:
  1097. case O2HB_DB_TYPE_FAILEDREGIONS:
  1098. spin_lock(&o2hb_live_lock);
  1099. memcpy(map, db->db_data, db->db_size);
  1100. spin_unlock(&o2hb_live_lock);
  1101. break;
  1102. case O2HB_DB_TYPE_REGION_LIVENODES:
  1103. spin_lock(&o2hb_live_lock);
  1104. reg = (struct o2hb_region *)db->db_data;
  1105. memcpy(map, reg->hr_live_node_bitmap, db->db_size);
  1106. spin_unlock(&o2hb_live_lock);
  1107. break;
  1108. case O2HB_DB_TYPE_REGION_NUMBER:
  1109. reg = (struct o2hb_region *)db->db_data;
  1110. out += snprintf(buf + out, PAGE_SIZE - out, "%d\n",
  1111. reg->hr_region_num);
  1112. goto done;
  1113. case O2HB_DB_TYPE_REGION_ELAPSED_TIME:
  1114. reg = (struct o2hb_region *)db->db_data;
  1115. lts = reg->hr_last_timeout_start;
  1116. /* If 0, it has never been set before */
  1117. if (lts)
  1118. lts = jiffies_to_msecs(jiffies - lts);
  1119. out += snprintf(buf + out, PAGE_SIZE - out, "%lu\n", lts);
  1120. goto done;
  1121. case O2HB_DB_TYPE_REGION_PINNED:
  1122. reg = (struct o2hb_region *)db->db_data;
  1123. out += snprintf(buf + out, PAGE_SIZE - out, "%u\n",
  1124. !!reg->hr_item_pinned);
  1125. goto done;
  1126. default:
  1127. goto done;
  1128. }
  1129. while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
  1130. out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
  1131. out += snprintf(buf + out, PAGE_SIZE - out, "\n");
  1132. done:
  1133. i_size_write(inode, out);
  1134. file->private_data = buf;
  1135. return 0;
  1136. bail:
  1137. return -ENOMEM;
  1138. }
  1139. static int o2hb_debug_release(struct inode *inode, struct file *file)
  1140. {
  1141. kfree(file->private_data);
  1142. return 0;
  1143. }
  1144. static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
  1145. size_t nbytes, loff_t *ppos)
  1146. {
  1147. return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
  1148. i_size_read(file->f_mapping->host));
  1149. }
  1150. #else
  1151. static int o2hb_debug_open(struct inode *inode, struct file *file)
  1152. {
  1153. return 0;
  1154. }
  1155. static int o2hb_debug_release(struct inode *inode, struct file *file)
  1156. {
  1157. return 0;
  1158. }
  1159. static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
  1160. size_t nbytes, loff_t *ppos)
  1161. {
  1162. return 0;
  1163. }
  1164. #endif /* CONFIG_DEBUG_FS */
  1165. static const struct file_operations o2hb_debug_fops = {
  1166. .open = o2hb_debug_open,
  1167. .release = o2hb_debug_release,
  1168. .read = o2hb_debug_read,
  1169. .llseek = generic_file_llseek,
  1170. };
  1171. void o2hb_exit(void)
  1172. {
  1173. debugfs_remove(o2hb_debug_failedregions);
  1174. debugfs_remove(o2hb_debug_quorumregions);
  1175. debugfs_remove(o2hb_debug_liveregions);
  1176. debugfs_remove(o2hb_debug_livenodes);
  1177. debugfs_remove(o2hb_debug_dir);
  1178. kfree(o2hb_db_livenodes);
  1179. kfree(o2hb_db_liveregions);
  1180. kfree(o2hb_db_quorumregions);
  1181. kfree(o2hb_db_failedregions);
  1182. }
  1183. static struct dentry *o2hb_debug_create(const char *name, struct dentry *dir,
  1184. struct o2hb_debug_buf **db, int db_len,
  1185. int type, int size, int len, void *data)
  1186. {
  1187. *db = kmalloc(db_len, GFP_KERNEL);
  1188. if (!*db)
  1189. return NULL;
  1190. (*db)->db_type = type;
  1191. (*db)->db_size = size;
  1192. (*db)->db_len = len;
  1193. (*db)->db_data = data;
  1194. return debugfs_create_file(name, S_IFREG|S_IRUSR, dir, *db,
  1195. &o2hb_debug_fops);
  1196. }
  1197. static int o2hb_debug_init(void)
  1198. {
  1199. int ret = -ENOMEM;
  1200. o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
  1201. if (!o2hb_debug_dir) {
  1202. mlog_errno(ret);
  1203. goto bail;
  1204. }
  1205. o2hb_debug_livenodes = o2hb_debug_create(O2HB_DEBUG_LIVENODES,
  1206. o2hb_debug_dir,
  1207. &o2hb_db_livenodes,
  1208. sizeof(*o2hb_db_livenodes),
  1209. O2HB_DB_TYPE_LIVENODES,
  1210. sizeof(o2hb_live_node_bitmap),
  1211. O2NM_MAX_NODES,
  1212. o2hb_live_node_bitmap);
  1213. if (!o2hb_debug_livenodes) {
  1214. mlog_errno(ret);
  1215. goto bail;
  1216. }
  1217. o2hb_debug_liveregions = o2hb_debug_create(O2HB_DEBUG_LIVEREGIONS,
  1218. o2hb_debug_dir,
  1219. &o2hb_db_liveregions,
  1220. sizeof(*o2hb_db_liveregions),
  1221. O2HB_DB_TYPE_LIVEREGIONS,
  1222. sizeof(o2hb_live_region_bitmap),
  1223. O2NM_MAX_REGIONS,
  1224. o2hb_live_region_bitmap);
  1225. if (!o2hb_debug_liveregions) {
  1226. mlog_errno(ret);
  1227. goto bail;
  1228. }
  1229. o2hb_debug_quorumregions =
  1230. o2hb_debug_create(O2HB_DEBUG_QUORUMREGIONS,
  1231. o2hb_debug_dir,
  1232. &o2hb_db_quorumregions,
  1233. sizeof(*o2hb_db_quorumregions),
  1234. O2HB_DB_TYPE_QUORUMREGIONS,
  1235. sizeof(o2hb_quorum_region_bitmap),
  1236. O2NM_MAX_REGIONS,
  1237. o2hb_quorum_region_bitmap);
  1238. if (!o2hb_debug_quorumregions) {
  1239. mlog_errno(ret);
  1240. goto bail;
  1241. }
  1242. o2hb_debug_failedregions =
  1243. o2hb_debug_create(O2HB_DEBUG_FAILEDREGIONS,
  1244. o2hb_debug_dir,
  1245. &o2hb_db_failedregions,
  1246. sizeof(*o2hb_db_failedregions),
  1247. O2HB_DB_TYPE_FAILEDREGIONS,
  1248. sizeof(o2hb_failed_region_bitmap),
  1249. O2NM_MAX_REGIONS,
  1250. o2hb_failed_region_bitmap);
  1251. if (!o2hb_debug_failedregions) {
  1252. mlog_errno(ret);
  1253. goto bail;
  1254. }
  1255. ret = 0;
  1256. bail:
  1257. if (ret)
  1258. o2hb_exit();
  1259. return ret;
  1260. }
  1261. int o2hb_init(void)
  1262. {
  1263. int i;
  1264. for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
  1265. INIT_LIST_HEAD(&o2hb_callbacks[i].list);
  1266. for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
  1267. INIT_LIST_HEAD(&o2hb_live_slots[i]);
  1268. INIT_LIST_HEAD(&o2hb_node_events);
  1269. memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
  1270. memset(o2hb_region_bitmap, 0, sizeof(o2hb_region_bitmap));
  1271. memset(o2hb_live_region_bitmap, 0, sizeof(o2hb_live_region_bitmap));
  1272. memset(o2hb_quorum_region_bitmap, 0, sizeof(o2hb_quorum_region_bitmap));
  1273. memset(o2hb_failed_region_bitmap, 0, sizeof(o2hb_failed_region_bitmap));
  1274. o2hb_dependent_users = 0;
  1275. return o2hb_debug_init();
  1276. }
  1277. /* if we're already in a callback then we're already serialized by the sem */
  1278. static void o2hb_fill_node_map_from_callback(unsigned long *map,
  1279. unsigned bytes)
  1280. {
  1281. BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
  1282. memcpy(map, &o2hb_live_node_bitmap, bytes);
  1283. }
  1284. /*
  1285. * get a map of all nodes that are heartbeating in any regions
  1286. */
  1287. void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
  1288. {
  1289. /* callers want to serialize this map and callbacks so that they
  1290. * can trust that they don't miss nodes coming to the party */
  1291. down_read(&o2hb_callback_sem);
  1292. spin_lock(&o2hb_live_lock);
  1293. o2hb_fill_node_map_from_callback(map, bytes);
  1294. spin_unlock(&o2hb_live_lock);
  1295. up_read(&o2hb_callback_sem);
  1296. }
  1297. EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
  1298. /*
  1299. * heartbeat configfs bits. The heartbeat set is a default set under
  1300. * the cluster set in nodemanager.c.
  1301. */
  1302. static struct o2hb_region *to_o2hb_region(struct config_item *item)
  1303. {
  1304. return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
  1305. }
  1306. /* drop_item only drops its ref after killing the thread, nothing should
  1307. * be using the region anymore. this has to clean up any state that
  1308. * attributes might have built up. */
  1309. static void o2hb_region_release(struct config_item *item)
  1310. {
  1311. int i;
  1312. struct page *page;
  1313. struct o2hb_region *reg = to_o2hb_region(item);
  1314. mlog(ML_HEARTBEAT, "hb region release (%s)\n", reg->hr_dev_name);
  1315. kfree(reg->hr_tmp_block);
  1316. if (reg->hr_slot_data) {
  1317. for (i = 0; i < reg->hr_num_pages; i++) {
  1318. page = reg->hr_slot_data[i];
  1319. if (page)
  1320. __free_page(page);
  1321. }
  1322. kfree(reg->hr_slot_data);
  1323. }
  1324. if (reg->hr_bdev)
  1325. blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
  1326. kfree(reg->hr_slots);
  1327. debugfs_remove(reg->hr_debug_livenodes);
  1328. debugfs_remove(reg->hr_debug_regnum);
  1329. debugfs_remove(reg->hr_debug_elapsed_time);
  1330. debugfs_remove(reg->hr_debug_pinned);
  1331. debugfs_remove(reg->hr_debug_dir);
  1332. kfree(reg->hr_db_livenodes);
  1333. kfree(reg->hr_db_regnum);
  1334. kfree(reg->hr_db_elapsed_time);
  1335. kfree(reg->hr_db_pinned);
  1336. spin_lock(&o2hb_live_lock);
  1337. list_del(&reg->hr_all_item);
  1338. spin_unlock(&o2hb_live_lock);
  1339. o2net_unregister_handler_list(&reg->hr_handler_list);
  1340. kfree(reg);
  1341. }
  1342. static int o2hb_read_block_input(struct o2hb_region *reg,
  1343. const char *page,
  1344. unsigned long *ret_bytes,
  1345. unsigned int *ret_bits)
  1346. {
  1347. unsigned long bytes;
  1348. char *p = (char *)page;
  1349. bytes = simple_strtoul(p, &p, 0);
  1350. if (!p || (*p && (*p != '\n')))
  1351. return -EINVAL;
  1352. /* Heartbeat and fs min / max block sizes are the same. */
  1353. if (bytes > 4096 || bytes < 512)
  1354. return -ERANGE;
  1355. if (hweight16(bytes) != 1)
  1356. return -EINVAL;
  1357. if (ret_bytes)
  1358. *ret_bytes = bytes;
  1359. if (ret_bits)
  1360. *ret_bits = ffs(bytes) - 1;
  1361. return 0;
  1362. }
  1363. static ssize_t o2hb_region_block_bytes_show(struct config_item *item,
  1364. char *page)
  1365. {
  1366. return sprintf(page, "%u\n", to_o2hb_region(item)->hr_block_bytes);
  1367. }
  1368. static ssize_t o2hb_region_block_bytes_store(struct config_item *item,
  1369. const char *page,
  1370. size_t count)
  1371. {
  1372. struct o2hb_region *reg = to_o2hb_region(item);
  1373. int status;
  1374. unsigned long block_bytes;
  1375. unsigned int block_bits;
  1376. if (reg->hr_bdev)
  1377. return -EINVAL;
  1378. status = o2hb_read_block_input(reg, page, &block_bytes,
  1379. &block_bits);
  1380. if (status)
  1381. return status;
  1382. reg->hr_block_bytes = (unsigned int)block_bytes;
  1383. reg->hr_block_bits = block_bits;
  1384. return count;
  1385. }
  1386. static ssize_t o2hb_region_start_block_show(struct config_item *item,
  1387. char *page)
  1388. {
  1389. return sprintf(page, "%llu\n", to_o2hb_region(item)->hr_start_block);
  1390. }
  1391. static ssize_t o2hb_region_start_block_store(struct config_item *item,
  1392. const char *page,
  1393. size_t count)
  1394. {
  1395. struct o2hb_region *reg = to_o2hb_region(item);
  1396. unsigned long long tmp;
  1397. char *p = (char *)page;
  1398. if (reg->hr_bdev)
  1399. return -EINVAL;
  1400. tmp = simple_strtoull(p, &p, 0);
  1401. if (!p || (*p && (*p != '\n')))
  1402. return -EINVAL;
  1403. reg->hr_start_block = tmp;
  1404. return count;
  1405. }
  1406. static ssize_t o2hb_region_blocks_show(struct config_item *item, char *page)
  1407. {
  1408. return sprintf(page, "%d\n", to_o2hb_region(item)->hr_blocks);
  1409. }
  1410. static ssize_t o2hb_region_blocks_store(struct config_item *item,
  1411. const char *page,
  1412. size_t count)
  1413. {
  1414. struct o2hb_region *reg = to_o2hb_region(item);
  1415. unsigned long tmp;
  1416. char *p = (char *)page;
  1417. if (reg->hr_bdev)
  1418. return -EINVAL;
  1419. tmp = simple_strtoul(p, &p, 0);
  1420. if (!p || (*p && (*p != '\n')))
  1421. return -EINVAL;
  1422. if (tmp > O2NM_MAX_NODES || tmp == 0)
  1423. return -ERANGE;
  1424. reg->hr_blocks = (unsigned int)tmp;
  1425. return count;
  1426. }
  1427. static ssize_t o2hb_region_dev_show(struct config_item *item, char *page)
  1428. {
  1429. unsigned int ret = 0;
  1430. if (to_o2hb_region(item)->hr_bdev)
  1431. ret = sprintf(page, "%s\n", to_o2hb_region(item)->hr_dev_name);
  1432. return ret;
  1433. }
  1434. static void o2hb_init_region_params(struct o2hb_region *reg)
  1435. {
  1436. reg->hr_slots_per_page = PAGE_SIZE >> reg->hr_block_bits;
  1437. reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
  1438. mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
  1439. reg->hr_start_block, reg->hr_blocks);
  1440. mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
  1441. reg->hr_block_bytes, reg->hr_block_bits);
  1442. mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
  1443. mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
  1444. }
  1445. static int o2hb_map_slot_data(struct o2hb_region *reg)
  1446. {
  1447. int i, j;
  1448. unsigned int last_slot;
  1449. unsigned int spp = reg->hr_slots_per_page;
  1450. struct page *page;
  1451. char *raw;
  1452. struct o2hb_disk_slot *slot;
  1453. reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
  1454. if (reg->hr_tmp_block == NULL)
  1455. return -ENOMEM;
  1456. reg->hr_slots = kcalloc(reg->hr_blocks,
  1457. sizeof(struct o2hb_disk_slot), GFP_KERNEL);
  1458. if (reg->hr_slots == NULL)
  1459. return -ENOMEM;
  1460. for(i = 0; i < reg->hr_blocks; i++) {
  1461. slot = &reg->hr_slots[i];
  1462. slot->ds_node_num = i;
  1463. INIT_LIST_HEAD(&slot->ds_live_item);
  1464. slot->ds_raw_block = NULL;
  1465. }
  1466. reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
  1467. mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
  1468. "at %u blocks per page\n",
  1469. reg->hr_num_pages, reg->hr_blocks, spp);
  1470. reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
  1471. GFP_KERNEL);
  1472. if (!reg->hr_slot_data)
  1473. return -ENOMEM;
  1474. for(i = 0; i < reg->hr_num_pages; i++) {
  1475. page = alloc_page(GFP_KERNEL);
  1476. if (!page)
  1477. return -ENOMEM;
  1478. reg->hr_slot_data[i] = page;
  1479. last_slot = i * spp;
  1480. raw = page_address(page);
  1481. for (j = 0;
  1482. (j < spp) && ((j + last_slot) < reg->hr_blocks);
  1483. j++) {
  1484. BUG_ON((j + last_slot) >= reg->hr_blocks);
  1485. slot = &reg->hr_slots[j + last_slot];
  1486. slot->ds_raw_block =
  1487. (struct o2hb_disk_heartbeat_block *) raw;
  1488. raw += reg->hr_block_bytes;
  1489. }
  1490. }
  1491. return 0;
  1492. }
  1493. /* Read in all the slots available and populate the tracking
  1494. * structures so that we can start with a baseline idea of what's
  1495. * there. */
  1496. static int o2hb_populate_slot_data(struct o2hb_region *reg)
  1497. {
  1498. int ret, i;
  1499. struct o2hb_disk_slot *slot;
  1500. struct o2hb_disk_heartbeat_block *hb_block;
  1501. ret = o2hb_read_slots(reg, reg->hr_blocks);
  1502. if (ret)
  1503. goto out;
  1504. /* We only want to get an idea of the values initially in each
  1505. * slot, so we do no verification - o2hb_check_slot will
  1506. * actually determine if each configured slot is valid and
  1507. * whether any values have changed. */
  1508. for(i = 0; i < reg->hr_blocks; i++) {
  1509. slot = &reg->hr_slots[i];
  1510. hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
  1511. /* Only fill the values that o2hb_check_slot uses to
  1512. * determine changing slots */
  1513. slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
  1514. slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
  1515. }
  1516. out:
  1517. return ret;
  1518. }
  1519. /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
  1520. static ssize_t o2hb_region_dev_store(struct config_item *item,
  1521. const char *page,
  1522. size_t count)
  1523. {
  1524. struct o2hb_region *reg = to_o2hb_region(item);
  1525. struct task_struct *hb_task;
  1526. long fd;
  1527. int sectsize;
  1528. char *p = (char *)page;
  1529. struct fd f;
  1530. struct inode *inode;
  1531. ssize_t ret = -EINVAL;
  1532. int live_threshold;
  1533. if (reg->hr_bdev)
  1534. goto out;
  1535. /* We can't heartbeat without having had our node number
  1536. * configured yet. */
  1537. if (o2nm_this_node() == O2NM_MAX_NODES)
  1538. goto out;
  1539. fd = simple_strtol(p, &p, 0);
  1540. if (!p || (*p && (*p != '\n')))
  1541. goto out;
  1542. if (fd < 0 || fd >= INT_MAX)
  1543. goto out;
  1544. f = fdget(fd);
  1545. if (f.file == NULL)
  1546. goto out;
  1547. if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
  1548. reg->hr_block_bytes == 0)
  1549. goto out2;
  1550. inode = igrab(f.file->f_mapping->host);
  1551. if (inode == NULL)
  1552. goto out2;
  1553. if (!S_ISBLK(inode->i_mode))
  1554. goto out3;
  1555. reg->hr_bdev = I_BDEV(f.file->f_mapping->host);
  1556. ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ, NULL);
  1557. if (ret) {
  1558. reg->hr_bdev = NULL;
  1559. goto out3;
  1560. }
  1561. inode = NULL;
  1562. bdevname(reg->hr_bdev, reg->hr_dev_name);
  1563. sectsize = bdev_logical_block_size(reg->hr_bdev);
  1564. if (sectsize != reg->hr_block_bytes) {
  1565. mlog(ML_ERROR,
  1566. "blocksize %u incorrect for device, expected %d",
  1567. reg->hr_block_bytes, sectsize);
  1568. ret = -EINVAL;
  1569. goto out3;
  1570. }
  1571. o2hb_init_region_params(reg);
  1572. /* Generation of zero is invalid */
  1573. do {
  1574. get_random_bytes(&reg->hr_generation,
  1575. sizeof(reg->hr_generation));
  1576. } while (reg->hr_generation == 0);
  1577. ret = o2hb_map_slot_data(reg);
  1578. if (ret) {
  1579. mlog_errno(ret);
  1580. goto out3;
  1581. }
  1582. ret = o2hb_populate_slot_data(reg);
  1583. if (ret) {
  1584. mlog_errno(ret);
  1585. goto out3;
  1586. }
  1587. INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
  1588. INIT_DELAYED_WORK(&reg->hr_nego_timeout_work, o2hb_nego_timeout);
  1589. /*
  1590. * A node is considered live after it has beat LIVE_THRESHOLD
  1591. * times. We're not steady until we've given them a chance
  1592. * _after_ our first read.
  1593. * The default threshold is bare minimum so as to limit the delay
  1594. * during mounts. For global heartbeat, the threshold doubled for the
  1595. * first region.
  1596. */
  1597. live_threshold = O2HB_LIVE_THRESHOLD;
  1598. if (o2hb_global_heartbeat_active()) {
  1599. spin_lock(&o2hb_live_lock);
  1600. if (bitmap_weight(o2hb_region_bitmap, O2NM_MAX_REGIONS) == 1)
  1601. live_threshold <<= 1;
  1602. spin_unlock(&o2hb_live_lock);
  1603. }
  1604. ++live_threshold;
  1605. atomic_set(&reg->hr_steady_iterations, live_threshold);
  1606. /* unsteady_iterations is triple the steady_iterations */
  1607. atomic_set(&reg->hr_unsteady_iterations, (live_threshold * 3));
  1608. hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
  1609. reg->hr_item.ci_name);
  1610. if (IS_ERR(hb_task)) {
  1611. ret = PTR_ERR(hb_task);
  1612. mlog_errno(ret);
  1613. goto out3;
  1614. }
  1615. spin_lock(&o2hb_live_lock);
  1616. reg->hr_task = hb_task;
  1617. spin_unlock(&o2hb_live_lock);
  1618. ret = wait_event_interruptible(o2hb_steady_queue,
  1619. atomic_read(&reg->hr_steady_iterations) == 0 ||
  1620. reg->hr_node_deleted);
  1621. if (ret) {
  1622. atomic_set(&reg->hr_steady_iterations, 0);
  1623. reg->hr_aborted_start = 1;
  1624. }
  1625. if (reg->hr_aborted_start) {
  1626. ret = -EIO;
  1627. goto out3;
  1628. }
  1629. if (reg->hr_node_deleted) {
  1630. ret = -EINVAL;
  1631. goto out3;
  1632. }
  1633. /* Ok, we were woken. Make sure it wasn't by drop_item() */
  1634. spin_lock(&o2hb_live_lock);
  1635. hb_task = reg->hr_task;
  1636. if (o2hb_global_heartbeat_active())
  1637. set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
  1638. spin_unlock(&o2hb_live_lock);
  1639. if (hb_task)
  1640. ret = count;
  1641. else
  1642. ret = -EIO;
  1643. if (hb_task && o2hb_global_heartbeat_active())
  1644. printk(KERN_NOTICE "o2hb: Heartbeat started on region %s (%s)\n",
  1645. config_item_name(&reg->hr_item), reg->hr_dev_name);
  1646. out3:
  1647. iput(inode);
  1648. out2:
  1649. fdput(f);
  1650. out:
  1651. if (ret < 0) {
  1652. if (reg->hr_bdev) {
  1653. blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
  1654. reg->hr_bdev = NULL;
  1655. }
  1656. }
  1657. return ret;
  1658. }
  1659. static ssize_t o2hb_region_pid_show(struct config_item *item, char *page)
  1660. {
  1661. struct o2hb_region *reg = to_o2hb_region(item);
  1662. pid_t pid = 0;
  1663. spin_lock(&o2hb_live_lock);
  1664. if (reg->hr_task)
  1665. pid = task_pid_nr(reg->hr_task);
  1666. spin_unlock(&o2hb_live_lock);
  1667. if (!pid)
  1668. return 0;
  1669. return sprintf(page, "%u\n", pid);
  1670. }
  1671. CONFIGFS_ATTR(o2hb_region_, block_bytes);
  1672. CONFIGFS_ATTR(o2hb_region_, start_block);
  1673. CONFIGFS_ATTR(o2hb_region_, blocks);
  1674. CONFIGFS_ATTR(o2hb_region_, dev);
  1675. CONFIGFS_ATTR_RO(o2hb_region_, pid);
  1676. static struct configfs_attribute *o2hb_region_attrs[] = {
  1677. &o2hb_region_attr_block_bytes,
  1678. &o2hb_region_attr_start_block,
  1679. &o2hb_region_attr_blocks,
  1680. &o2hb_region_attr_dev,
  1681. &o2hb_region_attr_pid,
  1682. NULL,
  1683. };
  1684. static struct configfs_item_operations o2hb_region_item_ops = {
  1685. .release = o2hb_region_release,
  1686. };
  1687. static const struct config_item_type o2hb_region_type = {
  1688. .ct_item_ops = &o2hb_region_item_ops,
  1689. .ct_attrs = o2hb_region_attrs,
  1690. .ct_owner = THIS_MODULE,
  1691. };
  1692. /* heartbeat set */
  1693. struct o2hb_heartbeat_group {
  1694. struct config_group hs_group;
  1695. /* some stuff? */
  1696. };
  1697. static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
  1698. {
  1699. return group ?
  1700. container_of(group, struct o2hb_heartbeat_group, hs_group)
  1701. : NULL;
  1702. }
  1703. static int o2hb_debug_region_init(struct o2hb_region *reg, struct dentry *dir)
  1704. {
  1705. int ret = -ENOMEM;
  1706. reg->hr_debug_dir =
  1707. debugfs_create_dir(config_item_name(&reg->hr_item), dir);
  1708. if (!reg->hr_debug_dir) {
  1709. mlog_errno(ret);
  1710. goto bail;
  1711. }
  1712. reg->hr_debug_livenodes =
  1713. o2hb_debug_create(O2HB_DEBUG_LIVENODES,
  1714. reg->hr_debug_dir,
  1715. &(reg->hr_db_livenodes),
  1716. sizeof(*(reg->hr_db_livenodes)),
  1717. O2HB_DB_TYPE_REGION_LIVENODES,
  1718. sizeof(reg->hr_live_node_bitmap),
  1719. O2NM_MAX_NODES, reg);
  1720. if (!reg->hr_debug_livenodes) {
  1721. mlog_errno(ret);
  1722. goto bail;
  1723. }
  1724. reg->hr_debug_regnum =
  1725. o2hb_debug_create(O2HB_DEBUG_REGION_NUMBER,
  1726. reg->hr_debug_dir,
  1727. &(reg->hr_db_regnum),
  1728. sizeof(*(reg->hr_db_regnum)),
  1729. O2HB_DB_TYPE_REGION_NUMBER,
  1730. 0, O2NM_MAX_NODES, reg);
  1731. if (!reg->hr_debug_regnum) {
  1732. mlog_errno(ret);
  1733. goto bail;
  1734. }
  1735. reg->hr_debug_elapsed_time =
  1736. o2hb_debug_create(O2HB_DEBUG_REGION_ELAPSED_TIME,
  1737. reg->hr_debug_dir,
  1738. &(reg->hr_db_elapsed_time),
  1739. sizeof(*(reg->hr_db_elapsed_time)),
  1740. O2HB_DB_TYPE_REGION_ELAPSED_TIME,
  1741. 0, 0, reg);
  1742. if (!reg->hr_debug_elapsed_time) {
  1743. mlog_errno(ret);
  1744. goto bail;
  1745. }
  1746. reg->hr_debug_pinned =
  1747. o2hb_debug_create(O2HB_DEBUG_REGION_PINNED,
  1748. reg->hr_debug_dir,
  1749. &(reg->hr_db_pinned),
  1750. sizeof(*(reg->hr_db_pinned)),
  1751. O2HB_DB_TYPE_REGION_PINNED,
  1752. 0, 0, reg);
  1753. if (!reg->hr_debug_pinned) {
  1754. mlog_errno(ret);
  1755. goto bail;
  1756. }
  1757. ret = 0;
  1758. bail:
  1759. return ret;
  1760. }
  1761. static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
  1762. const char *name)
  1763. {
  1764. struct o2hb_region *reg = NULL;
  1765. int ret;
  1766. reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
  1767. if (reg == NULL)
  1768. return ERR_PTR(-ENOMEM);
  1769. if (strlen(name) > O2HB_MAX_REGION_NAME_LEN) {
  1770. ret = -ENAMETOOLONG;
  1771. goto free;
  1772. }
  1773. spin_lock(&o2hb_live_lock);
  1774. reg->hr_region_num = 0;
  1775. if (o2hb_global_heartbeat_active()) {
  1776. reg->hr_region_num = find_first_zero_bit(o2hb_region_bitmap,
  1777. O2NM_MAX_REGIONS);
  1778. if (reg->hr_region_num >= O2NM_MAX_REGIONS) {
  1779. spin_unlock(&o2hb_live_lock);
  1780. ret = -EFBIG;
  1781. goto free;
  1782. }
  1783. set_bit(reg->hr_region_num, o2hb_region_bitmap);
  1784. }
  1785. list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
  1786. spin_unlock(&o2hb_live_lock);
  1787. config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
  1788. /* this is the same way to generate msg key as dlm, for local heartbeat,
  1789. * name is also the same, so make initial crc value different to avoid
  1790. * message key conflict.
  1791. */
  1792. reg->hr_key = crc32_le(reg->hr_region_num + O2NM_MAX_REGIONS,
  1793. name, strlen(name));
  1794. INIT_LIST_HEAD(&reg->hr_handler_list);
  1795. ret = o2net_register_handler(O2HB_NEGO_TIMEOUT_MSG, reg->hr_key,
  1796. sizeof(struct o2hb_nego_msg),
  1797. o2hb_nego_timeout_handler,
  1798. reg, NULL, &reg->hr_handler_list);
  1799. if (ret)
  1800. goto free;
  1801. ret = o2net_register_handler(O2HB_NEGO_APPROVE_MSG, reg->hr_key,
  1802. sizeof(struct o2hb_nego_msg),
  1803. o2hb_nego_approve_handler,
  1804. reg, NULL, &reg->hr_handler_list);
  1805. if (ret)
  1806. goto unregister_handler;
  1807. ret = o2hb_debug_region_init(reg, o2hb_debug_dir);
  1808. if (ret) {
  1809. config_item_put(&reg->hr_item);
  1810. goto unregister_handler;
  1811. }
  1812. return &reg->hr_item;
  1813. unregister_handler:
  1814. o2net_unregister_handler_list(&reg->hr_handler_list);
  1815. free:
  1816. kfree(reg);
  1817. return ERR_PTR(ret);
  1818. }
  1819. static void o2hb_heartbeat_group_drop_item(struct config_group *group,
  1820. struct config_item *item)
  1821. {
  1822. struct task_struct *hb_task;
  1823. struct o2hb_region *reg = to_o2hb_region(item);
  1824. int quorum_region = 0;
  1825. /* stop the thread when the user removes the region dir */
  1826. spin_lock(&o2hb_live_lock);
  1827. hb_task = reg->hr_task;
  1828. reg->hr_task = NULL;
  1829. reg->hr_item_dropped = 1;
  1830. spin_unlock(&o2hb_live_lock);
  1831. if (hb_task)
  1832. kthread_stop(hb_task);
  1833. if (o2hb_global_heartbeat_active()) {
  1834. spin_lock(&o2hb_live_lock);
  1835. clear_bit(reg->hr_region_num, o2hb_region_bitmap);
  1836. clear_bit(reg->hr_region_num, o2hb_live_region_bitmap);
  1837. if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
  1838. quorum_region = 1;
  1839. clear_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
  1840. spin_unlock(&o2hb_live_lock);
  1841. printk(KERN_NOTICE "o2hb: Heartbeat %s on region %s (%s)\n",
  1842. ((atomic_read(&reg->hr_steady_iterations) == 0) ?
  1843. "stopped" : "start aborted"), config_item_name(item),
  1844. reg->hr_dev_name);
  1845. }
  1846. /*
  1847. * If we're racing a dev_write(), we need to wake them. They will
  1848. * check reg->hr_task
  1849. */
  1850. if (atomic_read(&reg->hr_steady_iterations) != 0) {
  1851. reg->hr_aborted_start = 1;
  1852. atomic_set(&reg->hr_steady_iterations, 0);
  1853. wake_up(&o2hb_steady_queue);
  1854. }
  1855. config_item_put(item);
  1856. if (!o2hb_global_heartbeat_active() || !quorum_region)
  1857. return;
  1858. /*
  1859. * If global heartbeat active and there are dependent users,
  1860. * pin all regions if quorum region count <= CUT_OFF
  1861. */
  1862. spin_lock(&o2hb_live_lock);
  1863. if (!o2hb_dependent_users)
  1864. goto unlock;
  1865. if (bitmap_weight(o2hb_quorum_region_bitmap,
  1866. O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
  1867. o2hb_region_pin(NULL);
  1868. unlock:
  1869. spin_unlock(&o2hb_live_lock);
  1870. }
  1871. static ssize_t o2hb_heartbeat_group_dead_threshold_show(struct config_item *item,
  1872. char *page)
  1873. {
  1874. return sprintf(page, "%u\n", o2hb_dead_threshold);
  1875. }
  1876. static ssize_t o2hb_heartbeat_group_dead_threshold_store(struct config_item *item,
  1877. const char *page, size_t count)
  1878. {
  1879. unsigned long tmp;
  1880. char *p = (char *)page;
  1881. tmp = simple_strtoul(p, &p, 10);
  1882. if (!p || (*p && (*p != '\n')))
  1883. return -EINVAL;
  1884. /* this will validate ranges for us. */
  1885. o2hb_dead_threshold_set((unsigned int) tmp);
  1886. return count;
  1887. }
  1888. static ssize_t o2hb_heartbeat_group_mode_show(struct config_item *item,
  1889. char *page)
  1890. {
  1891. return sprintf(page, "%s\n",
  1892. o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
  1893. }
  1894. static ssize_t o2hb_heartbeat_group_mode_store(struct config_item *item,
  1895. const char *page, size_t count)
  1896. {
  1897. unsigned int i;
  1898. int ret;
  1899. size_t len;
  1900. len = (page[count - 1] == '\n') ? count - 1 : count;
  1901. if (!len)
  1902. return -EINVAL;
  1903. for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
  1904. if (strncasecmp(page, o2hb_heartbeat_mode_desc[i], len))
  1905. continue;
  1906. ret = o2hb_global_heartbeat_mode_set(i);
  1907. if (!ret)
  1908. printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
  1909. o2hb_heartbeat_mode_desc[i]);
  1910. return count;
  1911. }
  1912. return -EINVAL;
  1913. }
  1914. CONFIGFS_ATTR(o2hb_heartbeat_group_, dead_threshold);
  1915. CONFIGFS_ATTR(o2hb_heartbeat_group_, mode);
  1916. static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
  1917. &o2hb_heartbeat_group_attr_dead_threshold,
  1918. &o2hb_heartbeat_group_attr_mode,
  1919. NULL,
  1920. };
  1921. static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
  1922. .make_item = o2hb_heartbeat_group_make_item,
  1923. .drop_item = o2hb_heartbeat_group_drop_item,
  1924. };
  1925. static const struct config_item_type o2hb_heartbeat_group_type = {
  1926. .ct_group_ops = &o2hb_heartbeat_group_group_ops,
  1927. .ct_attrs = o2hb_heartbeat_group_attrs,
  1928. .ct_owner = THIS_MODULE,
  1929. };
  1930. /* this is just here to avoid touching group in heartbeat.h which the
  1931. * entire damn world #includes */
  1932. struct config_group *o2hb_alloc_hb_set(void)
  1933. {
  1934. struct o2hb_heartbeat_group *hs = NULL;
  1935. struct config_group *ret = NULL;
  1936. hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
  1937. if (hs == NULL)
  1938. goto out;
  1939. config_group_init_type_name(&hs->hs_group, "heartbeat",
  1940. &o2hb_heartbeat_group_type);
  1941. ret = &hs->hs_group;
  1942. out:
  1943. if (ret == NULL)
  1944. kfree(hs);
  1945. return ret;
  1946. }
  1947. void o2hb_free_hb_set(struct config_group *group)
  1948. {
  1949. struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
  1950. kfree(hs);
  1951. }
  1952. /* hb callback registration and issuing */
  1953. static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
  1954. {
  1955. if (type == O2HB_NUM_CB)
  1956. return ERR_PTR(-EINVAL);
  1957. return &o2hb_callbacks[type];
  1958. }
  1959. void o2hb_setup_callback(struct o2hb_callback_func *hc,
  1960. enum o2hb_callback_type type,
  1961. o2hb_cb_func *func,
  1962. void *data,
  1963. int priority)
  1964. {
  1965. INIT_LIST_HEAD(&hc->hc_item);
  1966. hc->hc_func = func;
  1967. hc->hc_data = data;
  1968. hc->hc_priority = priority;
  1969. hc->hc_type = type;
  1970. hc->hc_magic = O2HB_CB_MAGIC;
  1971. }
  1972. EXPORT_SYMBOL_GPL(o2hb_setup_callback);
  1973. /*
  1974. * In local heartbeat mode, region_uuid passed matches the dlm domain name.
  1975. * In global heartbeat mode, region_uuid passed is NULL.
  1976. *
  1977. * In local, we only pin the matching region. In global we pin all the active
  1978. * regions.
  1979. */
  1980. static int o2hb_region_pin(const char *region_uuid)
  1981. {
  1982. int ret = 0, found = 0;
  1983. struct o2hb_region *reg;
  1984. char *uuid;
  1985. assert_spin_locked(&o2hb_live_lock);
  1986. list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
  1987. if (reg->hr_item_dropped)
  1988. continue;
  1989. uuid = config_item_name(&reg->hr_item);
  1990. /* local heartbeat */
  1991. if (region_uuid) {
  1992. if (strcmp(region_uuid, uuid))
  1993. continue;
  1994. found = 1;
  1995. }
  1996. if (reg->hr_item_pinned || reg->hr_item_dropped)
  1997. goto skip_pin;
  1998. /* Ignore ENOENT only for local hb (userdlm domain) */
  1999. ret = o2nm_depend_item(&reg->hr_item);
  2000. if (!ret) {
  2001. mlog(ML_CLUSTER, "Pin region %s\n", uuid);
  2002. reg->hr_item_pinned = 1;
  2003. } else {
  2004. if (ret == -ENOENT && found)
  2005. ret = 0;
  2006. else {
  2007. mlog(ML_ERROR, "Pin region %s fails with %d\n",
  2008. uuid, ret);
  2009. break;
  2010. }
  2011. }
  2012. skip_pin:
  2013. if (found)
  2014. break;
  2015. }
  2016. return ret;
  2017. }
  2018. /*
  2019. * In local heartbeat mode, region_uuid passed matches the dlm domain name.
  2020. * In global heartbeat mode, region_uuid passed is NULL.
  2021. *
  2022. * In local, we only unpin the matching region. In global we unpin all the
  2023. * active regions.
  2024. */
  2025. static void o2hb_region_unpin(const char *region_uuid)
  2026. {
  2027. struct o2hb_region *reg;
  2028. char *uuid;
  2029. int found = 0;
  2030. assert_spin_locked(&o2hb_live_lock);
  2031. list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
  2032. if (reg->hr_item_dropped)
  2033. continue;
  2034. uuid = config_item_name(&reg->hr_item);
  2035. if (region_uuid) {
  2036. if (strcmp(region_uuid, uuid))
  2037. continue;
  2038. found = 1;
  2039. }
  2040. if (reg->hr_item_pinned) {
  2041. mlog(ML_CLUSTER, "Unpin region %s\n", uuid);
  2042. o2nm_undepend_item(&reg->hr_item);
  2043. reg->hr_item_pinned = 0;
  2044. }
  2045. if (found)
  2046. break;
  2047. }
  2048. }
  2049. static int o2hb_region_inc_user(const char *region_uuid)
  2050. {
  2051. int ret = 0;
  2052. spin_lock(&o2hb_live_lock);
  2053. /* local heartbeat */
  2054. if (!o2hb_global_heartbeat_active()) {
  2055. ret = o2hb_region_pin(region_uuid);
  2056. goto unlock;
  2057. }
  2058. /*
  2059. * if global heartbeat active and this is the first dependent user,
  2060. * pin all regions if quorum region count <= CUT_OFF
  2061. */
  2062. o2hb_dependent_users++;
  2063. if (o2hb_dependent_users > 1)
  2064. goto unlock;
  2065. if (bitmap_weight(o2hb_quorum_region_bitmap,
  2066. O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
  2067. ret = o2hb_region_pin(NULL);
  2068. unlock:
  2069. spin_unlock(&o2hb_live_lock);
  2070. return ret;
  2071. }
  2072. void o2hb_region_dec_user(const char *region_uuid)
  2073. {
  2074. spin_lock(&o2hb_live_lock);
  2075. /* local heartbeat */
  2076. if (!o2hb_global_heartbeat_active()) {
  2077. o2hb_region_unpin(region_uuid);
  2078. goto unlock;
  2079. }
  2080. /*
  2081. * if global heartbeat active and there are no dependent users,
  2082. * unpin all quorum regions
  2083. */
  2084. o2hb_dependent_users--;
  2085. if (!o2hb_dependent_users)
  2086. o2hb_region_unpin(NULL);
  2087. unlock:
  2088. spin_unlock(&o2hb_live_lock);
  2089. }
  2090. int o2hb_register_callback(const char *region_uuid,
  2091. struct o2hb_callback_func *hc)
  2092. {
  2093. struct o2hb_callback_func *f;
  2094. struct o2hb_callback *hbcall;
  2095. int ret;
  2096. BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
  2097. BUG_ON(!list_empty(&hc->hc_item));
  2098. hbcall = hbcall_from_type(hc->hc_type);
  2099. if (IS_ERR(hbcall)) {
  2100. ret = PTR_ERR(hbcall);
  2101. goto out;
  2102. }
  2103. if (region_uuid) {
  2104. ret = o2hb_region_inc_user(region_uuid);
  2105. if (ret) {
  2106. mlog_errno(ret);
  2107. goto out;
  2108. }
  2109. }
  2110. down_write(&o2hb_callback_sem);
  2111. list_for_each_entry(f, &hbcall->list, hc_item) {
  2112. if (hc->hc_priority < f->hc_priority) {
  2113. list_add_tail(&hc->hc_item, &f->hc_item);
  2114. break;
  2115. }
  2116. }
  2117. if (list_empty(&hc->hc_item))
  2118. list_add_tail(&hc->hc_item, &hbcall->list);
  2119. up_write(&o2hb_callback_sem);
  2120. ret = 0;
  2121. out:
  2122. mlog(ML_CLUSTER, "returning %d on behalf of %p for funcs %p\n",
  2123. ret, __builtin_return_address(0), hc);
  2124. return ret;
  2125. }
  2126. EXPORT_SYMBOL_GPL(o2hb_register_callback);
  2127. void o2hb_unregister_callback(const char *region_uuid,
  2128. struct o2hb_callback_func *hc)
  2129. {
  2130. BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
  2131. mlog(ML_CLUSTER, "on behalf of %p for funcs %p\n",
  2132. __builtin_return_address(0), hc);
  2133. /* XXX Can this happen _with_ a region reference? */
  2134. if (list_empty(&hc->hc_item))
  2135. return;
  2136. if (region_uuid)
  2137. o2hb_region_dec_user(region_uuid);
  2138. down_write(&o2hb_callback_sem);
  2139. list_del_init(&hc->hc_item);
  2140. up_write(&o2hb_callback_sem);
  2141. }
  2142. EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
  2143. int o2hb_check_node_heartbeating_no_sem(u8 node_num)
  2144. {
  2145. unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  2146. spin_lock(&o2hb_live_lock);
  2147. o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
  2148. spin_unlock(&o2hb_live_lock);
  2149. if (!test_bit(node_num, testing_map)) {
  2150. mlog(ML_HEARTBEAT,
  2151. "node (%u) does not have heartbeating enabled.\n",
  2152. node_num);
  2153. return 0;
  2154. }
  2155. return 1;
  2156. }
  2157. EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_no_sem);
  2158. int o2hb_check_node_heartbeating_from_callback(u8 node_num)
  2159. {
  2160. unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  2161. o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
  2162. if (!test_bit(node_num, testing_map)) {
  2163. mlog(ML_HEARTBEAT,
  2164. "node (%u) does not have heartbeating enabled.\n",
  2165. node_num);
  2166. return 0;
  2167. }
  2168. return 1;
  2169. }
  2170. EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
  2171. /*
  2172. * this is just a hack until we get the plumbing which flips file systems
  2173. * read only and drops the hb ref instead of killing the node dead.
  2174. */
  2175. void o2hb_stop_all_regions(void)
  2176. {
  2177. struct o2hb_region *reg;
  2178. mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
  2179. spin_lock(&o2hb_live_lock);
  2180. list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
  2181. reg->hr_unclean_stop = 1;
  2182. spin_unlock(&o2hb_live_lock);
  2183. }
  2184. EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
  2185. int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
  2186. {
  2187. struct o2hb_region *reg;
  2188. int numregs = 0;
  2189. char *p;
  2190. spin_lock(&o2hb_live_lock);
  2191. p = region_uuids;
  2192. list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
  2193. if (reg->hr_item_dropped)
  2194. continue;
  2195. mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
  2196. if (numregs < max_regions) {
  2197. memcpy(p, config_item_name(&reg->hr_item),
  2198. O2HB_MAX_REGION_NAME_LEN);
  2199. p += O2HB_MAX_REGION_NAME_LEN;
  2200. }
  2201. numregs++;
  2202. }
  2203. spin_unlock(&o2hb_live_lock);
  2204. return numregs;
  2205. }
  2206. EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
  2207. int o2hb_global_heartbeat_active(void)
  2208. {
  2209. return (o2hb_heartbeat_mode == O2HB_HEARTBEAT_GLOBAL);
  2210. }
  2211. EXPORT_SYMBOL(o2hb_global_heartbeat_active);