heartbeat.c 66 KB

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