heartbeat.c 69 KB

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