heartbeat.c 69 KB

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