heartbeat.c 47 KB

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