heartbeat.c 46 KB

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