heartbeat.c 67 KB

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