dlmrecovery.c 83 KB

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