heartbeat.c 54 KB

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