heartbeat.c 47 KB

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