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. struct o2hb_callback *hbcall;
  534. struct o2hb_node_event *event;
  535. /* Holding callback sem assures we don't alter the callback
  536. * lists when doing this, and serializes ourselves with other
  537. * processes wanting callbacks. */
  538. down_write(&o2hb_callback_sem);
  539. spin_lock(&o2hb_live_lock);
  540. while (!list_empty(&o2hb_node_events)
  541. && !list_empty(&queued_event->hn_item)) {
  542. event = list_entry(o2hb_node_events.next,
  543. struct o2hb_node_event,
  544. hn_item);
  545. list_del_init(&event->hn_item);
  546. spin_unlock(&o2hb_live_lock);
  547. mlog(ML_HEARTBEAT, "Node %s event for %d\n",
  548. event->hn_event_type == O2HB_NODE_UP_CB ? "UP" : "DOWN",
  549. event->hn_node_num);
  550. hbcall = hbcall_from_type(event->hn_event_type);
  551. /* We should *never* have gotten on to the list with a
  552. * bad type... This isn't something that we should try
  553. * to recover from. */
  554. BUG_ON(IS_ERR(hbcall));
  555. o2hb_fire_callbacks(hbcall, event->hn_node, event->hn_node_num);
  556. spin_lock(&o2hb_live_lock);
  557. }
  558. spin_unlock(&o2hb_live_lock);
  559. up_write(&o2hb_callback_sem);
  560. }
  561. static void o2hb_queue_node_event(struct o2hb_node_event *event,
  562. enum o2hb_callback_type type,
  563. struct o2nm_node *node,
  564. int node_num)
  565. {
  566. assert_spin_locked(&o2hb_live_lock);
  567. BUG_ON((!node) && (type != O2HB_NODE_DOWN_CB));
  568. event->hn_event_type = type;
  569. event->hn_node = node;
  570. event->hn_node_num = node_num;
  571. mlog(ML_HEARTBEAT, "Queue node %s event for node %d\n",
  572. type == O2HB_NODE_UP_CB ? "UP" : "DOWN", node_num);
  573. list_add_tail(&event->hn_item, &o2hb_node_events);
  574. }
  575. static void o2hb_shutdown_slot(struct o2hb_disk_slot *slot)
  576. {
  577. struct o2hb_node_event event =
  578. { .hn_item = LIST_HEAD_INIT(event.hn_item), };
  579. struct o2nm_node *node;
  580. int queued = 0;
  581. node = o2nm_get_node_by_num(slot->ds_node_num);
  582. if (!node)
  583. return;
  584. spin_lock(&o2hb_live_lock);
  585. if (!list_empty(&slot->ds_live_item)) {
  586. mlog(ML_HEARTBEAT, "Shutdown, node %d leaves region\n",
  587. slot->ds_node_num);
  588. list_del_init(&slot->ds_live_item);
  589. if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
  590. clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
  591. o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB, node,
  592. slot->ds_node_num);
  593. queued = 1;
  594. }
  595. }
  596. spin_unlock(&o2hb_live_lock);
  597. if (queued)
  598. o2hb_run_event_list(&event);
  599. o2nm_node_put(node);
  600. }
  601. static void o2hb_set_quorum_device(struct o2hb_region *reg)
  602. {
  603. if (!o2hb_global_heartbeat_active())
  604. return;
  605. /* Prevent race with o2hb_heartbeat_group_drop_item() */
  606. if (kthread_should_stop())
  607. return;
  608. /* Tag region as quorum only after thread reaches steady state */
  609. if (atomic_read(&reg->hr_steady_iterations) != 0)
  610. return;
  611. spin_lock(&o2hb_live_lock);
  612. if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
  613. goto unlock;
  614. /*
  615. * A region can be added to the quorum only when it sees all
  616. * live nodes heartbeat on it. In other words, the region has been
  617. * added to all nodes.
  618. */
  619. if (memcmp(reg->hr_live_node_bitmap, o2hb_live_node_bitmap,
  620. sizeof(o2hb_live_node_bitmap)))
  621. goto unlock;
  622. printk(KERN_NOTICE "o2hb: Region %s (%s) is now a quorum device\n",
  623. config_item_name(&reg->hr_item), reg->hr_dev_name);
  624. set_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
  625. /*
  626. * If global heartbeat active, unpin all regions if the
  627. * region count > CUT_OFF
  628. */
  629. if (o2hb_pop_count(&o2hb_quorum_region_bitmap,
  630. O2NM_MAX_REGIONS) > O2HB_PIN_CUT_OFF)
  631. o2hb_region_unpin(NULL);
  632. unlock:
  633. spin_unlock(&o2hb_live_lock);
  634. }
  635. static int o2hb_check_slot(struct o2hb_region *reg,
  636. struct o2hb_disk_slot *slot)
  637. {
  638. int changed = 0, gen_changed = 0;
  639. struct o2hb_node_event event =
  640. { .hn_item = LIST_HEAD_INIT(event.hn_item), };
  641. struct o2nm_node *node;
  642. struct o2hb_disk_heartbeat_block *hb_block = reg->hr_tmp_block;
  643. u64 cputime;
  644. unsigned int dead_ms = o2hb_dead_threshold * O2HB_REGION_TIMEOUT_MS;
  645. unsigned int slot_dead_ms;
  646. int tmp;
  647. int queued = 0;
  648. memcpy(hb_block, slot->ds_raw_block, reg->hr_block_bytes);
  649. /*
  650. * If a node is no longer configured but is still in the livemap, we
  651. * may need to clear that bit from the livemap.
  652. */
  653. node = o2nm_get_node_by_num(slot->ds_node_num);
  654. if (!node) {
  655. spin_lock(&o2hb_live_lock);
  656. tmp = test_bit(slot->ds_node_num, o2hb_live_node_bitmap);
  657. spin_unlock(&o2hb_live_lock);
  658. if (!tmp)
  659. return 0;
  660. }
  661. if (!o2hb_verify_crc(reg, hb_block)) {
  662. /* all paths from here will drop o2hb_live_lock for
  663. * us. */
  664. spin_lock(&o2hb_live_lock);
  665. /* Don't print an error on the console in this case -
  666. * a freshly formatted heartbeat area will not have a
  667. * crc set on it. */
  668. if (list_empty(&slot->ds_live_item))
  669. goto out;
  670. /* The node is live but pushed out a bad crc. We
  671. * consider it a transient miss but don't populate any
  672. * other values as they may be junk. */
  673. mlog(ML_ERROR, "Node %d has written a bad crc to %s\n",
  674. slot->ds_node_num, reg->hr_dev_name);
  675. o2hb_dump_slot(hb_block);
  676. slot->ds_equal_samples++;
  677. goto fire_callbacks;
  678. }
  679. /* we don't care if these wrap.. the state transitions below
  680. * clear at the right places */
  681. cputime = le64_to_cpu(hb_block->hb_seq);
  682. if (slot->ds_last_time != cputime)
  683. slot->ds_changed_samples++;
  684. else
  685. slot->ds_equal_samples++;
  686. slot->ds_last_time = cputime;
  687. /* The node changed heartbeat generations. We assume this to
  688. * mean it dropped off but came back before we timed out. We
  689. * want to consider it down for the time being but don't want
  690. * to lose any changed_samples state we might build up to
  691. * considering it live again. */
  692. if (slot->ds_last_generation != le64_to_cpu(hb_block->hb_generation)) {
  693. gen_changed = 1;
  694. slot->ds_equal_samples = 0;
  695. mlog(ML_HEARTBEAT, "Node %d changed generation (0x%llx "
  696. "to 0x%llx)\n", slot->ds_node_num,
  697. (long long)slot->ds_last_generation,
  698. (long long)le64_to_cpu(hb_block->hb_generation));
  699. }
  700. slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
  701. mlog(ML_HEARTBEAT, "Slot %d gen 0x%llx cksum 0x%x "
  702. "seq %llu last %llu changed %u equal %u\n",
  703. slot->ds_node_num, (long long)slot->ds_last_generation,
  704. le32_to_cpu(hb_block->hb_cksum),
  705. (unsigned long long)le64_to_cpu(hb_block->hb_seq),
  706. (unsigned long long)slot->ds_last_time, slot->ds_changed_samples,
  707. slot->ds_equal_samples);
  708. spin_lock(&o2hb_live_lock);
  709. fire_callbacks:
  710. /* dead nodes only come to life after some number of
  711. * changes at any time during their dead time */
  712. if (list_empty(&slot->ds_live_item) &&
  713. slot->ds_changed_samples >= O2HB_LIVE_THRESHOLD) {
  714. mlog(ML_HEARTBEAT, "Node %d (id 0x%llx) joined my region\n",
  715. slot->ds_node_num, (long long)slot->ds_last_generation);
  716. set_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
  717. /* first on the list generates a callback */
  718. if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
  719. mlog(ML_HEARTBEAT, "o2hb: Add node %d to live nodes "
  720. "bitmap\n", slot->ds_node_num);
  721. set_bit(slot->ds_node_num, o2hb_live_node_bitmap);
  722. o2hb_queue_node_event(&event, O2HB_NODE_UP_CB, node,
  723. slot->ds_node_num);
  724. changed = 1;
  725. queued = 1;
  726. }
  727. list_add_tail(&slot->ds_live_item,
  728. &o2hb_live_slots[slot->ds_node_num]);
  729. slot->ds_equal_samples = 0;
  730. /* We want to be sure that all nodes agree on the
  731. * number of milliseconds before a node will be
  732. * considered dead. The self-fencing timeout is
  733. * computed from this value, and a discrepancy might
  734. * result in heartbeat calling a node dead when it
  735. * hasn't self-fenced yet. */
  736. slot_dead_ms = le32_to_cpu(hb_block->hb_dead_ms);
  737. if (slot_dead_ms && slot_dead_ms != dead_ms) {
  738. /* TODO: Perhaps we can fail the region here. */
  739. mlog(ML_ERROR, "Node %d on device %s has a dead count "
  740. "of %u ms, but our count is %u ms.\n"
  741. "Please double check your configuration values "
  742. "for 'O2CB_HEARTBEAT_THRESHOLD'\n",
  743. slot->ds_node_num, reg->hr_dev_name, slot_dead_ms,
  744. dead_ms);
  745. }
  746. goto out;
  747. }
  748. /* if the list is dead, we're done.. */
  749. if (list_empty(&slot->ds_live_item))
  750. goto out;
  751. /* live nodes only go dead after enough consequtive missed
  752. * samples.. reset the missed counter whenever we see
  753. * activity */
  754. if (slot->ds_equal_samples >= o2hb_dead_threshold || gen_changed) {
  755. mlog(ML_HEARTBEAT, "Node %d left my region\n",
  756. slot->ds_node_num);
  757. clear_bit(slot->ds_node_num, reg->hr_live_node_bitmap);
  758. /* last off the live_slot generates a callback */
  759. list_del_init(&slot->ds_live_item);
  760. if (list_empty(&o2hb_live_slots[slot->ds_node_num])) {
  761. mlog(ML_HEARTBEAT, "o2hb: Remove node %d from live "
  762. "nodes bitmap\n", slot->ds_node_num);
  763. clear_bit(slot->ds_node_num, o2hb_live_node_bitmap);
  764. /* node can be null */
  765. o2hb_queue_node_event(&event, O2HB_NODE_DOWN_CB,
  766. node, slot->ds_node_num);
  767. changed = 1;
  768. queued = 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. if (queued)
  783. o2hb_run_event_list(&event);
  784. if (node)
  785. o2nm_node_put(node);
  786. return changed;
  787. }
  788. /* This could be faster if we just implmented a find_last_bit, but I
  789. * don't think the circumstances warrant it. */
  790. static int o2hb_highest_node(unsigned long *nodes,
  791. int numbits)
  792. {
  793. int highest, node;
  794. highest = numbits;
  795. node = -1;
  796. while ((node = find_next_bit(nodes, numbits, node + 1)) != -1) {
  797. if (node >= numbits)
  798. break;
  799. highest = node;
  800. }
  801. return highest;
  802. }
  803. static int o2hb_do_disk_heartbeat(struct o2hb_region *reg)
  804. {
  805. int i, ret, highest_node;
  806. int membership_change = 0, own_slot_ok = 0;
  807. unsigned long configured_nodes[BITS_TO_LONGS(O2NM_MAX_NODES)];
  808. unsigned long live_node_bitmap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  809. struct o2hb_bio_wait_ctxt write_wc;
  810. ret = o2nm_configured_node_map(configured_nodes,
  811. sizeof(configured_nodes));
  812. if (ret) {
  813. mlog_errno(ret);
  814. goto bail;
  815. }
  816. /*
  817. * If a node is not configured but is in the livemap, we still need
  818. * to read the slot so as to be able to remove it from the livemap.
  819. */
  820. o2hb_fill_node_map(live_node_bitmap, sizeof(live_node_bitmap));
  821. i = -1;
  822. while ((i = find_next_bit(live_node_bitmap,
  823. O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
  824. set_bit(i, configured_nodes);
  825. }
  826. highest_node = o2hb_highest_node(configured_nodes, O2NM_MAX_NODES);
  827. if (highest_node >= O2NM_MAX_NODES) {
  828. mlog(ML_NOTICE, "o2hb: No configured nodes found!\n");
  829. ret = -EINVAL;
  830. goto bail;
  831. }
  832. /* No sense in reading the slots of nodes that don't exist
  833. * yet. Of course, if the node definitions have holes in them
  834. * then we're reading an empty slot anyway... Consider this
  835. * best-effort. */
  836. ret = o2hb_read_slots(reg, highest_node + 1);
  837. if (ret < 0) {
  838. mlog_errno(ret);
  839. goto bail;
  840. }
  841. /* With an up to date view of the slots, we can check that no
  842. * other node has been improperly configured to heartbeat in
  843. * our slot. */
  844. own_slot_ok = o2hb_check_own_slot(reg);
  845. /* fill in the proper info for our next heartbeat */
  846. o2hb_prepare_block(reg, reg->hr_generation);
  847. ret = o2hb_issue_node_write(reg, &write_wc);
  848. if (ret < 0) {
  849. mlog_errno(ret);
  850. goto bail;
  851. }
  852. i = -1;
  853. while((i = find_next_bit(configured_nodes,
  854. O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES) {
  855. membership_change |= o2hb_check_slot(reg, &reg->hr_slots[i]);
  856. }
  857. /*
  858. * We have to be sure we've advertised ourselves on disk
  859. * before we can go to steady state. This ensures that
  860. * people we find in our steady state have seen us.
  861. */
  862. o2hb_wait_on_io(reg, &write_wc);
  863. if (write_wc.wc_error) {
  864. /* Do not re-arm the write timeout on I/O error - we
  865. * can't be sure that the new block ever made it to
  866. * disk */
  867. mlog(ML_ERROR, "Write error %d on device \"%s\"\n",
  868. write_wc.wc_error, reg->hr_dev_name);
  869. ret = write_wc.wc_error;
  870. goto bail;
  871. }
  872. /* Skip disarming the timeout if own slot has stale/bad data */
  873. if (own_slot_ok) {
  874. o2hb_set_quorum_device(reg);
  875. o2hb_arm_write_timeout(reg);
  876. }
  877. bail:
  878. /* let the person who launched us know when things are steady */
  879. if (atomic_read(&reg->hr_steady_iterations) != 0) {
  880. if (!ret && own_slot_ok && !membership_change) {
  881. if (atomic_dec_and_test(&reg->hr_steady_iterations))
  882. wake_up(&o2hb_steady_queue);
  883. }
  884. }
  885. if (atomic_read(&reg->hr_steady_iterations) != 0) {
  886. if (atomic_dec_and_test(&reg->hr_unsteady_iterations)) {
  887. printk(KERN_NOTICE "o2hb: Unable to stabilize "
  888. "heartbeart on region %s (%s)\n",
  889. config_item_name(&reg->hr_item),
  890. reg->hr_dev_name);
  891. atomic_set(&reg->hr_steady_iterations, 0);
  892. reg->hr_aborted_start = 1;
  893. wake_up(&o2hb_steady_queue);
  894. ret = -EIO;
  895. }
  896. }
  897. return ret;
  898. }
  899. /* Subtract b from a, storing the result in a. a *must* have a larger
  900. * value than b. */
  901. static void o2hb_tv_subtract(struct timeval *a,
  902. struct timeval *b)
  903. {
  904. /* just return 0 when a is after b */
  905. if (a->tv_sec < b->tv_sec ||
  906. (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) {
  907. a->tv_sec = 0;
  908. a->tv_usec = 0;
  909. return;
  910. }
  911. a->tv_sec -= b->tv_sec;
  912. a->tv_usec -= b->tv_usec;
  913. while ( a->tv_usec < 0 ) {
  914. a->tv_sec--;
  915. a->tv_usec += 1000000;
  916. }
  917. }
  918. static unsigned int o2hb_elapsed_msecs(struct timeval *start,
  919. struct timeval *end)
  920. {
  921. struct timeval res = *end;
  922. o2hb_tv_subtract(&res, start);
  923. return res.tv_sec * 1000 + res.tv_usec / 1000;
  924. }
  925. /*
  926. * we ride the region ref that the region dir holds. before the region
  927. * dir is removed and drops it ref it will wait to tear down this
  928. * thread.
  929. */
  930. static int o2hb_thread(void *data)
  931. {
  932. int i, ret;
  933. struct o2hb_region *reg = data;
  934. struct o2hb_bio_wait_ctxt write_wc;
  935. struct timeval before_hb, after_hb;
  936. unsigned int elapsed_msec;
  937. mlog(ML_HEARTBEAT|ML_KTHREAD, "hb thread running\n");
  938. set_user_nice(current, -20);
  939. /* Pin node */
  940. o2nm_depend_this_node();
  941. while (!kthread_should_stop() &&
  942. !reg->hr_unclean_stop && !reg->hr_aborted_start) {
  943. /* We track the time spent inside
  944. * o2hb_do_disk_heartbeat so that we avoid more than
  945. * hr_timeout_ms between disk writes. On busy systems
  946. * this should result in a heartbeat which is less
  947. * likely to time itself out. */
  948. do_gettimeofday(&before_hb);
  949. ret = o2hb_do_disk_heartbeat(reg);
  950. do_gettimeofday(&after_hb);
  951. elapsed_msec = o2hb_elapsed_msecs(&before_hb, &after_hb);
  952. mlog(ML_HEARTBEAT,
  953. "start = %lu.%lu, end = %lu.%lu, msec = %u\n",
  954. before_hb.tv_sec, (unsigned long) before_hb.tv_usec,
  955. after_hb.tv_sec, (unsigned long) after_hb.tv_usec,
  956. elapsed_msec);
  957. if (!kthread_should_stop() &&
  958. elapsed_msec < reg->hr_timeout_ms) {
  959. /* the kthread api has blocked signals for us so no
  960. * need to record the return value. */
  961. msleep_interruptible(reg->hr_timeout_ms - elapsed_msec);
  962. }
  963. }
  964. o2hb_disarm_write_timeout(reg);
  965. /* unclean stop is only used in very bad situation */
  966. for(i = 0; !reg->hr_unclean_stop && i < reg->hr_blocks; i++)
  967. o2hb_shutdown_slot(&reg->hr_slots[i]);
  968. /* Explicit down notification - avoid forcing the other nodes
  969. * to timeout on this region when we could just as easily
  970. * write a clear generation - thus indicating to them that
  971. * this node has left this region.
  972. */
  973. if (!reg->hr_unclean_stop && !reg->hr_aborted_start) {
  974. o2hb_prepare_block(reg, 0);
  975. ret = o2hb_issue_node_write(reg, &write_wc);
  976. if (ret == 0)
  977. o2hb_wait_on_io(reg, &write_wc);
  978. else
  979. mlog_errno(ret);
  980. }
  981. /* Unpin node */
  982. o2nm_undepend_this_node();
  983. mlog(ML_HEARTBEAT|ML_KTHREAD, "o2hb thread exiting\n");
  984. return 0;
  985. }
  986. #ifdef CONFIG_DEBUG_FS
  987. static int o2hb_debug_open(struct inode *inode, struct file *file)
  988. {
  989. struct o2hb_debug_buf *db = inode->i_private;
  990. struct o2hb_region *reg;
  991. unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  992. unsigned long lts;
  993. char *buf = NULL;
  994. int i = -1;
  995. int out = 0;
  996. /* max_nodes should be the largest bitmap we pass here */
  997. BUG_ON(sizeof(map) < db->db_size);
  998. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  999. if (!buf)
  1000. goto bail;
  1001. switch (db->db_type) {
  1002. case O2HB_DB_TYPE_LIVENODES:
  1003. case O2HB_DB_TYPE_LIVEREGIONS:
  1004. case O2HB_DB_TYPE_QUORUMREGIONS:
  1005. case O2HB_DB_TYPE_FAILEDREGIONS:
  1006. spin_lock(&o2hb_live_lock);
  1007. memcpy(map, db->db_data, db->db_size);
  1008. spin_unlock(&o2hb_live_lock);
  1009. break;
  1010. case O2HB_DB_TYPE_REGION_LIVENODES:
  1011. spin_lock(&o2hb_live_lock);
  1012. reg = (struct o2hb_region *)db->db_data;
  1013. memcpy(map, reg->hr_live_node_bitmap, db->db_size);
  1014. spin_unlock(&o2hb_live_lock);
  1015. break;
  1016. case O2HB_DB_TYPE_REGION_NUMBER:
  1017. reg = (struct o2hb_region *)db->db_data;
  1018. out += snprintf(buf + out, PAGE_SIZE - out, "%d\n",
  1019. reg->hr_region_num);
  1020. goto done;
  1021. case O2HB_DB_TYPE_REGION_ELAPSED_TIME:
  1022. reg = (struct o2hb_region *)db->db_data;
  1023. lts = reg->hr_last_timeout_start;
  1024. /* If 0, it has never been set before */
  1025. if (lts)
  1026. lts = jiffies_to_msecs(jiffies - lts);
  1027. out += snprintf(buf + out, PAGE_SIZE - out, "%lu\n", lts);
  1028. goto done;
  1029. case O2HB_DB_TYPE_REGION_PINNED:
  1030. reg = (struct o2hb_region *)db->db_data;
  1031. out += snprintf(buf + out, PAGE_SIZE - out, "%u\n",
  1032. !!reg->hr_item_pinned);
  1033. goto done;
  1034. default:
  1035. goto done;
  1036. }
  1037. while ((i = find_next_bit(map, db->db_len, i + 1)) < db->db_len)
  1038. out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
  1039. out += snprintf(buf + out, PAGE_SIZE - out, "\n");
  1040. done:
  1041. i_size_write(inode, out);
  1042. file->private_data = buf;
  1043. return 0;
  1044. bail:
  1045. return -ENOMEM;
  1046. }
  1047. static int o2hb_debug_release(struct inode *inode, struct file *file)
  1048. {
  1049. kfree(file->private_data);
  1050. return 0;
  1051. }
  1052. static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
  1053. size_t nbytes, loff_t *ppos)
  1054. {
  1055. return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
  1056. i_size_read(file->f_mapping->host));
  1057. }
  1058. #else
  1059. static int o2hb_debug_open(struct inode *inode, struct file *file)
  1060. {
  1061. return 0;
  1062. }
  1063. static int o2hb_debug_release(struct inode *inode, struct file *file)
  1064. {
  1065. return 0;
  1066. }
  1067. static ssize_t o2hb_debug_read(struct file *file, char __user *buf,
  1068. size_t nbytes, loff_t *ppos)
  1069. {
  1070. return 0;
  1071. }
  1072. #endif /* CONFIG_DEBUG_FS */
  1073. static const struct file_operations o2hb_debug_fops = {
  1074. .open = o2hb_debug_open,
  1075. .release = o2hb_debug_release,
  1076. .read = o2hb_debug_read,
  1077. .llseek = generic_file_llseek,
  1078. };
  1079. void o2hb_exit(void)
  1080. {
  1081. kfree(o2hb_db_livenodes);
  1082. kfree(o2hb_db_liveregions);
  1083. kfree(o2hb_db_quorumregions);
  1084. kfree(o2hb_db_failedregions);
  1085. debugfs_remove(o2hb_debug_failedregions);
  1086. debugfs_remove(o2hb_debug_quorumregions);
  1087. debugfs_remove(o2hb_debug_liveregions);
  1088. debugfs_remove(o2hb_debug_livenodes);
  1089. debugfs_remove(o2hb_debug_dir);
  1090. }
  1091. static struct dentry *o2hb_debug_create(const char *name, struct dentry *dir,
  1092. struct o2hb_debug_buf **db, int db_len,
  1093. int type, int size, int len, void *data)
  1094. {
  1095. *db = kmalloc(db_len, GFP_KERNEL);
  1096. if (!*db)
  1097. return NULL;
  1098. (*db)->db_type = type;
  1099. (*db)->db_size = size;
  1100. (*db)->db_len = len;
  1101. (*db)->db_data = data;
  1102. return debugfs_create_file(name, S_IFREG|S_IRUSR, dir, *db,
  1103. &o2hb_debug_fops);
  1104. }
  1105. static int o2hb_debug_init(void)
  1106. {
  1107. int ret = -ENOMEM;
  1108. o2hb_debug_dir = debugfs_create_dir(O2HB_DEBUG_DIR, NULL);
  1109. if (!o2hb_debug_dir) {
  1110. mlog_errno(ret);
  1111. goto bail;
  1112. }
  1113. o2hb_debug_livenodes = o2hb_debug_create(O2HB_DEBUG_LIVENODES,
  1114. o2hb_debug_dir,
  1115. &o2hb_db_livenodes,
  1116. sizeof(*o2hb_db_livenodes),
  1117. O2HB_DB_TYPE_LIVENODES,
  1118. sizeof(o2hb_live_node_bitmap),
  1119. O2NM_MAX_NODES,
  1120. o2hb_live_node_bitmap);
  1121. if (!o2hb_debug_livenodes) {
  1122. mlog_errno(ret);
  1123. goto bail;
  1124. }
  1125. o2hb_debug_liveregions = o2hb_debug_create(O2HB_DEBUG_LIVEREGIONS,
  1126. o2hb_debug_dir,
  1127. &o2hb_db_liveregions,
  1128. sizeof(*o2hb_db_liveregions),
  1129. O2HB_DB_TYPE_LIVEREGIONS,
  1130. sizeof(o2hb_live_region_bitmap),
  1131. O2NM_MAX_REGIONS,
  1132. o2hb_live_region_bitmap);
  1133. if (!o2hb_debug_liveregions) {
  1134. mlog_errno(ret);
  1135. goto bail;
  1136. }
  1137. o2hb_debug_quorumregions =
  1138. o2hb_debug_create(O2HB_DEBUG_QUORUMREGIONS,
  1139. o2hb_debug_dir,
  1140. &o2hb_db_quorumregions,
  1141. sizeof(*o2hb_db_quorumregions),
  1142. O2HB_DB_TYPE_QUORUMREGIONS,
  1143. sizeof(o2hb_quorum_region_bitmap),
  1144. O2NM_MAX_REGIONS,
  1145. o2hb_quorum_region_bitmap);
  1146. if (!o2hb_debug_quorumregions) {
  1147. mlog_errno(ret);
  1148. goto bail;
  1149. }
  1150. o2hb_debug_failedregions =
  1151. o2hb_debug_create(O2HB_DEBUG_FAILEDREGIONS,
  1152. o2hb_debug_dir,
  1153. &o2hb_db_failedregions,
  1154. sizeof(*o2hb_db_failedregions),
  1155. O2HB_DB_TYPE_FAILEDREGIONS,
  1156. sizeof(o2hb_failed_region_bitmap),
  1157. O2NM_MAX_REGIONS,
  1158. o2hb_failed_region_bitmap);
  1159. if (!o2hb_debug_failedregions) {
  1160. mlog_errno(ret);
  1161. goto bail;
  1162. }
  1163. ret = 0;
  1164. bail:
  1165. if (ret)
  1166. o2hb_exit();
  1167. return ret;
  1168. }
  1169. int o2hb_init(void)
  1170. {
  1171. int i;
  1172. for (i = 0; i < ARRAY_SIZE(o2hb_callbacks); i++)
  1173. INIT_LIST_HEAD(&o2hb_callbacks[i].list);
  1174. for (i = 0; i < ARRAY_SIZE(o2hb_live_slots); i++)
  1175. INIT_LIST_HEAD(&o2hb_live_slots[i]);
  1176. INIT_LIST_HEAD(&o2hb_node_events);
  1177. memset(o2hb_live_node_bitmap, 0, sizeof(o2hb_live_node_bitmap));
  1178. memset(o2hb_region_bitmap, 0, sizeof(o2hb_region_bitmap));
  1179. memset(o2hb_live_region_bitmap, 0, sizeof(o2hb_live_region_bitmap));
  1180. memset(o2hb_quorum_region_bitmap, 0, sizeof(o2hb_quorum_region_bitmap));
  1181. memset(o2hb_failed_region_bitmap, 0, sizeof(o2hb_failed_region_bitmap));
  1182. o2hb_dependent_users = 0;
  1183. return o2hb_debug_init();
  1184. }
  1185. /* if we're already in a callback then we're already serialized by the sem */
  1186. static void o2hb_fill_node_map_from_callback(unsigned long *map,
  1187. unsigned bytes)
  1188. {
  1189. BUG_ON(bytes < (BITS_TO_LONGS(O2NM_MAX_NODES) * sizeof(unsigned long)));
  1190. memcpy(map, &o2hb_live_node_bitmap, bytes);
  1191. }
  1192. /*
  1193. * get a map of all nodes that are heartbeating in any regions
  1194. */
  1195. void o2hb_fill_node_map(unsigned long *map, unsigned bytes)
  1196. {
  1197. /* callers want to serialize this map and callbacks so that they
  1198. * can trust that they don't miss nodes coming to the party */
  1199. down_read(&o2hb_callback_sem);
  1200. spin_lock(&o2hb_live_lock);
  1201. o2hb_fill_node_map_from_callback(map, bytes);
  1202. spin_unlock(&o2hb_live_lock);
  1203. up_read(&o2hb_callback_sem);
  1204. }
  1205. EXPORT_SYMBOL_GPL(o2hb_fill_node_map);
  1206. /*
  1207. * heartbeat configfs bits. The heartbeat set is a default set under
  1208. * the cluster set in nodemanager.c.
  1209. */
  1210. static struct o2hb_region *to_o2hb_region(struct config_item *item)
  1211. {
  1212. return item ? container_of(item, struct o2hb_region, hr_item) : NULL;
  1213. }
  1214. /* drop_item only drops its ref after killing the thread, nothing should
  1215. * be using the region anymore. this has to clean up any state that
  1216. * attributes might have built up. */
  1217. static void o2hb_region_release(struct config_item *item)
  1218. {
  1219. int i;
  1220. struct page *page;
  1221. struct o2hb_region *reg = to_o2hb_region(item);
  1222. mlog(ML_HEARTBEAT, "hb region release (%s)\n", reg->hr_dev_name);
  1223. kfree(reg->hr_tmp_block);
  1224. if (reg->hr_slot_data) {
  1225. for (i = 0; i < reg->hr_num_pages; i++) {
  1226. page = reg->hr_slot_data[i];
  1227. if (page)
  1228. __free_page(page);
  1229. }
  1230. kfree(reg->hr_slot_data);
  1231. }
  1232. if (reg->hr_bdev)
  1233. blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
  1234. kfree(reg->hr_slots);
  1235. kfree(reg->hr_db_regnum);
  1236. kfree(reg->hr_db_livenodes);
  1237. debugfs_remove(reg->hr_debug_livenodes);
  1238. debugfs_remove(reg->hr_debug_regnum);
  1239. debugfs_remove(reg->hr_debug_elapsed_time);
  1240. debugfs_remove(reg->hr_debug_pinned);
  1241. debugfs_remove(reg->hr_debug_dir);
  1242. spin_lock(&o2hb_live_lock);
  1243. list_del(&reg->hr_all_item);
  1244. spin_unlock(&o2hb_live_lock);
  1245. kfree(reg);
  1246. }
  1247. static int o2hb_read_block_input(struct o2hb_region *reg,
  1248. const char *page,
  1249. size_t count,
  1250. unsigned long *ret_bytes,
  1251. unsigned int *ret_bits)
  1252. {
  1253. unsigned long bytes;
  1254. char *p = (char *)page;
  1255. bytes = simple_strtoul(p, &p, 0);
  1256. if (!p || (*p && (*p != '\n')))
  1257. return -EINVAL;
  1258. /* Heartbeat and fs min / max block sizes are the same. */
  1259. if (bytes > 4096 || bytes < 512)
  1260. return -ERANGE;
  1261. if (hweight16(bytes) != 1)
  1262. return -EINVAL;
  1263. if (ret_bytes)
  1264. *ret_bytes = bytes;
  1265. if (ret_bits)
  1266. *ret_bits = ffs(bytes) - 1;
  1267. return 0;
  1268. }
  1269. static ssize_t o2hb_region_block_bytes_read(struct o2hb_region *reg,
  1270. char *page)
  1271. {
  1272. return sprintf(page, "%u\n", reg->hr_block_bytes);
  1273. }
  1274. static ssize_t o2hb_region_block_bytes_write(struct o2hb_region *reg,
  1275. const char *page,
  1276. size_t count)
  1277. {
  1278. int status;
  1279. unsigned long block_bytes;
  1280. unsigned int block_bits;
  1281. if (reg->hr_bdev)
  1282. return -EINVAL;
  1283. status = o2hb_read_block_input(reg, page, count,
  1284. &block_bytes, &block_bits);
  1285. if (status)
  1286. return status;
  1287. reg->hr_block_bytes = (unsigned int)block_bytes;
  1288. reg->hr_block_bits = block_bits;
  1289. return count;
  1290. }
  1291. static ssize_t o2hb_region_start_block_read(struct o2hb_region *reg,
  1292. char *page)
  1293. {
  1294. return sprintf(page, "%llu\n", reg->hr_start_block);
  1295. }
  1296. static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
  1297. const char *page,
  1298. size_t count)
  1299. {
  1300. unsigned long long tmp;
  1301. char *p = (char *)page;
  1302. if (reg->hr_bdev)
  1303. return -EINVAL;
  1304. tmp = simple_strtoull(p, &p, 0);
  1305. if (!p || (*p && (*p != '\n')))
  1306. return -EINVAL;
  1307. reg->hr_start_block = tmp;
  1308. return count;
  1309. }
  1310. static ssize_t o2hb_region_blocks_read(struct o2hb_region *reg,
  1311. char *page)
  1312. {
  1313. return sprintf(page, "%d\n", reg->hr_blocks);
  1314. }
  1315. static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
  1316. const char *page,
  1317. size_t count)
  1318. {
  1319. unsigned long tmp;
  1320. char *p = (char *)page;
  1321. if (reg->hr_bdev)
  1322. return -EINVAL;
  1323. tmp = simple_strtoul(p, &p, 0);
  1324. if (!p || (*p && (*p != '\n')))
  1325. return -EINVAL;
  1326. if (tmp > O2NM_MAX_NODES || tmp == 0)
  1327. return -ERANGE;
  1328. reg->hr_blocks = (unsigned int)tmp;
  1329. return count;
  1330. }
  1331. static ssize_t o2hb_region_dev_read(struct o2hb_region *reg,
  1332. char *page)
  1333. {
  1334. unsigned int ret = 0;
  1335. if (reg->hr_bdev)
  1336. ret = sprintf(page, "%s\n", reg->hr_dev_name);
  1337. return ret;
  1338. }
  1339. static void o2hb_init_region_params(struct o2hb_region *reg)
  1340. {
  1341. reg->hr_slots_per_page = PAGE_CACHE_SIZE >> reg->hr_block_bits;
  1342. reg->hr_timeout_ms = O2HB_REGION_TIMEOUT_MS;
  1343. mlog(ML_HEARTBEAT, "hr_start_block = %llu, hr_blocks = %u\n",
  1344. reg->hr_start_block, reg->hr_blocks);
  1345. mlog(ML_HEARTBEAT, "hr_block_bytes = %u, hr_block_bits = %u\n",
  1346. reg->hr_block_bytes, reg->hr_block_bits);
  1347. mlog(ML_HEARTBEAT, "hr_timeout_ms = %u\n", reg->hr_timeout_ms);
  1348. mlog(ML_HEARTBEAT, "dead threshold = %u\n", o2hb_dead_threshold);
  1349. }
  1350. static int o2hb_map_slot_data(struct o2hb_region *reg)
  1351. {
  1352. int i, j;
  1353. unsigned int last_slot;
  1354. unsigned int spp = reg->hr_slots_per_page;
  1355. struct page *page;
  1356. char *raw;
  1357. struct o2hb_disk_slot *slot;
  1358. reg->hr_tmp_block = kmalloc(reg->hr_block_bytes, GFP_KERNEL);
  1359. if (reg->hr_tmp_block == NULL) {
  1360. mlog_errno(-ENOMEM);
  1361. return -ENOMEM;
  1362. }
  1363. reg->hr_slots = kcalloc(reg->hr_blocks,
  1364. sizeof(struct o2hb_disk_slot), GFP_KERNEL);
  1365. if (reg->hr_slots == NULL) {
  1366. mlog_errno(-ENOMEM);
  1367. return -ENOMEM;
  1368. }
  1369. for(i = 0; i < reg->hr_blocks; i++) {
  1370. slot = &reg->hr_slots[i];
  1371. slot->ds_node_num = i;
  1372. INIT_LIST_HEAD(&slot->ds_live_item);
  1373. slot->ds_raw_block = NULL;
  1374. }
  1375. reg->hr_num_pages = (reg->hr_blocks + spp - 1) / spp;
  1376. mlog(ML_HEARTBEAT, "Going to require %u pages to cover %u blocks "
  1377. "at %u blocks per page\n",
  1378. reg->hr_num_pages, reg->hr_blocks, spp);
  1379. reg->hr_slot_data = kcalloc(reg->hr_num_pages, sizeof(struct page *),
  1380. GFP_KERNEL);
  1381. if (!reg->hr_slot_data) {
  1382. mlog_errno(-ENOMEM);
  1383. return -ENOMEM;
  1384. }
  1385. for(i = 0; i < reg->hr_num_pages; i++) {
  1386. page = alloc_page(GFP_KERNEL);
  1387. if (!page) {
  1388. mlog_errno(-ENOMEM);
  1389. return -ENOMEM;
  1390. }
  1391. reg->hr_slot_data[i] = page;
  1392. last_slot = i * spp;
  1393. raw = page_address(page);
  1394. for (j = 0;
  1395. (j < spp) && ((j + last_slot) < reg->hr_blocks);
  1396. j++) {
  1397. BUG_ON((j + last_slot) >= reg->hr_blocks);
  1398. slot = &reg->hr_slots[j + last_slot];
  1399. slot->ds_raw_block =
  1400. (struct o2hb_disk_heartbeat_block *) raw;
  1401. raw += reg->hr_block_bytes;
  1402. }
  1403. }
  1404. return 0;
  1405. }
  1406. /* Read in all the slots available and populate the tracking
  1407. * structures so that we can start with a baseline idea of what's
  1408. * there. */
  1409. static int o2hb_populate_slot_data(struct o2hb_region *reg)
  1410. {
  1411. int ret, i;
  1412. struct o2hb_disk_slot *slot;
  1413. struct o2hb_disk_heartbeat_block *hb_block;
  1414. ret = o2hb_read_slots(reg, reg->hr_blocks);
  1415. if (ret) {
  1416. mlog_errno(ret);
  1417. goto out;
  1418. }
  1419. /* We only want to get an idea of the values initially in each
  1420. * slot, so we do no verification - o2hb_check_slot will
  1421. * actually determine if each configured slot is valid and
  1422. * whether any values have changed. */
  1423. for(i = 0; i < reg->hr_blocks; i++) {
  1424. slot = &reg->hr_slots[i];
  1425. hb_block = (struct o2hb_disk_heartbeat_block *) slot->ds_raw_block;
  1426. /* Only fill the values that o2hb_check_slot uses to
  1427. * determine changing slots */
  1428. slot->ds_last_time = le64_to_cpu(hb_block->hb_seq);
  1429. slot->ds_last_generation = le64_to_cpu(hb_block->hb_generation);
  1430. }
  1431. out:
  1432. return ret;
  1433. }
  1434. /* this is acting as commit; we set up all of hr_bdev and hr_task or nothing */
  1435. static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
  1436. const char *page,
  1437. size_t count)
  1438. {
  1439. struct task_struct *hb_task;
  1440. long fd;
  1441. int sectsize;
  1442. char *p = (char *)page;
  1443. struct fd f;
  1444. struct inode *inode;
  1445. ssize_t ret = -EINVAL;
  1446. int live_threshold;
  1447. if (reg->hr_bdev)
  1448. goto out;
  1449. /* We can't heartbeat without having had our node number
  1450. * configured yet. */
  1451. if (o2nm_this_node() == O2NM_MAX_NODES)
  1452. goto out;
  1453. fd = simple_strtol(p, &p, 0);
  1454. if (!p || (*p && (*p != '\n')))
  1455. goto out;
  1456. if (fd < 0 || fd >= INT_MAX)
  1457. goto out;
  1458. f = fdget(fd);
  1459. if (f.file == NULL)
  1460. goto out;
  1461. if (reg->hr_blocks == 0 || reg->hr_start_block == 0 ||
  1462. reg->hr_block_bytes == 0)
  1463. goto out2;
  1464. inode = igrab(f.file->f_mapping->host);
  1465. if (inode == NULL)
  1466. goto out2;
  1467. if (!S_ISBLK(inode->i_mode))
  1468. goto out3;
  1469. reg->hr_bdev = I_BDEV(f.file->f_mapping->host);
  1470. ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ, NULL);
  1471. if (ret) {
  1472. reg->hr_bdev = NULL;
  1473. goto out3;
  1474. }
  1475. inode = NULL;
  1476. bdevname(reg->hr_bdev, reg->hr_dev_name);
  1477. sectsize = bdev_logical_block_size(reg->hr_bdev);
  1478. if (sectsize != reg->hr_block_bytes) {
  1479. mlog(ML_ERROR,
  1480. "blocksize %u incorrect for device, expected %d",
  1481. reg->hr_block_bytes, sectsize);
  1482. ret = -EINVAL;
  1483. goto out3;
  1484. }
  1485. o2hb_init_region_params(reg);
  1486. /* Generation of zero is invalid */
  1487. do {
  1488. get_random_bytes(&reg->hr_generation,
  1489. sizeof(reg->hr_generation));
  1490. } while (reg->hr_generation == 0);
  1491. ret = o2hb_map_slot_data(reg);
  1492. if (ret) {
  1493. mlog_errno(ret);
  1494. goto out3;
  1495. }
  1496. ret = o2hb_populate_slot_data(reg);
  1497. if (ret) {
  1498. mlog_errno(ret);
  1499. goto out3;
  1500. }
  1501. INIT_DELAYED_WORK(&reg->hr_write_timeout_work, o2hb_write_timeout);
  1502. /*
  1503. * A node is considered live after it has beat LIVE_THRESHOLD
  1504. * times. We're not steady until we've given them a chance
  1505. * _after_ our first read.
  1506. * The default threshold is bare minimum so as to limit the delay
  1507. * during mounts. For global heartbeat, the threshold doubled for the
  1508. * first region.
  1509. */
  1510. live_threshold = O2HB_LIVE_THRESHOLD;
  1511. if (o2hb_global_heartbeat_active()) {
  1512. spin_lock(&o2hb_live_lock);
  1513. if (o2hb_pop_count(&o2hb_region_bitmap, O2NM_MAX_REGIONS) == 1)
  1514. live_threshold <<= 1;
  1515. spin_unlock(&o2hb_live_lock);
  1516. }
  1517. ++live_threshold;
  1518. atomic_set(&reg->hr_steady_iterations, live_threshold);
  1519. /* unsteady_iterations is double the steady_iterations */
  1520. atomic_set(&reg->hr_unsteady_iterations, (live_threshold << 1));
  1521. hb_task = kthread_run(o2hb_thread, reg, "o2hb-%s",
  1522. reg->hr_item.ci_name);
  1523. if (IS_ERR(hb_task)) {
  1524. ret = PTR_ERR(hb_task);
  1525. mlog_errno(ret);
  1526. goto out3;
  1527. }
  1528. spin_lock(&o2hb_live_lock);
  1529. reg->hr_task = hb_task;
  1530. spin_unlock(&o2hb_live_lock);
  1531. ret = wait_event_interruptible(o2hb_steady_queue,
  1532. atomic_read(&reg->hr_steady_iterations) == 0);
  1533. if (ret) {
  1534. atomic_set(&reg->hr_steady_iterations, 0);
  1535. reg->hr_aborted_start = 1;
  1536. }
  1537. if (reg->hr_aborted_start) {
  1538. ret = -EIO;
  1539. goto out3;
  1540. }
  1541. /* Ok, we were woken. Make sure it wasn't by drop_item() */
  1542. spin_lock(&o2hb_live_lock);
  1543. hb_task = reg->hr_task;
  1544. if (o2hb_global_heartbeat_active())
  1545. set_bit(reg->hr_region_num, o2hb_live_region_bitmap);
  1546. spin_unlock(&o2hb_live_lock);
  1547. if (hb_task)
  1548. ret = count;
  1549. else
  1550. ret = -EIO;
  1551. if (hb_task && o2hb_global_heartbeat_active())
  1552. printk(KERN_NOTICE "o2hb: Heartbeat started on region %s (%s)\n",
  1553. config_item_name(&reg->hr_item), reg->hr_dev_name);
  1554. out3:
  1555. iput(inode);
  1556. out2:
  1557. fdput(f);
  1558. out:
  1559. if (ret < 0) {
  1560. if (reg->hr_bdev) {
  1561. blkdev_put(reg->hr_bdev, FMODE_READ|FMODE_WRITE);
  1562. reg->hr_bdev = NULL;
  1563. }
  1564. }
  1565. return ret;
  1566. }
  1567. static ssize_t o2hb_region_pid_read(struct o2hb_region *reg,
  1568. char *page)
  1569. {
  1570. pid_t pid = 0;
  1571. spin_lock(&o2hb_live_lock);
  1572. if (reg->hr_task)
  1573. pid = task_pid_nr(reg->hr_task);
  1574. spin_unlock(&o2hb_live_lock);
  1575. if (!pid)
  1576. return 0;
  1577. return sprintf(page, "%u\n", pid);
  1578. }
  1579. struct o2hb_region_attribute {
  1580. struct configfs_attribute attr;
  1581. ssize_t (*show)(struct o2hb_region *, char *);
  1582. ssize_t (*store)(struct o2hb_region *, const char *, size_t);
  1583. };
  1584. static struct o2hb_region_attribute o2hb_region_attr_block_bytes = {
  1585. .attr = { .ca_owner = THIS_MODULE,
  1586. .ca_name = "block_bytes",
  1587. .ca_mode = S_IRUGO | S_IWUSR },
  1588. .show = o2hb_region_block_bytes_read,
  1589. .store = o2hb_region_block_bytes_write,
  1590. };
  1591. static struct o2hb_region_attribute o2hb_region_attr_start_block = {
  1592. .attr = { .ca_owner = THIS_MODULE,
  1593. .ca_name = "start_block",
  1594. .ca_mode = S_IRUGO | S_IWUSR },
  1595. .show = o2hb_region_start_block_read,
  1596. .store = o2hb_region_start_block_write,
  1597. };
  1598. static struct o2hb_region_attribute o2hb_region_attr_blocks = {
  1599. .attr = { .ca_owner = THIS_MODULE,
  1600. .ca_name = "blocks",
  1601. .ca_mode = S_IRUGO | S_IWUSR },
  1602. .show = o2hb_region_blocks_read,
  1603. .store = o2hb_region_blocks_write,
  1604. };
  1605. static struct o2hb_region_attribute o2hb_region_attr_dev = {
  1606. .attr = { .ca_owner = THIS_MODULE,
  1607. .ca_name = "dev",
  1608. .ca_mode = S_IRUGO | S_IWUSR },
  1609. .show = o2hb_region_dev_read,
  1610. .store = o2hb_region_dev_write,
  1611. };
  1612. static struct o2hb_region_attribute o2hb_region_attr_pid = {
  1613. .attr = { .ca_owner = THIS_MODULE,
  1614. .ca_name = "pid",
  1615. .ca_mode = S_IRUGO | S_IRUSR },
  1616. .show = o2hb_region_pid_read,
  1617. };
  1618. static struct configfs_attribute *o2hb_region_attrs[] = {
  1619. &o2hb_region_attr_block_bytes.attr,
  1620. &o2hb_region_attr_start_block.attr,
  1621. &o2hb_region_attr_blocks.attr,
  1622. &o2hb_region_attr_dev.attr,
  1623. &o2hb_region_attr_pid.attr,
  1624. NULL,
  1625. };
  1626. static ssize_t o2hb_region_show(struct config_item *item,
  1627. struct configfs_attribute *attr,
  1628. char *page)
  1629. {
  1630. struct o2hb_region *reg = to_o2hb_region(item);
  1631. struct o2hb_region_attribute *o2hb_region_attr =
  1632. container_of(attr, struct o2hb_region_attribute, attr);
  1633. ssize_t ret = 0;
  1634. if (o2hb_region_attr->show)
  1635. ret = o2hb_region_attr->show(reg, page);
  1636. return ret;
  1637. }
  1638. static ssize_t o2hb_region_store(struct config_item *item,
  1639. struct configfs_attribute *attr,
  1640. const char *page, size_t count)
  1641. {
  1642. struct o2hb_region *reg = to_o2hb_region(item);
  1643. struct o2hb_region_attribute *o2hb_region_attr =
  1644. container_of(attr, struct o2hb_region_attribute, attr);
  1645. ssize_t ret = -EINVAL;
  1646. if (o2hb_region_attr->store)
  1647. ret = o2hb_region_attr->store(reg, page, count);
  1648. return ret;
  1649. }
  1650. static struct configfs_item_operations o2hb_region_item_ops = {
  1651. .release = o2hb_region_release,
  1652. .show_attribute = o2hb_region_show,
  1653. .store_attribute = o2hb_region_store,
  1654. };
  1655. static struct config_item_type o2hb_region_type = {
  1656. .ct_item_ops = &o2hb_region_item_ops,
  1657. .ct_attrs = o2hb_region_attrs,
  1658. .ct_owner = THIS_MODULE,
  1659. };
  1660. /* heartbeat set */
  1661. struct o2hb_heartbeat_group {
  1662. struct config_group hs_group;
  1663. /* some stuff? */
  1664. };
  1665. static struct o2hb_heartbeat_group *to_o2hb_heartbeat_group(struct config_group *group)
  1666. {
  1667. return group ?
  1668. container_of(group, struct o2hb_heartbeat_group, hs_group)
  1669. : NULL;
  1670. }
  1671. static int o2hb_debug_region_init(struct o2hb_region *reg, struct dentry *dir)
  1672. {
  1673. int ret = -ENOMEM;
  1674. reg->hr_debug_dir =
  1675. debugfs_create_dir(config_item_name(&reg->hr_item), dir);
  1676. if (!reg->hr_debug_dir) {
  1677. mlog_errno(ret);
  1678. goto bail;
  1679. }
  1680. reg->hr_debug_livenodes =
  1681. o2hb_debug_create(O2HB_DEBUG_LIVENODES,
  1682. reg->hr_debug_dir,
  1683. &(reg->hr_db_livenodes),
  1684. sizeof(*(reg->hr_db_livenodes)),
  1685. O2HB_DB_TYPE_REGION_LIVENODES,
  1686. sizeof(reg->hr_live_node_bitmap),
  1687. O2NM_MAX_NODES, reg);
  1688. if (!reg->hr_debug_livenodes) {
  1689. mlog_errno(ret);
  1690. goto bail;
  1691. }
  1692. reg->hr_debug_regnum =
  1693. o2hb_debug_create(O2HB_DEBUG_REGION_NUMBER,
  1694. reg->hr_debug_dir,
  1695. &(reg->hr_db_regnum),
  1696. sizeof(*(reg->hr_db_regnum)),
  1697. O2HB_DB_TYPE_REGION_NUMBER,
  1698. 0, O2NM_MAX_NODES, reg);
  1699. if (!reg->hr_debug_regnum) {
  1700. mlog_errno(ret);
  1701. goto bail;
  1702. }
  1703. reg->hr_debug_elapsed_time =
  1704. o2hb_debug_create(O2HB_DEBUG_REGION_ELAPSED_TIME,
  1705. reg->hr_debug_dir,
  1706. &(reg->hr_db_elapsed_time),
  1707. sizeof(*(reg->hr_db_elapsed_time)),
  1708. O2HB_DB_TYPE_REGION_ELAPSED_TIME,
  1709. 0, 0, reg);
  1710. if (!reg->hr_debug_elapsed_time) {
  1711. mlog_errno(ret);
  1712. goto bail;
  1713. }
  1714. reg->hr_debug_pinned =
  1715. o2hb_debug_create(O2HB_DEBUG_REGION_PINNED,
  1716. reg->hr_debug_dir,
  1717. &(reg->hr_db_pinned),
  1718. sizeof(*(reg->hr_db_pinned)),
  1719. O2HB_DB_TYPE_REGION_PINNED,
  1720. 0, 0, reg);
  1721. if (!reg->hr_debug_pinned) {
  1722. mlog_errno(ret);
  1723. goto bail;
  1724. }
  1725. ret = 0;
  1726. bail:
  1727. return ret;
  1728. }
  1729. static struct config_item *o2hb_heartbeat_group_make_item(struct config_group *group,
  1730. const char *name)
  1731. {
  1732. struct o2hb_region *reg = NULL;
  1733. int ret;
  1734. reg = kzalloc(sizeof(struct o2hb_region), GFP_KERNEL);
  1735. if (reg == NULL)
  1736. return ERR_PTR(-ENOMEM);
  1737. if (strlen(name) > O2HB_MAX_REGION_NAME_LEN) {
  1738. ret = -ENAMETOOLONG;
  1739. goto free;
  1740. }
  1741. spin_lock(&o2hb_live_lock);
  1742. reg->hr_region_num = 0;
  1743. if (o2hb_global_heartbeat_active()) {
  1744. reg->hr_region_num = find_first_zero_bit(o2hb_region_bitmap,
  1745. O2NM_MAX_REGIONS);
  1746. if (reg->hr_region_num >= O2NM_MAX_REGIONS) {
  1747. spin_unlock(&o2hb_live_lock);
  1748. ret = -EFBIG;
  1749. goto free;
  1750. }
  1751. set_bit(reg->hr_region_num, o2hb_region_bitmap);
  1752. }
  1753. list_add_tail(&reg->hr_all_item, &o2hb_all_regions);
  1754. spin_unlock(&o2hb_live_lock);
  1755. config_item_init_type_name(&reg->hr_item, name, &o2hb_region_type);
  1756. ret = o2hb_debug_region_init(reg, o2hb_debug_dir);
  1757. if (ret) {
  1758. config_item_put(&reg->hr_item);
  1759. goto free;
  1760. }
  1761. return &reg->hr_item;
  1762. free:
  1763. kfree(reg);
  1764. return ERR_PTR(ret);
  1765. }
  1766. static void o2hb_heartbeat_group_drop_item(struct config_group *group,
  1767. struct config_item *item)
  1768. {
  1769. struct task_struct *hb_task;
  1770. struct o2hb_region *reg = to_o2hb_region(item);
  1771. int quorum_region = 0;
  1772. /* stop the thread when the user removes the region dir */
  1773. spin_lock(&o2hb_live_lock);
  1774. hb_task = reg->hr_task;
  1775. reg->hr_task = NULL;
  1776. reg->hr_item_dropped = 1;
  1777. spin_unlock(&o2hb_live_lock);
  1778. if (hb_task)
  1779. kthread_stop(hb_task);
  1780. if (o2hb_global_heartbeat_active()) {
  1781. spin_lock(&o2hb_live_lock);
  1782. clear_bit(reg->hr_region_num, o2hb_region_bitmap);
  1783. clear_bit(reg->hr_region_num, o2hb_live_region_bitmap);
  1784. if (test_bit(reg->hr_region_num, o2hb_quorum_region_bitmap))
  1785. quorum_region = 1;
  1786. clear_bit(reg->hr_region_num, o2hb_quorum_region_bitmap);
  1787. spin_unlock(&o2hb_live_lock);
  1788. printk(KERN_NOTICE "o2hb: Heartbeat %s on region %s (%s)\n",
  1789. ((atomic_read(&reg->hr_steady_iterations) == 0) ?
  1790. "stopped" : "start aborted"), config_item_name(item),
  1791. reg->hr_dev_name);
  1792. }
  1793. /*
  1794. * If we're racing a dev_write(), we need to wake them. They will
  1795. * check reg->hr_task
  1796. */
  1797. if (atomic_read(&reg->hr_steady_iterations) != 0) {
  1798. reg->hr_aborted_start = 1;
  1799. atomic_set(&reg->hr_steady_iterations, 0);
  1800. wake_up(&o2hb_steady_queue);
  1801. }
  1802. config_item_put(item);
  1803. if (!o2hb_global_heartbeat_active() || !quorum_region)
  1804. return;
  1805. /*
  1806. * If global heartbeat active and there are dependent users,
  1807. * pin all regions if quorum region count <= CUT_OFF
  1808. */
  1809. spin_lock(&o2hb_live_lock);
  1810. if (!o2hb_dependent_users)
  1811. goto unlock;
  1812. if (o2hb_pop_count(&o2hb_quorum_region_bitmap,
  1813. O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
  1814. o2hb_region_pin(NULL);
  1815. unlock:
  1816. spin_unlock(&o2hb_live_lock);
  1817. }
  1818. struct o2hb_heartbeat_group_attribute {
  1819. struct configfs_attribute attr;
  1820. ssize_t (*show)(struct o2hb_heartbeat_group *, char *);
  1821. ssize_t (*store)(struct o2hb_heartbeat_group *, const char *, size_t);
  1822. };
  1823. static ssize_t o2hb_heartbeat_group_show(struct config_item *item,
  1824. struct configfs_attribute *attr,
  1825. char *page)
  1826. {
  1827. struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
  1828. struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
  1829. container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
  1830. ssize_t ret = 0;
  1831. if (o2hb_heartbeat_group_attr->show)
  1832. ret = o2hb_heartbeat_group_attr->show(reg, page);
  1833. return ret;
  1834. }
  1835. static ssize_t o2hb_heartbeat_group_store(struct config_item *item,
  1836. struct configfs_attribute *attr,
  1837. const char *page, size_t count)
  1838. {
  1839. struct o2hb_heartbeat_group *reg = to_o2hb_heartbeat_group(to_config_group(item));
  1840. struct o2hb_heartbeat_group_attribute *o2hb_heartbeat_group_attr =
  1841. container_of(attr, struct o2hb_heartbeat_group_attribute, attr);
  1842. ssize_t ret = -EINVAL;
  1843. if (o2hb_heartbeat_group_attr->store)
  1844. ret = o2hb_heartbeat_group_attr->store(reg, page, count);
  1845. return ret;
  1846. }
  1847. static ssize_t o2hb_heartbeat_group_threshold_show(struct o2hb_heartbeat_group *group,
  1848. char *page)
  1849. {
  1850. return sprintf(page, "%u\n", o2hb_dead_threshold);
  1851. }
  1852. static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group *group,
  1853. const char *page,
  1854. size_t count)
  1855. {
  1856. unsigned long tmp;
  1857. char *p = (char *)page;
  1858. tmp = simple_strtoul(p, &p, 10);
  1859. if (!p || (*p && (*p != '\n')))
  1860. return -EINVAL;
  1861. /* this will validate ranges for us. */
  1862. o2hb_dead_threshold_set((unsigned int) tmp);
  1863. return count;
  1864. }
  1865. static
  1866. ssize_t o2hb_heartbeat_group_mode_show(struct o2hb_heartbeat_group *group,
  1867. char *page)
  1868. {
  1869. return sprintf(page, "%s\n",
  1870. o2hb_heartbeat_mode_desc[o2hb_heartbeat_mode]);
  1871. }
  1872. static
  1873. ssize_t o2hb_heartbeat_group_mode_store(struct o2hb_heartbeat_group *group,
  1874. const char *page, size_t count)
  1875. {
  1876. unsigned int i;
  1877. int ret;
  1878. size_t len;
  1879. len = (page[count - 1] == '\n') ? count - 1 : count;
  1880. if (!len)
  1881. return -EINVAL;
  1882. for (i = 0; i < O2HB_HEARTBEAT_NUM_MODES; ++i) {
  1883. if (strnicmp(page, o2hb_heartbeat_mode_desc[i], len))
  1884. continue;
  1885. ret = o2hb_global_heartbeat_mode_set(i);
  1886. if (!ret)
  1887. printk(KERN_NOTICE "o2hb: Heartbeat mode set to %s\n",
  1888. o2hb_heartbeat_mode_desc[i]);
  1889. return count;
  1890. }
  1891. return -EINVAL;
  1892. }
  1893. static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_threshold = {
  1894. .attr = { .ca_owner = THIS_MODULE,
  1895. .ca_name = "dead_threshold",
  1896. .ca_mode = S_IRUGO | S_IWUSR },
  1897. .show = o2hb_heartbeat_group_threshold_show,
  1898. .store = o2hb_heartbeat_group_threshold_store,
  1899. };
  1900. static struct o2hb_heartbeat_group_attribute o2hb_heartbeat_group_attr_mode = {
  1901. .attr = { .ca_owner = THIS_MODULE,
  1902. .ca_name = "mode",
  1903. .ca_mode = S_IRUGO | S_IWUSR },
  1904. .show = o2hb_heartbeat_group_mode_show,
  1905. .store = o2hb_heartbeat_group_mode_store,
  1906. };
  1907. static struct configfs_attribute *o2hb_heartbeat_group_attrs[] = {
  1908. &o2hb_heartbeat_group_attr_threshold.attr,
  1909. &o2hb_heartbeat_group_attr_mode.attr,
  1910. NULL,
  1911. };
  1912. static struct configfs_item_operations o2hb_heartbeat_group_item_ops = {
  1913. .show_attribute = o2hb_heartbeat_group_show,
  1914. .store_attribute = o2hb_heartbeat_group_store,
  1915. };
  1916. static struct configfs_group_operations o2hb_heartbeat_group_group_ops = {
  1917. .make_item = o2hb_heartbeat_group_make_item,
  1918. .drop_item = o2hb_heartbeat_group_drop_item,
  1919. };
  1920. static struct config_item_type o2hb_heartbeat_group_type = {
  1921. .ct_group_ops = &o2hb_heartbeat_group_group_ops,
  1922. .ct_item_ops = &o2hb_heartbeat_group_item_ops,
  1923. .ct_attrs = o2hb_heartbeat_group_attrs,
  1924. .ct_owner = THIS_MODULE,
  1925. };
  1926. /* this is just here to avoid touching group in heartbeat.h which the
  1927. * entire damn world #includes */
  1928. struct config_group *o2hb_alloc_hb_set(void)
  1929. {
  1930. struct o2hb_heartbeat_group *hs = NULL;
  1931. struct config_group *ret = NULL;
  1932. hs = kzalloc(sizeof(struct o2hb_heartbeat_group), GFP_KERNEL);
  1933. if (hs == NULL)
  1934. goto out;
  1935. config_group_init_type_name(&hs->hs_group, "heartbeat",
  1936. &o2hb_heartbeat_group_type);
  1937. ret = &hs->hs_group;
  1938. out:
  1939. if (ret == NULL)
  1940. kfree(hs);
  1941. return ret;
  1942. }
  1943. void o2hb_free_hb_set(struct config_group *group)
  1944. {
  1945. struct o2hb_heartbeat_group *hs = to_o2hb_heartbeat_group(group);
  1946. kfree(hs);
  1947. }
  1948. /* hb callback registration and issuing */
  1949. static struct o2hb_callback *hbcall_from_type(enum o2hb_callback_type type)
  1950. {
  1951. if (type == O2HB_NUM_CB)
  1952. return ERR_PTR(-EINVAL);
  1953. return &o2hb_callbacks[type];
  1954. }
  1955. void o2hb_setup_callback(struct o2hb_callback_func *hc,
  1956. enum o2hb_callback_type type,
  1957. o2hb_cb_func *func,
  1958. void *data,
  1959. int priority)
  1960. {
  1961. INIT_LIST_HEAD(&hc->hc_item);
  1962. hc->hc_func = func;
  1963. hc->hc_data = data;
  1964. hc->hc_priority = priority;
  1965. hc->hc_type = type;
  1966. hc->hc_magic = O2HB_CB_MAGIC;
  1967. }
  1968. EXPORT_SYMBOL_GPL(o2hb_setup_callback);
  1969. /*
  1970. * In local heartbeat mode, region_uuid passed matches the dlm domain name.
  1971. * In global heartbeat mode, region_uuid passed is NULL.
  1972. *
  1973. * In local, we only pin the matching region. In global we pin all the active
  1974. * regions.
  1975. */
  1976. static int o2hb_region_pin(const char *region_uuid)
  1977. {
  1978. int ret = 0, found = 0;
  1979. struct o2hb_region *reg;
  1980. char *uuid;
  1981. assert_spin_locked(&o2hb_live_lock);
  1982. list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
  1983. if (reg->hr_item_dropped)
  1984. continue;
  1985. uuid = config_item_name(&reg->hr_item);
  1986. /* local heartbeat */
  1987. if (region_uuid) {
  1988. if (strcmp(region_uuid, uuid))
  1989. continue;
  1990. found = 1;
  1991. }
  1992. if (reg->hr_item_pinned || reg->hr_item_dropped)
  1993. goto skip_pin;
  1994. /* Ignore ENOENT only for local hb (userdlm domain) */
  1995. ret = o2nm_depend_item(&reg->hr_item);
  1996. if (!ret) {
  1997. mlog(ML_CLUSTER, "Pin region %s\n", uuid);
  1998. reg->hr_item_pinned = 1;
  1999. } else {
  2000. if (ret == -ENOENT && found)
  2001. ret = 0;
  2002. else {
  2003. mlog(ML_ERROR, "Pin region %s fails with %d\n",
  2004. uuid, ret);
  2005. break;
  2006. }
  2007. }
  2008. skip_pin:
  2009. if (found)
  2010. break;
  2011. }
  2012. return ret;
  2013. }
  2014. /*
  2015. * In local heartbeat mode, region_uuid passed matches the dlm domain name.
  2016. * In global heartbeat mode, region_uuid passed is NULL.
  2017. *
  2018. * In local, we only unpin the matching region. In global we unpin all the
  2019. * active regions.
  2020. */
  2021. static void o2hb_region_unpin(const char *region_uuid)
  2022. {
  2023. struct o2hb_region *reg;
  2024. char *uuid;
  2025. int found = 0;
  2026. assert_spin_locked(&o2hb_live_lock);
  2027. list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
  2028. if (reg->hr_item_dropped)
  2029. continue;
  2030. uuid = config_item_name(&reg->hr_item);
  2031. if (region_uuid) {
  2032. if (strcmp(region_uuid, uuid))
  2033. continue;
  2034. found = 1;
  2035. }
  2036. if (reg->hr_item_pinned) {
  2037. mlog(ML_CLUSTER, "Unpin region %s\n", uuid);
  2038. o2nm_undepend_item(&reg->hr_item);
  2039. reg->hr_item_pinned = 0;
  2040. }
  2041. if (found)
  2042. break;
  2043. }
  2044. }
  2045. static int o2hb_region_inc_user(const char *region_uuid)
  2046. {
  2047. int ret = 0;
  2048. spin_lock(&o2hb_live_lock);
  2049. /* local heartbeat */
  2050. if (!o2hb_global_heartbeat_active()) {
  2051. ret = o2hb_region_pin(region_uuid);
  2052. goto unlock;
  2053. }
  2054. /*
  2055. * if global heartbeat active and this is the first dependent user,
  2056. * pin all regions if quorum region count <= CUT_OFF
  2057. */
  2058. o2hb_dependent_users++;
  2059. if (o2hb_dependent_users > 1)
  2060. goto unlock;
  2061. if (o2hb_pop_count(&o2hb_quorum_region_bitmap,
  2062. O2NM_MAX_REGIONS) <= O2HB_PIN_CUT_OFF)
  2063. ret = o2hb_region_pin(NULL);
  2064. unlock:
  2065. spin_unlock(&o2hb_live_lock);
  2066. return ret;
  2067. }
  2068. void o2hb_region_dec_user(const char *region_uuid)
  2069. {
  2070. spin_lock(&o2hb_live_lock);
  2071. /* local heartbeat */
  2072. if (!o2hb_global_heartbeat_active()) {
  2073. o2hb_region_unpin(region_uuid);
  2074. goto unlock;
  2075. }
  2076. /*
  2077. * if global heartbeat active and there are no dependent users,
  2078. * unpin all quorum regions
  2079. */
  2080. o2hb_dependent_users--;
  2081. if (!o2hb_dependent_users)
  2082. o2hb_region_unpin(NULL);
  2083. unlock:
  2084. spin_unlock(&o2hb_live_lock);
  2085. }
  2086. int o2hb_register_callback(const char *region_uuid,
  2087. struct o2hb_callback_func *hc)
  2088. {
  2089. struct o2hb_callback_func *f;
  2090. struct o2hb_callback *hbcall;
  2091. int ret;
  2092. BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
  2093. BUG_ON(!list_empty(&hc->hc_item));
  2094. hbcall = hbcall_from_type(hc->hc_type);
  2095. if (IS_ERR(hbcall)) {
  2096. ret = PTR_ERR(hbcall);
  2097. goto out;
  2098. }
  2099. if (region_uuid) {
  2100. ret = o2hb_region_inc_user(region_uuid);
  2101. if (ret) {
  2102. mlog_errno(ret);
  2103. goto out;
  2104. }
  2105. }
  2106. down_write(&o2hb_callback_sem);
  2107. list_for_each_entry(f, &hbcall->list, hc_item) {
  2108. if (hc->hc_priority < f->hc_priority) {
  2109. list_add_tail(&hc->hc_item, &f->hc_item);
  2110. break;
  2111. }
  2112. }
  2113. if (list_empty(&hc->hc_item))
  2114. list_add_tail(&hc->hc_item, &hbcall->list);
  2115. up_write(&o2hb_callback_sem);
  2116. ret = 0;
  2117. out:
  2118. mlog(ML_CLUSTER, "returning %d on behalf of %p for funcs %p\n",
  2119. ret, __builtin_return_address(0), hc);
  2120. return ret;
  2121. }
  2122. EXPORT_SYMBOL_GPL(o2hb_register_callback);
  2123. void o2hb_unregister_callback(const char *region_uuid,
  2124. struct o2hb_callback_func *hc)
  2125. {
  2126. BUG_ON(hc->hc_magic != O2HB_CB_MAGIC);
  2127. mlog(ML_CLUSTER, "on behalf of %p for funcs %p\n",
  2128. __builtin_return_address(0), hc);
  2129. /* XXX Can this happen _with_ a region reference? */
  2130. if (list_empty(&hc->hc_item))
  2131. return;
  2132. if (region_uuid)
  2133. o2hb_region_dec_user(region_uuid);
  2134. down_write(&o2hb_callback_sem);
  2135. list_del_init(&hc->hc_item);
  2136. up_write(&o2hb_callback_sem);
  2137. }
  2138. EXPORT_SYMBOL_GPL(o2hb_unregister_callback);
  2139. int o2hb_check_node_heartbeating(u8 node_num)
  2140. {
  2141. unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  2142. o2hb_fill_node_map(testing_map, sizeof(testing_map));
  2143. if (!test_bit(node_num, testing_map)) {
  2144. mlog(ML_HEARTBEAT,
  2145. "node (%u) does not have heartbeating enabled.\n",
  2146. node_num);
  2147. return 0;
  2148. }
  2149. return 1;
  2150. }
  2151. EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating);
  2152. int o2hb_check_node_heartbeating_from_callback(u8 node_num)
  2153. {
  2154. unsigned long testing_map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  2155. o2hb_fill_node_map_from_callback(testing_map, sizeof(testing_map));
  2156. if (!test_bit(node_num, testing_map)) {
  2157. mlog(ML_HEARTBEAT,
  2158. "node (%u) does not have heartbeating enabled.\n",
  2159. node_num);
  2160. return 0;
  2161. }
  2162. return 1;
  2163. }
  2164. EXPORT_SYMBOL_GPL(o2hb_check_node_heartbeating_from_callback);
  2165. /* Makes sure our local node is configured with a node number, and is
  2166. * heartbeating. */
  2167. int o2hb_check_local_node_heartbeating(void)
  2168. {
  2169. u8 node_num;
  2170. /* if this node was set then we have networking */
  2171. node_num = o2nm_this_node();
  2172. if (node_num == O2NM_MAX_NODES) {
  2173. mlog(ML_HEARTBEAT, "this node has not been configured.\n");
  2174. return 0;
  2175. }
  2176. return o2hb_check_node_heartbeating(node_num);
  2177. }
  2178. EXPORT_SYMBOL_GPL(o2hb_check_local_node_heartbeating);
  2179. /*
  2180. * this is just a hack until we get the plumbing which flips file systems
  2181. * read only and drops the hb ref instead of killing the node dead.
  2182. */
  2183. void o2hb_stop_all_regions(void)
  2184. {
  2185. struct o2hb_region *reg;
  2186. mlog(ML_ERROR, "stopping heartbeat on all active regions.\n");
  2187. spin_lock(&o2hb_live_lock);
  2188. list_for_each_entry(reg, &o2hb_all_regions, hr_all_item)
  2189. reg->hr_unclean_stop = 1;
  2190. spin_unlock(&o2hb_live_lock);
  2191. }
  2192. EXPORT_SYMBOL_GPL(o2hb_stop_all_regions);
  2193. int o2hb_get_all_regions(char *region_uuids, u8 max_regions)
  2194. {
  2195. struct o2hb_region *reg;
  2196. int numregs = 0;
  2197. char *p;
  2198. spin_lock(&o2hb_live_lock);
  2199. p = region_uuids;
  2200. list_for_each_entry(reg, &o2hb_all_regions, hr_all_item) {
  2201. if (reg->hr_item_dropped)
  2202. continue;
  2203. mlog(0, "Region: %s\n", config_item_name(&reg->hr_item));
  2204. if (numregs < max_regions) {
  2205. memcpy(p, config_item_name(&reg->hr_item),
  2206. O2HB_MAX_REGION_NAME_LEN);
  2207. p += O2HB_MAX_REGION_NAME_LEN;
  2208. }
  2209. numregs++;
  2210. }
  2211. spin_unlock(&o2hb_live_lock);
  2212. return numregs;
  2213. }
  2214. EXPORT_SYMBOL_GPL(o2hb_get_all_regions);
  2215. int o2hb_global_heartbeat_active(void)
  2216. {
  2217. return (o2hb_heartbeat_mode == O2HB_HEARTBEAT_GLOBAL);
  2218. }
  2219. EXPORT_SYMBOL(o2hb_global_heartbeat_active);