heartbeat.c 55 KB

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