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