heartbeat.c 70 KB

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