heartbeat.c 59 KB

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