heartbeat.c 49 KB

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