heartbeat.c 67 KB

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