dlmrecovery.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmrecovery.c
  5. *
  6. * recovery stuff
  7. *
  8. * Copyright (C) 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/fs.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/highmem.h>
  31. #include <linux/utsname.h>
  32. #include <linux/init.h>
  33. #include <linux/sysctl.h>
  34. #include <linux/random.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/socket.h>
  37. #include <linux/inet.h>
  38. #include <linux/timer.h>
  39. #include <linux/kthread.h>
  40. #include <linux/delay.h>
  41. #include "cluster/heartbeat.h"
  42. #include "cluster/nodemanager.h"
  43. #include "cluster/tcp.h"
  44. #include "dlmapi.h"
  45. #include "dlmcommon.h"
  46. #include "dlmdomain.h"
  47. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_RECOVERY)
  48. #include "cluster/masklog.h"
  49. static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node);
  50. static int dlm_recovery_thread(void *data);
  51. void dlm_complete_recovery_thread(struct dlm_ctxt *dlm);
  52. int dlm_launch_recovery_thread(struct dlm_ctxt *dlm);
  53. void dlm_kick_recovery_thread(struct dlm_ctxt *dlm);
  54. static int dlm_do_recovery(struct dlm_ctxt *dlm);
  55. static int dlm_pick_recovery_master(struct dlm_ctxt *dlm);
  56. static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node);
  57. static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
  58. static int dlm_request_all_locks(struct dlm_ctxt *dlm,
  59. u8 request_from, u8 dead_node);
  60. static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node);
  61. static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res);
  62. static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
  63. const char *lockname, int namelen,
  64. int total_locks, u64 cookie,
  65. u8 flags, u8 master);
  66. static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
  67. struct dlm_migratable_lockres *mres,
  68. u8 send_to,
  69. struct dlm_lock_resource *res,
  70. int total_locks);
  71. static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
  72. struct dlm_lock_resource *res,
  73. struct dlm_migratable_lockres *mres);
  74. static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm);
  75. static int dlm_send_all_done_msg(struct dlm_ctxt *dlm,
  76. u8 dead_node, u8 send_to);
  77. static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node);
  78. static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
  79. struct list_head *list, u8 dead_node);
  80. static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
  81. u8 dead_node, u8 new_master);
  82. static void dlm_reco_ast(void *astdata);
  83. static void dlm_reco_bast(void *astdata, int blocked_type);
  84. static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st);
  85. static void dlm_request_all_locks_worker(struct dlm_work_item *item,
  86. void *data);
  87. static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data);
  88. static u64 dlm_get_next_mig_cookie(void);
  89. static spinlock_t dlm_reco_state_lock = SPIN_LOCK_UNLOCKED;
  90. static spinlock_t dlm_mig_cookie_lock = SPIN_LOCK_UNLOCKED;
  91. static u64 dlm_mig_cookie = 1;
  92. static u64 dlm_get_next_mig_cookie(void)
  93. {
  94. u64 c;
  95. spin_lock(&dlm_mig_cookie_lock);
  96. c = dlm_mig_cookie;
  97. if (dlm_mig_cookie == (~0ULL))
  98. dlm_mig_cookie = 1;
  99. else
  100. dlm_mig_cookie++;
  101. spin_unlock(&dlm_mig_cookie_lock);
  102. return c;
  103. }
  104. static inline void dlm_reset_recovery(struct dlm_ctxt *dlm)
  105. {
  106. spin_lock(&dlm->spinlock);
  107. clear_bit(dlm->reco.dead_node, dlm->recovery_map);
  108. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  109. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  110. spin_unlock(&dlm->spinlock);
  111. }
  112. /* Worker function used during recovery. */
  113. void dlm_dispatch_work(void *data)
  114. {
  115. struct dlm_ctxt *dlm = (struct dlm_ctxt *)data;
  116. LIST_HEAD(tmp_list);
  117. struct list_head *iter, *iter2;
  118. struct dlm_work_item *item;
  119. dlm_workfunc_t *workfunc;
  120. spin_lock(&dlm->work_lock);
  121. list_splice_init(&dlm->work_list, &tmp_list);
  122. spin_unlock(&dlm->work_lock);
  123. list_for_each_safe(iter, iter2, &tmp_list) {
  124. item = list_entry(iter, struct dlm_work_item, list);
  125. workfunc = item->func;
  126. list_del_init(&item->list);
  127. /* already have ref on dlm to avoid having
  128. * it disappear. just double-check. */
  129. BUG_ON(item->dlm != dlm);
  130. /* this is allowed to sleep and
  131. * call network stuff */
  132. workfunc(item, item->data);
  133. dlm_put(dlm);
  134. kfree(item);
  135. }
  136. }
  137. /*
  138. * RECOVERY THREAD
  139. */
  140. void dlm_kick_recovery_thread(struct dlm_ctxt *dlm)
  141. {
  142. /* wake the recovery thread
  143. * this will wake the reco thread in one of three places
  144. * 1) sleeping with no recovery happening
  145. * 2) sleeping with recovery mastered elsewhere
  146. * 3) recovery mastered here, waiting on reco data */
  147. wake_up(&dlm->dlm_reco_thread_wq);
  148. }
  149. /* Launch the recovery thread */
  150. int dlm_launch_recovery_thread(struct dlm_ctxt *dlm)
  151. {
  152. mlog(0, "starting dlm recovery thread...\n");
  153. dlm->dlm_reco_thread_task = kthread_run(dlm_recovery_thread, dlm,
  154. "dlm_reco_thread");
  155. if (IS_ERR(dlm->dlm_reco_thread_task)) {
  156. mlog_errno(PTR_ERR(dlm->dlm_reco_thread_task));
  157. dlm->dlm_reco_thread_task = NULL;
  158. return -EINVAL;
  159. }
  160. return 0;
  161. }
  162. void dlm_complete_recovery_thread(struct dlm_ctxt *dlm)
  163. {
  164. if (dlm->dlm_reco_thread_task) {
  165. mlog(0, "waiting for dlm recovery thread to exit\n");
  166. kthread_stop(dlm->dlm_reco_thread_task);
  167. dlm->dlm_reco_thread_task = NULL;
  168. }
  169. }
  170. /*
  171. * this is lame, but here's how recovery works...
  172. * 1) all recovery threads cluster wide will work on recovering
  173. * ONE node at a time
  174. * 2) negotiate who will take over all the locks for the dead node.
  175. * thats right... ALL the locks.
  176. * 3) once a new master is chosen, everyone scans all locks
  177. * and moves aside those mastered by the dead guy
  178. * 4) each of these locks should be locked until recovery is done
  179. * 5) the new master collects up all of secondary lock queue info
  180. * one lock at a time, forcing each node to communicate back
  181. * before continuing
  182. * 6) each secondary lock queue responds with the full known lock info
  183. * 7) once the new master has run all its locks, it sends a ALLDONE!
  184. * message to everyone
  185. * 8) upon receiving this message, the secondary queue node unlocks
  186. * and responds to the ALLDONE
  187. * 9) once the new master gets responses from everyone, he unlocks
  188. * everything and recovery for this dead node is done
  189. *10) go back to 2) while there are still dead nodes
  190. *
  191. */
  192. #define DLM_RECO_THREAD_TIMEOUT_MS (5 * 1000)
  193. static int dlm_recovery_thread(void *data)
  194. {
  195. int status;
  196. struct dlm_ctxt *dlm = data;
  197. unsigned long timeout = msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS);
  198. mlog(0, "dlm thread running for %s...\n", dlm->name);
  199. while (!kthread_should_stop()) {
  200. if (dlm_joined(dlm)) {
  201. status = dlm_do_recovery(dlm);
  202. if (status == -EAGAIN) {
  203. /* do not sleep, recheck immediately. */
  204. continue;
  205. }
  206. if (status < 0)
  207. mlog_errno(status);
  208. }
  209. wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
  210. kthread_should_stop(),
  211. timeout);
  212. }
  213. mlog(0, "quitting DLM recovery thread\n");
  214. return 0;
  215. }
  216. /* returns true when the recovery master has contacted us */
  217. static int dlm_reco_master_ready(struct dlm_ctxt *dlm)
  218. {
  219. int ready;
  220. spin_lock(&dlm->spinlock);
  221. ready = (dlm->reco.new_master != O2NM_INVALID_NODE_NUM);
  222. spin_unlock(&dlm->spinlock);
  223. return ready;
  224. }
  225. /* returns true if node is no longer in the domain
  226. * could be dead or just not joined */
  227. int dlm_is_node_dead(struct dlm_ctxt *dlm, u8 node)
  228. {
  229. int dead;
  230. spin_lock(&dlm->spinlock);
  231. dead = test_bit(node, dlm->domain_map);
  232. spin_unlock(&dlm->spinlock);
  233. return dead;
  234. }
  235. int dlm_wait_for_node_death(struct dlm_ctxt *dlm, u8 node, int timeout)
  236. {
  237. if (timeout) {
  238. mlog(ML_NOTICE, "%s: waiting %dms for notification of "
  239. "death of node %u\n", dlm->name, timeout, node);
  240. wait_event_timeout(dlm->dlm_reco_thread_wq,
  241. dlm_is_node_dead(dlm, node),
  242. msecs_to_jiffies(timeout));
  243. } else {
  244. mlog(ML_NOTICE, "%s: waiting indefinitely for notification "
  245. "of death of node %u\n", dlm->name, node);
  246. wait_event(dlm->dlm_reco_thread_wq,
  247. dlm_is_node_dead(dlm, node));
  248. }
  249. /* for now, return 0 */
  250. return 0;
  251. }
  252. /* callers of the top-level api calls (dlmlock/dlmunlock) should
  253. * block on the dlm->reco.event when recovery is in progress.
  254. * the dlm recovery thread will set this state when it begins
  255. * recovering a dead node (as the new master or not) and clear
  256. * the state and wake as soon as all affected lock resources have
  257. * been marked with the RECOVERY flag */
  258. static int dlm_in_recovery(struct dlm_ctxt *dlm)
  259. {
  260. int in_recovery;
  261. spin_lock(&dlm->spinlock);
  262. in_recovery = !!(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
  263. spin_unlock(&dlm->spinlock);
  264. return in_recovery;
  265. }
  266. void dlm_wait_for_recovery(struct dlm_ctxt *dlm)
  267. {
  268. wait_event(dlm->reco.event, !dlm_in_recovery(dlm));
  269. }
  270. static void dlm_begin_recovery(struct dlm_ctxt *dlm)
  271. {
  272. spin_lock(&dlm->spinlock);
  273. BUG_ON(dlm->reco.state & DLM_RECO_STATE_ACTIVE);
  274. dlm->reco.state |= DLM_RECO_STATE_ACTIVE;
  275. spin_unlock(&dlm->spinlock);
  276. }
  277. static void dlm_end_recovery(struct dlm_ctxt *dlm)
  278. {
  279. spin_lock(&dlm->spinlock);
  280. BUG_ON(!(dlm->reco.state & DLM_RECO_STATE_ACTIVE));
  281. dlm->reco.state &= ~DLM_RECO_STATE_ACTIVE;
  282. spin_unlock(&dlm->spinlock);
  283. wake_up(&dlm->reco.event);
  284. }
  285. static int dlm_do_recovery(struct dlm_ctxt *dlm)
  286. {
  287. int status = 0;
  288. int ret;
  289. spin_lock(&dlm->spinlock);
  290. /* check to see if the new master has died */
  291. if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM &&
  292. test_bit(dlm->reco.new_master, dlm->recovery_map)) {
  293. mlog(0, "new master %u died while recovering %u!\n",
  294. dlm->reco.new_master, dlm->reco.dead_node);
  295. /* unset the new_master, leave dead_node */
  296. dlm->reco.new_master = O2NM_INVALID_NODE_NUM;
  297. }
  298. /* select a target to recover */
  299. if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
  300. int bit;
  301. bit = find_next_bit (dlm->recovery_map, O2NM_MAX_NODES+1, 0);
  302. if (bit >= O2NM_MAX_NODES || bit < 0)
  303. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  304. else
  305. dlm->reco.dead_node = bit;
  306. } else if (!test_bit(dlm->reco.dead_node, dlm->recovery_map)) {
  307. /* BUG? */
  308. mlog(ML_ERROR, "dead_node %u no longer in recovery map!\n",
  309. dlm->reco.dead_node);
  310. dlm->reco.dead_node = O2NM_INVALID_NODE_NUM;
  311. }
  312. if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
  313. // mlog(0, "nothing to recover! sleeping now!\n");
  314. spin_unlock(&dlm->spinlock);
  315. /* return to main thread loop and sleep. */
  316. return 0;
  317. }
  318. mlog(0, "recovery thread found node %u in the recovery map!\n",
  319. dlm->reco.dead_node);
  320. spin_unlock(&dlm->spinlock);
  321. /* take write barrier */
  322. /* (stops the list reshuffling thread, proxy ast handling) */
  323. dlm_begin_recovery(dlm);
  324. if (dlm->reco.new_master == dlm->node_num)
  325. goto master_here;
  326. if (dlm->reco.new_master == O2NM_INVALID_NODE_NUM) {
  327. /* choose a new master, returns 0 if this node
  328. * is the master, -EEXIST if it's another node.
  329. * this does not return until a new master is chosen
  330. * or recovery completes entirely. */
  331. ret = dlm_pick_recovery_master(dlm);
  332. if (!ret) {
  333. /* already notified everyone. go. */
  334. goto master_here;
  335. }
  336. mlog(0, "another node will master this recovery session.\n");
  337. }
  338. mlog(0, "dlm=%s, new_master=%u, this node=%u, dead_node=%u\n",
  339. dlm->name, dlm->reco.new_master,
  340. dlm->node_num, dlm->reco.dead_node);
  341. /* it is safe to start everything back up here
  342. * because all of the dead node's lock resources
  343. * have been marked as in-recovery */
  344. dlm_end_recovery(dlm);
  345. /* sleep out in main dlm_recovery_thread loop. */
  346. return 0;
  347. master_here:
  348. mlog(0, "mastering recovery of %s:%u here(this=%u)!\n",
  349. dlm->name, dlm->reco.dead_node, dlm->node_num);
  350. status = dlm_remaster_locks(dlm, dlm->reco.dead_node);
  351. if (status < 0) {
  352. mlog(ML_ERROR, "error %d remastering locks for node %u, "
  353. "retrying.\n", status, dlm->reco.dead_node);
  354. /* yield a bit to allow any final network messages
  355. * to get handled on remaining nodes */
  356. msleep(100);
  357. } else {
  358. /* success! see if any other nodes need recovery */
  359. mlog(0, "DONE mastering recovery of %s:%u here(this=%u)!\n",
  360. dlm->name, dlm->reco.dead_node, dlm->node_num);
  361. dlm_reset_recovery(dlm);
  362. }
  363. dlm_end_recovery(dlm);
  364. /* continue and look for another dead node */
  365. return -EAGAIN;
  366. }
  367. static int dlm_remaster_locks(struct dlm_ctxt *dlm, u8 dead_node)
  368. {
  369. int status = 0;
  370. struct dlm_reco_node_data *ndata;
  371. struct list_head *iter;
  372. int all_nodes_done;
  373. int destroy = 0;
  374. int pass = 0;
  375. status = dlm_init_recovery_area(dlm, dead_node);
  376. if (status < 0)
  377. goto leave;
  378. /* safe to access the node data list without a lock, since this
  379. * process is the only one to change the list */
  380. list_for_each(iter, &dlm->reco.node_data) {
  381. ndata = list_entry (iter, struct dlm_reco_node_data, list);
  382. BUG_ON(ndata->state != DLM_RECO_NODE_DATA_INIT);
  383. ndata->state = DLM_RECO_NODE_DATA_REQUESTING;
  384. mlog(0, "requesting lock info from node %u\n",
  385. ndata->node_num);
  386. if (ndata->node_num == dlm->node_num) {
  387. ndata->state = DLM_RECO_NODE_DATA_DONE;
  388. continue;
  389. }
  390. status = dlm_request_all_locks(dlm, ndata->node_num, dead_node);
  391. if (status < 0) {
  392. mlog_errno(status);
  393. if (dlm_is_host_down(status))
  394. ndata->state = DLM_RECO_NODE_DATA_DEAD;
  395. else {
  396. destroy = 1;
  397. goto leave;
  398. }
  399. }
  400. switch (ndata->state) {
  401. case DLM_RECO_NODE_DATA_INIT:
  402. case DLM_RECO_NODE_DATA_FINALIZE_SENT:
  403. case DLM_RECO_NODE_DATA_REQUESTED:
  404. BUG();
  405. break;
  406. case DLM_RECO_NODE_DATA_DEAD:
  407. mlog(0, "node %u died after requesting "
  408. "recovery info for node %u\n",
  409. ndata->node_num, dead_node);
  410. // start all over
  411. destroy = 1;
  412. status = -EAGAIN;
  413. goto leave;
  414. case DLM_RECO_NODE_DATA_REQUESTING:
  415. ndata->state = DLM_RECO_NODE_DATA_REQUESTED;
  416. mlog(0, "now receiving recovery data from "
  417. "node %u for dead node %u\n",
  418. ndata->node_num, dead_node);
  419. break;
  420. case DLM_RECO_NODE_DATA_RECEIVING:
  421. mlog(0, "already receiving recovery data from "
  422. "node %u for dead node %u\n",
  423. ndata->node_num, dead_node);
  424. break;
  425. case DLM_RECO_NODE_DATA_DONE:
  426. mlog(0, "already DONE receiving recovery data "
  427. "from node %u for dead node %u\n",
  428. ndata->node_num, dead_node);
  429. break;
  430. }
  431. }
  432. mlog(0, "done requesting all lock info\n");
  433. /* nodes should be sending reco data now
  434. * just need to wait */
  435. while (1) {
  436. /* check all the nodes now to see if we are
  437. * done, or if anyone died */
  438. all_nodes_done = 1;
  439. spin_lock(&dlm_reco_state_lock);
  440. list_for_each(iter, &dlm->reco.node_data) {
  441. ndata = list_entry (iter, struct dlm_reco_node_data, list);
  442. mlog(0, "checking recovery state of node %u\n",
  443. ndata->node_num);
  444. switch (ndata->state) {
  445. case DLM_RECO_NODE_DATA_INIT:
  446. case DLM_RECO_NODE_DATA_REQUESTING:
  447. mlog(ML_ERROR, "bad ndata state for "
  448. "node %u: state=%d\n",
  449. ndata->node_num, ndata->state);
  450. BUG();
  451. break;
  452. case DLM_RECO_NODE_DATA_DEAD:
  453. mlog(ML_NOTICE, "node %u died after "
  454. "requesting recovery info for "
  455. "node %u\n", ndata->node_num,
  456. dead_node);
  457. spin_unlock(&dlm_reco_state_lock);
  458. // start all over
  459. destroy = 1;
  460. status = -EAGAIN;
  461. /* instead of spinning like crazy here,
  462. * wait for the domain map to catch up
  463. * with the network state. otherwise this
  464. * can be hit hundreds of times before
  465. * the node is really seen as dead. */
  466. wait_event_timeout(dlm->dlm_reco_thread_wq,
  467. dlm_is_node_dead(dlm,
  468. ndata->node_num),
  469. msecs_to_jiffies(1000));
  470. mlog(0, "waited 1 sec for %u, "
  471. "dead? %s\n", ndata->node_num,
  472. dlm_is_node_dead(dlm, ndata->node_num) ?
  473. "yes" : "no");
  474. goto leave;
  475. case DLM_RECO_NODE_DATA_RECEIVING:
  476. case DLM_RECO_NODE_DATA_REQUESTED:
  477. all_nodes_done = 0;
  478. break;
  479. case DLM_RECO_NODE_DATA_DONE:
  480. break;
  481. case DLM_RECO_NODE_DATA_FINALIZE_SENT:
  482. break;
  483. }
  484. }
  485. spin_unlock(&dlm_reco_state_lock);
  486. mlog(0, "pass #%d, all_nodes_done?: %s\n", ++pass,
  487. all_nodes_done?"yes":"no");
  488. if (all_nodes_done) {
  489. int ret;
  490. /* all nodes are now in DLM_RECO_NODE_DATA_DONE state
  491. * just send a finalize message to everyone and
  492. * clean up */
  493. mlog(0, "all nodes are done! send finalize\n");
  494. ret = dlm_send_finalize_reco_message(dlm);
  495. if (ret < 0)
  496. mlog_errno(ret);
  497. spin_lock(&dlm->spinlock);
  498. dlm_finish_local_lockres_recovery(dlm, dead_node,
  499. dlm->node_num);
  500. spin_unlock(&dlm->spinlock);
  501. mlog(0, "should be done with recovery!\n");
  502. mlog(0, "finishing recovery of %s at %lu, "
  503. "dead=%u, this=%u, new=%u\n", dlm->name,
  504. jiffies, dlm->reco.dead_node,
  505. dlm->node_num, dlm->reco.new_master);
  506. destroy = 1;
  507. status = ret;
  508. /* rescan everything marked dirty along the way */
  509. dlm_kick_thread(dlm, NULL);
  510. break;
  511. }
  512. /* wait to be signalled, with periodic timeout
  513. * to check for node death */
  514. wait_event_interruptible_timeout(dlm->dlm_reco_thread_wq,
  515. kthread_should_stop(),
  516. msecs_to_jiffies(DLM_RECO_THREAD_TIMEOUT_MS));
  517. }
  518. leave:
  519. if (destroy)
  520. dlm_destroy_recovery_area(dlm, dead_node);
  521. mlog_exit(status);
  522. return status;
  523. }
  524. static int dlm_init_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
  525. {
  526. int num=0;
  527. struct dlm_reco_node_data *ndata;
  528. spin_lock(&dlm->spinlock);
  529. memcpy(dlm->reco.node_map, dlm->domain_map, sizeof(dlm->domain_map));
  530. /* nodes can only be removed (by dying) after dropping
  531. * this lock, and death will be trapped later, so this should do */
  532. spin_unlock(&dlm->spinlock);
  533. while (1) {
  534. num = find_next_bit (dlm->reco.node_map, O2NM_MAX_NODES, num);
  535. if (num >= O2NM_MAX_NODES) {
  536. break;
  537. }
  538. BUG_ON(num == dead_node);
  539. ndata = kcalloc(1, sizeof(*ndata), GFP_KERNEL);
  540. if (!ndata) {
  541. dlm_destroy_recovery_area(dlm, dead_node);
  542. return -ENOMEM;
  543. }
  544. ndata->node_num = num;
  545. ndata->state = DLM_RECO_NODE_DATA_INIT;
  546. spin_lock(&dlm_reco_state_lock);
  547. list_add_tail(&ndata->list, &dlm->reco.node_data);
  548. spin_unlock(&dlm_reco_state_lock);
  549. num++;
  550. }
  551. return 0;
  552. }
  553. static void dlm_destroy_recovery_area(struct dlm_ctxt *dlm, u8 dead_node)
  554. {
  555. struct list_head *iter, *iter2;
  556. struct dlm_reco_node_data *ndata;
  557. LIST_HEAD(tmplist);
  558. spin_lock(&dlm_reco_state_lock);
  559. list_splice_init(&dlm->reco.node_data, &tmplist);
  560. spin_unlock(&dlm_reco_state_lock);
  561. list_for_each_safe(iter, iter2, &tmplist) {
  562. ndata = list_entry (iter, struct dlm_reco_node_data, list);
  563. list_del_init(&ndata->list);
  564. kfree(ndata);
  565. }
  566. }
  567. static int dlm_request_all_locks(struct dlm_ctxt *dlm, u8 request_from,
  568. u8 dead_node)
  569. {
  570. struct dlm_lock_request lr;
  571. enum dlm_status ret;
  572. mlog(0, "\n");
  573. mlog(0, "dlm_request_all_locks: dead node is %u, sending request "
  574. "to %u\n", dead_node, request_from);
  575. memset(&lr, 0, sizeof(lr));
  576. lr.node_idx = dlm->node_num;
  577. lr.dead_node = dead_node;
  578. // send message
  579. ret = DLM_NOLOCKMGR;
  580. ret = o2net_send_message(DLM_LOCK_REQUEST_MSG, dlm->key,
  581. &lr, sizeof(lr), request_from, NULL);
  582. /* negative status is handled by caller */
  583. if (ret < 0)
  584. mlog_errno(ret);
  585. // return from here, then
  586. // sleep until all received or error
  587. return ret;
  588. }
  589. int dlm_request_all_locks_handler(struct o2net_msg *msg, u32 len, void *data)
  590. {
  591. struct dlm_ctxt *dlm = data;
  592. struct dlm_lock_request *lr = (struct dlm_lock_request *)msg->buf;
  593. char *buf = NULL;
  594. struct dlm_work_item *item = NULL;
  595. if (!dlm_grab(dlm))
  596. return -EINVAL;
  597. BUG_ON(lr->dead_node != dlm->reco.dead_node);
  598. item = kcalloc(1, sizeof(*item), GFP_KERNEL);
  599. if (!item) {
  600. dlm_put(dlm);
  601. return -ENOMEM;
  602. }
  603. /* this will get freed by dlm_request_all_locks_worker */
  604. buf = (char *) __get_free_page(GFP_KERNEL);
  605. if (!buf) {
  606. kfree(item);
  607. dlm_put(dlm);
  608. return -ENOMEM;
  609. }
  610. /* queue up work for dlm_request_all_locks_worker */
  611. dlm_grab(dlm); /* get an extra ref for the work item */
  612. dlm_init_work_item(dlm, item, dlm_request_all_locks_worker, buf);
  613. item->u.ral.reco_master = lr->node_idx;
  614. item->u.ral.dead_node = lr->dead_node;
  615. spin_lock(&dlm->work_lock);
  616. list_add_tail(&item->list, &dlm->work_list);
  617. spin_unlock(&dlm->work_lock);
  618. schedule_work(&dlm->dispatched_work);
  619. dlm_put(dlm);
  620. return 0;
  621. }
  622. static void dlm_request_all_locks_worker(struct dlm_work_item *item, void *data)
  623. {
  624. struct dlm_migratable_lockres *mres;
  625. struct dlm_lock_resource *res;
  626. struct dlm_ctxt *dlm;
  627. LIST_HEAD(resources);
  628. struct list_head *iter;
  629. int ret;
  630. u8 dead_node, reco_master;
  631. dlm = item->dlm;
  632. dead_node = item->u.ral.dead_node;
  633. reco_master = item->u.ral.reco_master;
  634. mres = (struct dlm_migratable_lockres *)data;
  635. if (dead_node != dlm->reco.dead_node ||
  636. reco_master != dlm->reco.new_master) {
  637. /* show extra debug info if the recovery state is messed */
  638. mlog(ML_ERROR, "%s: bad reco state: reco(dead=%u, master=%u), "
  639. "request(dead=%u, master=%u)\n",
  640. dlm->name, dlm->reco.dead_node, dlm->reco.new_master,
  641. dead_node, reco_master);
  642. mlog(ML_ERROR, "%s: name=%.*s master=%u locks=%u/%u flags=%u "
  643. "entry[0]={c=%u:%llu,l=%u,f=%u,t=%d,ct=%d,hb=%d,n=%u}\n",
  644. dlm->name, mres->lockname_len, mres->lockname, mres->master,
  645. mres->num_locks, mres->total_locks, mres->flags,
  646. dlm_get_lock_cookie_node(mres->ml[0].cookie),
  647. dlm_get_lock_cookie_seq(mres->ml[0].cookie),
  648. mres->ml[0].list, mres->ml[0].flags,
  649. mres->ml[0].type, mres->ml[0].convert_type,
  650. mres->ml[0].highest_blocked, mres->ml[0].node);
  651. BUG();
  652. }
  653. BUG_ON(dead_node != dlm->reco.dead_node);
  654. BUG_ON(reco_master != dlm->reco.new_master);
  655. /* lock resources should have already been moved to the
  656. * dlm->reco.resources list. now move items from that list
  657. * to a temp list if the dead owner matches. note that the
  658. * whole cluster recovers only one node at a time, so we
  659. * can safely move UNKNOWN lock resources for each recovery
  660. * session. */
  661. dlm_move_reco_locks_to_list(dlm, &resources, dead_node);
  662. /* now we can begin blasting lockreses without the dlm lock */
  663. list_for_each(iter, &resources) {
  664. res = list_entry (iter, struct dlm_lock_resource, recovering);
  665. ret = dlm_send_one_lockres(dlm, res, mres, reco_master,
  666. DLM_MRES_RECOVERY);
  667. if (ret < 0)
  668. mlog_errno(ret);
  669. }
  670. /* move the resources back to the list */
  671. spin_lock(&dlm->spinlock);
  672. list_splice_init(&resources, &dlm->reco.resources);
  673. spin_unlock(&dlm->spinlock);
  674. ret = dlm_send_all_done_msg(dlm, dead_node, reco_master);
  675. if (ret < 0)
  676. mlog_errno(ret);
  677. free_page((unsigned long)data);
  678. }
  679. static int dlm_send_all_done_msg(struct dlm_ctxt *dlm, u8 dead_node, u8 send_to)
  680. {
  681. int ret, tmpret;
  682. struct dlm_reco_data_done done_msg;
  683. memset(&done_msg, 0, sizeof(done_msg));
  684. done_msg.node_idx = dlm->node_num;
  685. done_msg.dead_node = dead_node;
  686. mlog(0, "sending DATA DONE message to %u, "
  687. "my node=%u, dead node=%u\n", send_to, done_msg.node_idx,
  688. done_msg.dead_node);
  689. ret = o2net_send_message(DLM_RECO_DATA_DONE_MSG, dlm->key, &done_msg,
  690. sizeof(done_msg), send_to, &tmpret);
  691. /* negative status is ignored by the caller */
  692. if (ret >= 0)
  693. ret = tmpret;
  694. return ret;
  695. }
  696. int dlm_reco_data_done_handler(struct o2net_msg *msg, u32 len, void *data)
  697. {
  698. struct dlm_ctxt *dlm = data;
  699. struct dlm_reco_data_done *done = (struct dlm_reco_data_done *)msg->buf;
  700. struct list_head *iter;
  701. struct dlm_reco_node_data *ndata = NULL;
  702. int ret = -EINVAL;
  703. if (!dlm_grab(dlm))
  704. return -EINVAL;
  705. mlog(0, "got DATA DONE: dead_node=%u, reco.dead_node=%u, "
  706. "node_idx=%u, this node=%u\n", done->dead_node,
  707. dlm->reco.dead_node, done->node_idx, dlm->node_num);
  708. BUG_ON(done->dead_node != dlm->reco.dead_node);
  709. spin_lock(&dlm_reco_state_lock);
  710. list_for_each(iter, &dlm->reco.node_data) {
  711. ndata = list_entry (iter, struct dlm_reco_node_data, list);
  712. if (ndata->node_num != done->node_idx)
  713. continue;
  714. switch (ndata->state) {
  715. /* should have moved beyond INIT but not to FINALIZE yet */
  716. case DLM_RECO_NODE_DATA_INIT:
  717. case DLM_RECO_NODE_DATA_DEAD:
  718. case DLM_RECO_NODE_DATA_FINALIZE_SENT:
  719. mlog(ML_ERROR, "bad ndata state for node %u:"
  720. " state=%d\n", ndata->node_num,
  721. ndata->state);
  722. BUG();
  723. break;
  724. /* these states are possible at this point, anywhere along
  725. * the line of recovery */
  726. case DLM_RECO_NODE_DATA_DONE:
  727. case DLM_RECO_NODE_DATA_RECEIVING:
  728. case DLM_RECO_NODE_DATA_REQUESTED:
  729. case DLM_RECO_NODE_DATA_REQUESTING:
  730. mlog(0, "node %u is DONE sending "
  731. "recovery data!\n",
  732. ndata->node_num);
  733. ndata->state = DLM_RECO_NODE_DATA_DONE;
  734. ret = 0;
  735. break;
  736. }
  737. }
  738. spin_unlock(&dlm_reco_state_lock);
  739. /* wake the recovery thread, some node is done */
  740. if (!ret)
  741. dlm_kick_recovery_thread(dlm);
  742. if (ret < 0)
  743. mlog(ML_ERROR, "failed to find recovery node data for node "
  744. "%u\n", done->node_idx);
  745. dlm_put(dlm);
  746. mlog(0, "leaving reco data done handler, ret=%d\n", ret);
  747. return ret;
  748. }
  749. static void dlm_move_reco_locks_to_list(struct dlm_ctxt *dlm,
  750. struct list_head *list,
  751. u8 dead_node)
  752. {
  753. struct dlm_lock_resource *res;
  754. struct list_head *iter, *iter2;
  755. struct dlm_lock *lock;
  756. spin_lock(&dlm->spinlock);
  757. list_for_each_safe(iter, iter2, &dlm->reco.resources) {
  758. res = list_entry (iter, struct dlm_lock_resource, recovering);
  759. /* always prune any $RECOVERY entries for dead nodes,
  760. * otherwise hangs can occur during later recovery */
  761. if (dlm_is_recovery_lock(res->lockname.name,
  762. res->lockname.len)) {
  763. spin_lock(&res->spinlock);
  764. list_for_each_entry(lock, &res->granted, list) {
  765. if (lock->ml.node == dead_node) {
  766. mlog(0, "AHA! there was "
  767. "a $RECOVERY lock for dead "
  768. "node %u (%s)!\n",
  769. dead_node, dlm->name);
  770. list_del_init(&lock->list);
  771. dlm_lock_put(lock);
  772. break;
  773. }
  774. }
  775. spin_unlock(&res->spinlock);
  776. continue;
  777. }
  778. if (res->owner == dead_node) {
  779. mlog(0, "found lockres owned by dead node while "
  780. "doing recovery for node %u. sending it.\n",
  781. dead_node);
  782. list_del_init(&res->recovering);
  783. list_add_tail(&res->recovering, list);
  784. } else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
  785. mlog(0, "found UNKNOWN owner while doing recovery "
  786. "for node %u. sending it.\n", dead_node);
  787. list_del_init(&res->recovering);
  788. list_add_tail(&res->recovering, list);
  789. }
  790. }
  791. spin_unlock(&dlm->spinlock);
  792. }
  793. static inline int dlm_num_locks_in_lockres(struct dlm_lock_resource *res)
  794. {
  795. int total_locks = 0;
  796. struct list_head *iter, *queue = &res->granted;
  797. int i;
  798. for (i=0; i<3; i++) {
  799. list_for_each(iter, queue)
  800. total_locks++;
  801. queue++;
  802. }
  803. return total_locks;
  804. }
  805. static int dlm_send_mig_lockres_msg(struct dlm_ctxt *dlm,
  806. struct dlm_migratable_lockres *mres,
  807. u8 send_to,
  808. struct dlm_lock_resource *res,
  809. int total_locks)
  810. {
  811. u64 mig_cookie = be64_to_cpu(mres->mig_cookie);
  812. int mres_total_locks = be32_to_cpu(mres->total_locks);
  813. int sz, ret = 0, status = 0;
  814. u8 orig_flags = mres->flags,
  815. orig_master = mres->master;
  816. BUG_ON(mres->num_locks > DLM_MAX_MIGRATABLE_LOCKS);
  817. if (!mres->num_locks)
  818. return 0;
  819. sz = sizeof(struct dlm_migratable_lockres) +
  820. (mres->num_locks * sizeof(struct dlm_migratable_lock));
  821. /* add an all-done flag if we reached the last lock */
  822. orig_flags = mres->flags;
  823. BUG_ON(total_locks > mres_total_locks);
  824. if (total_locks == mres_total_locks)
  825. mres->flags |= DLM_MRES_ALL_DONE;
  826. /* send it */
  827. ret = o2net_send_message(DLM_MIG_LOCKRES_MSG, dlm->key, mres,
  828. sz, send_to, &status);
  829. if (ret < 0) {
  830. /* XXX: negative status is not handled.
  831. * this will end up killing this node. */
  832. mlog_errno(ret);
  833. } else {
  834. /* might get an -ENOMEM back here */
  835. ret = status;
  836. if (ret < 0) {
  837. mlog_errno(ret);
  838. if (ret == -EFAULT) {
  839. mlog(ML_ERROR, "node %u told me to kill "
  840. "myself!\n", send_to);
  841. BUG();
  842. }
  843. }
  844. }
  845. /* zero and reinit the message buffer */
  846. dlm_init_migratable_lockres(mres, res->lockname.name,
  847. res->lockname.len, mres_total_locks,
  848. mig_cookie, orig_flags, orig_master);
  849. return ret;
  850. }
  851. static void dlm_init_migratable_lockres(struct dlm_migratable_lockres *mres,
  852. const char *lockname, int namelen,
  853. int total_locks, u64 cookie,
  854. u8 flags, u8 master)
  855. {
  856. /* mres here is one full page */
  857. memset(mres, 0, PAGE_SIZE);
  858. mres->lockname_len = namelen;
  859. memcpy(mres->lockname, lockname, namelen);
  860. mres->num_locks = 0;
  861. mres->total_locks = cpu_to_be32(total_locks);
  862. mres->mig_cookie = cpu_to_be64(cookie);
  863. mres->flags = flags;
  864. mres->master = master;
  865. }
  866. /* returns 1 if this lock fills the network structure,
  867. * 0 otherwise */
  868. static int dlm_add_lock_to_array(struct dlm_lock *lock,
  869. struct dlm_migratable_lockres *mres, int queue)
  870. {
  871. struct dlm_migratable_lock *ml;
  872. int lock_num = mres->num_locks;
  873. ml = &(mres->ml[lock_num]);
  874. ml->cookie = lock->ml.cookie;
  875. ml->type = lock->ml.type;
  876. ml->convert_type = lock->ml.convert_type;
  877. ml->highest_blocked = lock->ml.highest_blocked;
  878. ml->list = queue;
  879. if (lock->lksb) {
  880. ml->flags = lock->lksb->flags;
  881. /* send our current lvb */
  882. if (ml->type == LKM_EXMODE ||
  883. ml->type == LKM_PRMODE) {
  884. /* if it is already set, this had better be a PR
  885. * and it has to match */
  886. if (mres->lvb[0] && (ml->type == LKM_EXMODE ||
  887. memcmp(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN))) {
  888. mlog(ML_ERROR, "mismatched lvbs!\n");
  889. __dlm_print_one_lock_resource(lock->lockres);
  890. BUG();
  891. }
  892. memcpy(mres->lvb, lock->lksb->lvb, DLM_LVB_LEN);
  893. }
  894. }
  895. ml->node = lock->ml.node;
  896. mres->num_locks++;
  897. /* we reached the max, send this network message */
  898. if (mres->num_locks == DLM_MAX_MIGRATABLE_LOCKS)
  899. return 1;
  900. return 0;
  901. }
  902. int dlm_send_one_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  903. struct dlm_migratable_lockres *mres,
  904. u8 send_to, u8 flags)
  905. {
  906. struct list_head *queue, *iter;
  907. int total_locks, i;
  908. u64 mig_cookie = 0;
  909. struct dlm_lock *lock;
  910. int ret = 0;
  911. BUG_ON(!(flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
  912. mlog(0, "sending to %u\n", send_to);
  913. total_locks = dlm_num_locks_in_lockres(res);
  914. if (total_locks > DLM_MAX_MIGRATABLE_LOCKS) {
  915. /* rare, but possible */
  916. mlog(0, "argh. lockres has %d locks. this will "
  917. "require more than one network packet to "
  918. "migrate\n", total_locks);
  919. mig_cookie = dlm_get_next_mig_cookie();
  920. }
  921. dlm_init_migratable_lockres(mres, res->lockname.name,
  922. res->lockname.len, total_locks,
  923. mig_cookie, flags, res->owner);
  924. total_locks = 0;
  925. for (i=DLM_GRANTED_LIST; i<=DLM_BLOCKED_LIST; i++) {
  926. queue = dlm_list_idx_to_ptr(res, i);
  927. list_for_each(iter, queue) {
  928. lock = list_entry (iter, struct dlm_lock, list);
  929. /* add another lock. */
  930. total_locks++;
  931. if (!dlm_add_lock_to_array(lock, mres, i))
  932. continue;
  933. /* this filled the lock message,
  934. * we must send it immediately. */
  935. ret = dlm_send_mig_lockres_msg(dlm, mres, send_to,
  936. res, total_locks);
  937. if (ret < 0) {
  938. // TODO
  939. mlog(ML_ERROR, "dlm_send_mig_lockres_msg "
  940. "returned %d, TODO\n", ret);
  941. BUG();
  942. }
  943. }
  944. }
  945. /* flush any remaining locks */
  946. ret = dlm_send_mig_lockres_msg(dlm, mres, send_to, res, total_locks);
  947. if (ret < 0) {
  948. // TODO
  949. mlog(ML_ERROR, "dlm_send_mig_lockres_msg returned %d, "
  950. "TODO\n", ret);
  951. BUG();
  952. }
  953. return ret;
  954. }
  955. /*
  956. * this message will contain no more than one page worth of
  957. * recovery data, and it will work on only one lockres.
  958. * there may be many locks in this page, and we may need to wait
  959. * for additional packets to complete all the locks (rare, but
  960. * possible).
  961. */
  962. /*
  963. * NOTE: the allocation error cases here are scary
  964. * we really cannot afford to fail an alloc in recovery
  965. * do we spin? returning an error only delays the problem really
  966. */
  967. int dlm_mig_lockres_handler(struct o2net_msg *msg, u32 len, void *data)
  968. {
  969. struct dlm_ctxt *dlm = data;
  970. struct dlm_migratable_lockres *mres =
  971. (struct dlm_migratable_lockres *)msg->buf;
  972. int ret = 0;
  973. u8 real_master;
  974. char *buf = NULL;
  975. struct dlm_work_item *item = NULL;
  976. struct dlm_lock_resource *res = NULL;
  977. if (!dlm_grab(dlm))
  978. return -EINVAL;
  979. BUG_ON(!(mres->flags & (DLM_MRES_RECOVERY|DLM_MRES_MIGRATION)));
  980. real_master = mres->master;
  981. if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  982. /* cannot migrate a lockres with no master */
  983. BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
  984. }
  985. mlog(0, "%s message received from node %u\n",
  986. (mres->flags & DLM_MRES_RECOVERY) ?
  987. "recovery" : "migration", mres->master);
  988. if (mres->flags & DLM_MRES_ALL_DONE)
  989. mlog(0, "all done flag. all lockres data received!\n");
  990. ret = -ENOMEM;
  991. buf = kmalloc(be16_to_cpu(msg->data_len), GFP_KERNEL);
  992. item = kcalloc(1, sizeof(*item), GFP_KERNEL);
  993. if (!buf || !item)
  994. goto leave;
  995. /* lookup the lock to see if we have a secondary queue for this
  996. * already... just add the locks in and this will have its owner
  997. * and RECOVERY flag changed when it completes. */
  998. res = dlm_lookup_lockres(dlm, mres->lockname, mres->lockname_len);
  999. if (res) {
  1000. /* this will get a ref on res */
  1001. /* mark it as recovering/migrating and hash it */
  1002. spin_lock(&res->spinlock);
  1003. if (mres->flags & DLM_MRES_RECOVERY) {
  1004. res->state |= DLM_LOCK_RES_RECOVERING;
  1005. } else {
  1006. if (res->state & DLM_LOCK_RES_MIGRATING) {
  1007. /* this is at least the second
  1008. * lockres message */
  1009. mlog(0, "lock %.*s is already migrating\n",
  1010. mres->lockname_len,
  1011. mres->lockname);
  1012. } else if (res->state & DLM_LOCK_RES_RECOVERING) {
  1013. /* caller should BUG */
  1014. mlog(ML_ERROR, "node is attempting to migrate "
  1015. "lock %.*s, but marked as recovering!\n",
  1016. mres->lockname_len, mres->lockname);
  1017. ret = -EFAULT;
  1018. spin_unlock(&res->spinlock);
  1019. goto leave;
  1020. }
  1021. res->state |= DLM_LOCK_RES_MIGRATING;
  1022. }
  1023. spin_unlock(&res->spinlock);
  1024. } else {
  1025. /* need to allocate, just like if it was
  1026. * mastered here normally */
  1027. res = dlm_new_lockres(dlm, mres->lockname, mres->lockname_len);
  1028. if (!res)
  1029. goto leave;
  1030. /* to match the ref that we would have gotten if
  1031. * dlm_lookup_lockres had succeeded */
  1032. dlm_lockres_get(res);
  1033. /* mark it as recovering/migrating and hash it */
  1034. if (mres->flags & DLM_MRES_RECOVERY)
  1035. res->state |= DLM_LOCK_RES_RECOVERING;
  1036. else
  1037. res->state |= DLM_LOCK_RES_MIGRATING;
  1038. spin_lock(&dlm->spinlock);
  1039. __dlm_insert_lockres(dlm, res);
  1040. spin_unlock(&dlm->spinlock);
  1041. /* now that the new lockres is inserted,
  1042. * make it usable by other processes */
  1043. spin_lock(&res->spinlock);
  1044. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  1045. spin_unlock(&res->spinlock);
  1046. /* add an extra ref for just-allocated lockres
  1047. * otherwise the lockres will be purged immediately */
  1048. dlm_lockres_get(res);
  1049. }
  1050. /* at this point we have allocated everything we need,
  1051. * and we have a hashed lockres with an extra ref and
  1052. * the proper res->state flags. */
  1053. ret = 0;
  1054. if (mres->master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1055. /* migration cannot have an unknown master */
  1056. BUG_ON(!(mres->flags & DLM_MRES_RECOVERY));
  1057. mlog(0, "recovery has passed me a lockres with an "
  1058. "unknown owner.. will need to requery: "
  1059. "%.*s\n", mres->lockname_len, mres->lockname);
  1060. } else {
  1061. spin_lock(&res->spinlock);
  1062. dlm_change_lockres_owner(dlm, res, dlm->node_num);
  1063. spin_unlock(&res->spinlock);
  1064. }
  1065. /* queue up work for dlm_mig_lockres_worker */
  1066. dlm_grab(dlm); /* get an extra ref for the work item */
  1067. memcpy(buf, msg->buf, be16_to_cpu(msg->data_len)); /* copy the whole message */
  1068. dlm_init_work_item(dlm, item, dlm_mig_lockres_worker, buf);
  1069. item->u.ml.lockres = res; /* already have a ref */
  1070. item->u.ml.real_master = real_master;
  1071. spin_lock(&dlm->work_lock);
  1072. list_add_tail(&item->list, &dlm->work_list);
  1073. spin_unlock(&dlm->work_lock);
  1074. schedule_work(&dlm->dispatched_work);
  1075. leave:
  1076. dlm_put(dlm);
  1077. if (ret < 0) {
  1078. if (buf)
  1079. kfree(buf);
  1080. if (item)
  1081. kfree(item);
  1082. }
  1083. mlog_exit(ret);
  1084. return ret;
  1085. }
  1086. static void dlm_mig_lockres_worker(struct dlm_work_item *item, void *data)
  1087. {
  1088. struct dlm_ctxt *dlm = data;
  1089. struct dlm_migratable_lockres *mres;
  1090. int ret = 0;
  1091. struct dlm_lock_resource *res;
  1092. u8 real_master;
  1093. dlm = item->dlm;
  1094. mres = (struct dlm_migratable_lockres *)data;
  1095. res = item->u.ml.lockres;
  1096. real_master = item->u.ml.real_master;
  1097. if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1098. /* this case is super-rare. only occurs if
  1099. * node death happens during migration. */
  1100. again:
  1101. ret = dlm_lockres_master_requery(dlm, res, &real_master);
  1102. if (ret < 0) {
  1103. mlog(0, "dlm_lockres_master_requery ret=%d\n",
  1104. ret);
  1105. goto again;
  1106. }
  1107. if (real_master == DLM_LOCK_RES_OWNER_UNKNOWN) {
  1108. mlog(0, "lockres %.*s not claimed. "
  1109. "this node will take it.\n",
  1110. res->lockname.len, res->lockname.name);
  1111. } else {
  1112. mlog(0, "master needs to respond to sender "
  1113. "that node %u still owns %.*s\n",
  1114. real_master, res->lockname.len,
  1115. res->lockname.name);
  1116. /* cannot touch this lockres */
  1117. goto leave;
  1118. }
  1119. }
  1120. ret = dlm_process_recovery_data(dlm, res, mres);
  1121. if (ret < 0)
  1122. mlog(0, "dlm_process_recovery_data returned %d\n", ret);
  1123. else
  1124. mlog(0, "dlm_process_recovery_data succeeded\n");
  1125. if ((mres->flags & (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) ==
  1126. (DLM_MRES_MIGRATION|DLM_MRES_ALL_DONE)) {
  1127. ret = dlm_finish_migration(dlm, res, mres->master);
  1128. if (ret < 0)
  1129. mlog_errno(ret);
  1130. }
  1131. leave:
  1132. kfree(data);
  1133. mlog_exit(ret);
  1134. }
  1135. int dlm_lockres_master_requery(struct dlm_ctxt *dlm,
  1136. struct dlm_lock_resource *res, u8 *real_master)
  1137. {
  1138. struct dlm_node_iter iter;
  1139. int nodenum;
  1140. int ret = 0;
  1141. *real_master = DLM_LOCK_RES_OWNER_UNKNOWN;
  1142. /* we only reach here if one of the two nodes in a
  1143. * migration died while the migration was in progress.
  1144. * at this point we need to requery the master. we
  1145. * know that the new_master got as far as creating
  1146. * an mle on at least one node, but we do not know
  1147. * if any nodes had actually cleared the mle and set
  1148. * the master to the new_master. the old master
  1149. * is supposed to set the owner to UNKNOWN in the
  1150. * event of a new_master death, so the only possible
  1151. * responses that we can get from nodes here are
  1152. * that the master is new_master, or that the master
  1153. * is UNKNOWN.
  1154. * if all nodes come back with UNKNOWN then we know
  1155. * the lock needs remastering here.
  1156. * if any node comes back with a valid master, check
  1157. * to see if that master is the one that we are
  1158. * recovering. if so, then the new_master died and
  1159. * we need to remaster this lock. if not, then the
  1160. * new_master survived and that node will respond to
  1161. * other nodes about the owner.
  1162. * if there is an owner, this node needs to dump this
  1163. * lockres and alert the sender that this lockres
  1164. * was rejected. */
  1165. spin_lock(&dlm->spinlock);
  1166. dlm_node_iter_init(dlm->domain_map, &iter);
  1167. spin_unlock(&dlm->spinlock);
  1168. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  1169. /* do not send to self */
  1170. if (nodenum == dlm->node_num)
  1171. continue;
  1172. ret = dlm_do_master_requery(dlm, res, nodenum, real_master);
  1173. if (ret < 0) {
  1174. mlog_errno(ret);
  1175. if (!dlm_is_host_down(ret))
  1176. BUG();
  1177. /* host is down, so answer for that node would be
  1178. * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
  1179. }
  1180. if (*real_master != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1181. mlog(0, "lock master is %u\n", *real_master);
  1182. break;
  1183. }
  1184. }
  1185. return ret;
  1186. }
  1187. int dlm_do_master_requery(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  1188. u8 nodenum, u8 *real_master)
  1189. {
  1190. int ret = -EINVAL;
  1191. struct dlm_master_requery req;
  1192. int status = DLM_LOCK_RES_OWNER_UNKNOWN;
  1193. memset(&req, 0, sizeof(req));
  1194. req.node_idx = dlm->node_num;
  1195. req.namelen = res->lockname.len;
  1196. memcpy(req.name, res->lockname.name, res->lockname.len);
  1197. ret = o2net_send_message(DLM_MASTER_REQUERY_MSG, dlm->key,
  1198. &req, sizeof(req), nodenum, &status);
  1199. /* XXX: negative status not handled properly here. */
  1200. if (ret < 0)
  1201. mlog_errno(ret);
  1202. else {
  1203. BUG_ON(status < 0);
  1204. BUG_ON(status > DLM_LOCK_RES_OWNER_UNKNOWN);
  1205. *real_master = (u8) (status & 0xff);
  1206. mlog(0, "node %u responded to master requery with %u\n",
  1207. nodenum, *real_master);
  1208. ret = 0;
  1209. }
  1210. return ret;
  1211. }
  1212. /* this function cannot error, so unless the sending
  1213. * or receiving of the message failed, the owner can
  1214. * be trusted */
  1215. int dlm_master_requery_handler(struct o2net_msg *msg, u32 len, void *data)
  1216. {
  1217. struct dlm_ctxt *dlm = data;
  1218. struct dlm_master_requery *req = (struct dlm_master_requery *)msg->buf;
  1219. struct dlm_lock_resource *res = NULL;
  1220. int master = DLM_LOCK_RES_OWNER_UNKNOWN;
  1221. u32 flags = DLM_ASSERT_MASTER_REQUERY;
  1222. if (!dlm_grab(dlm)) {
  1223. /* since the domain has gone away on this
  1224. * node, the proper response is UNKNOWN */
  1225. return master;
  1226. }
  1227. spin_lock(&dlm->spinlock);
  1228. res = __dlm_lookup_lockres(dlm, req->name, req->namelen);
  1229. if (res) {
  1230. spin_lock(&res->spinlock);
  1231. master = res->owner;
  1232. if (master == dlm->node_num) {
  1233. int ret = dlm_dispatch_assert_master(dlm, res,
  1234. 0, 0, flags);
  1235. if (ret < 0) {
  1236. mlog_errno(-ENOMEM);
  1237. /* retry!? */
  1238. BUG();
  1239. }
  1240. }
  1241. spin_unlock(&res->spinlock);
  1242. }
  1243. spin_unlock(&dlm->spinlock);
  1244. dlm_put(dlm);
  1245. return master;
  1246. }
  1247. static inline struct list_head *
  1248. dlm_list_num_to_pointer(struct dlm_lock_resource *res, int list_num)
  1249. {
  1250. struct list_head *ret;
  1251. BUG_ON(list_num < 0);
  1252. BUG_ON(list_num > 2);
  1253. ret = &(res->granted);
  1254. ret += list_num;
  1255. return ret;
  1256. }
  1257. /* TODO: do ast flush business
  1258. * TODO: do MIGRATING and RECOVERING spinning
  1259. */
  1260. /*
  1261. * NOTE about in-flight requests during migration:
  1262. *
  1263. * Before attempting the migrate, the master has marked the lockres as
  1264. * MIGRATING and then flushed all of its pending ASTS. So any in-flight
  1265. * requests either got queued before the MIGRATING flag got set, in which
  1266. * case the lock data will reflect the change and a return message is on
  1267. * the way, or the request failed to get in before MIGRATING got set. In
  1268. * this case, the caller will be told to spin and wait for the MIGRATING
  1269. * flag to be dropped, then recheck the master.
  1270. * This holds true for the convert, cancel and unlock cases, and since lvb
  1271. * updates are tied to these same messages, it applies to lvb updates as
  1272. * well. For the lock case, there is no way a lock can be on the master
  1273. * queue and not be on the secondary queue since the lock is always added
  1274. * locally first. This means that the new target node will never be sent
  1275. * a lock that he doesn't already have on the list.
  1276. * In total, this means that the local lock is correct and should not be
  1277. * updated to match the one sent by the master. Any messages sent back
  1278. * from the master before the MIGRATING flag will bring the lock properly
  1279. * up-to-date, and the change will be ordered properly for the waiter.
  1280. * We will *not* attempt to modify the lock underneath the waiter.
  1281. */
  1282. static int dlm_process_recovery_data(struct dlm_ctxt *dlm,
  1283. struct dlm_lock_resource *res,
  1284. struct dlm_migratable_lockres *mres)
  1285. {
  1286. struct dlm_migratable_lock *ml;
  1287. struct list_head *queue;
  1288. struct dlm_lock *newlock = NULL;
  1289. struct dlm_lockstatus *lksb = NULL;
  1290. int ret = 0;
  1291. int i;
  1292. struct list_head *iter;
  1293. struct dlm_lock *lock = NULL;
  1294. mlog(0, "running %d locks for this lockres\n", mres->num_locks);
  1295. for (i=0; i<mres->num_locks; i++) {
  1296. ml = &(mres->ml[i]);
  1297. BUG_ON(ml->highest_blocked != LKM_IVMODE);
  1298. newlock = NULL;
  1299. lksb = NULL;
  1300. queue = dlm_list_num_to_pointer(res, ml->list);
  1301. /* if the lock is for the local node it needs to
  1302. * be moved to the proper location within the queue.
  1303. * do not allocate a new lock structure. */
  1304. if (ml->node == dlm->node_num) {
  1305. /* MIGRATION ONLY! */
  1306. BUG_ON(!(mres->flags & DLM_MRES_MIGRATION));
  1307. spin_lock(&res->spinlock);
  1308. list_for_each(iter, queue) {
  1309. lock = list_entry (iter, struct dlm_lock, list);
  1310. if (lock->ml.cookie != ml->cookie)
  1311. lock = NULL;
  1312. else
  1313. break;
  1314. }
  1315. /* lock is always created locally first, and
  1316. * destroyed locally last. it must be on the list */
  1317. if (!lock) {
  1318. u64 c = ml->cookie;
  1319. mlog(ML_ERROR, "could not find local lock "
  1320. "with cookie %u:%llu!\n",
  1321. dlm_get_lock_cookie_node(c),
  1322. dlm_get_lock_cookie_seq(c));
  1323. BUG();
  1324. }
  1325. BUG_ON(lock->ml.node != ml->node);
  1326. /* see NOTE above about why we do not update
  1327. * to match the master here */
  1328. /* move the lock to its proper place */
  1329. /* do not alter lock refcount. switching lists. */
  1330. list_del_init(&lock->list);
  1331. list_add_tail(&lock->list, queue);
  1332. spin_unlock(&res->spinlock);
  1333. mlog(0, "just reordered a local lock!\n");
  1334. continue;
  1335. }
  1336. /* lock is for another node. */
  1337. newlock = dlm_new_lock(ml->type, ml->node,
  1338. be64_to_cpu(ml->cookie), NULL);
  1339. if (!newlock) {
  1340. ret = -ENOMEM;
  1341. goto leave;
  1342. }
  1343. lksb = newlock->lksb;
  1344. dlm_lock_attach_lockres(newlock, res);
  1345. if (ml->convert_type != LKM_IVMODE) {
  1346. BUG_ON(queue != &res->converting);
  1347. newlock->ml.convert_type = ml->convert_type;
  1348. }
  1349. lksb->flags |= (ml->flags &
  1350. (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
  1351. if (mres->lvb[0]) {
  1352. if (lksb->flags & DLM_LKSB_PUT_LVB) {
  1353. /* other node was trying to update
  1354. * lvb when node died. recreate the
  1355. * lksb with the updated lvb. */
  1356. memcpy(lksb->lvb, mres->lvb, DLM_LVB_LEN);
  1357. } else {
  1358. /* otherwise, the node is sending its
  1359. * most recent valid lvb info */
  1360. BUG_ON(ml->type != LKM_EXMODE &&
  1361. ml->type != LKM_PRMODE);
  1362. if (res->lvb[0] && (ml->type == LKM_EXMODE ||
  1363. memcmp(res->lvb, mres->lvb, DLM_LVB_LEN))) {
  1364. mlog(ML_ERROR, "received bad lvb!\n");
  1365. __dlm_print_one_lock_resource(res);
  1366. BUG();
  1367. }
  1368. memcpy(res->lvb, mres->lvb, DLM_LVB_LEN);
  1369. }
  1370. }
  1371. /* NOTE:
  1372. * wrt lock queue ordering and recovery:
  1373. * 1. order of locks on granted queue is
  1374. * meaningless.
  1375. * 2. order of locks on converting queue is
  1376. * LOST with the node death. sorry charlie.
  1377. * 3. order of locks on the blocked queue is
  1378. * also LOST.
  1379. * order of locks does not affect integrity, it
  1380. * just means that a lock request may get pushed
  1381. * back in line as a result of the node death.
  1382. * also note that for a given node the lock order
  1383. * for its secondary queue locks is preserved
  1384. * relative to each other, but clearly *not*
  1385. * preserved relative to locks from other nodes.
  1386. */
  1387. spin_lock(&res->spinlock);
  1388. dlm_lock_get(newlock);
  1389. list_add_tail(&newlock->list, queue);
  1390. spin_unlock(&res->spinlock);
  1391. }
  1392. mlog(0, "done running all the locks\n");
  1393. leave:
  1394. if (ret < 0) {
  1395. mlog_errno(ret);
  1396. if (newlock)
  1397. dlm_lock_put(newlock);
  1398. }
  1399. mlog_exit(ret);
  1400. return ret;
  1401. }
  1402. void dlm_move_lockres_to_recovery_list(struct dlm_ctxt *dlm,
  1403. struct dlm_lock_resource *res)
  1404. {
  1405. int i;
  1406. struct list_head *queue, *iter, *iter2;
  1407. struct dlm_lock *lock;
  1408. res->state |= DLM_LOCK_RES_RECOVERING;
  1409. if (!list_empty(&res->recovering))
  1410. list_del_init(&res->recovering);
  1411. list_add_tail(&res->recovering, &dlm->reco.resources);
  1412. /* find any pending locks and put them back on proper list */
  1413. for (i=DLM_BLOCKED_LIST; i>=DLM_GRANTED_LIST; i--) {
  1414. queue = dlm_list_idx_to_ptr(res, i);
  1415. list_for_each_safe(iter, iter2, queue) {
  1416. lock = list_entry (iter, struct dlm_lock, list);
  1417. dlm_lock_get(lock);
  1418. if (lock->convert_pending) {
  1419. /* move converting lock back to granted */
  1420. BUG_ON(i != DLM_CONVERTING_LIST);
  1421. mlog(0, "node died with convert pending "
  1422. "on %.*s. move back to granted list.\n",
  1423. res->lockname.len, res->lockname.name);
  1424. dlm_revert_pending_convert(res, lock);
  1425. lock->convert_pending = 0;
  1426. } else if (lock->lock_pending) {
  1427. /* remove pending lock requests completely */
  1428. BUG_ON(i != DLM_BLOCKED_LIST);
  1429. mlog(0, "node died with lock pending "
  1430. "on %.*s. remove from blocked list and skip.\n",
  1431. res->lockname.len, res->lockname.name);
  1432. /* lock will be floating until ref in
  1433. * dlmlock_remote is freed after the network
  1434. * call returns. ok for it to not be on any
  1435. * list since no ast can be called
  1436. * (the master is dead). */
  1437. dlm_revert_pending_lock(res, lock);
  1438. lock->lock_pending = 0;
  1439. } else if (lock->unlock_pending) {
  1440. /* if an unlock was in progress, treat as
  1441. * if this had completed successfully
  1442. * before sending this lock state to the
  1443. * new master. note that the dlm_unlock
  1444. * call is still responsible for calling
  1445. * the unlockast. that will happen after
  1446. * the network call times out. for now,
  1447. * just move lists to prepare the new
  1448. * recovery master. */
  1449. BUG_ON(i != DLM_GRANTED_LIST);
  1450. mlog(0, "node died with unlock pending "
  1451. "on %.*s. remove from blocked list and skip.\n",
  1452. res->lockname.len, res->lockname.name);
  1453. dlm_commit_pending_unlock(res, lock);
  1454. lock->unlock_pending = 0;
  1455. } else if (lock->cancel_pending) {
  1456. /* if a cancel was in progress, treat as
  1457. * if this had completed successfully
  1458. * before sending this lock state to the
  1459. * new master */
  1460. BUG_ON(i != DLM_CONVERTING_LIST);
  1461. mlog(0, "node died with cancel pending "
  1462. "on %.*s. move back to granted list.\n",
  1463. res->lockname.len, res->lockname.name);
  1464. dlm_commit_pending_cancel(res, lock);
  1465. lock->cancel_pending = 0;
  1466. }
  1467. dlm_lock_put(lock);
  1468. }
  1469. }
  1470. }
  1471. /* removes all recovered locks from the recovery list.
  1472. * sets the res->owner to the new master.
  1473. * unsets the RECOVERY flag and wakes waiters. */
  1474. static void dlm_finish_local_lockres_recovery(struct dlm_ctxt *dlm,
  1475. u8 dead_node, u8 new_master)
  1476. {
  1477. int i;
  1478. struct list_head *iter, *iter2;
  1479. struct hlist_node *hash_iter;
  1480. struct hlist_head *bucket;
  1481. struct dlm_lock_resource *res;
  1482. mlog_entry_void();
  1483. assert_spin_locked(&dlm->spinlock);
  1484. list_for_each_safe(iter, iter2, &dlm->reco.resources) {
  1485. res = list_entry (iter, struct dlm_lock_resource, recovering);
  1486. if (res->owner == dead_node) {
  1487. list_del_init(&res->recovering);
  1488. spin_lock(&res->spinlock);
  1489. dlm_change_lockres_owner(dlm, res, new_master);
  1490. res->state &= ~DLM_LOCK_RES_RECOVERING;
  1491. __dlm_dirty_lockres(dlm, res);
  1492. spin_unlock(&res->spinlock);
  1493. wake_up(&res->wq);
  1494. }
  1495. }
  1496. /* this will become unnecessary eventually, but
  1497. * for now we need to run the whole hash, clear
  1498. * the RECOVERING state and set the owner
  1499. * if necessary */
  1500. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  1501. bucket = &(dlm->lockres_hash[i]);
  1502. hlist_for_each_entry(res, hash_iter, bucket, hash_node) {
  1503. if (res->state & DLM_LOCK_RES_RECOVERING) {
  1504. if (res->owner == dead_node) {
  1505. mlog(0, "(this=%u) res %.*s owner=%u "
  1506. "was not on recovering list, but "
  1507. "clearing state anyway\n",
  1508. dlm->node_num, res->lockname.len,
  1509. res->lockname.name, new_master);
  1510. } else if (res->owner == dlm->node_num) {
  1511. mlog(0, "(this=%u) res %.*s owner=%u "
  1512. "was not on recovering list, "
  1513. "owner is THIS node, clearing\n",
  1514. dlm->node_num, res->lockname.len,
  1515. res->lockname.name, new_master);
  1516. } else
  1517. continue;
  1518. if (!list_empty(&res->recovering)) {
  1519. mlog(0, "%s:%.*s: lockres was "
  1520. "marked RECOVERING, owner=%u\n",
  1521. dlm->name, res->lockname.len,
  1522. res->lockname.name, res->owner);
  1523. list_del_init(&res->recovering);
  1524. }
  1525. spin_lock(&res->spinlock);
  1526. dlm_change_lockres_owner(dlm, res, new_master);
  1527. res->state &= ~DLM_LOCK_RES_RECOVERING;
  1528. __dlm_dirty_lockres(dlm, res);
  1529. spin_unlock(&res->spinlock);
  1530. wake_up(&res->wq);
  1531. }
  1532. }
  1533. }
  1534. }
  1535. static inline int dlm_lvb_needs_invalidation(struct dlm_lock *lock, int local)
  1536. {
  1537. if (local) {
  1538. if (lock->ml.type != LKM_EXMODE &&
  1539. lock->ml.type != LKM_PRMODE)
  1540. return 1;
  1541. } else if (lock->ml.type == LKM_EXMODE)
  1542. return 1;
  1543. return 0;
  1544. }
  1545. static void dlm_revalidate_lvb(struct dlm_ctxt *dlm,
  1546. struct dlm_lock_resource *res, u8 dead_node)
  1547. {
  1548. struct list_head *iter, *queue;
  1549. struct dlm_lock *lock;
  1550. int blank_lvb = 0, local = 0;
  1551. int i;
  1552. u8 search_node;
  1553. assert_spin_locked(&dlm->spinlock);
  1554. assert_spin_locked(&res->spinlock);
  1555. if (res->owner == dlm->node_num)
  1556. /* if this node owned the lockres, and if the dead node
  1557. * had an EX when he died, blank out the lvb */
  1558. search_node = dead_node;
  1559. else {
  1560. /* if this is a secondary lockres, and we had no EX or PR
  1561. * locks granted, we can no longer trust the lvb */
  1562. search_node = dlm->node_num;
  1563. local = 1; /* check local state for valid lvb */
  1564. }
  1565. for (i=DLM_GRANTED_LIST; i<=DLM_CONVERTING_LIST; i++) {
  1566. queue = dlm_list_idx_to_ptr(res, i);
  1567. list_for_each(iter, queue) {
  1568. lock = list_entry (iter, struct dlm_lock, list);
  1569. if (lock->ml.node == search_node) {
  1570. if (dlm_lvb_needs_invalidation(lock, local)) {
  1571. /* zero the lksb lvb and lockres lvb */
  1572. blank_lvb = 1;
  1573. memset(lock->lksb->lvb, 0, DLM_LVB_LEN);
  1574. }
  1575. }
  1576. }
  1577. }
  1578. if (blank_lvb) {
  1579. mlog(0, "clearing %.*s lvb, dead node %u had EX\n",
  1580. res->lockname.len, res->lockname.name, dead_node);
  1581. memset(res->lvb, 0, DLM_LVB_LEN);
  1582. }
  1583. }
  1584. static void dlm_free_dead_locks(struct dlm_ctxt *dlm,
  1585. struct dlm_lock_resource *res, u8 dead_node)
  1586. {
  1587. struct list_head *iter, *tmpiter;
  1588. struct dlm_lock *lock;
  1589. /* this node is the lockres master:
  1590. * 1) remove any stale locks for the dead node
  1591. * 2) if the dead node had an EX when he died, blank out the lvb
  1592. */
  1593. assert_spin_locked(&dlm->spinlock);
  1594. assert_spin_locked(&res->spinlock);
  1595. /* TODO: check pending_asts, pending_basts here */
  1596. list_for_each_safe(iter, tmpiter, &res->granted) {
  1597. lock = list_entry (iter, struct dlm_lock, list);
  1598. if (lock->ml.node == dead_node) {
  1599. list_del_init(&lock->list);
  1600. dlm_lock_put(lock);
  1601. }
  1602. }
  1603. list_for_each_safe(iter, tmpiter, &res->converting) {
  1604. lock = list_entry (iter, struct dlm_lock, list);
  1605. if (lock->ml.node == dead_node) {
  1606. list_del_init(&lock->list);
  1607. dlm_lock_put(lock);
  1608. }
  1609. }
  1610. list_for_each_safe(iter, tmpiter, &res->blocked) {
  1611. lock = list_entry (iter, struct dlm_lock, list);
  1612. if (lock->ml.node == dead_node) {
  1613. list_del_init(&lock->list);
  1614. dlm_lock_put(lock);
  1615. }
  1616. }
  1617. /* do not kick thread yet */
  1618. __dlm_dirty_lockres(dlm, res);
  1619. }
  1620. /* if this node is the recovery master, and there are no
  1621. * locks for a given lockres owned by this node that are in
  1622. * either PR or EX mode, zero out the lvb before requesting.
  1623. *
  1624. */
  1625. static void dlm_do_local_recovery_cleanup(struct dlm_ctxt *dlm, u8 dead_node)
  1626. {
  1627. struct hlist_node *iter;
  1628. struct dlm_lock_resource *res;
  1629. int i;
  1630. struct hlist_head *bucket;
  1631. struct dlm_lock *lock;
  1632. /* purge any stale mles */
  1633. dlm_clean_master_list(dlm, dead_node);
  1634. /*
  1635. * now clean up all lock resources. there are two rules:
  1636. *
  1637. * 1) if the dead node was the master, move the lockres
  1638. * to the recovering list. set the RECOVERING flag.
  1639. * this lockres needs to be cleaned up before it can
  1640. * be used further.
  1641. *
  1642. * 2) if this node was the master, remove all locks from
  1643. * each of the lockres queues that were owned by the
  1644. * dead node. once recovery finishes, the dlm thread
  1645. * can be kicked again to see if any ASTs or BASTs
  1646. * need to be fired as a result.
  1647. */
  1648. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  1649. bucket = &(dlm->lockres_hash[i]);
  1650. hlist_for_each_entry(res, iter, bucket, hash_node) {
  1651. /* always prune any $RECOVERY entries for dead nodes,
  1652. * otherwise hangs can occur during later recovery */
  1653. if (dlm_is_recovery_lock(res->lockname.name,
  1654. res->lockname.len)) {
  1655. spin_lock(&res->spinlock);
  1656. list_for_each_entry(lock, &res->granted, list) {
  1657. if (lock->ml.node == dead_node) {
  1658. mlog(0, "AHA! there was "
  1659. "a $RECOVERY lock for dead "
  1660. "node %u (%s)!\n",
  1661. dead_node, dlm->name);
  1662. list_del_init(&lock->list);
  1663. dlm_lock_put(lock);
  1664. break;
  1665. }
  1666. }
  1667. spin_unlock(&res->spinlock);
  1668. continue;
  1669. }
  1670. spin_lock(&res->spinlock);
  1671. /* zero the lvb if necessary */
  1672. dlm_revalidate_lvb(dlm, res, dead_node);
  1673. if (res->owner == dead_node)
  1674. dlm_move_lockres_to_recovery_list(dlm, res);
  1675. else if (res->owner == dlm->node_num) {
  1676. dlm_free_dead_locks(dlm, res, dead_node);
  1677. __dlm_lockres_calc_usage(dlm, res);
  1678. }
  1679. spin_unlock(&res->spinlock);
  1680. }
  1681. }
  1682. }
  1683. static void __dlm_hb_node_down(struct dlm_ctxt *dlm, int idx)
  1684. {
  1685. assert_spin_locked(&dlm->spinlock);
  1686. /* check to see if the node is already considered dead */
  1687. if (!test_bit(idx, dlm->live_nodes_map)) {
  1688. mlog(0, "for domain %s, node %d is already dead. "
  1689. "another node likely did recovery already.\n",
  1690. dlm->name, idx);
  1691. return;
  1692. }
  1693. /* check to see if we do not care about this node */
  1694. if (!test_bit(idx, dlm->domain_map)) {
  1695. /* This also catches the case that we get a node down
  1696. * but haven't joined the domain yet. */
  1697. mlog(0, "node %u already removed from domain!\n", idx);
  1698. return;
  1699. }
  1700. clear_bit(idx, dlm->live_nodes_map);
  1701. /* Clean up join state on node death. */
  1702. if (dlm->joining_node == idx) {
  1703. mlog(0, "Clearing join state for node %u\n", idx);
  1704. __dlm_set_joining_node(dlm, DLM_LOCK_RES_OWNER_UNKNOWN);
  1705. }
  1706. /* make sure local cleanup occurs before the heartbeat events */
  1707. if (!test_bit(idx, dlm->recovery_map))
  1708. dlm_do_local_recovery_cleanup(dlm, idx);
  1709. /* notify anything attached to the heartbeat events */
  1710. dlm_hb_event_notify_attached(dlm, idx, 0);
  1711. mlog(0, "node %u being removed from domain map!\n", idx);
  1712. clear_bit(idx, dlm->domain_map);
  1713. /* wake up migration waiters if a node goes down.
  1714. * perhaps later we can genericize this for other waiters. */
  1715. wake_up(&dlm->migration_wq);
  1716. if (test_bit(idx, dlm->recovery_map))
  1717. mlog(0, "domain %s, node %u already added "
  1718. "to recovery map!\n", dlm->name, idx);
  1719. else
  1720. set_bit(idx, dlm->recovery_map);
  1721. }
  1722. void dlm_hb_node_down_cb(struct o2nm_node *node, int idx, void *data)
  1723. {
  1724. struct dlm_ctxt *dlm = data;
  1725. if (!dlm_grab(dlm))
  1726. return;
  1727. spin_lock(&dlm->spinlock);
  1728. __dlm_hb_node_down(dlm, idx);
  1729. spin_unlock(&dlm->spinlock);
  1730. dlm_put(dlm);
  1731. }
  1732. void dlm_hb_node_up_cb(struct o2nm_node *node, int idx, void *data)
  1733. {
  1734. struct dlm_ctxt *dlm = data;
  1735. if (!dlm_grab(dlm))
  1736. return;
  1737. spin_lock(&dlm->spinlock);
  1738. set_bit(idx, dlm->live_nodes_map);
  1739. /* do NOT notify mle attached to the heartbeat events.
  1740. * new nodes are not interesting in mastery until joined. */
  1741. spin_unlock(&dlm->spinlock);
  1742. dlm_put(dlm);
  1743. }
  1744. static void dlm_reco_ast(void *astdata)
  1745. {
  1746. struct dlm_ctxt *dlm = astdata;
  1747. mlog(0, "ast for recovery lock fired!, this=%u, dlm=%s\n",
  1748. dlm->node_num, dlm->name);
  1749. }
  1750. static void dlm_reco_bast(void *astdata, int blocked_type)
  1751. {
  1752. struct dlm_ctxt *dlm = astdata;
  1753. mlog(0, "bast for recovery lock fired!, this=%u, dlm=%s\n",
  1754. dlm->node_num, dlm->name);
  1755. }
  1756. static void dlm_reco_unlock_ast(void *astdata, enum dlm_status st)
  1757. {
  1758. mlog(0, "unlockast for recovery lock fired!\n");
  1759. }
  1760. /*
  1761. * dlm_pick_recovery_master will continually attempt to use
  1762. * dlmlock() on the special "$RECOVERY" lockres with the
  1763. * LKM_NOQUEUE flag to get an EX. every thread that enters
  1764. * this function on each node racing to become the recovery
  1765. * master will not stop attempting this until either:
  1766. * a) this node gets the EX (and becomes the recovery master),
  1767. * or b) dlm->reco.new_master gets set to some nodenum
  1768. * != O2NM_INVALID_NODE_NUM (another node will do the reco).
  1769. * so each time a recovery master is needed, the entire cluster
  1770. * will sync at this point. if the new master dies, that will
  1771. * be detected in dlm_do_recovery */
  1772. static int dlm_pick_recovery_master(struct dlm_ctxt *dlm)
  1773. {
  1774. enum dlm_status ret;
  1775. struct dlm_lockstatus lksb;
  1776. int status = -EINVAL;
  1777. mlog(0, "starting recovery of %s at %lu, dead=%u, this=%u\n",
  1778. dlm->name, jiffies, dlm->reco.dead_node, dlm->node_num);
  1779. again:
  1780. memset(&lksb, 0, sizeof(lksb));
  1781. ret = dlmlock(dlm, LKM_EXMODE, &lksb, LKM_NOQUEUE|LKM_RECOVERY,
  1782. DLM_RECOVERY_LOCK_NAME, dlm_reco_ast, dlm, dlm_reco_bast);
  1783. mlog(0, "%s: dlmlock($RECOVERY) returned %d, lksb=%d\n",
  1784. dlm->name, ret, lksb.status);
  1785. if (ret == DLM_NORMAL) {
  1786. mlog(0, "dlm=%s dlmlock says I got it (this=%u)\n",
  1787. dlm->name, dlm->node_num);
  1788. /* got the EX lock. check to see if another node
  1789. * just became the reco master */
  1790. if (dlm_reco_master_ready(dlm)) {
  1791. mlog(0, "%s: got reco EX lock, but %u will "
  1792. "do the recovery\n", dlm->name,
  1793. dlm->reco.new_master);
  1794. status = -EEXIST;
  1795. } else {
  1796. status = 0;
  1797. /* see if recovery was already finished elsewhere */
  1798. spin_lock(&dlm->spinlock);
  1799. if (dlm->reco.dead_node == O2NM_INVALID_NODE_NUM) {
  1800. status = -EINVAL;
  1801. mlog(0, "%s: got reco EX lock, but "
  1802. "node got recovered already\n", dlm->name);
  1803. if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
  1804. mlog(ML_ERROR, "%s: new master is %u "
  1805. "but no dead node!\n",
  1806. dlm->name, dlm->reco.new_master);
  1807. BUG();
  1808. }
  1809. }
  1810. spin_unlock(&dlm->spinlock);
  1811. }
  1812. /* if this node has actually become the recovery master,
  1813. * set the master and send the messages to begin recovery */
  1814. if (!status) {
  1815. mlog(0, "%s: dead=%u, this=%u, sending "
  1816. "begin_reco now\n", dlm->name,
  1817. dlm->reco.dead_node, dlm->node_num);
  1818. status = dlm_send_begin_reco_message(dlm,
  1819. dlm->reco.dead_node);
  1820. /* this always succeeds */
  1821. BUG_ON(status);
  1822. /* set the new_master to this node */
  1823. spin_lock(&dlm->spinlock);
  1824. dlm->reco.new_master = dlm->node_num;
  1825. spin_unlock(&dlm->spinlock);
  1826. }
  1827. /* recovery lock is a special case. ast will not get fired,
  1828. * so just go ahead and unlock it. */
  1829. ret = dlmunlock(dlm, &lksb, 0, dlm_reco_unlock_ast, dlm);
  1830. if (ret == DLM_DENIED) {
  1831. mlog(0, "got DLM_DENIED, trying LKM_CANCEL\n");
  1832. ret = dlmunlock(dlm, &lksb, LKM_CANCEL, dlm_reco_unlock_ast, dlm);
  1833. }
  1834. if (ret != DLM_NORMAL) {
  1835. /* this would really suck. this could only happen
  1836. * if there was a network error during the unlock
  1837. * because of node death. this means the unlock
  1838. * is actually "done" and the lock structure is
  1839. * even freed. we can continue, but only
  1840. * because this specific lock name is special. */
  1841. mlog(ML_ERROR, "dlmunlock returned %d\n", ret);
  1842. }
  1843. } else if (ret == DLM_NOTQUEUED) {
  1844. mlog(0, "dlm=%s dlmlock says another node got it (this=%u)\n",
  1845. dlm->name, dlm->node_num);
  1846. /* another node is master. wait on
  1847. * reco.new_master != O2NM_INVALID_NODE_NUM
  1848. * for at most one second */
  1849. wait_event_timeout(dlm->dlm_reco_thread_wq,
  1850. dlm_reco_master_ready(dlm),
  1851. msecs_to_jiffies(1000));
  1852. if (!dlm_reco_master_ready(dlm)) {
  1853. mlog(0, "%s: reco master taking awhile\n",
  1854. dlm->name);
  1855. goto again;
  1856. }
  1857. /* another node has informed this one that it is reco master */
  1858. mlog(0, "%s: reco master %u is ready to recover %u\n",
  1859. dlm->name, dlm->reco.new_master, dlm->reco.dead_node);
  1860. status = -EEXIST;
  1861. } else {
  1862. struct dlm_lock_resource *res;
  1863. /* dlmlock returned something other than NOTQUEUED or NORMAL */
  1864. mlog(ML_ERROR, "%s: got %s from dlmlock($RECOVERY), "
  1865. "lksb.status=%s\n", dlm->name, dlm_errname(ret),
  1866. dlm_errname(lksb.status));
  1867. res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
  1868. DLM_RECOVERY_LOCK_NAME_LEN);
  1869. if (res) {
  1870. dlm_print_one_lock_resource(res);
  1871. dlm_lockres_put(res);
  1872. } else {
  1873. mlog(ML_ERROR, "recovery lock not found\n");
  1874. }
  1875. BUG();
  1876. }
  1877. return status;
  1878. }
  1879. static int dlm_send_begin_reco_message(struct dlm_ctxt *dlm, u8 dead_node)
  1880. {
  1881. struct dlm_begin_reco br;
  1882. int ret = 0;
  1883. struct dlm_node_iter iter;
  1884. int nodenum;
  1885. int status;
  1886. mlog_entry("%u\n", dead_node);
  1887. mlog(0, "dead node is %u\n", dead_node);
  1888. spin_lock(&dlm->spinlock);
  1889. dlm_node_iter_init(dlm->domain_map, &iter);
  1890. spin_unlock(&dlm->spinlock);
  1891. clear_bit(dead_node, iter.node_map);
  1892. memset(&br, 0, sizeof(br));
  1893. br.node_idx = dlm->node_num;
  1894. br.dead_node = dead_node;
  1895. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  1896. ret = 0;
  1897. if (nodenum == dead_node) {
  1898. mlog(0, "not sending begin reco to dead node "
  1899. "%u\n", dead_node);
  1900. continue;
  1901. }
  1902. if (nodenum == dlm->node_num) {
  1903. mlog(0, "not sending begin reco to self\n");
  1904. continue;
  1905. }
  1906. retry:
  1907. ret = -EINVAL;
  1908. mlog(0, "attempting to send begin reco msg to %d\n",
  1909. nodenum);
  1910. ret = o2net_send_message(DLM_BEGIN_RECO_MSG, dlm->key,
  1911. &br, sizeof(br), nodenum, &status);
  1912. /* negative status is handled ok by caller here */
  1913. if (ret >= 0)
  1914. ret = status;
  1915. if (dlm_is_host_down(ret)) {
  1916. /* node is down. not involved in recovery
  1917. * so just keep going */
  1918. mlog(0, "%s: node %u was down when sending "
  1919. "begin reco msg (%d)\n", dlm->name, nodenum, ret);
  1920. ret = 0;
  1921. }
  1922. if (ret < 0) {
  1923. struct dlm_lock_resource *res;
  1924. /* this is now a serious problem, possibly ENOMEM
  1925. * in the network stack. must retry */
  1926. mlog_errno(ret);
  1927. mlog(ML_ERROR, "begin reco of dlm %s to node %u "
  1928. " returned %d\n", dlm->name, nodenum, ret);
  1929. res = dlm_lookup_lockres(dlm, DLM_RECOVERY_LOCK_NAME,
  1930. DLM_RECOVERY_LOCK_NAME_LEN);
  1931. if (res) {
  1932. dlm_print_one_lock_resource(res);
  1933. dlm_lockres_put(res);
  1934. } else {
  1935. mlog(ML_ERROR, "recovery lock not found\n");
  1936. }
  1937. /* sleep for a bit in hopes that we can avoid
  1938. * another ENOMEM */
  1939. msleep(100);
  1940. goto retry;
  1941. }
  1942. }
  1943. return ret;
  1944. }
  1945. int dlm_begin_reco_handler(struct o2net_msg *msg, u32 len, void *data)
  1946. {
  1947. struct dlm_ctxt *dlm = data;
  1948. struct dlm_begin_reco *br = (struct dlm_begin_reco *)msg->buf;
  1949. /* ok to return 0, domain has gone away */
  1950. if (!dlm_grab(dlm))
  1951. return 0;
  1952. mlog(0, "node %u wants to recover node %u\n",
  1953. br->node_idx, br->dead_node);
  1954. dlm_fire_domain_eviction_callbacks(dlm, br->dead_node);
  1955. spin_lock(&dlm->spinlock);
  1956. if (dlm->reco.new_master != O2NM_INVALID_NODE_NUM) {
  1957. if (test_bit(dlm->reco.new_master, dlm->recovery_map)) {
  1958. mlog(0, "%s: new_master %u died, changing "
  1959. "to %u\n", dlm->name, dlm->reco.new_master,
  1960. br->node_idx);
  1961. } else {
  1962. mlog(0, "%s: new_master %u NOT DEAD, changing "
  1963. "to %u\n", dlm->name, dlm->reco.new_master,
  1964. br->node_idx);
  1965. /* may not have seen the new master as dead yet */
  1966. }
  1967. }
  1968. if (dlm->reco.dead_node != O2NM_INVALID_NODE_NUM) {
  1969. mlog(ML_NOTICE, "%s: dead_node previously set to %u, "
  1970. "node %u changing it to %u\n", dlm->name,
  1971. dlm->reco.dead_node, br->node_idx, br->dead_node);
  1972. }
  1973. dlm->reco.new_master = br->node_idx;
  1974. dlm->reco.dead_node = br->dead_node;
  1975. if (!test_bit(br->dead_node, dlm->recovery_map)) {
  1976. mlog(0, "recovery master %u sees %u as dead, but this "
  1977. "node has not yet. marking %u as dead\n",
  1978. br->node_idx, br->dead_node, br->dead_node);
  1979. if (!test_bit(br->dead_node, dlm->domain_map) ||
  1980. !test_bit(br->dead_node, dlm->live_nodes_map))
  1981. mlog(0, "%u not in domain/live_nodes map "
  1982. "so setting it in reco map manually\n",
  1983. br->dead_node);
  1984. /* force the recovery cleanup in __dlm_hb_node_down
  1985. * both of these will be cleared in a moment */
  1986. set_bit(br->dead_node, dlm->domain_map);
  1987. set_bit(br->dead_node, dlm->live_nodes_map);
  1988. __dlm_hb_node_down(dlm, br->dead_node);
  1989. }
  1990. spin_unlock(&dlm->spinlock);
  1991. dlm_kick_recovery_thread(dlm);
  1992. dlm_put(dlm);
  1993. return 0;
  1994. }
  1995. static int dlm_send_finalize_reco_message(struct dlm_ctxt *dlm)
  1996. {
  1997. int ret = 0;
  1998. struct dlm_finalize_reco fr;
  1999. struct dlm_node_iter iter;
  2000. int nodenum;
  2001. int status;
  2002. mlog(0, "finishing recovery for node %s:%u\n",
  2003. dlm->name, dlm->reco.dead_node);
  2004. spin_lock(&dlm->spinlock);
  2005. dlm_node_iter_init(dlm->domain_map, &iter);
  2006. spin_unlock(&dlm->spinlock);
  2007. memset(&fr, 0, sizeof(fr));
  2008. fr.node_idx = dlm->node_num;
  2009. fr.dead_node = dlm->reco.dead_node;
  2010. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  2011. if (nodenum == dlm->node_num)
  2012. continue;
  2013. ret = o2net_send_message(DLM_FINALIZE_RECO_MSG, dlm->key,
  2014. &fr, sizeof(fr), nodenum, &status);
  2015. if (ret >= 0) {
  2016. ret = status;
  2017. if (dlm_is_host_down(ret)) {
  2018. /* this has no effect on this recovery
  2019. * session, so set the status to zero to
  2020. * finish out the last recovery */
  2021. mlog(ML_ERROR, "node %u went down after this "
  2022. "node finished recovery.\n", nodenum);
  2023. ret = 0;
  2024. }
  2025. }
  2026. if (ret < 0) {
  2027. mlog_errno(ret);
  2028. break;
  2029. }
  2030. }
  2031. return ret;
  2032. }
  2033. int dlm_finalize_reco_handler(struct o2net_msg *msg, u32 len, void *data)
  2034. {
  2035. struct dlm_ctxt *dlm = data;
  2036. struct dlm_finalize_reco *fr = (struct dlm_finalize_reco *)msg->buf;
  2037. /* ok to return 0, domain has gone away */
  2038. if (!dlm_grab(dlm))
  2039. return 0;
  2040. mlog(0, "node %u finalizing recovery of node %u\n",
  2041. fr->node_idx, fr->dead_node);
  2042. spin_lock(&dlm->spinlock);
  2043. if (dlm->reco.new_master != fr->node_idx) {
  2044. mlog(ML_ERROR, "node %u sent recovery finalize msg, but node "
  2045. "%u is supposed to be the new master, dead=%u\n",
  2046. fr->node_idx, dlm->reco.new_master, fr->dead_node);
  2047. BUG();
  2048. }
  2049. if (dlm->reco.dead_node != fr->dead_node) {
  2050. mlog(ML_ERROR, "node %u sent recovery finalize msg for dead "
  2051. "node %u, but node %u is supposed to be dead\n",
  2052. fr->node_idx, fr->dead_node, dlm->reco.dead_node);
  2053. BUG();
  2054. }
  2055. dlm_finish_local_lockres_recovery(dlm, fr->dead_node, fr->node_idx);
  2056. spin_unlock(&dlm->spinlock);
  2057. dlm_reset_recovery(dlm);
  2058. dlm_kick_recovery_thread(dlm);
  2059. dlm_put(dlm);
  2060. return 0;
  2061. }