dlmrecovery.c 87 KB

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