heartbeat.c 65 KB

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