heartbeat.c 70 KB

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