heartbeat.c 62 KB

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