heartbeat.c 68 KB

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