heartbeat.c 68 KB

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