heartbeat.c 49 KB

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