heartbeat.c 53 KB

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