heartbeat.c 62 KB

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