heartbeat.c 47 KB

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