heartbeat.c 47 KB

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