heartbeat.c 49 KB

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