dlmrecovery.c 84 KB

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